summaryrefslogtreecommitdiff
path: root/raveos-gnome-theme/theme-data/extensions/installed/appindicatorsupport@rgcjonas.gmail.com/preferences/customIconPage.js
blob: b1325624a7db1628c581d8c8a68f84affe788918 (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
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
/* exported  GeneralPage*/

import Adw from 'gi://Adw';
import Gtk from 'gi://Gtk';
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import {gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';

const IconData = GObject.registerClass({
    GTypeName: 'IconData',
    Properties: {
        'id': GObject.ParamSpec.string(
            'id',
            'Id',
            'A read and write string property',
            GObject.ParamFlags.READWRITE,
            ''
        ),
        'name': GObject.ParamSpec.string(
            'name',
            'Name',
            'A read and write string property',
            GObject.ParamFlags.READWRITE,
            ''
        ),
        'at-name': GObject.ParamSpec.string(
            'at-name',
            'At Name',
            'A read and write string property',
            GObject.ParamFlags.READWRITE,
            ''
        ),

    },
}, class IconData extends GObject.Object {
    constructor(props = {}) {
        super(props);
    }

    get id() {
        if (this._id === 'undefined')
            this._id = null;
        return this._id;
    }

    set id(value) {
        if (this.id === value)
            return;

        this._id = value;
        this.notify('id');
    }

    get name() {
        if (this._name === 'undefined')
            this._name = null;
        return this._name;
    }

    set name(value) {
        if (this.name === value)
            return;

        this._name = value;
        this.notify('name');
    }

    get atName() {
        if (this._atName === 'undefined')
            this._atName = null;
        return this._atName;
    }

    set atName(value) {
        if (this.atName === value)
            return;

        this._atName = value;
        this.notify('at-name');
    }
});

export var CustomIconPage = GObject.registerClass(
class AppIndicatorCustomIconPage extends Adw.PreferencesPage {
    _init(settings, settingsKey) {
        super._init({
            title: _('Custom Icons'),
            icon_name: 'custom-icons-symbolic',
            name: 'Custom Icons Page',
        });
        this._settings = settings;
        this._settingsKey = settingsKey;

        this.group = new Adw.PreferencesGroup();

        this.container = new Gtk.Box({
            halign: Gtk.Align.FILL,
            hexpand: true,
            orientation: Gtk.Orientation.VERTICAL,
        });

        this.listStore = new Gio.ListStore({
            item_type: IconData,
        });

        const initList = this._settings.get_value(this._settingsKey.CUSTOM_ICONS).deep_unpack();
        initList.forEach(pair => {
            let data = new IconData({
                id: pair[0],
                name: pair[1],
                atName: pair[2],
            });
            this.listStore.append(data);
        });

        this.listStore.append(new IconData({id: '', name: '', atName: ''}));

        this.selectionModel = new Gtk.SingleSelection({
            model: this.listStore,
        });

        this.columnView = new Gtk.ColumnView({
            hexpand: true,
            model: this.selectionModel,
            reorderable: false,
        });

        const columnTitles =  [
            _('Indicator ID'),
            _('Icon Name'),
            _('Attention Icon Name'),
        ];
        const indicatorIdColumn = new Gtk.ColumnViewColumn({
            title: columnTitles[0],
            expand: true,
        });
        const iconNameColumn = new Gtk.ColumnViewColumn({
            title: columnTitles[1],
            expand: true,
        });
        const attentionIconNameColumn = new Gtk.ColumnViewColumn({
            title: columnTitles[2],
            expand: true,
        });


        const factoryId = new Gtk.SignalListItemFactory();
        factoryId.connect('setup', (_widget, item) => {
            const label = new Gtk.EditableLabel({text: ''});
            item.set_child(label);
        });
        factoryId.connect('bind', (_widget, item) => {
            const label = item.get_child();
            const data = item.get_item();
            label.set_text(data.id);
            label.bind_property('text', data, 'id', GObject.BindingFlags.SYNC_CREATE);
            label.connect('changed', () => this._autoSave(item));
        });

        const factoryName = new Gtk.SignalListItemFactory();
        factoryName.connect('setup', (_widget, item) => {
            const label = new Gtk.EditableLabel({text: ''});
            item.set_child(label);
        });
        factoryName.connect('bind', (_widget, item) => {
            const label = item.get_child();
            const data = item.get_item();
            label.set_text(data.name);
            label.bind_property('text', data, 'name', GObject.BindingFlags.SYNC_CREATE);
            label.connect('changed', () => this._autoSave(item));
        });

        const factoryItName = new Gtk.SignalListItemFactory();
        factoryItName.connect('setup', (_widget, item) => {
            const label = new Gtk.EditableLabel({text: ''});
            item.set_child(label);
        });
        factoryItName.connect('bind', (_widget, item) => {
            const label = item.get_child();
            const data = item.get_item();
            label.set_text(data.atName);
            label.bind_property('text', data, 'at-name', GObject.BindingFlags.SYNC_CREATE);
            label.connect('changed', () => this._autoSave(item));
        });

        indicatorIdColumn.set_factory(factoryId);
        iconNameColumn.set_factory(factoryName);
        attentionIconNameColumn.set_factory(factoryItName);

        this.columnView.append_column(indicatorIdColumn);
        this.columnView.append_column(iconNameColumn);
        this.columnView.append_column(attentionIconNameColumn);

        this.container.append(this.columnView);

        this.group.add(this.container);
        this.add(this.group);
    }

    _autoSave(item) {
        const storeLength = this.listStore.get_n_items();
        const newStore = [];

        for (let i = 0; i < storeLength; i++) {
            const currentItem = this.listStore.get_item(i);
            const id = currentItem.id;
            const name = currentItem.name;
            const atName = currentItem.atName;
            if (id && name)
                newStore.push([id, name, atName || '']);
        }

        this._settings.set_value(this._settingsKey.CUSTOM_ICONS,
            new GLib.Variant('a(sss)', newStore));

        const id = item.get_item().id;
        const name = item.get_item().name;

        /* dynamic new entry*/
        if (storeLength === 1 && (id || name))
            this.listStore.append(new IconData({id: '', name: '', atName: ''}));

        if (storeLength > 1) {
            if ((id || name) && item.get_position() >= storeLength - 1)
                this.listStore.append(new IconData({id: '', name: '', atName: ''}));
        }
    }

    _autoDelete(item) {
        const storeLength = this.listStore.get_n_items();
        const id = item.get_item().id;
        const name = item.get_item().name;
        if (storeLength > 1 && !id && !name &&
            item.get_position() < storeLength - 1)
            this.listStore.remove(item.get_position());
    }
});