Skip to content

Commit

Permalink
Merge pull request #88 from swsnr/2-reorder-devices
Browse files Browse the repository at this point in the history
Add shortcuts to reorder devices
  • Loading branch information
swsnr authored Jan 10, 2025
2 parents 483f5a0 + f76806f commit e72f18d
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 52 deletions.
19 changes: 19 additions & 0 deletions resources/de.swsnr.turnon.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@
<url type="vcs-browser">https://github.com/swsnr/turnon</url>
<launchable type="desktop-id">de.swsnr.turnon.desktop</launchable>
<releases>
<!-- To start release notes for an upcoming release, copy the following template.
Upon release, cargo release will replace it accordingly.
<release version="next" date="9999-01-01">
<description>
</description>
<url>https://github.com/swsnr/turnon/releases/tag/next</url>
</release>
-->
<release version="next" date="9999-01-01">
<description>
<p>Reorder devices with Alt+Up and Alt+Down</p>
</description>
<issues>
<issue url="https://github.com/swsnr/turnon/issues/2">GH-2</issue>
</issues>
<url>https://github.com/swsnr/turnon/releases/tag/next</url>
</release>
<release version="2.2.0" date="2025-01-05">
<description>
<p>Refine application icon to make it sharper at smaller sizes, and redesign symbolic icon.</p>
Expand Down
60 changes: 35 additions & 25 deletions resources/gtk/help-overlay.blp
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,30 @@ Gtk.ShortcutsWindow help_overlay {
section-name: "shortcuts";

Gtk.ShortcutsGroup {
title: C_("shortcuts group", "Devices");
title: C_("shortcuts group", "General");

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Add a new device");
accelerator: "<Ctrl>N";
title: C_("shortcut description", "Show shortcuts");
action-name: "win.show-help-overlay";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Toggle network scanning");
action-name: "app.scan-network";
title: C_("shortcut description", "Quit");
action-name: "app.quit";
}
}

Gtk.ShortcutsGroup {
title: C_("shortcuts group", "Single device");

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Turn on device");
accelerator: "Return";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Edit device");
accelerator: "<Alt>Return";
}
title: C_("shortcuts group", "Devices");

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Ask to delete device");
accelerator: "Delete";
title: C_("shortcut description", "Add a new device");
accelerator: "<Ctrl>N";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Immediately delete device without confirmation");
accelerator: "<Ctrl>Delete";
title: C_("shortcut description", "Toggle network scanning");
action-name: "app.scan-network";
}
}

Expand All @@ -74,16 +64,36 @@ Gtk.ShortcutsWindow help_overlay {
}

Gtk.ShortcutsGroup {
title: C_("shortcuts group", "General");
title: C_("shortcuts group", "Single device");

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Show shortcuts");
action-name: "win.show-help-overlay";
title: C_("shortcut description", "Turn on device");
accelerator: "Return";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Quit");
action-name: "app.quit";
title: C_("shortcut description", "Edit device");
accelerator: "<Alt>Return";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Ask to delete device");
accelerator: "Delete";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Immediately delete device without confirmation");
accelerator: "<Ctrl>Delete";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Move device upwards");
accelerator: "<Alt>Up";
}

