Skip to content

Commit

Permalink
Add working shortcut serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenDB committed Sep 26, 2023
1 parent 72410c4 commit 1e09575
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 289 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ set(SRC_FILES
src/ui/RoomSettings.h
src/ui/RoomSummary.cpp
src/ui/RoomSummary.h
src/ui/ShortcutRegistry.cpp
src/ui/ShortcutRegistry.h
src/ui/KeySequenceRegistry.cpp
src/ui/KeySequenceRegistry.h
src/ui/Theme.cpp
src/ui/Theme.h
src/ui/UIA.cpp
Expand Down
84 changes: 68 additions & 16 deletions resources/qml/Root.qml
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,14 @@ Pane {
onActivated: Qt.quit()
}

EditableShortcut {
EditableKeySequence {
id: quickSwitcherShortcut

name: qsTr("Room search")
description: qsTr("Opens a search bar for quick switching between rooms")
shortcut: "Ctrl+K"
defaultKeySequence: "Ctrl+K"
}
Shortcut {
sequence: quickSwitcherShortcut.shortcut
sequence: quickSwitcherShortcut.keySequence

onActivated: {
var component = Qt.createComponent("qrc:/resources/qml/QuickSwitcher.qml");
Expand All @@ -134,45 +133,98 @@ Pane {
}
}

EditableShortcut {
EditableKeySequence {
id: nextRoomWithActivityShortcut

name: qsTr("Next room with activity")
description: qsTr("Switches to the next unread room in the roomlist")
// Add alternative shortcut, because sometimes Alt+A is stolen by the TextEdit
shortcuts: ["Alt+A", "Ctrl+Shift+A"]
defaultKeySequences: ["Alt+A", "Ctrl+Shift+A"]
}
Shortcut {
sequences: nextRoomWithActivityShortcut.shortcuts
sequences: nextRoomWithActivityShortcut.keySequence

onActivated: Rooms.nextRoomWithActivity()
}

EditableShortcut {
EditableKeySequence {
id: nextRoomShortcut

name: qsTr("Next room")
description: qsTr("Switches to the room below the room that is currently open")
shortcut: "Ctrl+Down"
defaultKeySequence: "Ctrl+Down"
}
Shortcut {
sequence: nextRoomShortcut.shortcut
sequence: nextRoomShortcut.keySequence

onActivated: Rooms.nextRoom()
}

EditableShortcut {
EditableKeySequence {
id: previousRoomShortcut

name: qsTr("Previous room")
description: qsTr("Switches to the room above the room that is currently open")
shortcut: "Ctrl+Up"
defaultKeySequence: "Ctrl+Up"
}
Shortcut {
sequence: previousRoomShortcut.shortcut
sequence: previousRoomShortcut.keySequence

onActivated: Rooms.previousRoom()
}

EditableKeySequence {
id: nextSpaceShortcut

name: qsTr("Next space")
}
Shortcut {
sequence: nextSpaceShortcut.keySequence

// onActivated: Communities.setCurrentTagId(model.id)
}

EditableKeySequence {
id: previousSpaceShortcut

name: qsTr("Previous space")
}
Shortcut {
sequence: previousSpaceShortcut.keySequence

// onActivated: Communities.setCurrentTagId(model.id)
}

EditableKeySequence {
id: allRoomsSpaceShortcut

name: qsTr("Show all rooms")
}
Shortcut {
sequence: allRoomsSpaceShortcut.keySequence

onActivated: Communities.setCurrentTagId("global")
}

EditableKeySequence {
id: favoriteRoomsShortcut

name: qsTr("Show favorite rooms")
}
Shortcut {
sequence: favoriteRoomsShortcut.keySequence

onActivated: Communities.setCurrentTagId("m.favourite")
}

EditableKeySequence {
id: directChatsShortcut

name: qsTr("Show direct chats")
}
Shortcut {
sequence: directChatsShortcut.keySequence

onActivated: Communities.setCurrentTagId("dm")
}

Connections {
function onOpenJoinRoomDialog() {
var component = Qt.createComponent("qrc:/resources/qml/dialogs/JoinRoomDialog.qml");
Expand Down
37 changes: 14 additions & 23 deletions resources/qml/dialogs/ShortcutEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,31 @@ ApplicationWindow {
anchors.fill: parent

ListView {
model: ShortcutRegistry
model: KeySequenceRegistry

delegate: RowLayout {
id: del

required property string name
required property string description
required property string shortcut
required property string keySequence

spacing: Nheko.paddingMedium
width: ListView.view.width
height: implicitHeight + Nheko.paddingSmall * 2

ColumnLayout {
spacing: Nheko.paddingSmall

Label {
text: del.name
font.bold: true
font.pointSize: fontMetrics.font.pointSize * 1.1
}

Label {
text: del.description
}
Label {
text: del.name
}

Item { Layout.fillWidth: true }

Button {
property bool selectingNewShortcut: false
property bool selectingNewKeySequence: false

text: selectingNewShortcut ? qsTr("Input..") : del.shortcut
onClicked: selectingNewShortcut = !selectingNewShortcut
text: selectingNewKeySequence ? qsTr("Input..") : (del.keySequence === "" ? "None" : del.keySequence)
onClicked: selectingNewKeySequence = !selectingNewKeySequence
Keys.onPressed: event => {
if (!selectingNewShortcut)
if (!selectingNewKeySequence)
return;
event.accepted = true;

Expand All @@ -77,12 +66,14 @@ ApplicationWindow {
if (event.modifiers & Qt.ShiftModifier)
keySequence += "Shift+";

if (event.key === 0 || event.key === Qt.Key_unknown || event.key === Qt.Key_Control || event.key === Qt.Key_Alt || event.key === Qt.Key_AltGr || event.key === Qt.Key_Meta || event.key === Qt.Key_Shift)
if (event.key === 0 || event.key === Qt.Key_unknown || event.key === Qt.Key_Control || event.key === Qt.Key_Alt ||
event.key === Qt.Key_AltGr || event.key === Qt.Key_Meta || event.key === Qt.Key_Super_L || event.key === Qt.Key_Super_R ||
event.key === Qt.Key_Shift)
keySequence += "...";
else {
keySequence += ShortcutRegistry.keycodeToChar(event.key);
ShortcutRegistry.changeShortcut(del.name, keySequence);
selectingNewShortcut = false;
keySequence += KeySequenceRegistry.keycodeToChar(event.key);
KeySequenceRegistry.changeKeySequence(del.name, keySequence);
selectingNewKeySequence = false;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ MainWindow *MainWindow::instance_ = nullptr;
MainWindow::MainWindow(QWindow *parent)
: QQuickView(parent)
, userSettings_{UserSettings::instance()}
, shortcuts_{new ShortcutRegistry}
{
instance_ = this;

Expand Down
3 changes: 1 addition & 2 deletions src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <QSharedPointer>
#include <QSystemTrayIcon>

#include "ShortcutRegistry.h"
#include "KeySequenceRegistry.h"
#include "UserSettingsPage.h"
#include "dock/Dock.h"

Expand Down Expand Up @@ -141,7 +141,6 @@ private slots:
//! Tray icon that shows the unread message count.
TrayIcon *trayIcon_;
Dock *dock_;
ShortcutRegistry *shortcuts_;

MxcImageProvider *imgProvider = nullptr;

Expand Down
25 changes: 22 additions & 3 deletions src/UserSettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ QSharedPointer<UserSettings> UserSettings::instance_;

UserSettings::UserSettings()
{
connect(
QCoreApplication::instance(), &QCoreApplication::aboutToQuit, []() { instance_.clear(); });
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, []() {
instance_->save();
instance_.clear();
});
}

QSharedPointer<UserSettings>
Expand Down Expand Up @@ -164,6 +166,16 @@ UserSettings::load(std::optional<QString> profile)
disableCertificateValidation_ =
settings.value(QStringLiteral("disable_certificate_validation"), false).toBool();

settings.beginGroup(QStringLiteral("user"));
settings.beginGroup(QStringLiteral("shortcuts"));
QMap<QString, QStringList> bindings;
for (const auto &key : settings.childKeys())
bindings[key] = settings.value(key).toStringList();
qDebug() << "restoring with size:" << bindings.size();
KeySequenceRegistry::instance()->restoreBindings(bindings);
settings.endGroup(); // user/shortcuts
settings.endGroup(); // user/shortcuts

applyTheme();
}

Expand Down Expand Up @@ -882,7 +894,7 @@ UserSettings::save()
settings.beginGroup(QStringLiteral("sidebar"));
settings.setValue(QStringLiteral("community_list_width"), communityListWidth_);
settings.setValue(QStringLiteral("room_list_width"), roomListWidth_);
settings.endGroup(); // window
settings.endGroup(); // sidebar

settings.beginGroup(QStringLiteral("timeline"));
settings.setValue(QStringLiteral("buttons"), buttonsInTimeline_);
Expand Down Expand Up @@ -939,6 +951,13 @@ UserSettings::save()
settings.setValue(QStringLiteral("space_background_maintenance"), updateSpaceVias_);
settings.setValue(QStringLiteral("expired_events_background_maintenance"), expireEvents_);

settings.beginGroup(QStringLiteral("shortcuts"));
auto bindings = KeySequenceRegistry::instance()->dumpBindings();
for (const auto &[name, sequences] : bindings.asKeyValueRange())
settings.setValue(name, sequences);
qDebug() << "saved with size:" << bindings.size();
settings.endGroup(); // shortcuts

settings.endGroup(); // user

QString prefix = (profile_ != QLatin1String("") && profile_ != QLatin1String("default"))
Expand Down
Loading

0 comments on commit 1e09575

Please sign in to comment.