diff --git a/src/Widgets/DisplaysOverlay.vala b/src/Widgets/DisplaysOverlay.vala index c87bb2f4..f60e0f14 100644 --- a/src/Widgets/DisplaysOverlay.vala +++ b/src/Widgets/DisplaysOverlay.vala @@ -36,6 +36,8 @@ public class Display.DisplaysOverlay : Gtk.Overlay { private unowned Display.MonitorManager monitor_manager; public int active_displays { get; set; default = 0; } + private List display_widgets; + private static Gtk.CssProvider display_provider; private static string[] colors = { @@ -62,11 +64,15 @@ public class Display.DisplaysOverlay : Gtk.Overlay { @define-color TEXT_COLOR %s; """; - public DisplaysOverlay () { - var grid = new Gtk.Grid (); + construct { + var grid = new Gtk.Grid () { + hexpand = true, + vexpand = true + }; grid.get_style_context ().add_class (Gtk.STYLE_CLASS_VIEW); - grid.expand = true; - add (grid); + child = grid; + + display_widgets = new List (); monitor_manager = Display.MonitorManager.get_default (); monitor_manager.notify["virtual-monitor-number"].connect (() => rescan_displays ()); @@ -107,11 +113,10 @@ public class Display.DisplaysOverlay : Gtk.Overlay { public void rescan_displays () { scanning = true; - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - child.destroy (); - } - }); + foreach (unowned var widget in display_widgets) { + display_widgets.remove (widget); + widget.destroy (); + } active_displays = 0; foreach (var virtual_monitor in monitor_manager.virtual_monitors) { @@ -129,31 +134,25 @@ public class Display.DisplaysOverlay : Gtk.Overlay { return; } - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - if (((DisplayWidget) child).virtual_monitor.is_active) { - ((DisplayWidget) child).display_window.show_all (); - } + foreach (unowned var widget in display_widgets) { + if (widget.virtual_monitor.is_active) { + widget.display_window.show_all (); } - }); + } } public void hide_windows () { - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - ((DisplayWidget) child).display_window.hide (); - } - }); + foreach (unowned var widget in display_widgets) { + widget.display_window.hide (); + }; } private void change_active_displays_sensitivity () { - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - if (((DisplayWidget) child).virtual_monitor.is_active) { - ((DisplayWidget) child).only_display = (active_displays == 1); - } + foreach (unowned var widget in display_widgets) { + if (widget.virtual_monitor.is_active) { + widget.only_display = (active_displays == 1); } - }); + } } private void check_configuration_changed () { @@ -167,18 +166,15 @@ public class Display.DisplaysOverlay : Gtk.Overlay { int max_width = int.MIN; int max_height = int.MIN; - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - var display_widget = (DisplayWidget) child; - int x, y, width, height; - display_widget.get_geometry (out x, out y, out width, out height); - - added_width += width; - added_height += height; - max_width = int.max (max_width, x + width); - max_height = int.max (max_height, y + height); - } - }); + foreach (unowned var display_widget in display_widgets) { + int x, y, width, height; + display_widget.get_geometry (out x, out y, out width, out height); + + added_width += width; + added_height += height; + max_width = int.max (max_width, x + width); + max_height = int.max (max_height, y + height); + } current_allocated_width = get_allocated_width (); current_allocated_height = get_allocated_height (); @@ -195,6 +191,8 @@ public class Display.DisplaysOverlay : Gtk.Overlay { current_allocated_width = 0; current_allocated_height = 0; add_overlay (display_widget); + display_widgets.append (display_widget); + var provider = new Gtk.CssProvider (); try { var color_number = (get_children ().length () - 2) % 7; @@ -274,15 +272,13 @@ public class Display.DisplaysOverlay : Gtk.Overlay { } private void set_as_primary (Display.VirtualMonitor new_primary) { - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - var display_widget = child as DisplayWidget; - var virtual_monitor = display_widget.virtual_monitor; - var is_primary = virtual_monitor == new_primary; - display_widget.set_primary (is_primary); - virtual_monitor.primary = is_primary; - } - }); + foreach (unowned var widget in display_widgets) { + var virtual_monitor = widget.virtual_monitor; + var is_primary = virtual_monitor == new_primary; + widget.set_primary (is_primary); + virtual_monitor.primary = is_primary; + } + foreach (var virtual_monitor in monitor_manager.virtual_monitors) { virtual_monitor.primary = virtual_monitor == new_primary; } @@ -318,12 +314,12 @@ public class Display.DisplaysOverlay : Gtk.Overlay { widget_points [4] = y + height / 2 - 1; // y_center widget_points [5] = y + height - 1; // y_end - foreach (var child in get_children ()) { - if (!(child is DisplayWidget) || (DisplayWidget) child == display_widget) { + foreach (unowned var widget in display_widgets) { + if (widget == display_widget) { continue; } - var anchor = (DisplayWidget) child; + var anchor = widget; anchor.get_geometry (out x, out y, out width, out height); anchor_points [0] = x; // x_start anchor_points [1] = x + width / 2 - 1; // x_center @@ -361,16 +357,9 @@ public class Display.DisplaysOverlay : Gtk.Overlay { } private void close_gaps () { - var display_widgets = new List (); - foreach (var child in get_children ()) { - if (child is DisplayWidget) { - display_widgets.append ((DisplayWidget) child); - } - } - - foreach (var display_widget in display_widgets) { - if (!is_connected (display_widget, display_widgets)) { - snap_edges (display_widget); + foreach (unowned var widget in display_widgets) { + if (!is_connected (widget, display_widgets)) { + snap_edges (widget); } } } @@ -405,28 +394,23 @@ public class Display.DisplaysOverlay : Gtk.Overlay { private void verify_global_positions () { int min_x = int.MAX; int min_y = int.MAX; - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - var display_widget = (DisplayWidget) child; - int x, y, width, height; - display_widget.get_geometry (out x, out y, out width, out height); - min_x = int.min (min_x, x); - min_y = int.min (min_y, y); - } - }); + + foreach (unowned var display_widget in display_widgets) { + int x, y, width, height; + display_widget.get_geometry (out x, out y, out width, out height); + min_x = int.min (min_x, x); + min_y = int.min (min_y, y); + } if (min_x == 0 && min_y == 0) { return; } - get_children ().foreach ((child) => { - if (child is DisplayWidget) { - var display_widget = (DisplayWidget) child; - int x, y, width, height; - display_widget.get_geometry (out x, out y, out width, out height); - display_widget.set_geometry (x - min_x, y - min_y, width, height); - } - }); + foreach (unowned var display_widget in display_widgets) { + int x, y, width, height; + display_widget.get_geometry (out x, out y, out width, out height); + display_widget.set_geometry (x - min_x, y - min_y, width, height); + } } // If widget is intersects with any other widgets -> move other widgets to fix intersection @@ -440,12 +424,11 @@ public class Display.DisplaysOverlay : Gtk.Overlay { source_display_widget.get_geometry (out source_x, out source_y, out source_width, out source_height); Gdk.Rectangle src_rect = { source_x, source_y, source_width, source_height }; - foreach (var child in get_children ()) { - if (!(child is DisplayWidget) || (DisplayWidget) child == source_display_widget) { + foreach (unowned var other_display_widget in display_widgets) { + if (other_display_widget == source_display_widget) { continue; } - var other_display_widget = (DisplayWidget) child; int other_x, other_y, other_width, other_height; other_display_widget.get_geometry (out other_x, out other_y, out other_width, out other_height); Gdk.Rectangle test_rect = { other_x, other_y, other_width, other_height }; @@ -478,10 +461,13 @@ public class Display.DisplaysOverlay : Gtk.Overlay { // Snap last_moved debug ("Snapping displays"); var anchors = new List (); - get_children ().foreach ((child) => { - if (!(child is DisplayWidget) || last_moved.equals ((DisplayWidget)child)) return; - anchors.append ((DisplayWidget) child); - }); + + foreach (unowned var widget in display_widgets) { + if (last_moved.equals (widget)) { + return; + } + anchors.append (widget); + } snap_widget (last_moved, anchors);