File: base-headerbar.vala

package info (click to toggle)
gnome-taquin 3.38.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,184 kB
  • sloc: xml: 81; python: 11; makefile: 6
file content (309 lines) | stat: -rw-r--r-- 10,670 bytes parent folder | download | duplicates (2)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
  This file is part of GNOME Taquin

  GNOME Taquin 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.

  GNOME Taquin 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 GNOME Taquin.  If not, see <https://www.gnu.org/licenses/>.
*/

using Gtk;

[GtkTemplate (ui = "/org/gnome/Taquin/ui/base-headerbar.ui")]
private class BaseHeaderBar : NightTimeAwareHeaderBar, AdaptativeWidget
{
    [GtkChild] protected Box center_box;

    construct
    {
        center_box.valign = Align.FILL;

        register_modes ();
    }

    /*\
    * * properties
    \*/

    private bool has_a_phone_size = false;
    protected bool disable_popovers = false;
    protected bool disable_action_bar = false;
    protected virtual void set_window_size (AdaptativeWidget.WindowSize new_size)
    {
        has_a_phone_size = AdaptativeWidget.WindowSize.is_phone_size (new_size);
        disable_popovers = has_a_phone_size
                        || AdaptativeWidget.WindowSize.is_extra_thin (new_size);

        bool _disable_action_bar = disable_popovers
                                || AdaptativeWidget.WindowSize.is_extra_flat (new_size);
        if (disable_action_bar != _disable_action_bar)
        {
            disable_action_bar = _disable_action_bar;
            if (disable_action_bar)
            {
                set_show_close_button (false);
                quit_button_stack.show ();
                ltr_right_separator.visible = current_mode_id == default_mode_id;
            }
            else
            {
                ltr_right_separator.hide ();
                quit_button_stack.hide ();
                set_show_close_button (true);
            }
        }

        update_hamburger_menu ();
    }

    /*\
    * * hamburger menu
    \*/

    [CCode (notify = false)] public string about_action_label     { internal get; protected construct; } // TODO add default = _("About");
    [CCode (notify = false)] public bool   has_help               { private  get; protected construct; default = false; }
    [CCode (notify = false)] public bool   has_keyboard_shortcuts { private  get; protected construct; default = false; }

    protected override void update_hamburger_menu ()
    {
        GLib.Menu menu = new GLib.Menu ();

/*        if (current_type == ViewType.OBJECT && !ModelUtils.is_folder_path (current_path))   // TODO a better way to copy various representations of a key name/value/path
        {
            Variant variant = new Variant.string (model.get_suggested_key_copy_text (current_path, browser_view.last_context_id));
            menu.append (_("Copy descriptor"), "app.copy(" + variant.print (false) + ")");
        }
        else if (current_type != ViewType.SEARCH) */

        populate_menu (ref menu);

        append_app_actions_section (ref menu);

        menu.freeze ();
        info_button.set_menu_model ((MenuModel) menu);
    }

    protected virtual void populate_menu (ref GLib.Menu menu) {}

    private void append_app_actions_section (ref GLib.Menu menu)    // FIXME mnemonics?
    {
        GLib.Menu section = new GLib.Menu ();
        append_or_not_night_mode_entry (ref section);
        append_or_not_keyboard_shortcuts_entry (has_keyboard_shortcuts, !has_a_phone_size, ref section);
        append_or_not_help_entry (has_help, ref section);
        append_about_entry (about_action_label, ref section);
        section.freeze ();
        menu.append_section (null, section);
    }

    private static inline void append_or_not_keyboard_shortcuts_entry (bool      has_keyboard_shortcuts,
                                                                       bool      show_keyboard_shortcuts,
                                                                   ref GLib.Menu section)
    {
        // TODO something in small windows
        if (!has_keyboard_shortcuts || !show_keyboard_shortcuts)
            return;

        /* Translators: usual menu entry of the hamburger menu (with a mnemonic that appears pressing Alt) */
        section.append (_("_Keyboard Shortcuts"), "win.show-help-overlay");
    }

    private static inline void append_or_not_help_entry (bool has_help, ref GLib.Menu section)
    {
        if (!has_help)
            return;

        /* Translators: usual menu entry of the hamburger menu (with a mnemonic that appears pressing Alt) */
        section.append (_("_Help"), "base.help");
    }

    private static inline void append_about_entry (string about_action_label, ref GLib.Menu section)
    {
        section.append (about_action_label, "base.about");
    }

    protected inline void hide_hamburger_menu ()
    {
        if (info_button.active)
            info_button.active = false;
    }

