File: base-window.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 (553 lines) | stat: -rw-r--r-- 17,878 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
  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;

private interface BaseApplication : Gtk.Application
{
    internal abstract void copy (string text);

    // same as about dialog; skipped: wrap_license, license, license_type (forced at GPL v3+ == License.GPL_3_0), logo
    internal abstract void get_about_dialog_infos (out string [] artists,
                                                   out string [] authors,
                                                   out string    comments,
                                                   out string    copyright,
                                                   out string [] documenters,
                                                   out string    logo_icon_name,
                                                   out string    program_name,
                                                   out string    translator_credits,
                                                   out string    version,
                                                   out string    website,
                                                   out string    website_label);
}

[GtkTemplate (ui = "/org/gnome/Taquin/ui/base-window.ui")]
private class BaseWindow : AdaptativeWindow, AdaptativeWidget
{
    private BaseView main_view;
    [CCode (notify = false)] public BaseView base_view
    {
        protected get { return main_view; }
        protected construct
        {
            BaseView? _value = value;
            if (_value == null)
                assert_not_reached ();

            main_view = value;
            value.vexpand = true;
            value.visible = true;
            add_to_main_grid (value);
        }
    }

    private BaseHeaderBar headerbar;

    construct
    {
        headerbar = (BaseHeaderBar) nta_headerbar;

        init_keyboard ();

        install_action_entries ();

        add_adaptative_child (headerbar);
        add_adaptative_child (main_view);
        add_adaptative_child (this);
    }

    /*\
    * * main layout
    \*/

    [GtkChild] private Grid main_grid;
    [GtkChild] private Button unfullscreen_button;
    [GtkChild] private Overlay main_overlay;

    protected void add_to_main_overlay (Widget widget)
    {
        main_overlay.add_overlay (widget);
    }

    protected void add_to_main_grid (Widget widget)
    {
        main_grid.add (widget);
    }

    protected override void on_fullscreen ()
    {
        unfullscreen_button.show ();
    }

    protected override void on_unfullscreen ()
    {
        unfullscreen_button.hide ();
    }

    /*\
    * * action entries
    \*/

    protected SimpleAction escape_action;

    private void install_action_entries ()
    {
        SimpleActionGroup action_group = new SimpleActionGroup ();
        action_group.add_action_entries (action_entries, this);
        insert_action_group ("base", action_group);

        GLib.Action? tmp_action = action_group.lookup_action ("escape");
        if (tmp_action == null)
            assert_not_reached ();
        escape_action = (SimpleAction) (!) tmp_action;
        escape_action.set_enabled (false);
    }

    private const GLib.ActionEntry [] action_entries =
    {
        { "copy",               copy                },  // <P>c
        { "copy-alt",           copy_alt            },  // <P>C

        { "paste",              paste               },  // <P>v
        { "paste-alt",          paste_alt           },  // <P>V

        { "escape",             _escape_pressed     },  // Escape
        { "toggle-hamburger",   toggle_hamburger    },  // F10
        { "menu",               menu_pressed        },  // Menu

        { "show-default-view",  show_default_view   },

        { "help",               help                },
        { "about",              about               },

        { "unfullscreen",       unfullscreen        }
    };

    /*\
    * * keyboard copy actions
    \*/

    protected virtual bool handle_copy_text (out string copy_text)
    {
        return main_view.handle_copy_text (out copy_text);
    }
    protected virtual bool get_alt_copy_text (out string copy_text)
    {
        return no_copy_text (out copy_text);
    }
    internal static bool no_copy_text (out string copy_text)
    {
        copy_text = "";
        return false;
    }
    internal static bool copy_clipboard_text (out string copy_text)
    {
        string? nullable_selection = Clipboard.@get (Gdk.SELECTION_PRIMARY).wait_for_text ();
        if (nullable_selection != null)
        {
             string selection = ((!) nullable_selection).dup ();
             if (selection != "")
             {
                copy_text = selection;
                return true;
             }
        }
        return no_copy_text (out copy_text);
    }
    internal static inline bool is_empty_text (string text)
    {
        return text == "";
    }

    private void copy (/* SimpleAction action, Variant? path_variant */)
    {
        Widget? focus = get_focus ();
        if (focus != null)
        {
            if ((!) focus is Editable)  // GtkEntry, GtkSearchEntry, GtkSpinButton
            {
                int garbage1, garbage2;
                if (((Editable) (!) focus).get_selection_bounds (out garbage1, out garbage2))
                {
                    ((Editable) (!) focus).copy_clipboard ();
                    return;
                }
            }
            else if ((!) focus is TextView)
            {
                if (((TextView) (!) focus).get_buffer ().get_has_selection ())
                {
                    ((TextView) (!) focus).copy_clipboard ();
                    return;
                }
            }
            else if ((!) focus is Label)
            {
                int garbage1, garbage2;
                if (((Label) (!) focus).get_selection_bounds (out garbage1, out garbage2))
                {
                    ((Label) (!) focus).copy_clipboard ();
                    return;
                }
            }
        }

        main_view.close_popovers ();

        string text;
        if (handle_copy_text (out text))
            copy_text (text);
    }

