Skip to content

Commit c743219

Browse files
authored
Use mutter provided supported scales (#399)
1 parent a1d1157 commit c743219

File tree

3 files changed

+78
-47
lines changed

3 files changed

+78
-47
lines changed

src/Objects/MonitorManager.vala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,6 @@ public class Display.MonitorManager : GLib.Object {
191191
add_virtual_monitor (virtual_monitor);
192192
}
193193

194-
virtual_monitor.x = mutter_logical_monitor.x;
195-
virtual_monitor.y = mutter_logical_monitor.y;
196-
virtual_monitor.current_x = mutter_logical_monitor.x;
197-
virtual_monitor.current_y = mutter_logical_monitor.y;
198-
virtual_monitor.scale = mutter_logical_monitor.scale;
199-
virtual_monitor.transform = mutter_logical_monitor.transform;
200-
virtual_monitor.primary = mutter_logical_monitor.primary;
201194
foreach (var mutter_info in mutter_logical_monitor.monitors) {
202195
foreach (var monitor in monitors) {
203196
if (compare_monitor_with_mutter_info (monitor, mutter_info)) {
@@ -213,6 +206,14 @@ public class Display.MonitorManager : GLib.Object {
213206
}
214207
}
215208
}
209+
210+
virtual_monitor.x = mutter_logical_monitor.x;
211+
virtual_monitor.y = mutter_logical_monitor.y;
212+
virtual_monitor.current_x = mutter_logical_monitor.x;
213+
virtual_monitor.current_y = mutter_logical_monitor.y;
214+
virtual_monitor.scale = mutter_logical_monitor.scale;
215+
virtual_monitor.transform = mutter_logical_monitor.transform;
216+
virtual_monitor.primary = mutter_logical_monitor.primary;
216217
}
217218

218219
// Look for any monitors that aren't part of a virtual monitor (hence disabled)

src/Objects/VirtualMonitor.vala

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,45 @@
2020
*/
2121

2222
public class Display.VirtualMonitor : GLib.Object {
23+
public class Scale : GLib.Object {
24+
public double scale { get; construct; }
25+
public string string_representation { get; construct; }
26+
27+
public Scale (double scale) {
28+
Object (
29+
scale: scale,
30+
string_representation: "%d %%".printf ((int) Math.round (scale * 100))
31+
);
32+
}
33+
}
34+
2335
public int x { get; set; }
2436
public int y { get; set; }
2537
public int current_x { get; set; }
2638
public int current_y { get; set; }
27-
public double scale { get; set; }
39+
public Gtk.SingleSelection available_scales { get; construct; }
2840
public DisplayTransform transform { get; set; }
2941
public bool primary { get; set; }
3042
public Gee.LinkedList<Display.Monitor> monitors { get; construct; }
3143

3244
public signal void modes_changed ();
3345

46+
public double scale {
47+
get {
48+
return ((Scale) available_scales.selected_item).scale;
49+
}
50+
set {
51+
update_available_scales ();
52+
for (int i = 0; i < available_scales.get_n_items (); i++) {
53+
if (value == ((Scale) available_scales.get_item (i)).scale) {
54+
available_scales.selected = i;
55+
return;
56+
}
57+
}
58+
critical ("Unsupported scale %f for current mode", value);
59+
}
60+
}
61+
3462
/*
3563
* Used to distinguish two VirtualMonitors from each other.
3664
* We make up and ID by sum all hashes of
@@ -68,8 +96,13 @@ public class Display.VirtualMonitor : GLib.Object {
6896
}
6997
}
7098

99+
private ListStore available_scales_store;
100+
71101
construct {
72102
monitors = new Gee.LinkedList<Display.Monitor> ();
103+
104+
available_scales_store = new ListStore (typeof (Scale));
105+
available_scales = new Gtk.SingleSelection (available_scales_store);
73106
}
74107

75108
public unowned string get_display_name () {
@@ -114,6 +147,23 @@ public class Display.VirtualMonitor : GLib.Object {
114147
}
115148
}
116149

150+
private void update_available_scales () {
151+
Scale[] scales = {};
152+
foreach (var mode in get_available_modes ()) {
153+
if (!mode.is_current) {
154+
continue;
155+
}
156+
157+
foreach (var scale in mode.supported_scales) {
158+
scales += new Scale (scale);
159+
}
160+
161+
break;
162+
}
163+
164+
available_scales_store.splice (0, available_scales_store.get_n_items (), scales);
165+
}
166+
117167
public Display.MonitorMode? get_mode_for_resolution (int width, int height) {
118168
foreach (var mode in get_available_modes ()) {
119169
if (mode.width == width && mode.height == height) {
@@ -149,6 +199,8 @@ public class Display.VirtualMonitor : GLib.Object {
149199
mode.is_current = mode == current_mode;
150200
}
151201
}
202+
203+
scale = current_mode.preferred_scale;
152204
}
153205

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

src/Widgets/DisplayWidget.vala

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@ public struct Display.Resolution {
2727
}
2828

2929
public class Display.DisplayWidget : Gtk.Box {
30-
private static double[] scales;
31-
private static string[] string_scales;
32-
33-
static construct {
34-
unowned var monitor_manager = MonitorManager.get_default ();
35-
36-
if (monitor_manager.fractional_scale_enabled) {
37-
scales = { 0.75, 1.00, 1.25, 1.50, 1.75, 2.00 };
38-
} else {
39-
scales = { 1.00, 2.00 };
40-
}
41-
42-
foreach (var scale in scales) {
43-
string_scales += "%d %%".printf ((int) (scale * 100));
44-
}
45-
}
46-
4730
public signal void set_as_primary ();
4831
public signal void check_position ();
4932
public signal void configuration_changed ();
@@ -256,10 +239,24 @@ public class Display.DisplayWidget : Gtk.Box {
256239

257240
populate_refresh_rates ();
258241

259-
scale_drop_down = new Gtk.DropDown.from_strings (string_scales) {
242+
var scale_drop_down_factory = new Gtk.SignalListItemFactory ();
243+
scale_drop_down_factory.setup.connect ((obj) => {
244+
var list_item = (Gtk.ListItem) obj;
245+
list_item.child = new Gtk.Label (null) { xalign = 0 };
246+
});
247+
scale_drop_down_factory.bind.connect ((obj) => {
248+
var list_item = (Gtk.ListItem) obj;
249+
var item = (VirtualMonitor.Scale) list_item.item;
250+
var scale_label = (Gtk.Label) list_item.child;
251+
scale_label.label = item.string_representation;
252+
});
253+
254+
scale_drop_down = new Gtk.DropDown (virtual_monitor.available_scales, null) {
260255
margin_start = 12,
261-
margin_end = 12
256+
margin_end = 12,
257+
factory = scale_drop_down_factory
262258
};
259+
virtual_monitor.available_scales.bind_property ("selected", scale_drop_down, "selected", BIDIRECTIONAL | SYNC_CREATE);
263260

264261
var scale_label = new Granite.HeaderLabel (_("Scaling factor")) {
265262
mnemonic_widget = scale_drop_down
@@ -432,21 +429,10 @@ public class Display.DisplayWidget : Gtk.Box {
432429
}
433430
});
434431

435-
virtual_monitor.notify["scale"].connect (update_selected_scale);
436-
update_selected_scale ();
437-
scale_drop_down.notify["selected-item"].connect ((drop_down, param_spec) => {
432+
scale_drop_down.notify["selected-item"].connect (() => {
438433
// Prevent breaking autohide by closing popover
439434
popover.popdown ();
440435

441-
var i = ((Gtk.DropDown) drop_down).selected;
442-
443-
if (i < 0 || i > scales.length) {
444-
warning ("Invalid scale selected.");
445-
return;
446-
}
447-
448-
virtual_monitor.scale = scales[i];
449-
450436
configuration_changed ();
451437
});
452438

@@ -460,14 +446,6 @@ public class Display.DisplayWidget : Gtk.Box {
460446
check_position ();
461447
}
462448

463-
private void update_selected_scale () {
464-
for (uint i = 0; i < scales.length; i++) {
465-
if (scales[i] == virtual_monitor.scale) {
466-
scale_drop_down.selected = i;
467-
}
468-
}
469-
}
470-
471449
private void populate_refresh_rates () {
472450
refresh_list_store.clear ();
473451

0 commit comments

Comments
 (0)