Debian Codegraph @

src/utils/EditableLabel.vala

raw
1 /*
2 * Copyright (c) 2017 Lains
3 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Co-authored by: Corentin Noël <[email protected]>
19 *
20 */
21
22 public class DesktopFolder.EditableLabel : Gtk.EventBox {
23 /**
24 * @name changed
25 * @description Signal when the label has been changed
26 */
27 public signal void changed (string new_title);
28
29 /**
30 * @name show_popup
31 * @description signal to allow third parties to show a popup when the right button has been pressed
32 * @param {Gdk.EventButton} event the event button produced for right click
33 * @return {bool} true-> the event has been captured and processed, false otherwise
34 */
35 public signal bool show_popup (Gdk.EventButton event);
36
37 /**
38 * @name on_start_editing
39 * @description Signal when the label has changed to entry
40 * @deprecated
41 */
42 public signal void on_start_editing ();
43
44 /**
45 * @name on_stop_editing
46 * @description Signal when the enyty has changed to label
47 * @deprecated
48 */
49 public signal void on_stop_editing ();
50
51 public Gtk.Label title_label { private set; public get; }
52 private Gtk.Entry title_entry;
53 private Gtk.Stack stack;
54
55 public string text {
56 get {
57 return title_label.label;
58 }
59 set {
60 title_label.label = value;
61 }
62 }
63
64 public bool editing {
65 private set {
66 if (value) {
67 // debug("set editing true");
68 title_entry.text = title_label.label;
69
70 // trying to get the same size as label
71 this.update_entry_width ();
72
73 stack.set_visible_child (title_entry);
74 title_entry.grab_focus ();
75 } else {
76 // debug("set editing false");
77 title_entry.text = title_entry.text.strip ();
78 if (title_entry.text.strip () != "" && title_label.label != title_entry.text) {
79 // title_label.label = title_entry.text;
80 changed (title_entry.text);
81 }
82 stack.set_visible_child (title_label);
83 title_entry.width_request = -1;
84 }
85 }
86 public get {
87 return stack.get_visible_child () == title_entry;
88 }
89 }
90
91 public EditableLabel (string ? title_name) {
92
93 title_label = new Gtk.Label (title_name);
94 title_label.hexpand = true;
95 /*
96 title_label.valign = Gtk.Align.CENTER;
97 title_label.halign = Gtk.Align.FILL;
98 */
99
100 title_label.get_style_context ().add_class ("df_label");
101 title_label.wrap_mode = Pango.WrapMode.WORD_CHAR;
102 title_label.set_line_wrap (true);
103 title_label.set_justify (Gtk.Justification.CENTER);
104 title_label.set_ellipsize (Pango.EllipsizeMode.END);
105 title_label.set_lines (1);
106 title_label.valign = Gtk.Align.CENTER;
107 title_label.halign = Gtk.Align.FILL;
108
109 title_entry = new Gtk.Entry ();
110 title_entry.halign = Gtk.Align.CENTER;
111 title_entry.valign = Gtk.Align.FILL;
112 title_entry.expand = true;
113 // title_entry.set_style (title_label.get_style ());
114 // Minimum entry with
115 title_entry.set_width_chars (1);
116
117 stack = new Gtk.Stack ();
118 stack.transition_type = Gtk.StackTransitionType.CROSSFADE;
119 stack.add (title_label);
120 stack.add (title_entry);
121 add (stack);
122
123 // Clicked on the title
124 // This event should be managed only by this.title_label
125 this.button_press_event.connect ((event) => {
126 if (event.type == Gdk.EventType.@2BUTTON_PRESS) {
127 this.start_editing ();
128 return true;
129 }
130 if (event.type == Gdk.EventType.BUTTON_PRESS &&
131 (event.button == Gdk.BUTTON_SECONDARY)) {
132 return this.show_popup (event);
133 }
134
135 return false;
136 });
137
138 // If press intro while editting
139 this.title_entry.activate.connect (() => {
140 // debug ("title_entry.activate.connect");
141 this.stop_editing ();
142 });
143
144 // focus lost while editing
145 this.title_entry.focus_out_event.connect ((event) => {
146 // debug ("title_entry.focus_out_event.connect");
147 this.stop_editing ();
148 return false;
149 });
150
151 // keyboard shortcuts
152 this.key_release_event.connect (this.on_key);
153 this.key_press_event.connect (this.on_key);
154 }
155
156 /**
157 * @name set_margin
158 * @description set the left and right margin for the label and entry widgets
159 * @param {int} margin the margin to apply
160 */
161 public void set_margin (int margin) {
162 this.title_label.margin_start = margin;
163 this.title_label.margin_end = margin;
164 this.title_entry.margin_start = margin;
165 this.title_entry.margin_end = margin;
166 }
167
168 /**
169 * @name on_key
170 * @description the key event captured for the window
171 * @param EventKey event the event produced
172 * @return bool @see the on_key signal
173 */
174 private bool on_key (Gdk.EventKey event) {
175 // debug ("EditableLabel on_key, event: %s", event.type == Gdk.EventType.KEY_RELEASE ? "KEY_RELEASE" : event.type == Gdk.EventType.KEY_PRESS ? "KEY_PRESS" : "OTRO");
176 int key = (int) event.keyval;
177 // debug ("EditableLabel event key %d", key);
178
179 var mods = event.state & Gtk.accelerator_get_default_mod_mask ();
180 bool control_pressed = ((mods & Gdk.ModifierType.CONTROL_MASK) != 0);
181
182 const int ESCAPE_KEY = 65307;
183
184 if (control_pressed) {
185 if (key == 'z' || key == 'Z') {
186 this.undo_changes ();
187 }
188 } else if (key == ESCAPE_KEY) {
189 // debug("undo changes");
190 this.undo_changes ();
191 this.stop_editing ();
192 } else {
193 this.update_entry_width ();
194 }
195
196 return true;
197 }
198
199 /**
200 * Updates Entry width to fit its content
201 */
202 private void update_entry_width () {
203 int width, height;
204 const int margin = 10;
205 title_entry.get_layout ().get_size (out width, out height);
206 width = (width / Pango.SCALE) + margin;
207
208 Gtk.Allocation label_allocation;
209 title_label.get_allocation (out label_allocation);
210
211 if (width > label_allocation.width) {
212 title_entry.width_request = label_allocation.width;
213 } else {
214 title_entry.width_request = width;
215 }
216 }
217
218 /**
219 * @name start_editing
220 * @description Actions to be performed to start editing
221 */
222 public void start_editing () {
223 editing = true;
224 on_start_editing ();
225 }
226
227 /**
228 * @name stop_editing
229 * @description Actions to be performed to stop editing
230 */
231 public void stop_editing () {
232 if (editing == true) {
233 // debug("stop editing");
234 editing = false;
235 on_stop_editing ();
236 }
237 }
238
239 /**
240 * @name undo_changes
241 * @description Actions to be performed to undo chenges when editing
242 */
243 private void undo_changes () {
244 title_entry.text = title_label.label;
245 }
246
247 /**
248 * @name set_lines
249 * @description Sets the number of lines of the Gtk.label component
250 * @see Gkt.label
251 */
252 public void set_lines (int n) {
253 this.title_label.set_lines (n);
254 }
255
256 /**
257 * @name set_lines
258 * @description Sets the mode used to ellipsize (add an ellipsis: "...") to
259 * the text if there is not enough space to render the entire string.
260 * @see Gkt.label
261 */
262 public void set_ellipsize (Pango.EllipsizeMode mode) {
263 this.title_label.set_ellipsize (mode);
264 }
265
266 }