    private void copy_alt (/* SimpleAction action, Variant? path_variant */)
    {
        if (main_view.is_in_in_window_mode ())        // TODO better
            return;

        main_view.close_popovers ();

        string text;
        if (get_alt_copy_text (out text))
            copy_text (text);
    }

    private inline void copy_text (string text)
        requires (!is_empty_text (text))
    {
        ((BaseApplication) get_application ()).copy ((!) text);
    }

    /*\
    * * keyboard paste actions
    \*/

    protected virtual void paste_text (string? text) {}

    private void paste (/* SimpleAction action, Variant? variant */)
    {
        if (main_view.is_in_in_window_mode ())
            return;

        Widget? focus = get_focus ();
        if (focus != null)
        {
            if ((!) focus is Entry)
            {
                ((Entry) (!) focus).paste_clipboard ();
                return;
            }
            if ((!) focus is TextView)
            {
                ((TextView) (!) focus).paste_clipboard ();
                return;
            }
        }

        paste_clipboard_content ();
    }

    private void paste_alt (/* SimpleAction action, Variant? variant */)
    {
        close_in_window_panels ();

        paste_clipboard_content ();
    }

    private void paste_clipboard_content ()
    {
        Gdk.Display? display = Gdk.Display.get_default ();
        if (display == null)    // ?
            return;

        string? clipboard_content;
        if (get_clipboard_content (out clipboard_content))
            paste_text (clipboard_content);
    }

    private static inline bool get_clipboard_content (out string? clipboard_content)
    {
        Gdk.Display? display = Gdk.Display.get_default ();
        if (display == null)            // ?
        {
            clipboard_content = null;   // garbage
            return false;
        }

        clipboard_content = Clipboard.get_default ((!) display).wait_for_text ();
        return true;
    }

    /*\
    * * keyboard open menus actions
    \*/

    private void toggle_hamburger (/* SimpleAction action, Variant? variant */)
    {
        headerbar.toggle_hamburger_menu ();
        main_view.close_popovers ();
    }

    protected virtual void menu_pressed (/* SimpleAction action, Variant? variant */)
    {
        headerbar.toggle_hamburger_menu ();
        main_view.close_popovers ();
    }

    /*\
    * * keyboard user actions
    \*/

    private Gtk.EventControllerKey key_controller;    // for keeping in memory

    private void init_keyboard ()  // called on construct
    {
        key_controller = new Gtk.EventControllerKey (this);
        key_controller.key_pressed.connect (on_key_pressed);
    }

    protected inline bool on_key_pressed (Gtk.EventControllerKey _key_controller, uint keyval, uint keycode, Gdk.ModifierType state)
    {
        return _on_key_press_event (keyval, state);
    }
    private bool _on_key_press_event (uint keyval, Gdk.ModifierType state)
    {
        string name = (!) (Gdk.keyval_name (keyval) ?? "");

        if (name == "F1") // TODO fix dance done with the F1 & <Primary>F1 shortcuts that show help overlay
        {
            headerbar.close_popovers ();
            main_view.close_popovers ();
            if ((state & Gdk.ModifierType.CONTROL_MASK) != 0)
                return false;                           // help overlay
            if ((state & Gdk.ModifierType.SHIFT_MASK) == 0)
                return show_application_help (this, help_string_or_empty);   // fallback on help overlay (TODO test)
            about ();
            return true;
        }

        return false;
    }

    /*\
    * * help
    \*/

    [CCode (notify = false)] public string help_string_or_empty { private get; protected construct; default = ""; }

    private void help (/* SimpleAction action, Variant? variant */)
    {
        show_application_help (this, help_string_or_empty);
    }

    private static inline bool show_application_help (BaseWindow _this, string help_string_or_empty)
    {
        if (help_string_or_empty == "")
            return false;

        bool success;
        try
        {
            show_uri_on_window (_this, help_string_or_empty, get_current_event_time ());
            success = true;
        }
        catch (Error e)
        {
            warning ("Failed to show help: %s", e.message);
            success = false;
        }
        return success;
    }

    /*\
    * * adaptative stuff
    \*/

    private bool disable_popovers = false;
    protected virtual void set_window_size (AdaptativeWidget.WindowSize new_size)
    {
        bool _disable_popovers = AdaptativeWidget.WindowSize.is_phone_size (new_size)
                              || AdaptativeWidget.WindowSize.is_extra_thin (new_size);
        if (disable_popovers != _disable_popovers)
        {
            disable_popovers = _disable_popovers;
            if (in_window_about)
                show_default_view ();
        }
    }

    /*\
    * * in-window panels
    \*/

    protected virtual void close_in_window_panels ()
    {
        hide_notification ();
        headerbar.close_popovers ();
        if (in_window_about)
            show_default_view ();
    }

