Debian Codegraph @

src/widgets/variableRow.js

raw
1 /* exported FlatsealVariableRow */
2
3 /* variableRow.js
4 *
5 * Copyright 2020 Martin Abente Lahaye
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 const {GObject, Gtk} = imports.gi;
22
23 const {FlatsealOverrideStatusIcon} = imports.widgets.overrideStatusIcon;
24 const {VAR_REGEXP} = imports.models.variables;
25
26 const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT;
27
28 var validity = {
29 VALID: 'valid',
30 NOTVALID: 'not-valid',
31 };
32
33 const _notValidMsg = _('This is not a valid option');
34
35
36 var FlatsealVariableRow = GObject.registerClass({
37 GTypeName: 'FlatsealVariableRow',
38 Template: 'resource:///com/github/tchx84/Flatseal/widgets/variableRow.ui',
39 InternalChildren: ['entry', 'button', 'image', 'statusBox'],
40 Properties: {
41 text: GObject.ParamSpec.string(
42 'text',
43 'text',
44 'text',
45 _propFlags, ''),
46 },
47 Signals: {
48 'remove-requested': {},
49 },
50 }, class FlatsealVariableRow extends Gtk.Box {
51 _init() {
52 super._init({});
53 this._setup();
54 }
55
56 _setup() {
57 this._expression = new RegExp(VAR_REGEXP);
58
59 this._entry.connect('notify::text', this._changed.bind(this));
60 this._button.connect('clicked', this._remove.bind(this));
61
62 this._statusIcon = new FlatsealOverrideStatusIcon();
63 this._statusBox.append(this._statusIcon);
64
65 this._validate();
66 }
67
68 _remove() {
69 this.emit('remove-requested');
70 }
71
72 _changed() {
73 this._validate();
74 this.notify('text');
75 }
76
77 _validate() {
78 const context = this.get_style_context();
79
80 if (context.has_class(validity.VALID))
81 context.remove_class(validity.VALID);
82 else if (context.has_class(validity.NOTVALID))
83 context.remove_class(validity.NOTVALID);
84
85 if (this._expression.test(this.text)) {
86 context.add_class(validity.VALID);
87 this._image.set_tooltip_text('');
88 } else {
89 context.add_class(validity.NOTVALID);
90 this._image.set_tooltip_text(_notValidMsg);
91 }
92 }
93
94 get text() {
95 if (!this._entry)
96 return '';
97 return this._entry.get_text();
98 }
99
100 set text(text) {
101 if (this.text === text)
102 return;
103 this._entry.set_text(text);
104 }
105
106 get status() {
107 return this._statusIcon.status;
108 }
109
110 set status(status) {
111 this._statusIcon.status = status;
112 }
113 });