Debian Codegraph @

src/widgets/globalInfoViewer.js

raw
1 /* exported FlatsealGlobalInfoViewer */
2 /* eslint accessor-pairs: */
3
4 /* globalInfoViewer.js
5 *
6 * Copyright 2022 Martin Abente Lahaye
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 const {GObject, Gtk} = imports.gi;
23
24 const _propFlags = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT;
25 const {info, permissions, portals} = imports.models;
26
27 const styles = {
28 NORMAL: 'normal',
29 COMPACT: 'compact',
30 };
31
32 var FlatsealGlobalInfoViewer = GObject.registerClass({
33 GTypeName: 'FlatsealGlobalInfoViewer',
34 Template: 'resource:///com/github/tchx84/Flatseal/widgets/globalInfoViewer.ui',
35 InternalChildren: ['icon', 'title', 'description', 'flatpak', 'portal', 'overrides'],
36 Properties: {
37 compact: GObject.ParamSpec.boolean(
38 'compact',
39 'compact',
40 'compact',
41 _propFlags, false),
42 },
43 }, class FlatsealGlobalInfoViewer extends Gtk.Box {
44 _init() {
45 super._init();
46 this._compact = false;
47 this._setup();
48 }
49
50 _setup() {
51 this.constructor._setIfAvailable(this._flatpak, info.getDefault().getVersion());
52 this.constructor._setIfAvailable(this._portal, portals.getDefault().getVersion());
53 this.constructor._setIfAvailable(this._overrides, permissions.getDefault()._getBaseOverridesPath());
54 }
55
56 static _setIfAvailable(label, value) {
57 if (!value)
58 return;
59 label.set_label(value.toString());
60 }
61
62 set compact(value) {
63 if (typeof this._icon === 'undefined')
64 return;
65 if (this._compact === value)
66 return;
67
68 const orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL;
69 const alignment = value ? Gtk.Align.CENTER : Gtk.Align.START;
70
71 this.set_orientation(orientation);
72 this._icon.halign = alignment;
73 this._title.halign = alignment;
74 this._description.halign = alignment;
75 this.halign = alignment;
76
77 const style = value ? styles.COMPACT : styles.NORMAL;
78 const context = this.get_style_context();
79
80 if (context.has_class(styles.NORMAL))
81 context.remove_class(styles.NORMAL);
82 if (context.has_class(styles.COMPACT))
83 context.remove_class(styles.COMPACT);
84
85 context.add_class(style);
86
87 this._compact = value;
88 }
89 });