    private void _escape_pressed (/* SimpleAction action, Variant? path_variant */)
    {
        escape_pressed ();  // returns true if something is done
    }
    protected virtual bool escape_pressed ()
    {
        if (in_window_about)
        {
            show_default_view ();
            return true;
        }
        return false;
    }

    protected virtual void show_default_view (/* SimpleAction action, Variant? path_variant */)
    {
        if (in_window_about)
        {
            in_window_about = false;
            escape_action.set_enabled (escape_action_enabled);
            headerbar.show_default_view ();
            main_view.show_default_view ();
        }
        else
            assert_not_reached ();
    }

    /*\
    * * about dialog
    \*/

    private AboutDialog about_dialog;
    private Gtk.EventControllerKey about_dialog_key_controller;    // for keeping in memory

    private void show_about_dialog ()
    {
        if (should_init_about_dialog)
        {
            create_about_dialog ();
            about_dialog.response.connect ((_about_dialog, response) => _about_dialog.hide ());
            about_dialog_key_controller = new Gtk.EventControllerKey (about_dialog);
            about_dialog_key_controller.key_pressed.connect (on_about_dialog_key_pressed);
            about_dialog.set_transient_for (this);
            should_init_about_dialog = false;
        }
        about_dialog.run ();
    }
    private inline bool on_about_dialog_key_pressed (Gtk.EventControllerKey _about_dialog_key_controller, uint keyval, uint keycode, Gdk.ModifierType state)
    {
        if (((!) (Gdk.keyval_name (keyval) ?? "") == "F1")
         && ((state & Gdk.ModifierType.SHIFT_MASK) != 0))
        {
            about_dialog.response (ResponseType.CANCEL);
            return true;
        }
        return false;
    }

    private bool should_init_about_dialog = true;
    private void create_about_dialog ()
    {
        string [] artists, authors, documenters;
        string comments, copyright, logo_icon_name, program_name, translator_credits, version, website, website_label;

        ((BaseApplication) get_application ()).get_about_dialog_infos (out artists, out authors, out comments, out copyright, out documenters, out logo_icon_name, out program_name, out translator_credits, out version, out website, out website_label);

        about_dialog = new AboutDialog ();
        about_dialog.set_title (headerbar.about_action_label);
        about_dialog.set_wrap_license (true);
        about_dialog.set_license_type (License.GPL_3_0);    // forced, 1/3
        if (artists.length > 0)         about_dialog.set_artists            (artists);
        if (authors.length > 0)         about_dialog.set_authors            (authors);
        if (comments != "")             about_dialog.set_comments           (comments);
        if (copyright != "")            about_dialog.set_copyright          (copyright);
        if (documenters.length > 0)     about_dialog.set_documenters        (documenters);
        if (logo_icon_name != "")       about_dialog.set_logo_icon_name     (logo_icon_name);
        if (program_name != "")         about_dialog.set_program_name       (program_name);         else assert_not_reached ();
        if (translator_credits != "")   about_dialog.set_translator_credits (translator_credits);
        if (version != "")              about_dialog.set_version            (version);
        if (website != "")              about_dialog.set_website            (website);
        if (website_label != "")        about_dialog.set_website_label      (website_label);
    }

    /*\
    * * in-window about
    \*/

    private void about (/* SimpleAction action, Variant? path_variant */)
    {
        if (disable_popovers)
            toggle_in_window_about ();
        else
            show_about_dialog ();
    }

    [CCode (notify = false)] protected bool in_window_about { protected get; private set; default = false; }

    private void toggle_in_window_about ()
    {
        if (should_init_in_window_about)
        {
            init_in_window_about ();
            should_init_in_window_about = false;
            show_about_view ();
        }
        else if (in_window_about)
            show_default_view ();
        else
            show_about_view ();
    }

    private bool should_init_in_window_about = true;
    private void init_in_window_about ()
    {
        string [] artists, authors, documenters;
        string comments, copyright, logo_icon_name, program_name, translator_credits, version, website, website_label;

        ((BaseApplication) get_application ()).get_about_dialog_infos (out artists, out authors, out comments, out copyright, out documenters, out logo_icon_name, out program_name, out translator_credits, out version, out website, out website_label);

        main_view.create_about_list                                   (ref artists, ref authors, ref comments, ref copyright, ref documenters, ref logo_icon_name, ref program_name, ref translator_credits, ref version, ref website, ref website_label);
    }

    private bool escape_action_enabled = false;
    private inline void show_about_view ()
        requires (in_window_about == false)
    {
        close_in_window_panels ();

        escape_action_enabled = escape_action.enabled;
        escape_action.set_enabled (true);
        in_window_about = true;
        headerbar.show_about_view ();
        main_view.show_about_view ();
        set_focus_visible (false);  // about-list grabs focus
    }

    /*\
    * * notifications
    \*/

    internal void show_notification (string notification)
    {
        main_view.show_notification (notification);
    }

    protected void hide_notification ()
    {
        main_view.hide_notification ();
    }
}