    internal void toggle_hamburger_menu ()
    {
        if (info_button.visible)
            info_button.active = !info_button.active;
        else
            toggle_view_menu ();
    }
    protected virtual void toggle_view_menu () {}

    /*\
    * * modes
    \*/

    protected signal void change_mode (uint8 mode_id);

    private uint8 last_mode_id = 0; // 0 is default mode
    protected uint8 register_new_mode ()
    {
        return ++last_mode_id;
    }

    protected static bool is_not_requested_mode (uint8 mode_id, uint8 requested_mode_id, ref bool mode_is_active)
    {
        if (mode_id == requested_mode_id)
        {
            if (mode_is_active)
                assert_not_reached ();
            mode_is_active = true;
            return false;
        }
        else
        {
            mode_is_active = false;
            return true;
        }
    }

    private uint8 current_mode_id = default_mode_id;
    private void register_modes ()
    {
        register_default_mode ();
        register_about_mode ();

        this.change_mode.connect (update_current_mode_id);
    }
    private static void update_current_mode_id (BaseHeaderBar _this, uint8 requested_mode_id)
    {
        _this.current_mode_id = requested_mode_id;
    }

    /*\
    * * default widgets
    \*/

    [GtkChild] private   Button     go_back_button;
    [GtkChild] private   Separator  ltr_left_separator;
    [GtkChild] private   Label      title_label;
    [GtkChild] private   MenuButton info_button;
    [GtkChild] private   Separator  ltr_right_separator;

    [GtkChild] protected Stack      quit_button_stack;

    protected void set_default_widgets_states (string?  title_label_text_or_null,
                                               bool     show_go_back_button,
                                               bool     show_ltr_left_separator,
                                               bool     show_info_button,
                                               bool     show_ltr_right_separator,
                                               bool     show_quit_button_stack)
    {
        go_back_button.visible = show_go_back_button;
        ltr_left_separator.visible = show_ltr_left_separator;
        if (title_label_text_or_null == null)
        {
            title_label.set_label ("");
            title_label.hide ();
        }
        else
        {
            title_label.set_label ((!) title_label_text_or_null);
            title_label.show ();
        }
        info_button.visible = show_info_button;
        ltr_right_separator.visible = show_ltr_right_separator;
        quit_button_stack.visible = show_quit_button_stack;
    }

    /*\
    * * default mode
    \*/

    protected const uint8 default_mode_id = 0;
    private bool default_mode_on = true;
    protected bool no_in_window_mode { get { return default_mode_on; }}

    internal void show_default_view ()
    {
        change_mode (default_mode_id);
    }

    private void register_default_mode ()
    {
        this.change_mode.connect (mode_changed_default);
    }

    private static void mode_changed_default (BaseHeaderBar _this, uint8 requested_mode_id)
    {
        if (is_not_requested_mode (default_mode_id, requested_mode_id, ref _this.default_mode_on))
            return;

        _this.set_default_widgets_default_states (_this);
    }

    protected virtual void set_default_widgets_default_states (BaseHeaderBar _this)
    {
        _this.set_default_widgets_states (/* title_label text or null */ null,
                                          /* show go_back_button      */ false,
                                          /* show ltr_left_separator  */ false,
                                          /* show info_button         */ true,
                                          /* show ltr_right_separator */ _this.disable_action_bar,
                                          /* show quit_button_stack   */ _this.disable_action_bar);
    }

    /*\
    * * about mode
    \*/

    private uint8 about_mode_id = 0;
    private bool about_mode_on = false;

    internal void show_about_view ()
        requires (about_mode_id > 0)
    {
        change_mode (about_mode_id);
    }

    private void register_about_mode ()
    {
        about_mode_id = register_new_mode ();

        this.change_mode.connect (mode_changed_about);
    }

    private static void mode_changed_about (BaseHeaderBar _this, uint8 requested_mode_id)
        requires (_this.about_mode_id > 0)
    {
        if (is_not_requested_mode (_this.about_mode_id, requested_mode_id, ref _this.about_mode_on))
            return;

        /* Translators: on really small windows, the about dialog is replaced by an in-window view; here is the name of the view, displayed in the headerbar */
        _this.set_default_widgets_states (_("About"),   /* title_label text or null */
                                          true,         /* show go_back_button      */
                                          false,        /* show ltr_left_separator  */
                                          false,        /* show info_button         */
                                          false,        /* show ltr_right_separator */
                                          true);        /* show quit_button_stack   */
    }

    /*\
    * * popovers methods
    \*/

    internal virtual void close_popovers ()
    {
        hide_hamburger_menu ();
    }

    internal virtual bool has_popover ()
    {
        return info_button.active;
    }
}