|
1 | | -const GObject = imports.gi.GObject |
2 | | -const Main = imports.ui.main |
3 | | -const PanelMenu = imports.ui.panelMenu |
4 | | -const PopupMenu = imports.ui.popupMenu |
5 | | -const St = imports.gi.St |
6 | | -const Me = imports.misc.extensionUtils.getCurrentExtension() |
7 | | -const Convenience = Me.imports.convenience |
8 | | -const PopupServiceItem = Me.imports.widgets.PopupServiceItem |
9 | | -const PopupSettingsItem = Me.imports.widgets.PopupSettingsItem |
10 | | -const Utils = Me.imports.utils |
11 | | - |
12 | | -const SystemdManager = GObject.registerClass( |
13 | | - class SystemdManager extends PanelMenu.Button { |
14 | | - _init() { |
15 | | - this._settings = Convenience.getSettings() |
16 | | - this._settings.connect('changed', () => this._refresh()) |
17 | | - |
18 | | - super._init(0.2, null, false) |
19 | | - |
20 | | - const icon = new St.Icon({ |
21 | | - icon_name: 'system-run-symbolic', |
22 | | - style_class: 'system-status-icon' |
23 | | - }) |
| 1 | +import GObject from 'gi://GObject' |
| 2 | +import St from 'gi://St' |
| 3 | +import * as Ext from 'resource:///org/gnome/shell/extensions/extension.js' |
| 4 | +import * as Main from 'resource:///org/gnome/shell/ui/main.js' |
| 5 | +import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js' |
| 6 | +import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js' |
| 7 | +import * as Utils from './utils.js' |
| 8 | +import { PopupServiceItem, PopupSettingsItem } from './widgets.js' |
| 9 | + |
| 10 | +class SystemdManager extends PanelMenu.Button { |
| 11 | + static { |
| 12 | + GObject.registerClass(this) |
| 13 | + } |
24 | 14 |
|
25 | | - this.add_actor(icon) |
26 | | - this.refresh() |
| 15 | + constructor(ext) { |
| 16 | + super(0.2, null, false) |
27 | 17 |
|
28 | | - this.menu.connect('open-state-changed', () => { |
29 | | - if (this.menu.isOpen) { |
30 | | - this.refresh() |
31 | | - } |
32 | | - }) |
33 | | - } |
| 18 | + this._settings = ext.getSettings() |
| 19 | + this._settings.connect('changed', () => this.refresh(ext)) |
34 | 20 |
|
35 | | - refresh() { |
36 | | - this.menu.removeAll() |
| 21 | + const icon = new St.Icon({ |
| 22 | + icon_name: 'system-run-symbolic', |
| 23 | + style_class: 'system-status-icon' |
| 24 | + }) |
37 | 25 |
|
38 | | - const entries = this._settings.get_strv('systemd') |
39 | | - const showAdd = this._settings.get_boolean('show-add') |
40 | | - const showRestart = this._settings.get_boolean('show-restart') |
41 | | - const showMask = this._settings.get_boolean('show-mask') |
42 | | - const cmdMethod = this._settings.get_enum('command-method') |
| 26 | + this.add_actor(icon) |
| 27 | + this.refresh(ext) |
43 | 28 |
|
44 | | - const services = entries.map(data => JSON.parse(data)) |
45 | | - const fetchStates = (type, flag) => Utils.getServicesState( |
46 | | - type, flag, services.filter(ob => ob.type == type).map(ob => ob.service) |
47 | | - ) |
| 29 | + this.menu.connect('open-state-changed', () => { |
| 30 | + if (this.menu.isOpen) { |
| 31 | + this.refresh(ext) |
| 32 | + } |
| 33 | + }) |
| 34 | + } |
48 | 35 |
|
49 | | - const stateTypes = ['system', 'user'] |
50 | | - const activeStates = stateTypes.reduce( |
51 | | - (all, type) => ({ ...all, [type]: fetchStates(type, 'is-active') }), {} |
52 | | - ) |
| 36 | + refresh(ext) { |
| 37 | + this.menu.removeAll() |
53 | 38 |
|
54 | | - const enabledStates = stateTypes.reduce( |
55 | | - (all, type) => ({...all, [type]: fetchStates(type, 'is-enabled')}), {} |
56 | | - ) |
| 39 | + const entries = this._settings.get_strv('systemd') |
| 40 | + const showAdd = this._settings.get_boolean('show-add') |
| 41 | + const showRestart = this._settings.get_boolean('show-restart') |
| 42 | + const showMask = this._settings.get_boolean('show-mask') |
| 43 | + const cmdMethod = this._settings.get_enum('command-method') |
57 | 44 |
|
58 | | - services.forEach(({ type, name, service }) => { |
59 | | - const isActive = activeStates[type][service] |
60 | | - const isEnabled = enabledStates[type][service] |
| 45 | + const services = entries.map(data => JSON.parse(data)) |
| 46 | + const fetchStates = (type, flag) => Utils.getServicesState( |
| 47 | + type, flag, services.filter(ob => ob.type == type).map(ob => ob.service) |
| 48 | + ) |
61 | 49 |
|
62 | | - const entry = new PopupServiceItem(name, { isActive, isEnabled, showRestart, showMask }) |
63 | | - this.menu.addMenuItem(entry) |
| 50 | + const stateTypes = ['system', 'user'] |
| 51 | + const activeStates = stateTypes.reduce( |
| 52 | + (all, type) => ({ ...all, [type]: fetchStates(type, 'is-active') }), {} |
| 53 | + ) |
64 | 54 |
|
65 | | - entry.connect('toggled', (actor, active) => { |
66 | | - const action = active ? 'start' : 'stop' |
67 | | - Utils.runServiceAction(cmdMethod, action, type, service) |
68 | | - }) |
| 55 | + const enabledStates = stateTypes.reduce( |
| 56 | + (all, type) => ({...all, [type]: fetchStates(type, 'is-enabled')}), {} |
| 57 | + ) |
69 | 58 |
|
70 | | - entry.connect('restarted', () => { |
71 | | - Utils.runServiceAction(cmdMethod, 'restart', type, service) |
72 | | - this.menu.close() |
73 | | - }) |
| 59 | + services.forEach(({ type, name, service }) => { |
| 60 | + const isActive = activeStates[type][service] |
| 61 | + const isEnabled = enabledStates[type][service] |
74 | 62 |
|
75 | | - entry.connect('mask-toggled', (actor, masked) => { |
76 | | - const action = masked ? 'unmask' : 'mask' |
77 | | - Utils.runServiceAction(cmdMethod, action, type, service) |
| 63 | + const entry = new PopupServiceItem(name, { isActive, isEnabled, showRestart, showMask }) |
| 64 | + this.menu.addMenuItem(entry) |
78 | 65 |
|
79 | | - this.menu.close() |
80 | | - }) |
| 66 | + entry.connect('toggled', (actor, active) => { |
| 67 | + const action = active ? 'start' : 'stop' |
| 68 | + Utils.runServiceAction(cmdMethod, action, type, service) |
81 | 69 | }) |
82 | 70 |
|
83 | | - if (showAdd && entries.length > 0) { |
84 | | - const separator = new PopupMenu.PopupSeparatorMenuItem() |
85 | | - this.menu.addMenuItem(separator) |
86 | | - } |
| 71 | + entry.connect('restarted', () => { |
| 72 | + Utils.runServiceAction(cmdMethod, 'restart', type, service) |
| 73 | + this.menu.close() |
| 74 | + }) |
87 | 75 |
|
88 | | - if (showAdd) { |
89 | | - const settings = new PopupSettingsItem('Add services') |
90 | | - this.menu.addMenuItem(settings) |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | -) |
| 76 | + entry.connect('mask-toggled', (actor, masked) => { |
| 77 | + const action = masked ? 'unmask' : 'mask' |
| 78 | + Utils.runServiceAction(cmdMethod, action, type, service) |
95 | 79 |
|
96 | | -let servicesManager = null |
| 80 | + this.menu.close() |
| 81 | + }) |
| 82 | + }) |
97 | 83 |
|
98 | | -function enable() { |
99 | | - servicesManager = new SystemdManager() |
100 | | - Main.panel.addToStatusArea('systemdManager', servicesManager) |
| 84 | + if (showAdd && entries.length > 0) { |
| 85 | + const separator = new PopupMenu.PopupSeparatorMenuItem() |
| 86 | + this.menu.addMenuItem(separator) |
| 87 | + } |
| 88 | + |
| 89 | + if (showAdd) { |
| 90 | + const settings = new PopupSettingsItem(ext, 'Add services') |
| 91 | + this.menu.addMenuItem(settings) |
| 92 | + } |
| 93 | + } |
101 | 94 | } |
102 | 95 |
|
103 | | -function disable() { |
104 | | - servicesManager.destroy() |
105 | | - servicesManager = null |
| 96 | +export default class Extension extends Ext.Extension { |
| 97 | + enable() { |
| 98 | + this.button = new SystemdManager(this) |
| 99 | + Main.panel.addToStatusArea('systemdManager', this.button) |
| 100 | + } |
| 101 | + |
| 102 | + disable() { |
| 103 | + this.button.destroy() |
| 104 | + this.button = null |
| 105 | + } |
106 | 106 | } |
0 commit comments