|
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) |
17 | 4 | * |
18 | 5 | * Authored by: Christian Dywan <christian@twotoasts.de> |
19 | 6 | * Corentin Noël <corentin@elementaryos.org> |
20 | 7 | */ |
21 | 8 |
|
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; |
24 | 12 |
|
25 | | - Gtk.Label more_label; |
26 | | - Gtk.Box main_box; |
| 13 | + construct { |
| 14 | + more_label = new Gtk.Label ("") { |
| 15 | + valign = END |
| 16 | + }; |
27 | 17 |
|
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); |
35 | 20 |
|
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 | + } |
39 | 23 |
|
40 | | - children.sort (compare_children); |
| 24 | + public override void add (Gtk.Widget widget) { |
| 25 | + var children = main_box.get_children (); |
| 26 | + children.append (widget); |
41 | 27 |
|
42 | | - int index = children.index (widget); |
43 | | - main_box.add (widget); |
44 | | - main_box.reorder_child (widget, index); |
| 28 | + children.sort (compare_children); |
45 | 29 |
|
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); |
49 | 33 |
|
| 34 | + widget.destroy.connect (() => { |
50 | 35 | queue_resize (); |
51 | | - } |
| 36 | + }); |
52 | 37 |
|
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 | + } |
61 | 40 |
|
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 (); |
73 | 43 |
|
74 | | - bool last = (shown_children == children_length - 1); |
| 44 | + children.sort (compare_children); |
75 | 45 |
|
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 | + } |
80 | 49 |
|
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 | + } |
91 | 58 |
|
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; |
99 | 83 | } |
| 84 | + } else { |
| 85 | + should_hide = false; |
| 86 | + height += child_height; |
100 | 87 | } |
101 | 88 |
|
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 (); |
106 | 92 | } else { |
107 | | - more_label.hide (); |
| 93 | + show_revealer_now ((Gtk.Revealer)child); |
| 94 | + child.show (); |
| 95 | + shown_children++; |
108 | 96 | } |
109 | 97 | } |
110 | 98 |
|
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 (); |
120 | 105 | } |
| 106 | + } |
121 | 107 |
|
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; |
132 | 111 |
|
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 | + } |
139 | 118 |
|
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; |
146 | 122 |
|
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 | + } |
149 | 129 |
|
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 | + } |
154 | 136 |
|
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 | + } |
158 | 143 |
|
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; |
167 | 147 |
|
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 | + }; |
169 | 156 | } |
0 commit comments