File: HeaderLabel.vala

package info (click to toggle)
granite-7 7.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,876 kB
  • sloc: xml: 86; makefile: 9
file content (148 lines) | stat: -rw-r--r-- 4,166 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
/*
 * Copyright 2017-2022 elementary, Inc. (https://elementary.io)
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */


/**
 * HeaderLabel contains a start-aligned {@link Gtk.Label} with the "heading" style class.
 * Optionally it can contain a secondary {@link Gtk.Label} to provide additional context
 */
public class Granite.HeaderLabel : Gtk.Widget {
    [Version (since = "7.7.0")]
    public enum Size {
        H1,
        H2,
        H3,
        H4;

        public string to_string () {
            switch (this) {
                case H1:
                    return "title-1";
                case H2:
                    return "title-2";
                case H3:
                    return "title-3";
                case H4:
                    return "title-4";
                default:
                    return "";
            }
        }
    }

    /**
     * The primary header label string
     */
    public string label { get; construct set; }

    /**
     * The widget to be activated when the labels mnemonic key is pressed. Also sets #this as screenreader label.
     */
    [Version (since = "7.4.0")]
    public Gtk.Widget? mnemonic_widget { get; set; }

    /**
     * The size of #this
     * Only use one {@link Size.H1} per page. It represents the main heading/subject for the whole page
     */
    [Version (since = "7.7.0")]
    public Size size { get; set; default = H4; }

    private Gtk.Label? secondary_label = null;
    /**
     * Optional secondary label string displayed below the header
     */
    [Version (since = "7.1.0")]
    public string? secondary_text {
        get {
            return secondary_label != null ? secondary_label.label : null;
        }
        set {
            if (secondary_label != null) {
                if (value == null || value == "") {
                    secondary_label.unparent ();
                    secondary_label = null;
                } else {
                    secondary_label.label = value;
                }
            } else if (value != null) {
                secondary_label = new Gtk.Label (value) {
                    use_markup = true,
                    wrap = true,
                    xalign = 0
                };
                secondary_label.add_css_class ("subtitle");

                secondary_label.set_parent (this);
            }

            update_accessible_description (value);
        }
    }

    /**
     * Create a new HeaderLabel
     */
    public HeaderLabel (string label) {
        Object (label: label);
    }

    class construct {
        set_css_name ("header");
    }

    static construct {
        set_layout_manager_type (typeof (Gtk.BoxLayout));
    }

    construct {
        var label_widget = new Gtk.Label (label) {
            wrap = true,
            xalign = 0
        };
        label_widget.add_css_class ("heading");
        label_widget.set_parent (this);

        ((Gtk.BoxLayout) get_layout_manager ()).orientation = Gtk.Orientation.VERTICAL;

        bind_property ("label", label_widget, "label");
        bind_property ("mnemonic-widget", label_widget, "mnemonic-widget");

        notify["mnemonic-widget"].connect (() => {
            update_accessible_description (secondary_text);
        });

        update_size ();
        notify["size"].connect (update_size);
    }

    private void update_size () {
        unowned var enum_class = (EnumClass) typeof (Size).class_peek ();
        foreach (unowned var val in enum_class.values) {
            var css_class = ((Size) val.value).to_string ();
            if (css_class != "" && has_css_class (css_class)) {
                remove_css_class (css_class);
            }
        }

        if (size.to_string () == "") {
            return;
        }

        add_css_class (size.to_string ());
    }

    private void update_accessible_description (string? description) {
        if (mnemonic_widget != null) {
            mnemonic_widget.update_property (Gtk.AccessibleProperty.DESCRIPTION, description, -1);
        }
    }

    ~HeaderLabel () {
        while (get_first_child () != null) {
            get_first_child ().unparent ();
        }
    }
}