Skip to content

Commit

Permalink
Turn scan network into an app action
Browse files Browse the repository at this point in the history
It never was a window action, semantically: It changed the discovery
state of the global app model, and hence affected the entire app.

Let's make this apparent by lifting the action to the app, which also
removes the redundant property and overall simplifies state tracking.
  • Loading branch information
swsnr committed Jan 1, 2025
1 parent 3b2399c commit 410ec88
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion resources/gtk/help-overlay.blp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gtk.ShortcutsWindow help_overlay {

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

Expand Down
2 changes: 1 addition & 1 deletion resources/gtk/help-overlay.ui
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut description">Toggle network scanning</property>
<property name="accelerator">F5</property>
<property name="action-name">app.scan-network</property>
</object>
</child>
</object>
Expand Down
6 changes: 3 additions & 3 deletions resources/ui/turnon-application-window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ template $TurnOnApplicationWindow: Adw.ApplicationWindow {

[start]
Gtk.ToggleButton toggle_scan_network {
action-name: "win.toggle-scan-network";
action-name: "app.scan-network";
icon-name: "waves-and-screen-symbolic";
tooltip-text: C_("application-window.action.win.scan-network.tooltip", "Scan the network for devices");
tooltip-text: C_("application-window.action.app.scan-network.tooltip", "Scan the network for devices");
}

[end]
Expand Down Expand Up @@ -79,7 +79,7 @@ template $TurnOnApplicationWindow: Adw.ApplicationWindow {

Gtk.Button {
label: C_("application-window.status-page.button.label", "Scan network");
action-name: "win.toggle-scan-network";
action-name: "app.scan-network";

styles [
"pill",
Expand Down
6 changes: 3 additions & 3 deletions resources/ui/turnon-application-window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
</child>
<child type="start">
<object class="GtkToggleButton" id="toggle_scan_network">
<property name="action-name">win.toggle-scan-network</property>
<property name="action-name">app.scan-network</property>
<property name="icon-name">waves-and-screen-symbolic</property>
<property name="tooltip-text" translatable="yes" context="application-window.action.win.scan-network.tooltip">Scan the network for devices</property>
<property name="tooltip-text" translatable="yes" context="application-window.action.app.scan-network.tooltip">Scan the network for devices</property>
</object>
</child>
<child type="end">
Expand Down Expand Up @@ -81,7 +81,7 @@ corresponding .blp file and regenerate this file with blueprint-compiler.
<child>
<object class="GtkButton">
<property name="label" translatable="yes" context="application-window.status-page.button.label">Scan network</property>
<property name="action-name">win.toggle-scan-network</property>
<property name="action-name">app.scan-network</property>
<style>
<class name="pill"/>
<class name="suggested-action"/>
Expand Down
30 changes: 21 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ impl TurnOnApplication {
dialog.present(app.active_window().as_ref());
})
.build(),
ActionEntry::builder("scan-network")
.state(false.into())
.change_state(|app: &TurnOnApplication, action, state| {
// The default activate handler should safely toggle the boolean state of this action
// so we can expect the state to be set at this point.
let state = state.unwrap();
app.devices()
.discovered_devices()
.set_discovery_enabled(state.get::<bool>().unwrap());
action.set_state(state);
})
.build(),
ActionEntry::builder("quit")
.activate(|app: &TurnOnApplication, _, _| app.quit())
.build(),
Expand All @@ -122,8 +134,17 @@ impl TurnOnApplication {
];
self.add_action_entries(actions);

// We do _not_ add a global shortcut for app.add-device because we handle adding devices a bit more flexible:
//
// Device rows have their own action to add a new device, which enables adding a new device from a discovered
// device with pre-filled fields. We use the Ctrl+N shortcut for this action too, so that the user gets a
// prefilled dialog when they press Ctrl+N while a discovered device is focused, or an empty dialog otherwise.
//
// If we set a global shortcut here, it'd always override the shortcut of the device rows, and users would
// always just get the empty dialog.
self.set_accels_for_action("window.close", &["<Control>w"]);
self.set_accels_for_action("app.quit", &["<Control>q"]);
self.set_accels_for_action("app.scan-network", &["F5"]);
}
}

Expand Down Expand Up @@ -366,15 +387,6 @@ mod imp {
window.add_css_class("devel");
}
window.bind_model(&self.devices);
window.connect_scan_network_notify(glib::clone!(
#[strong]
app,
move |window| {
app.devices()
.discovered_devices()
.set_discovery_enabled(window.scan_network());
}
));
window.present();
}
}
Expand Down
13 changes: 1 addition & 12 deletions src/app/widgets/application_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TurnOnApplicationWindow {
}

mod imp {
use std::cell::{Cell, RefCell};
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;

Expand All @@ -58,8 +58,6 @@ mod imp {
pub struct TurnOnApplicationWindow {
settings: gio::Settings,
#[property(get, set)]
scan_network: Cell<bool>,
#[property(get, set)]
startpage_icon_name: RefCell<String>,
#[template_child]
devices_list: TemplateChild<gtk::ListBox>,
Expand Down Expand Up @@ -228,7 +226,6 @@ mod imp {
gio::SettingsBackend::NONE,
None,
),
scan_network: Default::default(),
startpage_icon_name: Default::default(),
devices_list: Default::default(),
feedback: Default::default(),
Expand All @@ -237,15 +234,7 @@ mod imp {

fn class_init(klass: &mut Self::Class) {
klass.bind_template();

klass.install_property_action("win.toggle-scan-network", "scan-network");

klass.add_binding_action(Key::N, ModifierType::CONTROL_MASK, "app.add-device");
klass.add_binding_action(
Key::F5,
ModifierType::NO_MODIFIER_MASK,
"win.toggle-scan-network",
);
}

fn instance_init(obj: &InitializingObject<Self>) {
Expand Down

0 comments on commit 410ec88

Please sign in to comment.