Skip to content

Commit 7c3a3a2

Browse files
committed
Use drop down for refresh rates
1 parent db7e59e commit 7c3a3a2

File tree

2 files changed

+79
-94
lines changed

2 files changed

+79
-94
lines changed

src/Objects/VirtualMonitor.vala

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ public class Display.VirtualMonitor : GLib.Object {
2929
}
3030
}
3131

32+
public class RefreshRate : Object, Utils.StringRepresentable {
33+
public string string_representation { get; construct; }
34+
public MonitorMode mode { get; construct; }
35+
36+
public RefreshRate (MonitorMode mode) {
37+
Object (mode: mode, string_representation: _("%g Hz").printf (Math.round (mode.frequency)));
38+
}
39+
}
40+
3241
public int x { get; set; }
3342
public int y { get; set; }
3443
public int current_x { get; set; }
3544
public int current_y { get; set; }
3645
public double scale { get; set; }
3746
public Gtk.SingleSelection available_transforms { get; construct; }
47+
public Gtk.SingleSelection available_refresh_rates { get; construct; }
3848
public bool primary { get; set; }
3949
public Gee.LinkedList<Display.Monitor> monitors { get; construct; }
4050

@@ -87,6 +97,7 @@ public class Display.VirtualMonitor : GLib.Object {
8797
}
8898

8999
private ListStore available_transforms_store;
100+
private ListStore available_refresh_rates_store;
90101

