Skip to content

Commit 9a66b7a

Browse files
authored
VAutoHider: clean up (#898)
1 parent 7276d59 commit 9a66b7a

1 file changed

Lines changed: 120 additions & 133 deletions

File tree

src/Grid/VAutoHider.vala

Lines changed: 120 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,156 @@
1-
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
2-
/*-
3-
* Copyright (c) 2011-2015 Maya Developers (http://launchpad.net/maya)
4-
*
5-
* This program is free software: you can redistribute it and/or modify
6-
* it under the terms of the GNU Library General Public License as published by
7-
* the Free Software Foundation, either version 2.1 of the License, or
8-
* (at your option) any later version.
9-
*
10-
* This program is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU Library General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU Library General Public License
16-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* SPDX-FileCopyrightText: 2011-2025 elementary, Inc. (https://elementary.io)
174
*
185
* Authored by: Christian Dywan <christian@twotoasts.de>
196
* Corentin Noël <corentin@elementaryos.org>
207
*/
218

22-
namespace Maya.View {
23-
public class VAutoHider : Gtk.Bin {
9+
public class Maya.View.VAutoHider : Gtk.Bin {
10+
private Gtk.Label more_label;
11+
private Gtk.Box main_box;
2412

25-
Gtk.Label more_label;
26-
Gtk.Box main_box;
13+
construct {
14+
more_label = new Gtk.Label ("") {
15+
valign = END
16+
};
2717

28-
public VAutoHider () {
29-
more_label = new Gtk.Label ("");
30-
more_label.set_alignment (0.5f, 1);
31-
main_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
32-
main_box.pack_end (more_label);
33-
base.add (main_box);
34-
}
18+
main_box = new Gtk.Box (VERTICAL, 0);
19+
main_box.pack_end (more_label);
3520

36-
public override void add (Gtk.Widget widget) {
37-
List<weak Gtk.Widget> children = main_box.get_children ();
38-
children.append (widget);
21+
base.add (main_box);
22+
}
3923

40-
children.sort (compare_children);
24+
public override void add (Gtk.Widget widget) {
25+
var children = main_box.get_children ();
26+
children.append (widget);
4127

42-
int index = children.index (widget);
43-
main_box.add (widget);
44-
main_box.reorder_child (widget, index);
28+
children.sort (compare_children);
4529

46-
widget.destroy.connect (() => {
47-
queue_resize ();
48-
});
30+
int index = children.index (widget);
31+
main_box.add (widget);
32+
main_box.reorder_child (widget, index);
4933

34+
widget.destroy.connect (() => {
5035
queue_resize ();
51-
}
36+
});
5237

53-
public override void size_allocate (Gtk.Allocation allocation) {
54-
base.size_allocate (allocation);
55-
int global_height = allocation.height;
56-
int children_length = (int)main_box.get_children ().length () - 1;
57-
if (children_length == 0) {
58-
more_label.hide ();
59-
return;
60-
}
38+
queue_resize ();
39+
}
6140

62-
int height = 0;
63-
int more_label_height;
64-
int shown_children = 0;
65-
more_label.show ();
66-
more_label.vexpand = false;
67-
more_label.get_preferred_height (out more_label_height, null);
68-
more_label.vexpand = true;
69-
more_label.hide ();
70-
foreach (var child in main_box.get_children ()) {
71-
if (child == more_label)
72-
continue;
41+
public void update (Gtk.Widget widget) {
42+
var children = main_box.get_children ();
7343

74-
bool last = (shown_children == children_length - 1);
44+
children.sort (compare_children);
7545

76-
int child_height;
77-
child.show ();
78-
child.get_preferred_height (out child_height, null);
79-
child.hide ();
46+
int index = children.index (widget);
47+
main_box.reorder_child (widget, index);
48+
}
8049

81-
bool should_hide;
82-
if (global_height - more_label_height < child_height + height) {
83-
should_hide = true;
84-
if (last && (global_height >= child_height + height)) {
85-
should_hide = false;
86-
}
87-
} else {
88-
should_hide = false;
89-
height += child_height;
90-
}
50+
public override void size_allocate (Gtk.Allocation allocation) {
51+
base.size_allocate (allocation);
52+
int global_height = allocation.height;
53+
int children_length = (int)main_box.get_children ().length () - 1;
54+
if (children_length == 0) {
55+
more_label.hide ();
56+
return;
57+
}
9158

92-
if (should_hide) {
93-
hide_revealer_now ((Gtk.Revealer)child);
94-
child.hide ();
95-
} else {
96-
show_revealer_now ((Gtk.Revealer)child);
97-
child.show ();
98-
shown_children++;
59+
int height = 0;
60+
int more_label_height;
61+
int shown_children = 0;
62+
more_label.show ();
63+
more_label.vexpand = false;
64+
more_label.get_preferred_height (out more_label_height, null);
65+
more_label.vexpand = true;
66+
more_label.hide ();
67+
foreach (var child in main_box.get_children ()) {
68+
if (child == more_label)
69+
continue;
70+
71+
bool last = (shown_children == children_length - 1);
72+
73+
int child_height;
74+
child.show ();
75+
child.get_preferred_height (out child_height, null);
76+
child.hide ();
77+
78+
bool should_hide;
79+
if (global_height - more_label_height < child_height + height) {
80+
should_hide = true;
81+
if (last && (global_height >= child_height + height)) {
82+
should_hide = false;
9983
}
84+
} else {
85+
should_hide = false;
86+
height += child_height;
10087
}
10188

102-
int more = children_length - shown_children;
103-
if (shown_children != children_length && more > 0) {
104-
more_label.show ();
105-
more_label.set_label (_("%u more…").printf ((uint)more));
89+
if (should_hide) {
90+
hide_revealer_now ((Gtk.Revealer)child);
91+
child.hide ();
10692
} else {
107-
more_label.hide ();
93+
show_revealer_now ((Gtk.Revealer)child);
94+
child.show ();
95+
shown_children++;
10896
}
10997
}
11098

111-
private void hide_revealer_now (Gtk.Revealer revealer) {
112-
if (revealer.child_revealed == false)
113-
return;
114-
115-
var reveal_duration = revealer.transition_duration;
116-
revealer.transition_duration = 0;
117-
revealer.set_reveal_child (false);
118-
revealer.transition_duration = reveal_duration;
119-
revealer.hide ();
99+
int more = children_length - shown_children;
100+
if (shown_children != children_length && more > 0) {
101+
more_label.show ();
102+
more_label.set_label (_("%u more…").printf ((uint)more));
103+
} else {
104+
more_label.hide ();
120105
}
106+
}
121107

122-
private void show_revealer_now (Gtk.Revealer revealer) {
123-
if (revealer.child_revealed == true)
124-
return;
125-
126-
var reveal_duration = revealer.transition_duration;
127-
revealer.transition_duration = 0;
128-
revealer.set_reveal_child (true);
129-
revealer.transition_duration = reveal_duration;
130-
revealer.show ();
131-
}
108+
private void hide_revealer_now (Gtk.Revealer revealer) {
109+
if (revealer.child_revealed == false)
110+
return;
132111

133-
public override void get_preferred_width (out int minimum_width, out int natural_width) {
134-
base.get_preferred_width (out minimum_width, out natural_width);
135-
more_label.get_preferred_width (out minimum_width, null);
136-
if (minimum_width > natural_width)
137-
natural_width = minimum_width;
138-
}
112+
var reveal_duration = revealer.transition_duration;
113+
revealer.transition_duration = 0;
114+
revealer.set_reveal_child (false);
115+
revealer.transition_duration = reveal_duration;
116+
revealer.hide ();
117+
}
139118

140-
public override void get_preferred_height (out int minimum_height, out int natural_height) {
141-
base.get_preferred_height (out minimum_height, out natural_height);
142-
more_label.get_preferred_height (out minimum_height, null);
143-
if (minimum_height > natural_height)
144-
natural_height = minimum_height;
145-
}
119+
private void show_revealer_now (Gtk.Revealer revealer) {
120+
if (revealer.child_revealed == true)
121+
return;
146122

147-
public void update (Gtk.Widget widget) {
148-
List<weak Gtk.Widget> children = main_box.get_children ();
123+
var reveal_duration = revealer.transition_duration;
124+
revealer.transition_duration = 0;
125+
revealer.set_reveal_child (true);
126+
revealer.transition_duration = reveal_duration;
127+
revealer.show ();
128+
}
149129

150-
children.sort (compare_children);
151-
int index = children.index (widget);
152-
main_box.reorder_child (widget, index);
153-
}
130+
public override void get_preferred_width (out int minimum_width, out int natural_width) {
131+
base.get_preferred_width (out minimum_width, out natural_width);
132+
more_label.get_preferred_width (out minimum_width, null);
133+
if (minimum_width > natural_width)
134+
natural_width = minimum_width;
135+
}
154136

155-
public static GLib.CompareFunc<weak Gtk.Widget> compare_children = (a, b) => {
156-
EventButton a2 = a as EventButton;
157-
EventButton b2 = b as EventButton;
137+
public override void get_preferred_height (out int minimum_height, out int natural_height) {
138+
base.get_preferred_height (out minimum_height, out natural_height);
139+
more_label.get_preferred_height (out minimum_height, null);
140+
if (minimum_height > natural_height)
141+
natural_height = minimum_height;
142+
}
158143

159-
if (a2 == null) {
160-
return 1;
161-
} else if (b2 == null) {
162-
return 0;
163-
} else {
164-
return Util.compare_events (a2.comp, b2.comp);
165-
}
166-
};
144+
public static GLib.CompareFunc<weak Gtk.Widget> compare_children = (a, b) => {
145+
EventButton a2 = a as EventButton;
146+
EventButton b2 = b as EventButton;
167147

168-
}
148+
if (a2 == null) {
149+
return 1;
150+
} else if (b2 == null) {
151+
return 0;
152+
} else {
153+
return Util.compare_events (a2.comp, b2.comp);
154+
}
155+
};
169156
}

0 commit comments

Comments
 (0)