Gtk.ShortcutsShortcut {
title: C_("shortcut description", "Move device downwards");
accelerator: "<Alt>Down";
}
}
}
Expand Down
66 changes: 39 additions & 27 deletions resources/gtk/help-overlay.ui
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,34 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
<property name="section-name">shortcuts</property>
<child>
<object class="GtkShortcutsGroup">
<property name="title" translatable="yes" context="shortcuts group">Devices</property>
<property name="title" translatable="yes" context="shortcuts group">General</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Add a new device</property>
<property name="accelerator">&lt;Ctrl&gt;N</property>
<property name="title" translatable="yes" context="shortcut description">Show shortcuts</property>
<property name="action-name">win.show-help-overlay</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Toggle network scanning</property>
<property name="action-name">app.scan-network</property>
<property name="title" translatable="yes" context="shortcut description">Quit</property>
<property name="action-name">app.quit</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="title" translatable="yes" context="shortcuts group">Single device</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Turn on device</property>
<property name="accelerator">Return</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Edit device</property>
<property name="accelerator">&lt;Alt&gt;Return</property>
</object>
</child>
<property name="title" translatable="yes" context="shortcuts group">Devices</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Ask to delete device</property>
<property name="accelerator">Delete</property>
<property name="title" translatable="yes" context="shortcut description">Add a new device</property>
<property name="accelerator">&lt;Ctrl&gt;N</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Immediately delete device without confirmation</property>
<property name="accelerator">&lt;Ctrl&gt;Delete</property>
<property name="title" translatable="yes" context="shortcut description">Toggle network scanning</property>
<property name="action-name">app.scan-network</property>
</object>
</child>
</object>
Expand Down Expand Up @@ -81,17 +69,41 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
</child>
<child>
<object class="GtkShortcutsGroup">
<property name="title" translatable="yes" context="shortcuts group">General</property>
<property name="title" translatable="yes" context="shortcuts group">Single device</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Show shortcuts</property>
<property name="action-name">win.show-help-overlay</property>
<property name="title" translatable="yes" context="shortcut description">Turn on device</property>
<property name="accelerator">Return</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Quit</property>
<property name="action-name">app.quit</property>
<property name="title" translatable="yes" context="shortcut description">Edit device</property>
<property name="accelerator">&lt;Alt&gt;Return</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Ask to delete device</property>
<property name="accelerator">Delete</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Immediately delete device without confirmation</property>
<property name="accelerator">&lt;Ctrl&gt;Delete</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Move device upwards</property>
<property name="accelerator">&lt;Alt&gt;Up</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Move device downwards</property>
<property name="accelerator">&lt;Alt&gt;Down</property>
</object>
</child>
</object>
Expand Down
23 changes: 23 additions & 0 deletions src/app/widgets/application_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,35 @@ mod imp {
devices.registered_devices().append(device);
}
));
row.connect_moved(glib::clone!(
#[strong]
devices,
move |_, device, direction| {
println!("Would swap {} with {direction}", device.label());
let devices = devices.registered_devices();
if let Some(current_index) = devices.find(device) {
let swap_index = current_index as i64 + direction as i64;
if 0 <= swap_index && swap_index < devices.n_items() as i64 {
if let Some(device_swapped) = devices.item(swap_index as u32) {
// We remove the other device, not the device being moved; this
// retains the widget for the device being moved in views consuming
// the model, meaning it remains focused, and we can repeatedly
// move the same device to rearrange it.
devices.remove(swap_index as u32);
devices.insert(current_index, &device_swapped);
}
}
}
}
));

let is_registered = devices.registered_devices().find(device).is_some();
row.action_set_enabled("row.ask-delete", is_registered);
row.action_set_enabled("row.delete", is_registered);
row.action_set_enabled("row.edit", is_registered);
row.action_set_enabled("row.add", !is_registered);
row.action_set_enabled("row.move-up", is_registered);
row.action_set_enabled("row.move-down", is_registered);
if !is_registered {
row.add_css_class("discovered");
}
Expand Down
37 changes: 37 additions & 0 deletions src/app/widgets/device_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ impl DeviceRow {
),
)
}

pub fn connect_moved<F>(&self, callback: F) -> glib::SignalHandlerId
where
F: Fn(&Self, &Device, i32) + 'static,
{
self.connect_local(
"moved",
false,
glib::clone!(
#[weak(rename_to=row)]
&self,
#[upgrade_or_default]
move |args| {
let device = &args[1].get().expect("No device passed as signal argument?");
let direction = args[2]
.get()
.expect("No direction passed as signal argument?");
callback(&row, device, direction);
None
}
),
)
}
}

impl Default for DeviceRow {
Expand Down Expand Up @@ -131,6 +154,14 @@ mod imp {
klass.bind_template();
klass.bind_template_callbacks();

klass.install_action("row.move-up", None, |row, _, _| {
let direction: i32 = -1;
row.emit_by_name::<()>("moved", &[&row.device(), &direction]);
});
klass.install_action("row.move-down", None, |row, _, _| {
let direction: i32 = 1;
row.emit_by_name::<()>("moved", &[&row.device(), &direction]);
});
klass.install_action("row.ask-delete", None, |row, _, _| {
row.imp().set_suffix_mode("confirm-delete");
});
Expand Down Expand Up @@ -161,6 +192,8 @@ mod imp {
dialog.present(Some(row));
});

klass.add_binding_action(Key::Up, ModifierType::ALT_MASK, "row.move-up");
klass.add_binding_action(Key::Down, ModifierType::ALT_MASK, "row.move-down");
klass.add_binding_action(Key::Return, ModifierType::ALT_MASK, "row.edit");
klass.add_binding_action(Key::N, ModifierType::CONTROL_MASK, "row.add");
klass.add_binding_action(
Expand Down Expand Up @@ -197,6 +230,10 @@ mod imp {
.action()
.param_types([Device::static_type()])
.build(),
Signal::builder("moved")
.action()
.param_types([Device::static_type(), i32::static_type()])
.build(),
]
});
SIGNALS.as_ref()
Expand Down

0 comments on commit e72f18d

Please sign in to comment.