Skip to content

Commit

Permalink
PreferencesView: Use Setitngs.bind to lessen scope of variables
Browse files Browse the repository at this point in the history
  • Loading branch information
ryonakano committed Oct 12, 2024
1 parent 872ca26 commit d12b8da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
34 changes: 21 additions & 13 deletions src/Views/PreferencesView/PreferencesGeneralPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
*/

public class Monitor.PreferencesGeneralPage : Granite.SettingsPage {
public Gtk.Switch background_switch;
public Gtk.Switch enable_smooth_lines_switch;

private Gtk.Adjustment update_time_adjustment;

public PreferencesGeneralPage () {
Expand All @@ -26,17 +23,15 @@
var background_label = new Gtk.Label (_("Start in background:"));
background_label.halign = Gtk.Align.START;

background_switch = new Gtk.Switch ();
background_switch.state = MonitorApp.settings.get_boolean ("background-state");
var background_switch = new Gtk.Switch ();
background_switch.halign = Gtk.Align.END;
background_switch.hexpand = true;


var enable_smooth_lines_label = new Gtk.Label (_("Draw smooth lines on CPU chart (requires restart):"));
enable_smooth_lines_label.halign = Gtk.Align.START;

enable_smooth_lines_switch = new Gtk.Switch ();
enable_smooth_lines_switch.state = MonitorApp.settings.get_boolean ("smooth-lines-state");
var enable_smooth_lines_switch = new Gtk.Switch ();
enable_smooth_lines_switch.halign = Gtk.Align.END;
enable_smooth_lines_switch.hexpand = true;

Expand Down Expand Up @@ -69,13 +64,26 @@

add (content_area);

background_switch.notify["active"].connect (() => {
MonitorApp.settings.set_boolean ("background-state", background_switch.state);
});
MonitorApp.settings.bind ("background-state", background_switch, "state", DEFAULT);

enable_smooth_lines_switch.notify["active"].connect (() => {
MonitorApp.settings.set_boolean ("smooth-lines-state", enable_smooth_lines_switch.state);
});
// Allow changing the background preference only when the indicator is enabled
MonitorApp.settings.bind ("indicator-state", background_switch, "sensitive", GET);

// Disable the background preference when the indicator is enabled
MonitorApp.settings.bind_with_mapping (
"indicator-state", background_switch, "state", GET,
(switch_state, settings_state, user_data) => {
bool state = settings_state.get_boolean ();
if (!state) {
switch_state.set_boolean (false);
}

return true;
},
(SettingsBindSetMappingShared) null, null, null
);

MonitorApp.settings.bind ("smooth-lines-state", enable_smooth_lines_switch, "state", DEFAULT);

update_time_adjustment.value_changed.connect (() => {
MonitorApp.settings.set_int ("update-time", (int) update_time_adjustment.get_value ());
Expand Down
12 changes: 0 additions & 12 deletions src/Views/PreferencesView/PreferencesView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
private PreferencesIndicatorPage indicator_page = new PreferencesIndicatorPage ();

construct {
set_background_switch_state ();
general_page.background_switch.notify["active"].connect (() => set_background_switch_state ());
indicator_page.status_switch.notify["active"].connect (() => set_background_switch_state ());

height_request = 300;
width_request = 500;
set_position (135);
Expand All @@ -27,12 +23,4 @@
pack1 (settings_sidebar, true, false);
pack2 (stack, true, false);
}

private void set_background_switch_state () {
general_page.background_switch.sensitive = indicator_page.status_switch.active;

if (!indicator_page.status_switch.active) {
general_page.background_switch.state = false;
}
}
}

0 comments on commit d12b8da

Please sign in to comment.