File: EditableLabel.vala

package info (click to toggle)
desktopfolder 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,488 kB
  • sloc: xml: 207; sh: 9; makefile: 8; python: 6
file content (246 lines) | stat: -rw-r--r-- 8,048 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 *  Copyright (c) 2017 Lains
 *  Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Co-authored by: Corentin Noël <corentin@elementary.io>
 *
 */

public class DesktopFolder.EditableLabel : Gtk.EventBox {
    /**
     * @name changed
     * @description Signal when the label has been changed
     */
    public signal void changed (string new_title);

    /**
     * @name show_popup
     * @description signal to allow third parties to show a popup when the right button has been pressed
     * @param {Gdk.EventButton} event the event button produced for right click
     * @return {bool} true-> the event has been captured and processed, false otherwise
     */
    public signal bool show_popup (Gdk.EventButton event);

    /**
     * @name on_start_editing
     * @description Signal when the label has changed to entry
     * @deprecated
     */
    public signal void on_start_editing ();

    /**
     * @name on_stop_editing
     * @description Signal when the enyty has changed to label
     * @deprecated
     */
    public signal void on_stop_editing ();

    private Gtk.Label title_label { private set; public get; }
    private Gtk.Entry title_entry;
    private Gtk.Stack stack;

    public string text {
        get {
            return title_label.label;
        }
        set {
            title_label.label = value;
        }
    }

    public bool editing {
        private set {
            if (value) {
                // debug("set editing true");
                title_entry.text = title_label.label;

                // trying to get the same size as label
                Gtk.Allocation label_allocation;
                title_label.get_allocation (out label_allocation);
                title_entry.width_request = label_allocation.width - 40;

                stack.set_visible_child (title_entry);
                title_entry.grab_focus_without_selecting ();
            } else {
                // debug("set editing false");
                title_entry.text = title_entry.text.strip ();
                if (title_entry.text.strip () != "" && title_label.label != title_entry.text) {
                    // title_label.label = title_entry.text;
                    changed (title_entry.text);
                }
                stack.set_visible_child (title_label);
                title_entry.width_request = -1;
            }
        }
        public get {
            return stack.get_visible_child () == title_entry;
        }
    }

    public EditableLabel (string ? title_name) {

        title_label         = new Gtk.Label (title_name);
        title_label.hexpand = true;
        /*
           title_label.valign = Gtk.Align.CENTER;
           title_label.halign = Gtk.Align.FILL;
         */

        title_label.get_style_context ().add_class ("df_label");
        title_label.wrap_mode = Pango.WrapMode.WORD_CHAR;
        title_label.set_line_wrap (true);
        title_label.set_justify (Gtk.Justification.CENTER);
        title_label.set_ellipsize (Pango.EllipsizeMode.END);
        title_label.set_lines (1);
        title_label.valign = Gtk.Align.CENTER;
        title_label.halign = Gtk.Align.FILL;

        title_entry        = new Gtk.Entry ();
        title_entry.halign = Gtk.Align.CENTER;
        title_entry.valign = Gtk.Align.FILL;
        title_entry.expand = true;
        // title_entry.set_style (title_label.get_style ());
        // Minimum entry with
        title_entry.set_width_chars (1);

        stack                 = new Gtk.Stack ();
        stack.transition_type = Gtk.StackTransitionType.CROSSFADE;
        stack.add (title_label);
        stack.add (title_entry);
        add (stack);

        // Clicked on the title
        // This event should be managed only by this.title_label
        this.button_press_event.connect ((event) => {
            if (event.type == Gdk.EventType.@2BUTTON_PRESS) {
                this.start_editing ();
                return true;
            }
            if (event.type == Gdk.EventType.BUTTON_PRESS &&
            (event.button == Gdk.BUTTON_SECONDARY)) {
                return this.show_popup (event);
            }

            return false;
        });

        // If press intro while editting
        this.title_entry.activate.connect (() => {
            // debug ("title_entry.activate.connect");
            this.stop_editing ();
        });

        // focus lost while editing
        this.title_entry.focus_out_event.connect ((event) => {
            // debug ("title_entry.focus_out_event.connect");
            this.stop_editing ();
            return false;
        });

        // keyboard shortcuts
        this.key_release_event.connect (this.on_key);
        this.key_press_event.connect (this.on_key);
    }

    /**
     * @name set_margin
     * @description set the left and right margin for the label and entry widgets
     * @param {int} margin the margin to apply
     */
    public void set_margin (int margin) {
        this.title_label.margin_start = margin;
        this.title_label.margin_end   = margin;
        this.title_entry.margin_start = margin;
        this.title_entry.margin_end   = margin;
    }

    /**
     * @name on_key
     * @description the key event captured for the window
     * @param EventKey event the event produced
     * @return bool @see the on_key signal
     */
    private bool on_key (Gdk.EventKey event) {
        // debug ("EditableLabel on_key, event: %s", event.type == Gdk.EventType.KEY_RELEASE ? "KEY_RELEASE" : event.type == Gdk.EventType.KEY_PRESS ? "KEY_PRESS" : "OTRO");
        int key = (int) event.keyval;
        // debug ("EditableLabel event key %d", key);

        var  mods            = event.state & Gtk.accelerator_get_default_mod_mask ();
        bool control_pressed = ((mods & Gdk.ModifierType.CONTROL_MASK) != 0);

        const int ESCAPE_KEY = 65307;

        if (control_pressed) {
            if (key == 'z' || key == 'Z') {
                this.undo_changes ();
            }
        } else if (key == ESCAPE_KEY) {
            this.undo_changes ();
            this.stop_editing ();
        }

        return true;
    }

    /**
     * @name start_editing
     * @description Actions to be performed to start editing
     */
    public void start_editing () {
        editing = true;
        on_start_editing ();
    }

    /**
     * @name stop_editing
     * @description Actions to be performed to stop editing
     */
    public void stop_editing () {
        if (editing == true) {
            // debug("stop editing");
            editing = false;
            on_stop_editing ();
        }
    }

    /**
     * @name undo_changes
     * @description Actions to be performed to undo chenges when editing
     */
    private void undo_changes () {
        title_entry.text = title_label.label;
    }

    /**
     * @name set_lines
     * @description Sets the number of lines of the Gtk.label component
     * @see Gkt.label
     */
    public void set_lines (int n) {
        this.title_label.set_lines (n);
    }

    /**
     * @name set_lines
     * @description Sets the mode used to ellipsize (add an ellipsis: "...") to
     * the text if there is not enough space to render the entire string.
     * @see Gkt.label
     */
    public void set_ellipsize (Pango.EllipsizeMode mode) {
        this.title_label.set_ellipsize (mode);
    }

}