Skip to content

Commit 6967a99

Browse files
authored
BaseIconGroup: Use FlowBox instead of manual layouting (#484)
1 parent d6dea6e commit 6967a99

File tree

3 files changed

+57
-78
lines changed

3 files changed

+57
-78
lines changed

data/Application.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ icongroup {
8080
padding-bottom: 0;
8181
}
8282

83-
icongroup .icon-group-box {
83+
icongroup .icon-group-bin {
8484
background: alpha(@base_color, 0.4);
8585
box-shadow:
8686
inset 0 -1px 0 0 alpha(@highlight_color, 0.2),
@@ -94,7 +94,7 @@ icongroup .icon-group-box {
9494
border-spacing: 3px;
9595
}
9696

97-
.reduce-transparency icongroup box {
97+
.reduce-transparency icongroup bin {
9898
background: @base_color;
9999
}
100100

src/BaseIconGroup.vala

Lines changed: 47 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,72 @@
44
*/
55

66
public abstract class Dock.BaseIconGroup : BaseItem {
7-
private class EmptyWidget : Gtk.Widget {}
8-
97
private const int MAX_IN_ROW = 2;
108
private const int MAX_N_CHILDREN = MAX_IN_ROW * MAX_IN_ROW;
119

1210
public ListModel icons { get; construct; }
1311

14-
private Gtk.Grid grid;
15-
1612
class construct {
1713
set_css_name ("icongroup");
1814
}
1915

2016
construct {
21-
grid = new Gtk.Grid () {
22-
hexpand = true,
23-
vexpand = true,
17+
var slice = new Gtk.SliceListModel (icons, 0, MAX_N_CHILDREN);
18+
19+
var flow_box = new Gtk.FlowBox () {
20+
max_children_per_line = MAX_IN_ROW,
21+
min_children_per_line = MAX_IN_ROW,
22+
selection_mode = NONE,
2423
halign = CENTER,
25-
valign = CENTER
24+
valign = CENTER,
2625
};
26+
flow_box.bind_model (slice, create_flow_box_child);
2727

28-
var box = new Gtk.Box (VERTICAL, 0);
29-
box.add_css_class ("icon-group-box");
30-
box.append (grid);
31-
32-
overlay.child = box;
33-
34-
update_icons ();
35-
icons.items_changed.connect (update_icons);
36-
notify["icon-size"].connect (update_icons);
28+
var bin = new Granite.Bin () {
29+
child = flow_box
30+
};
31+
bin.add_css_class ("icon-group-bin");
32+
bind_property ("icon-size", bin, "width-request", SYNC_CREATE);
33+
bind_property ("icon-size", bin, "height-request", SYNC_CREATE);
3734

38-
bind_property ("icon-size", box, "width-request", SYNC_CREATE);
39-
bind_property ("icon-size", box, "height-request", SYNC_CREATE);
35+
overlay.child = bin;
4036
}
4137

42-
private void update_icons () {
43-
unowned Gtk.Widget? child;
44-
while ((child = grid.get_first_child ()) != null) {
45-
grid.remove (child);
46-
}
47-
48-
var grid_spacing = get_grid_spacing ();
49-
grid.row_spacing = grid_spacing;
50-
grid.column_spacing = grid_spacing;
51-
52-
var new_pixel_size = get_pixel_size ();
53-
int i;
54-
for (i = 0; i < uint.min (icons.get_n_items (), 4); i++) {
55-
var image = new Gtk.Image.from_gicon ((Icon) icons.get_item (i)) {
56-
pixel_size = new_pixel_size
57-
};
58-
59-
grid.attach (image, i % MAX_IN_ROW, i / MAX_IN_ROW, 1, 1);
60-
}
61-
62-
// We always need to attach at least 3 elements for grid to be square and properly aligned
63-
for (; i < 3; i++) {
64-
var empty_widget = new EmptyWidget ();
65-
empty_widget.set_size_request (new_pixel_size, new_pixel_size);
66-
67-
grid.attach (empty_widget, i % MAX_IN_ROW, i / MAX_IN_ROW, 1, 1);
68-
}
38+
private Gtk.Widget create_flow_box_child (Object? item) {
39+
var image = new Gtk.Image.from_gicon ((Icon) item);
40+
bind_property ("icon-size", image, "pixel-size", SYNC_CREATE, (binding, from_value, ref to_value) => {
41+
var icon_size = from_value.get_int ();
42+
to_value.set_int (get_pixel_size (icon_size));
43+
return true;
44+
});
45+
// We use margin instead of grid spacing because grid spacing in combination with
46+
// min children per line causes the flow box to request the grid spacing as additional width
47+
// even when there is only one child making it off center.
48+
bind_property ("icon-size", image, "margin-start", SYNC_CREATE, icon_size_to_margin);
49+
bind_property ("icon-size", image, "margin-top", SYNC_CREATE, icon_size_to_margin);
50+
bind_property ("icon-size", image, "margin-end", SYNC_CREATE, icon_size_to_margin);
51+
bind_property ("icon-size", image, "margin-bottom", SYNC_CREATE, icon_size_to_margin);
52+
53+
return new Gtk.FlowBoxChild () {
54+
child = image,
55+
can_target = false
56+
};
6957
}
7058

71-
private int get_pixel_size () {
72-
var pixel_size = 8;
73-
74-
switch (icon_size) {
75-
case 64:
76-
pixel_size = 24;
77-
break;
78-
case 48:
79-
pixel_size = 16;
80-
break;
81-
case 32:
82-
pixel_size = 8;
83-
break;
84-
default:
85-
pixel_size = (int) Math.round (icon_size / 3);
86-
break;
87-
}
88-
89-
return pixel_size;
59+
private static bool icon_size_to_margin (Binding binding, Value from_value, ref Value to_value) {
60+
var icon_size = from_value.get_int ();
61+
var pixel_size = get_pixel_size (icon_size);
62+
var spacing = (int) Math.round ((icon_size - pixel_size * MAX_IN_ROW) / 6);
63+
to_value.set_int (spacing);
64+
return true;
9065
}
9166

92-
private int get_grid_spacing () {
93-
var pixel_size = get_pixel_size ();
94-
95-
return (int) Math.round ((icon_size - pixel_size * MAX_IN_ROW) / 3);
67+
private static int get_pixel_size (int for_icon_size) {
68+
switch (for_icon_size) {
69+
case 64: return 24;
70+
case 48: return 16;
71+
case 32: return 8;
72+
default: return (int) Math.round (for_icon_size / 3);
73+
}
9674
}
9775
}

src/WorkspaceSystem/DynamicWorkspaceItem.vala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ public class Dock.DynamicWorkspaceIcon : BaseItem {
1919
};
2020
add_image.add_css_class ("add-image");
2121

22-
// Gtk.Box is used here to keep css nodes consistent with IconGroup
23-
var box = new Gtk.Box (VERTICAL, 0);
24-
box.add_css_class ("icon-group-box");
25-
box.append (add_image);
22+
// Granite.Bin is used here to keep css nodes consistent with IconGroup
23+
var bin = new Granite.Bin () {
24+
child = add_image
25+
};
26+
bin.add_css_class ("icon-group-bin");
2627

27-
overlay.child = box;
28+
overlay.child = bin;
2829

2930
WorkspaceSystem.get_default ().workspace_added.connect (update_active_state);
3031
WorkspaceSystem.get_default ().workspace_removed.connect (update_active_state);
3132
WindowSystem.get_default ().notify["active-workspace"].connect (update_active_state);
3233

33-
dock_settings.bind ("icon-size", box, "width-request", DEFAULT);
34-
dock_settings.bind ("icon-size", box, "height-request", DEFAULT);
34+
dock_settings.bind ("icon-size", bin, "width-request", DEFAULT);
35+
dock_settings.bind ("icon-size", bin, "height-request", DEFAULT);
3536

3637
dock_settings.bind_with_mapping (
3738
"icon-size", add_image, "pixel_size", DEFAULT | GET,

0 commit comments

Comments
 (0)