91102
construct {
92103
monitors = new Gee.LinkedList<Display.Monitor> ();
@@ -99,6 +110,16 @@ public class Display.VirtualMonitor : GLib.Object {
99110
for (int i = 0; i <= DisplayTransform.FLIPPED_ROTATION_270; i++) {
100111
available_transforms_store.append (new Transform ((DisplayTransform) i));
101112
}
113+
114+
available_refresh_rates_store = new ListStore (typeof (RefreshRate));
115+
available_refresh_rates = new Gtk.SingleSelection (available_refresh_rates_store) {
116+
autoselect = true
117+
};
118+
119+
available_refresh_rates.selection_changed.connect (() =>
120+
set_current_mode (((RefreshRate) available_refresh_rates.get_item (available_refresh_rates.selected)).mode));
121+
122+
Idle.add_once (update_available_refresh_rates);
102123
}
103124

104125
public unowned string get_display_name () {
@@ -154,6 +175,12 @@ public class Display.VirtualMonitor : GLib.Object {
154175
}
155176

156177
public void set_current_mode (Display.MonitorMode current_mode) {
178+
var old_current_mode = monitors[0].current_mode;
179+
180+
if (old_current_mode == current_mode) {
181+
return;
182+
}
183+
157184
if (is_mirror) {
158185
monitors.foreach ((_monitor) => {
159186
bool mode_found = false;
@@ -178,6 +205,49 @@ public class Display.VirtualMonitor : GLib.Object {
178205
mode.is_current = mode == current_mode;
179206
}
180207
}
208+
209+
update_available_refresh_rates ();
210+
}
211+
212+
private void update_available_refresh_rates () {
213+
int active_width, active_height;
214+
get_current_mode_size (out active_width, out active_height);
215+
216+
double[] frequencies = {};
217+
RefreshRate[] refresh_rates = {};
218+
uint to_select = 0;
219+
foreach (var mode in get_available_modes ()) {
220+
if (mode.width != active_width || mode.height != active_height) {
221+
continue;
222+
}
223+
224+
if (mode.frequency in frequencies) {
225+
continue;
226+
}
227+
228+
bool freq_already_added = false;
229+
foreach (var freq in frequencies) {
230+
if ((mode.frequency - freq).abs () < 1) {
231+
freq_already_added = true;
232+
break;
233+
}
234+
}
235+
236+
if (freq_already_added) {
237+
continue;
238+
}
239+
240+
frequencies += mode.frequency;
241+
242+
refresh_rates += new RefreshRate (mode);
243+
244+
if (mode.is_current) {
245+
to_select = refresh_rates.length - 1;
246+
}
247+
}
248+
249+
available_refresh_rates_store.splice (0, available_refresh_rates_store.get_n_items (), refresh_rates);
250+
available_refresh_rates.selected = to_select;
181251
}
182252

183253
public static string generate_id_from_monitors (MutterReadMonitorInfo[] infos) {

src/Widgets/DisplayWidget.vala

Lines changed: 9 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ public class Display.DisplayWidget : Gtk.Box {
6565
private Gtk.ComboBox resolution_combobox;
6666
private Gtk.TreeStore resolution_tree_store;
6767

68-
private Gtk.ComboBox refresh_combobox;
69-
private Gtk.ListStore refresh_list_store;
70-
7168
private Gtk.DropDown scale_drop_down;
7269

7370
private int real_width = 0;
@@ -80,12 +77,6 @@ public class Display.DisplayWidget : Gtk.Box {
8077
TOTAL
8178
}
8279

83-
private enum RefreshColumns {
84-
NAME,
85-
VALUE,
86-
TOTAL
87-
}
88-
8980
public DisplayWidget (Display.VirtualMonitor virtual_monitor, string bg_color, string text_color) {
9081
Object (
9182
virtual_monitor: virtual_monitor,
@@ -145,20 +136,17 @@ public class Display.DisplayWidget : Gtk.Box {
145136
mnemonic_widget = rotation_drop_down
146137
};
147138

148-
refresh_list_store = new Gtk.ListStore (RefreshColumns.TOTAL, typeof (string), typeof (Display.MonitorMode));
149-
refresh_combobox = new Gtk.ComboBox.with_model (refresh_list_store) {
139+
var refresh_drop_down = new Gtk.DropDown (virtual_monitor.available_refresh_rates, null) {
150140
margin_start = 12,
151-
margin_end = 12
141+
margin_end = 12,
142+
factory = Utils.create_string_list_item_factory ()
152143
};
144+
virtual_monitor.available_refresh_rates.bind_property ("selected", refresh_drop_down, "selected", BIDIRECTIONAL | SYNC_CREATE);
153145

154146
var refresh_label = new Granite.HeaderLabel (_("Refresh Rate")) {
155-
mnemonic_widget = refresh_combobox
147+
mnemonic_widget = refresh_drop_down
156148
};
157149

158-
text_renderer = new Gtk.CellRendererText ();
159-
refresh_combobox.pack_start (text_renderer, true);
160-
refresh_combobox.add_attribute (text_renderer, "text", RefreshColumns.NAME);
161-
162150
// Build resolution menu
163151
// First, get list of unique resolutions from available modes.
164152
Resolution[] resolutions = {};
@@ -238,8 +226,6 @@ public class Display.DisplayWidget : Gtk.Box {
238226

239227
resolution_combobox.sensitive = usable_resolutions > 1;
240228

241-
populate_refresh_rates ();
242-
243229
scale_drop_down = new Gtk.DropDown.from_strings (string_scales) {
244230
margin_start = 12,
245231
margin_end = 12
@@ -259,7 +245,7 @@ public class Display.DisplayWidget : Gtk.Box {
259245
popover_box.append (rotation_label);
260246
popover_box.append (rotation_drop_down);
261247
popover_box.append (refresh_label);
262-
popover_box.append (refresh_combobox);
248+
popover_box.append (refresh_drop_down);
263249

264250
if (!MonitorManager.get_default ().global_scale_required) {
265251
popover_box.append (scale_label);
@@ -291,12 +277,11 @@ public class Display.DisplayWidget : Gtk.Box {
291277

292278
use_switch.bind_property ("active", resolution_combobox, "sensitive");
293279
use_switch.bind_property ("active", rotation_drop_down, "sensitive");
294-
use_switch.bind_property ("active", refresh_combobox, "sensitive");
280+
use_switch.bind_property ("active", refresh_drop_down, "sensitive");
295281
use_switch.bind_property ("active", scale_drop_down, "sensitive");
296282

297283
use_switch.notify["active"].connect (() => {
298284
if (resolution_combobox.active == -1) resolution_combobox.set_active (0);
299-
if (refresh_combobox.active == -1) refresh_combobox.set_active (0);
300285

301286
if (use_switch.active) {
302287
remove_css_class ("disabled");
@@ -334,7 +319,6 @@ public class Display.DisplayWidget : Gtk.Box {
334319
}
335320

336321
virtual_monitor.set_current_mode (new_mode);
337-
populate_refresh_rates ();
338322
configuration_changed ();
339323
check_position ();
340324
});
@@ -343,26 +327,16 @@ public class Display.DisplayWidget : Gtk.Box {
343327
// Prevent breaking autohide by closing popover
344328
popover.popdown ();
345329

346-
var transform = (DisplayTransform)(rotation_drop_down.selected);
347-
348330
update_transformed_style ();
349331

350332
configuration_changed ();
351333
});
352334

353-
refresh_combobox.changed.connect (() => {
335+
refresh_drop_down.notify["selected"].connect (() => {
354336
// Prevent breaking autohide by closing popover
355337
popover.popdown ();
356338

357-
Value val;
358-
Gtk.TreeIter iter;
359-
if (refresh_combobox.get_active_iter (out iter)) {
360-
refresh_list_store.get_value (iter, RefreshColumns.VALUE, out val);
361-
Display.MonitorMode new_mode = (Display.MonitorMode) val;
362-
virtual_monitor.set_current_mode (new_mode);
363-
configuration_changed ();
364-
check_position ();
365-
}
339+
configuration_changed ();
366340
});
367341

368342
virtual_monitor.notify["scale"].connect (update_selected_scale);
@@ -399,65 +373,6 @@ public class Display.DisplayWidget : Gtk.Box {
399373
}
400374
}
401375

402-
private void populate_refresh_rates () {
403-
refresh_list_store.clear ();
404-
405-
Gtk.TreeIter iter;
406-
int added = 0;
407-
if (resolution_combobox.get_active_iter (out iter)) {
408-
int active_width, active_height;
409-
if (resolution_combobox.get_active_iter (out iter)) {
410-
resolution_tree_store.get (iter,
411-
ResolutionColumns.WIDTH, out active_width,
412-
ResolutionColumns.HEIGHT, out active_height
413-
);
414-
} else {
415-
return;
416-
}
417-
418-
double[] frequencies = {};
419-
bool refresh_set = false;
420-
foreach (var mode in virtual_monitor.get_available_modes ()) {
421-
if (mode.width != active_width || mode.height != active_height) {
422-
continue;
423-
}
424-
425-
if (mode.frequency in frequencies) {
426-
continue;
427-
}
428-
429-
bool freq_already_added = false;
430-
foreach (var freq in frequencies) {
431-
if ((mode.frequency - freq).abs () < 1) {
432-
freq_already_added = true;
433-
break;
434-
}
435-
}
436-
437-
if (freq_already_added) {
438-
continue;
439-
}
440-
441-
frequencies += mode.frequency;
442-
443-
var freq_name = _("%g Hz").printf (Math.roundf ((float)mode.frequency));
444-
refresh_list_store.append (out iter);
445-
refresh_list_store.set (iter, ResolutionColumns.NAME, freq_name, RefreshColumns.VALUE, mode);
446-
added++;
447-
if (mode.is_current) {
448-
refresh_combobox.set_active_iter (iter);
449-
refresh_set = true;
450-
}
451-
}
452-
453-
if (!refresh_set) {
454-
refresh_combobox.set_active (0);
455-
}
456-
}
457-
458-
refresh_combobox.sensitive = added > 1;
459-
}
460-
461376
private void on_monitor_modes_changed () {
462377
set_active_resolution_from_current_mode ();
463378
}

0 commit comments

Comments
 (0)