File: AccelLabel.vala

package info (click to toggle)
granite 6.2.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,768 kB
  • sloc: python: 10; makefile: 8
file content (113 lines) | stat: -rw-r--r-- 3,539 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
/*
 * Copyright 2019 elementary, Inc. (https://elementary.io)
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/**
 * AccelLabel is meant to be used as a {@link Gtk.MenuItem} child for displaying
 * a {@link GLib.Action}'s accelerator alongside the Menu Item label.
 *
 * The class itself is similar to it's Gtk equivalent {@link Gtk.AccelLabel}
 * but follows elementary OS design conventions. Specifically, this class uses
 * {@link Granite.accel_to_string} for accelerator string parsing.
 *
 * ''Example''<<BR>>
 * {{{
 *   var copy_menuitem = new Gtk.MenuItem ();
 *   copy_menuitem.set_action_name (ACTION_PREFIX + ACTION_COPY);
 *   copy_menuitem.add (new Granite.AccelLabel.from_action_name (_("Copy"), copy_menuitem.action_name));
 * }}}
 *
 */
public class Granite.AccelLabel : Gtk.Grid {
    /**
     * The name of the {@link GLib.Action} used to retrieve action accelerators
     */
    public string action_name { get; construct set; }

    /**
     * A {@link Gtk.accelerator_parse} style accel string like “<Control>a” or “<Super>Right”
     */
    public string? accel_string { get; construct set; }

    /**
     * The user-facing menu item label
     */
    public string label { get; construct set; }

    /**
     * Creates a new AccelLabel from a label and an accelerator string
     *
     * @param label displayed to the user as the menu item name
     * @param accel an accelerator label like “<Control>a” or “<Super>Right”
     */
    public AccelLabel (string label, string? accel_string = null) {
        Object (
            label: label,
            accel_string: accel_string
        );
    }

    /**
     * Creates a new AccelLabel from a label and an action name
     *
     * @param label displayed to the user as the menu item name
     * @param action_name name of the {@link GLib.Action} used to retrieve action accelerators
     */
    public AccelLabel.from_action_name (string label, string action_name) {
        Object (
            label: label,
            action_name: action_name
        );
    }

    static construct {
        Granite.init ();
    }

    construct {
        var label = new Gtk.Label (label);
        label.hexpand = true;
        label.margin_end = 6;
        label.xalign = 0;

        column_spacing = 3;
        add (label);

        update_accels ();

        notify["accel-string"].connect (update_accels);
        notify["action-name"].connect (update_accels);

        bind_property ("label", label, "label");
    }

    private void update_accels () {
        GLib.List<unowned Gtk.Widget> list = get_children ();
        for (int i = 0; i < list.length () - 1; i++) {
            list.nth_data (i).destroy ();
        }

        string[] accels = {""};
        if (accel_string != null && accel_string != "") {
            accels = Granite.accel_to_string (accel_string).split (" + ");
        } else if (action_name != null && action_name != "") {
            accel_string = ((Gtk.Application) GLib.Application.get_default ()).get_accels_for_action (action_name)[0];
        }

        if (accels[0] != "") {
            foreach (unowned string accel in accels) {
                if (accel == "") {
                    continue;
                }
                var accel_label = new Gtk.Label (accel);

                var accel_label_context = accel_label.get_style_context ();
                accel_label_context.add_class (Granite.STYLE_CLASS_KEYCAP);

                add (accel_label);
            }
        }
        show_all ();
    }
}