aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/util/label_hybrid.vala
blob: d880ba6fa4ac93bc6ca4789ab07b07afe39f9ec5 (plain) (blame)
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
using Gee;
using Gtk;

namespace Dino.Ui.Util {

public class LabelHybrid : Widget {

    public Stack stack = new Stack();
    public Label label = new Label("") { visible=true, max_width_chars=1, ellipsize=Pango.EllipsizeMode.END };
    protected Button button = new Button() { has_frame=false, visible=true };

    internal virtual void init(Widget widget) {
        this.layout_manager = new BinLayout();
        stack.insert_after(this, null);
        button.child = label;
        stack.add_named(button, "label");
        stack.add_named(widget, "widget");

        button.clicked.connect(() => {
            show_widget();
        });
    }

    public void show_widget() {
        stack.visible_child_name = "widget";
        stack.get_child_by_name("widget").grab_focus();
    }

    public void show_label() {
        stack.visible_child_name = "label";
    }
}

public class EntryLabelHybrid : LabelHybrid {

    public string text {
        get { return entry.text; }
        set {
            entry.text = value;
            set_label_label(value);
        }
    }

    public bool visibility {
        get { return entry.visibility; }
        set { entry.visibility = value; }
    }

    public float xalign {
        get { return label.xalign; }
        set {
            label.xalign = value;
            entry.set_alignment(value);
        }
    }

    private Entry? entry_;
    public Entry entry {
        get {
            if (entry_ == null) {
                entry_ = new Entry() { visible=true };
                init(entry_);
            }
            return entry_;
        }
        set { entry_ = value; }
    }

    public EntryLabelHybrid.wrap(Entry e) {
        init(e);
    }

    internal override void init(Widget widget) {
        Entry? e = widget as Entry; if (e == null) return;
        entry = e;
        base.init(entry);
        update_label();

        var key_events = new EventControllerKey();
        key_events.key_released.connect(on_key_released);
        entry.add_controller(key_events);

        var focus_events = new EventControllerFocus();
        focus_events.leave.connect(on_focus_leave);
        entry.add_controller(focus_events);
    }

    private void on_key_released(uint keyval) {
        if (keyval == Gdk.Key.Return) {
            show_label();
        } else {
            set_label_label(entry.text);
        }
    }

    private void on_focus_leave() {
        show_label();
    }

    private void set_label_label(string value) {
        if (visibility) {
            label.label = value;
        } else {
            string filler = "";
            for (int i = 0; i < value.length; i++) filler += entry.get_invisible_char().to_string();
            label.label = filler;
        }
    }

    private void update_label() {
        text = text;
    }
}

public class ComboBoxTextLabelHybrid : LabelHybrid {

    public int active {
        get { return combobox.active; }
        set { combobox.active = value; }
    }

    public float xalign {
        get { return label.xalign; }
        set { label.xalign = value; }
    }

    private ComboBoxText combobox_;
    public ComboBoxText combobox {
        get {
            if (combobox_ == null) {
                combobox_ = new ComboBoxText() { visible=true };
                init(combobox_);
            }
            return combobox_;
        }
        set { combobox_ = combobox; }
    }

    public ComboBoxTextLabelHybrid.wrap(ComboBoxText cb) {
        combobox_ = cb;
        init(cb);
    }

    public void append(string id, string text) { combobox.append(id, text); }
    public string get_active_text() { return combobox.get_active_text(); }

    internal override void init(Widget widget) {
        ComboBoxText? combobox = widget as ComboBoxText; if (combobox == null) return;
        base.init(combobox);
        update_label();

        combobox.changed.connect(() => {
            update_label();
            show_label();
        });
        button.clicked.connect(() => {
            combobox.popup();
        });

        var focus_events = new EventControllerFocus();
        focus_events.leave.connect(on_focus_leave);
        combobox.add_controller(focus_events);
    }

    private void on_focus_leave() {
            update_label();
            show_label();
    }

    private void update_label() {
        label.label = combobox.get_active_text();
    }
}

public class LabelHybridGroup {

    private Gee.List<LabelHybrid> hybrids = new ArrayList<LabelHybrid>();

    public void add(LabelHybrid hybrid) {
        hybrids.add(hybrid);

        hybrid.notify["visible-child-name"].connect(() => {
            if (hybrid.stack.visible_child_name == "label") return;
            foreach (LabelHybrid h in hybrids) {
                if (h != hybrid) {
                    h.stack.set_visible_child_name("label");
                }
            }
        });
    }
}

}