summaryrefslogtreecommitdiffstats
path: root/extensions/45/middleclickclose/src/prefs.js
blob: 91d9f13ca4dbf752231ed00e84997d8e7ab09872 (plain)
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
/**
    This program 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 2 of the License, or
    (at your option) any later version.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
**/

import Adw from 'gi://Adw';
import Gtk from 'gi://Gtk';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';

import { ExtensionPreferences, gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';

export default class MiddleClickClosePreferences extends ExtensionPreferences {
    getPreferencesWidget() {
        let page = new Adw.PreferencesPage();
        let group = new Adw.PreferencesGroup();

        group.add(this.buildPreference("close-button", {
            nicks: {
                left: _("Left"),
                middle: _("Middle"),
                right: _("Right"),
                'button 4': _("Button 4"),
                'button 5': _("Button 5"),
                'button 6': _("Button 6"),
                'button 7': _("Button 7"),
                'button 8': _("Button 8"),
                'button 9': _("Button 9"),
            },
        }));

        group.add(this.buildPreference("rearrange-delay", {
            step: 50,
        }));

        page.add(group);
        return page;
    }

    buildPreference(key, opts) {
        opts ??= {}

        const settings = this.getSettings();
        const setting = settings.create_action(key);
        const schema = settings.settings_schema.get_key(key);

        opts.title ??= schema.get_summary() || schema.get_name();
        opts.subtitle ??= schema.get_description();

        const ty = schema.get_value_type().dup_string();
        const [range_ty, range] = schema.get_range().recursiveUnpack();

        if (range_ty == "enum") {
            opts.nicks ??= {};
            let row = new Adw.ComboRow({
                title: opts.title,
                subtitle: opts.subtitle,
                model: new Gtk.StringList({ strings: range.map(nick => opts.nicks[nick]) }),
                selected: range.indexOf(setting.state.unpack()),
            });

            row.connect('notify::selected', () => {
                setting.activate(GLib.Variant.new_string(range[row.selected]));
            });

            return row;
        } else if (range_ty == "range") {
            opts.lower ??= range[0]
            opts.upper ??= range[1]
        }

        if (["i"].includes(ty)) {
            let adjustment = new Gtk.Adjustment({
                lower: opts.lower,
                upper: opts.upper,
                step_increment: opts.step,
                value: setting.state.unpack(),
            });

            let row = new Adw.SpinRow({
                title: opts.title,
                subtitle: opts.subtitle,
                numeric: true,
                adjustment,
            });

            adjustment.connect("value-changed", adj => {
                setting.activate(GLib.Variant.new_int32(adj.value));
            });

            return row;
        }

        // Yeah... I'm not gonna implement a full introspector here. Just add whatever is required
        // when needed.
        throw new GObject.NotImplementedError();
    }
};