|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-3.0-or-later |
| 3 | + * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io) |
| 4 | + */ |
| 5 | + |
| 6 | +public class Monitor.PreferencesView : Gtk.Bin { |
| 7 | + private Gtk.Adjustment update_freq_adjustment; |
| 8 | + |
| 9 | + construct { |
| 10 | + update_freq_adjustment = new Gtk.Adjustment (MonitorApp.settings.get_int ("update-time"), 1, 5, 1.0, 1, 0); |
| 11 | + var update_freq_scale = new Gtk.Scale (HORIZONTAL, update_freq_adjustment) { |
| 12 | + hexpand = true, |
| 13 | + draw_value = false, |
| 14 | + round_digits = 0, |
| 15 | + }; |
| 16 | + |
| 17 | + var update_freq_label = new Gtk.Label (_("Update frequency")) { |
| 18 | + halign = START, |
| 19 | + mnemonic_widget = update_freq_scale |
| 20 | + }; |
| 21 | + |
| 22 | + var update_freq_description = new Gtk.Label (_("Requires restart")) { |
| 23 | + halign = START, |
| 24 | + margin_bottom = 6 |
| 25 | + }; |
| 26 | + update_freq_description.get_style_context ().add_class (Gtk.STYLE_CLASS_DIM_LABEL); |
| 27 | + update_freq_description.get_style_context ().add_class (Granite.STYLE_CLASS_SMALL_LABEL); |
| 28 | + |
| 29 | + update_freq_scale.add_mark (1.0, BOTTOM, _("1s")); |
| 30 | + update_freq_scale.add_mark (2.0, BOTTOM, _("2s")); |
| 31 | + update_freq_scale.add_mark (3.0, BOTTOM, _("3s")); |
| 32 | + update_freq_scale.add_mark (4.0, BOTTOM, _("4s")); |
| 33 | + update_freq_scale.add_mark (5.0, BOTTOM, _("5s")); |
| 34 | + |
| 35 | + var update_freq_box = new Gtk.Box (VERTICAL, 0) { |
| 36 | + margin_start = 12, |
| 37 | + margin_end = 12, |
| 38 | + margin_top = 12, |
| 39 | + margin_bottom = 12 |
| 40 | + }; |
| 41 | + update_freq_box.add (update_freq_label); |
| 42 | + update_freq_box.add (update_freq_description); |
| 43 | + update_freq_box.add (update_freq_scale); |
| 44 | + |
| 45 | + update_freq_adjustment.value_changed.connect (() => { |
| 46 | + MonitorApp.settings.set_int ("update-time", (int) update_freq_adjustment.get_value ()); |
| 47 | + }); |
| 48 | + |
| 49 | + var background_switch = new Granite.SwitchModelButton (_("Start in background")); |
| 50 | + |
| 51 | + var enable_smooth_lines_switch = new Granite.SwitchModelButton (_("Draw smooth lines on CPU chart")) { |
| 52 | + description = _("Requires restart") |
| 53 | + }; |
| 54 | + |
| 55 | + var dbusserver = DBusServer.get_default (); |
| 56 | + |
| 57 | + var indicator_switch = new Granite.SwitchModelButton (_("Show in panel")); |
| 58 | + indicator_switch.notify["active"].connect (() => { |
| 59 | + dbusserver.indicator_state (indicator_switch.active); |
| 60 | + }); |
| 61 | + |
| 62 | + var cpu_check = new Gtk.CheckButton.with_label (_("CPU percentage")); |
| 63 | + cpu_check.toggled.connect (() => { |
| 64 | + dbusserver.indicator_cpu_state (cpu_check.active); |
| 65 | + }); |
| 66 | + |
| 67 | + var cpu_freq_check = new Gtk.CheckButton.with_label (_("CPU frequency")); |
| 68 | + cpu_freq_check.toggled.connect (() => { |
| 69 | + dbusserver.indicator_cpu_frequency_state (cpu_freq_check.active); |
| 70 | + }); |
| 71 | + |
| 72 | + var cpu_temp_check = new Gtk.CheckButton.with_label (_("CPU temperature")); |
| 73 | + cpu_temp_check.toggled.connect (() => { |
| 74 | + dbusserver.indicator_cpu_temperature_state (cpu_temp_check.active); |
| 75 | + }); |
| 76 | + |
| 77 | + var memory_check = new Gtk.CheckButton.with_label (_("RAM percentage")); |
| 78 | + memory_check.toggled.connect (() => { |
| 79 | + dbusserver.indicator_memory_state (memory_check.active); |
| 80 | + }); |
| 81 | + |
| 82 | + var network_upload_check = new Gtk.CheckButton.with_label (_("Network upload")); |
| 83 | + network_upload_check.toggled.connect (() => { |
| 84 | + dbusserver.indicator_network_up_state (network_upload_check.active); |
| 85 | + }); |
| 86 | + |
| 87 | + var network_download_check = new Gtk.CheckButton.with_label (_("Network download")); |
| 88 | + network_download_check.toggled.connect (() => { |
| 89 | + dbusserver.indicator_network_down_state (network_download_check.active); |
| 90 | + }); |
| 91 | + |
| 92 | + var gpu_check = new Gtk.CheckButton.with_label (_("GPU percentage")); |
| 93 | + gpu_check.toggled.connect (() => { |
| 94 | + dbusserver.indicator_gpu_state (gpu_check.active); |
| 95 | + }); |
| 96 | + |
| 97 | + var gpu_memory_check = new Gtk.CheckButton.with_label (_("VRAM percentage")); |
| 98 | + gpu_memory_check.toggled.connect (() => { |
| 99 | + dbusserver.indicator_gpu_memory_state (gpu_memory_check.active); |
| 100 | + }); |
| 101 | + |
| 102 | + var gpu_temp_check = new Gtk.CheckButton.with_label (_("GPU temperature")); |
| 103 | + gpu_temp_check.toggled.connect (() => { |
| 104 | + dbusserver.indicator_gpu_temperature_state (gpu_temp_check.active); |
| 105 | + }); |
| 106 | + |
| 107 | + var row = 0; |
| 108 | + |
| 109 | + var indicator_options_box = new Gtk.Box (VERTICAL, 6) { |
| 110 | + margin_top = 6, |
| 111 | + margin_end = 12, |
| 112 | + margin_bottom = 6, |
| 113 | + margin_start = 12 |
| 114 | + }; |
| 115 | + indicator_options_box.add (cpu_check); |
| 116 | + indicator_options_box.add (cpu_freq_check); |
| 117 | + indicator_options_box.add (cpu_temp_check); |
| 118 | + indicator_options_box.add (new Gtk.Separator (HORIZONTAL)); |
| 119 | + indicator_options_box.add (memory_check); |
| 120 | + indicator_options_box.add (new Gtk.Separator (HORIZONTAL)); |
| 121 | + indicator_options_box.add (gpu_check); |
| 122 | + indicator_options_box.add (gpu_memory_check); |
| 123 | + indicator_options_box.add (gpu_temp_check); |
| 124 | + indicator_options_box.add (new Gtk.Separator (HORIZONTAL)); |
| 125 | + indicator_options_box.add (network_upload_check); |
| 126 | + indicator_options_box.add (network_download_check); |
| 127 | + |
| 128 | + var indicator_options_revealer = new Gtk.Revealer () { |
| 129 | + child = indicator_options_box |
| 130 | + }; |
| 131 | + |
| 132 | + var box = new Gtk.Box (VERTICAL, 0) { |
| 133 | + margin_bottom = 6 |
| 134 | + }; |
| 135 | + box.add (update_freq_box); |
| 136 | + box.add (enable_smooth_lines_switch); |
| 137 | + box.add (background_switch); |
| 138 | + box.add (indicator_switch); |
| 139 | + box.add (indicator_options_revealer); |
| 140 | + |
| 141 | + child = box; |
| 142 | + |
| 143 | + indicator_switch.bind_property ("active", indicator_options_revealer, "reveal-child", SYNC_CREATE); |
| 144 | + |
| 145 | + var settings = new GLib.Settings ("io.elementary.monitor.settings"); |
| 146 | + settings.bind ("background-state", background_switch, "active", DEFAULT); |
| 147 | + // Allow changing the background preference only when the indicator is enabled |
| 148 | + settings.bind ("indicator-state", background_switch, "sensitive", GET); |
| 149 | + settings.bind ("smooth-lines-state", enable_smooth_lines_switch, "active", DEFAULT); |
| 150 | + |
| 151 | + settings.bind ("indicator-state", indicator_switch, "active", DEFAULT); |
| 152 | + settings.bind ("indicator-cpu-state", cpu_check, "active", DEFAULT); |
| 153 | + settings.bind ("indicator-cpu-frequency-state", cpu_freq_check, "active", DEFAULT); |
| 154 | + settings.bind ("indicator-cpu-temperature-state", cpu_temp_check, "active", DEFAULT); |
| 155 | + settings.bind ("indicator-memory-state", memory_check, "active", DEFAULT); |
| 156 | + settings.bind ("indicator-gpu-temperature-state", gpu_temp_check, "active", DEFAULT); |
| 157 | + settings.bind ("indicator-gpu-memory-state", gpu_memory_check, "active", DEFAULT); |
| 158 | + settings.bind ("indicator-gpu-state", gpu_check, "active", DEFAULT); |
| 159 | + settings.bind ("indicator-network-download-state", network_download_check, "active", DEFAULT); |
| 160 | + settings.bind ("indicator-network-upload-state", network_upload_check, "active", DEFAULT); |
| 161 | + |
| 162 | + // Disable the background preference when the indicator is enabled |
| 163 | + settings.bind_with_mapping ( |
| 164 | + "indicator-state", background_switch, "active", GET, |
| 165 | + (switch_state, settings_state, user_data) => { |
| 166 | + bool state = settings_state.get_boolean (); |
| 167 | + if (!state) { |
| 168 | + switch_state.set_boolean (false); |
| 169 | + } |
| 170 | + |
| 171 | + return true; |
| 172 | + }, |
| 173 | + (SettingsBindSetMappingShared) null, null, null |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments