Skip to content

Commit 55a74f9

Browse files
authored
ShortcutsList: store shortcuts in a listmodel (#551)
1 parent b0adc91 commit 55a74f9

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

src/Shortcuts/Backend/ConflictsManager.vala

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@ class Keyboard.Shortcuts.ConflictsManager : GLib.Object {
2626
unowned var list = ShortcutsList.get_default ();
2727

2828
for (int group_id = 0; group_id < SectionID.CUSTOM; group_id++) {
29-
string[] actions, keys;
30-
Schema[] schemas;
29+
var listmodel = list.get_model (group_id);
30+
for (int i = 0; i < listmodel.n_items; i++) {
31+
var shortcut_action = (Keyboard.Shortcuts.Action) listmodel.get_item (i);
3132

32-
name = "";
33-
group = ((SectionID) group_id).to_string ();
34-
list.get_group (group_id, out actions, out schemas, out keys);
33+
var action_shortcut = Settings.get_default ().get_val (shortcut_action.schema, shortcut_action.key);
3534

36-
// For every action in group there is a corresponding schema and key entry
37-
// so only need to iterate actions
38-
for (int i = 0; i < actions.length; i++) {
39-
var action_shortcut = Settings.get_default ().get_val (schemas[i], keys[i]);
4035
if (shortcut.is_equal (action_shortcut)) {
41-
name = actions[i];
36+
name = shortcut_action.action;
4237
return true;
4338
}
4439
}

src/Shortcuts/Backend/Settings.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
namespace Keyboard.Shortcuts {
21-
private enum Schema { WM, MUTTER, GALA, MEDIA, SOUND_INDICATOR, IBUS, DOCK, COUNT }
21+
public enum Schema { WM, MUTTER, GALA, MEDIA, SOUND_INDICATOR, IBUS, DOCK, COUNT }
2222

2323
// helper class for gsettings
2424
// note that media key are stored as strings, all others as string vectors

src/Shortcuts/Backend/ShortcutsList.vala

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,22 @@ namespace Keyboard.Shortcuts {
2121
struct Group {
2222
public string icon_name;
2323
public string label;
24-
public string[] actions;
25-
public Schema[] schemas;
26-
public string[] keys;
24+
25+
public GLib.ListStore list;
26+
}
27+
28+
public class Action : Object {
29+
public Schema schema { get; construct; }
30+
public string action { get; construct; }
31+
public string key { get; construct; }
32+
33+
public Action (Schema schema, string action, string key) {
34+
Object (
35+
schema: schema,
36+
action: action,
37+
key: key
38+
);
39+
}
2740
}
2841

2942
class ShortcutsList : GLib.Object {
@@ -48,7 +61,9 @@ namespace Keyboard.Shortcuts {
4861
private ShortcutsList () {}
4962

5063
construct {
51-
windows_group = {};
64+
windows_group = Group () {
65+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
66+
};
5267
windows_group.icon_name = "io.elementary.settings.keyboard.windows";
5368
windows_group.label = _("Windows");
5469
add_action (ref windows_group, Schema.WM, _("Close"), "close");
@@ -74,7 +89,9 @@ namespace Keyboard.Shortcuts {
7489
add_action (ref windows_group, Schema.WM, _("Move to right display"), "move-to-monitor-right");
7590
add_action (ref windows_group, Schema.WM, _("Move to left display"), "move-to-monitor-left");
7691

77-
workspaces_group = {};
92+
workspaces_group = Group () {
93+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
94+
};
7895
workspaces_group.icon_name = "preferences-desktop-workspaces";
7996
workspaces_group.label = _("Workspaces");
8097
add_action (ref workspaces_group, Schema.GALA, _("Multitasking View"), "toggle-multitasking-view");
@@ -105,7 +122,9 @@ namespace Keyboard.Shortcuts {
105122
add_action (ref workspaces_group, Schema.WM, _("Move to left workspace"), "move-to-workspace-left");
106123
add_action (ref workspaces_group, Schema.WM, _("Move to right workspace"), "move-to-workspace-right");
107124

108-
screenshot_group = {};
125+
screenshot_group = Group () {
126+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
127+
};
109128
screenshot_group.icon_name = "accessories-screenshot-tool";
110129
screenshot_group.label = _("Screenshots");
111130
add_action (ref screenshot_group, Schema.GALA, _("Grab the whole screen"), "screenshot");
@@ -115,7 +134,9 @@ namespace Keyboard.Shortcuts {
115134
add_action (ref screenshot_group, Schema.GALA, _("Select an area to grab"), "area-screenshot");
116135
add_action (ref screenshot_group, Schema.GALA, _("Copy an area to clipboard"), "area-screenshot-clip");
117136

118-
launchers_group = {};
137+
launchers_group = Group () {
138+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
139+
};
119140
launchers_group.icon_name = "io.elementary.settings.keyboard.applications";
120141
launchers_group.label = _("Applications");
121142
add_action (ref launchers_group, Schema.MEDIA, _("Email"), "email");
@@ -133,7 +154,9 @@ namespace Keyboard.Shortcuts {
133154
add_action (ref launchers_group, Schema.DOCK, _("Launch eighth dock item"), "launch-dock-8");
134155
add_action (ref launchers_group, Schema.DOCK, _("Launch ninth dock item"), "launch-dock-9");
135156

136-
media_group = {};
157+
media_group = Group () {
158+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
159+
};
137160
media_group.icon_name = "applications-multimedia";
138161
media_group.label = _("Media");
139162
add_action (ref media_group, Schema.SOUND_INDICATOR, _("Volume Up"), "volume-up");
@@ -146,7 +169,9 @@ namespace Keyboard.Shortcuts {
146169
add_action (ref media_group, Schema.MEDIA, _("Next Track"), "next");
147170
add_action (ref media_group, Schema.MEDIA, _("Eject"), "eject");
148171

149-
a11y_group = {};
172+
a11y_group = Group () {
173+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
174+
};
150175
a11y_group.icon_name = "preferences-desktop-accessibility";
151176
a11y_group.label = _("Universal Access");
152177
add_action (ref a11y_group, Schema.MEDIA, _("Decrease Text Size"), "decrease-text-size");
@@ -156,23 +181,29 @@ namespace Keyboard.Shortcuts {
156181
add_action (ref a11y_group, Schema.MEDIA, _("Toggle On Screen Keyboard"), "on-screen-keyboard");
157182
add_action (ref a11y_group, Schema.MEDIA, _("Toggle Screenreader"), "screenreader");
158183

159-
system_group = {};
184+
system_group = Group () {
185+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
186+
};
160187
system_group.icon_name = "preferences-system";
161188
system_group.label = _("System");
162189
add_action (ref system_group, Schema.GALA, _("Applications Menu"), "panel-main-menu");
163190
add_action (ref system_group, Schema.MEDIA, _("Lock"), "screensaver");
164191
add_action (ref system_group, Schema.MEDIA, _("Log Out"), "logout");
165192
add_action (ref system_group, Schema.MUTTER, _("Cycle display mode"), "switch-monitor");
166193

167-
keyboard_layouts_group = {};
194+
keyboard_layouts_group = Group () {
195+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
196+
};
168197
keyboard_layouts_group.icon_name = "preferences-desktop-locale";
169198
keyboard_layouts_group.label = _("Keyboard Layouts");
170199
add_action (ref keyboard_layouts_group, Schema.GALA, _("Switch Keyboard Layout"), "switch-input-source");
171200
add_action (ref keyboard_layouts_group, Schema.GALA, _("Switch Keyboard Layout Backwards"), "switch-input-source-backward");
172201
add_action (ref keyboard_layouts_group, Schema.IBUS, _("Enable Emoji Typing"), "hotkey");
173202
add_action (ref keyboard_layouts_group, Schema.IBUS, _("Enable Unicode Typing"), "unicode-hotkey");
174203

175-
custom_group = {};
204+
custom_group = Group () {
205+
list = new GLib.ListStore (typeof (Keyboard.Shortcuts.Action))
206+
};
176207
custom_group.icon_name = "applications-other";
177208
custom_group.label = _("Custom");
178209

@@ -188,17 +219,16 @@ namespace Keyboard.Shortcuts {
188219
};
189220
}
190221

191-
public void get_group (SectionID group, out string[] a, out Schema[] s, out string[] k) {
192-
a = groups[group].actions;
193-
s = groups[group].schemas;
194-
k = groups[group].keys;
195-
return;
222+
public ListStore get_model (SectionID group) {
223+
return groups[group].list;
196224
}
197225

198226
public void add_action (ref Group group, Schema schema, string action, string key) {
199-
group.keys += key;
200-
group.schemas += schema;
201-
group.actions += action;
227+
var action_object = new Keyboard.Shortcuts.Action (schema, action, key);
228+
229+
if (Settings.get_default ().valid (schema, key)) {
230+
group.list.append (action_object);
231+
}
202232
}
203233
}
204234
}

src/Shortcuts/Widgets/ShortcutListBox.vala

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,27 @@
2020
private class Keyboard.Shortcuts.ShortcutListBox : Gtk.Box {
2121
public SectionID group { get; construct; }
2222

23-
private string[] actions;
24-
private Schema[] schemas;
25-
private string[] keys;
26-
2723
public ShortcutListBox (SectionID group) {
2824
Object (group: group);
2925
}
3026

3127
construct {
32-
ShortcutsList.get_default ().get_group (group, out actions, out schemas, out keys);
28+
var sizegroup = new Gtk.SizeGroup (VERTICAL);
3329

3430
var list_box = new Gtk.ListBox () {
3531
hexpand = true
3632
};
33+
list_box.bind_model (
34+
ShortcutsList.get_default ().get_model (group),
35+
(object) => {
36+
var action_object = (Keyboard.Shortcuts.Action) object;
3737

38-
var sizegroup = new Gtk.SizeGroup (Gtk.SizeGroupMode.VERTICAL);
39-
40-
for (int i = 0; i < actions.length; i++) {
41-
if (Settings.get_default ().valid (schemas[i], keys[i])) {
42-
var row = new ShortcutRow (actions[i], schemas[i], keys[i]);
43-
list_box.append (row);
44-
38+
var row = new ShortcutRow (action_object.action, action_object.schema, action_object.key);
4539
sizegroup.add_widget (row);
40+
41+
return row;
4642
}
47-
}
43+
);
4844

4945
append (list_box);
5046
}

0 commit comments

Comments
 (0)