File: SwitchModelButton.vala

package info (click to toggle)
granite-7 7.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,956 kB
  • sloc: python: 10; makefile: 5
file content (94 lines) | stat: -rw-r--r-- 2,931 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
/*
 * Copyright 2021 elementary, Inc. (https://elementary.io)
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/**
 * SwitchModelButton is a {@link Gtk.ToggleButton} containing a {@link Gtk.Label}
 * and a {@link Gtk.Switch} and using the menuitem css name. It can optionally
 * show description text when activated.
 *
 * ''Example''<<BR>>
 * {{{
 *   var switchmodelbutton = new Granite.SwitchModelButton ("With Description") {
 *       active = true,
 *       description = "A description of additional affects related to the activation state of this switch"
 *   };
 * }}}
 */
public class Granite.SwitchModelButton : Gtk.ToggleButton {
    /**
     * The label for the button.
     */
    public string text { get; construct set; }

    /**
     * Small, dim description text shown when active.
     */
    public string? description { get; set; }

    public SwitchModelButton (string text) {
        Object (text: text);
    }

    class construct {
        set_css_name ("modelbutton");
    }

    construct {
        var label = new Gtk.Label (text) {
            ellipsize = Pango.EllipsizeMode.MIDDLE,
            halign = Gtk.Align.START,
            hexpand = true,
            vexpand = true,
            max_width_chars = 25,
        };

        var description_label = new Gtk.Label (null) {
            max_width_chars = 25,
            wrap = true,
            xalign = 0
        };

        unowned var description_style_context = description_label.get_style_context ();
        description_style_context.add_class (Granite.STYLE_CLASS_SMALL_LABEL);
        description_style_context.add_class (Granite.STYLE_CLASS_DIM_LABEL);

        var description_revealer = new Gtk.Revealer () {
            child = description_label
        };

        var button_switch = new Gtk.Switch () {
            valign = Gtk.Align.START
        };

        var grid = new Gtk.Grid () {
            column_spacing = 12
        };
        grid.attach (label, 0, 0);
        grid.attach (button_switch, 1, 0, 1, 2);

        child = grid;

        bind_property ("text", label, "label");
        bind_property ("description", description_label, "label");
        bind_property ("active", button_switch, "active", GLib.BindingFlags.BIDIRECTIONAL);

        var controller = new Gtk.GestureClick ();
        add_controller (controller);

        // Binding active doesn't trigger the switch animation; we must listen and manually activate
        controller.released.connect (() => {
            button_switch.activate ();
        });

        notify["description"].connect (() => {
            if (description == null || description == "") {
                grid.remove (description_revealer);
            } else {
                grid.attach (description_revealer, 0, 1);
                button_switch.bind_property ("active", description_revealer, "reveal-child", GLib.BindingFlags.SYNC_CREATE);
            }
        });
    }
}