Skip to content

Commit

Permalink
optimize indicators for error/starting state
Browse files Browse the repository at this point in the history
fixes #11
  • Loading branch information
jonian committed Aug 22, 2022
1 parent dc8cb87 commit 85e74b7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
16 changes: 8 additions & 8 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ const SystemdManager = GObject.registerClass(
const cmdMethod = this._settings.get_enum('command-method')

const services = entries.map(data => JSON.parse(data))
const fetchStates = type => Utils.isServiceActive(
const fetchStates = type => Utils.getServicesState(
type, services.filter(ob => ob.type == type).map(ob => ob.service)
)

const stateTypes = ['system', 'user']
const activeState = stateTypes.reduce(
const unitStates = stateTypes.reduce(
(all, type) => ({ ...all, [type]: fetchStates(type) }), {}
)

services.forEach(({ type, name, service }) => {
const active = activeState[type][service]
const widget = new PopupServiceItem(name, active, showRestart)
const state = unitStates[type][service]
const entry = new PopupServiceItem(name, state, showRestart)

this.menu.addMenuItem(widget)
this.menu.addMenuItem(entry)

widget.connect('toggled', () => {
const action = active ? 'stop' : 'start'
entry.connect('toggled', (actor, active) => {
const action = active ? 'start' : 'stop'
Utils.runServiceAction(cmdMethod, action, type, service)
})

widget.connect('restarted', () => {
entry.connect('restarted', () => {
Utils.runServiceAction(cmdMethod, 'restart', type, service)
this.menu.close()
})
Expand Down
5 changes: 5 additions & 0 deletions [email protected]/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
min-height: 18px;
}

.systemd-manager-error {
color: #e01b24;
font-weight: bold;
}

.systemd-manager-item .toggle-switch {
margin-left: 10px;
}
Expand Down
4 changes: 2 additions & 2 deletions [email protected]/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ function getServicesList(type) {
})
}

function isServiceActive(type, services) {
function getServicesState(type, services) {
const res = systemctl(type, ['is-active', ...services])
const out = Bytes.toString(res[1])

return out.split('\n').reduce(
(all, value, idx) => ({ ...all, [services[idx]]: value == 'active' }), {}
(all, value, idx) => ({ ...all, [services[idx]]: value }), {}
)
}

Expand Down
34 changes: 30 additions & 4 deletions [email protected]/widgets.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
const Atk = imports.gi.Atk
const St = imports.gi.St
const GObject = imports.gi.GObject
const Animation = imports.ui.animation
const Panel = imports.ui.panel
const PopupMenu = imports.ui.popupMenu
const Util = imports.misc.util
const ExtensionUtils = imports.misc.extensionUtils
const Me = ExtensionUtils.getCurrentExtension()
const VERSION = Me.imports.utils.VERSION

const LOADING_STATES = ['reloading', 'activating', 'deactivating', 'maintenance']

var PopupServiceItem = GObject.registerClass({
Signals: {
'restarted': {}
}
}, class PopupServiceItem extends PopupMenu.PopupSwitchMenuItem {
_init(text, active, showRestart) {
_init(text, state, showRestart) {
const loading = LOADING_STATES.includes(state)
const active = state == 'active'
const reactive = loading == false
const failure = state == 'failed'

super._init(text, active, { style_class: 'systemd-manager-item' })

if (failure) {
this.label.add_style_class_name('systemd-manager-error')
}

if (loading) {
const spinner = new Animation.Spinner(Panel.PANEL_ICON_SIZE, {
animate: true,
hideOnStop: true
})

this.insert_child_above(spinner, this.label)

this.reactive = false
spinner.play()
}

if (showRestart) {
const icon = new St.Icon({
icon_name: 'view-refresh-symbolic',
Expand All @@ -22,9 +48,9 @@ var PopupServiceItem = GObject.registerClass({

const button = new St.Button({
x_align: 1,
reactive: true,
can_focus: true,
track_hover: true,
reactive: reactive,
can_focus: reactive,
track_hover: reactive,
accessible_name: 'restart',
style_class: 'system-menu-action systemd-manager-button',
child: icon
Expand Down

0 comments on commit 85e74b7

Please sign in to comment.