quick-settings: rebuild grid on CC open, dynamic tile visibility

parent 4286390e
namespace XimperShellNotificationCenter.Widgets { namespace XimperShellNotificationCenter.Widgets {
private struct TileRow { private struct TileRow {
Gtk.Box row_box;
Gtk.Revealer revealer; Gtk.Revealer revealer;
QuickSettingsTile ?active_submenu_tile; QuickSettingsTile ?active_submenu_tile;
} }
...@@ -24,16 +25,15 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -24,16 +25,15 @@ namespace XimperShellNotificationCenter.Widgets {
case "caffeine": case "caffeine":
tile = new CaffeineTile (cfg); tile = new CaffeineTile (cfg);
break; break;
case "night-light":
tile = new NightLightTile (cfg);
break;
default: default:
warning ("Unknown quick-settings tile: %s", warning ("Unknown quick-settings tile: %s",
type_id); type_id);
return null; return null;
} }
if (!tile.is_available ()) {
return null;
}
return tile; return tile;
} }
...@@ -45,7 +45,9 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -45,7 +45,9 @@ namespace XimperShellNotificationCenter.Widgets {
} }
List<QuickSettingsTile> tiles = new List<QuickSettingsTile> (); List<QuickSettingsTile> tiles = new List<QuickSettingsTile> ();
List<Gtk.Widget> grid_children = new List<Gtk.Widget> ();
TileRow[] rows = {}; TileRow[] rows = {};
Gtk.SizeGroup ?size_group = null;
int columns = 2; int columns = 2;
public QuickSettings (string suffix) { public QuickSettings (string suffix) {
...@@ -86,15 +88,71 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -86,15 +88,71 @@ namespace XimperShellNotificationCenter.Widgets {
tiles.append (tile); tiles.append (tile);
} }
// Connect signals once
foreach (unowned QuickSettingsTile tile in tiles) {
tile.submenu_requested.connect (() => {
int row = find_tile_row (tile);
if (row >= 0) {
on_submenu_requested (tile, row);
}
});
tile.submenu_update_requested.connect (() => {
int row = find_tile_row (tile);
if (row >= 0) {
update_submenu (tile, row);
}
});
}
build_grid (); build_grid ();
} }
private int find_tile_row (QuickSettingsTile tile) {
for (int i = 0; i < rows.length; i++) {
for (var child = rows[i].row_box.get_first_child ();
child != null;
child = child.get_next_sibling ()) {
if (child == tile) return i;
}
}
return -1;
}
private void clear_grid () {
// Unparent tiles from row boxes
foreach (unowned QuickSettingsTile tile in tiles) {
if (tile.get_parent () != null) {
tile.unparent ();
}
}
// Remove row boxes and revealers
foreach (unowned Gtk.Widget child in grid_children) {
remove (child);
}
grid_children = new List<Gtk.Widget> ();
rows = {};
size_group = null;
}
private void build_grid () { private void build_grid () {
clear_grid ();
int row_index = 0; int row_index = 0;
unowned List<QuickSettingsTile> iter = tiles; size_group = new Gtk.SizeGroup (
var size_group = new Gtk.SizeGroup (
Gtk.SizeGroupMode.VERTICAL); Gtk.SizeGroupMode.VERTICAL);
// Collect available tiles
List<unowned QuickSettingsTile> visible_tiles =
new List<unowned QuickSettingsTile> ();
foreach (unowned QuickSettingsTile tile in tiles) {
if (tile.is_available ()) {
visible_tiles.append (tile);
}
}
unowned List<unowned QuickSettingsTile> iter =
visible_tiles;
while (iter != null) { while (iter != null) {
var row_box = new Gtk.Box ( var row_box = new Gtk.Box (
Gtk.Orientation.HORIZONTAL, 8); Gtk.Orientation.HORIZONTAL, 8);
...@@ -107,15 +165,13 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -107,15 +165,13 @@ namespace XimperShellNotificationCenter.Widgets {
revealer.set_transition_duration (200); revealer.set_transition_duration (200);
rows += TileRow () { rows += TileRow () {
row_box = row_box,
revealer = revealer, revealer = revealer,
active_submenu_tile = null, active_submenu_tile = null,
}; };
int current_row = row_index;
for (int col = 0; col < columns; col++) { for (int col = 0; col < columns; col++) {
if (iter == null) { if (iter == null) {
// Pad with empty box for alignment
var spacer = new Gtk.Box ( var spacer = new Gtk.Box (
Gtk.Orientation.HORIZONTAL, 0); Gtk.Orientation.HORIZONTAL, 0);
row_box.append (spacer); row_box.append (spacer);
...@@ -125,20 +181,13 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -125,20 +181,13 @@ namespace XimperShellNotificationCenter.Widgets {
unowned QuickSettingsTile tile = iter.data; unowned QuickSettingsTile tile = iter.data;
size_group.add_widget (tile); size_group.add_widget (tile);
row_box.append (tile); row_box.append (tile);
// Connect submenu signals
tile.submenu_requested.connect (() => {
on_submenu_requested (tile, current_row);
});
tile.submenu_update_requested.connect (() => {
update_submenu (tile, current_row);
});
iter = iter.next; iter = iter.next;
} }
append (row_box); append (row_box);
append (revealer); append (revealer);
grid_children.append (row_box);
grid_children.append (revealer);
row_index++; row_index++;
} }
} }
...@@ -183,17 +232,21 @@ namespace XimperShellNotificationCenter.Widgets { ...@@ -183,17 +232,21 @@ namespace XimperShellNotificationCenter.Widgets {
} }
public override void on_cc_visibility_change (bool val) { public override void on_cc_visibility_change (bool val) {
if (!val) { // Let tiles update their visibility first
foreach (unowned QuickSettingsTile tile in tiles) {
tile.on_cc_visibility_change (val);
}
if (val) {
// Rebuild grid with updated visibility
build_grid ();
} else {
// Close all submenus // Close all submenus
for (int i = 0; i < rows.length; i++) { for (int i = 0; i < rows.length; i++) {
rows[i].revealer.set_reveal_child (false); rows[i].revealer.set_reveal_child (false);
rows[i].active_submenu_tile = null; rows[i].active_submenu_tile = null;
} }
} }
foreach (unowned QuickSettingsTile tile in tiles) {
tile.on_cc_visibility_change (val);
}
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment