| 1 |
// SPDX-FileCopyrightText: 2012 Giovanni Campagna <[email protected]> |
| 2 |
// SPDX-FileCopyrightText: 2014 Florian Müllner <[email protected]> |
| 3 |
// |
| 4 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
|
| 6 |
import Adw from 'gi://Adw'; |
| 7 |
import Gio from 'gi://Gio'; |
| 8 |
import GioUnix from 'gi://GioUnix'; |
| 9 |
import GLib from 'gi://GLib'; |
| 10 |
import GObject from 'gi://GObject'; |
| 11 |
import Gtk from 'gi://Gtk'; |
| 12 |
|
| 13 |
import {ExtensionPreferences, gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js'; |
| 14 |
|
| 15 |
const SETTINGS_KEY = 'application-list'; |
| 16 |
|
| 17 |
const WORKSPACE_MAX = 36; // compiled in limit of mutter |
| 18 |
|
| 19 |
class NewItem extends GObject.Object {} |
| 20 |
GObject.registerClass(NewItem); |
| 21 |
|
| 22 |
class NewItemModel extends GObject.Object { |
| 23 |
static [GObject.interfaces] = [Gio.ListModel]; |
| 24 |
static { |
| 25 |
GObject.registerClass(this); |
| 26 |
} |
| 27 |
|
| 28 |
#item = new NewItem(); |
| 29 |
|
| 30 |
vfunc_get_item_type() { |
| 31 |
return NewItem; |
| 32 |
} |
| 33 |
|
| 34 |
vfunc_get_n_items() { |
| 35 |
return 1; |
| 36 |
} |
| 37 |
|
| 38 |
vfunc_get_item(_pos) { |
| 39 |
return this.#item; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
class Rule extends GObject.Object { |
| 44 |
static [GObject.properties] = { |
| 45 |
'app-info': GObject.ParamSpec.object( |
| 46 |
'app-info', null, null, |
| 47 |
GObject.ParamFlags.READWRITE, |
| 48 |
GioUnix.DesktopAppInfo), |
| 49 |
'workspace': GObject.ParamSpec.uint( |
| 50 |
'workspace', null, null, |
| 51 |
GObject.ParamFlags.READWRITE, |
| 52 |
1, WORKSPACE_MAX, 1), |
| 53 |
}; |
| 54 |
|
| 55 |
static { |
| 56 |
GObject.registerClass(this); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
class RulesList extends GObject.Object { |
| 61 |
static [GObject.interfaces] = [Gio.ListModel]; |
| 62 |
static { |
| 63 |
GObject.registerClass(this); |
| 64 |
} |
| 65 |
|
| 66 |
#settings; |
| 67 |
#rules = []; |
| 68 |
#changedId; |
| 69 |
|
| 70 |
constructor(settings) { |
| 71 |
super(); |
| 72 |
|
| 73 |
this.#settings = settings; |
| 74 |
this.#changedId = |
| 75 |
this.#settings.connect(`changed::${SETTINGS_KEY}`, |
| 76 |
() => this.#sync()); |
| 77 |
this.#sync(); |
| 78 |
} |
| 79 |
|
| 80 |
append(appInfo) { |
| 81 |
const pos = this.#rules.length; |
| 82 |
|
| 83 |
this.#rules.push(new Rule({appInfo})); |
| 84 |
this.#saveRules(); |
| 85 |
|
| 86 |
this.items_changed(pos, 0, 1); |
| 87 |
} |
| 88 |
|
| 89 |
remove(id) { |
| 90 |
const pos = this.#rules.findIndex(r => r.appInfo.get_id() === id); |
| 91 |
if (pos < 0) |
| 92 |
return; |
| 93 |
|
| 94 |
this.#rules.splice(pos, 1); |
| 95 |
this.#saveRules(); |
| 96 |
|
| 97 |
this.items_changed(pos, 1, 0); |
| 98 |
} |
| 99 |
|
| 100 |
changeWorkspace(id, workspace) { |
| 101 |
const pos = this.#rules.findIndex(r => r.appInfo.get_id() === id); |
| 102 |
if (pos < 0) |
| 103 |
return; |
| 104 |
|
| 105 |
this.#rules[pos].set({workspace}); |
| 106 |
this.#saveRules(); |
| 107 |
} |
| 108 |
|
| 109 |
#saveRules() { |
| 110 |
this.#settings.block_signal_handler(this.#changedId); |
| 111 |
this.#settings.set_strv(SETTINGS_KEY, |
| 112 |
this.#rules.map(r => `${r.app_info.get_id()}:${r.workspace}`)); |
| 113 |
this.#settings.unblock_signal_handler(this.#changedId); |
| 114 |
} |
| 115 |
|
| 116 |
#sync() { |
| 117 |
const removed = this.#rules.length; |
| 118 |
|
| 119 |
this.#rules = []; |
| 120 |
for (const stringRule of this.#settings.get_strv(SETTINGS_KEY)) { |
| 121 |
const [id, workspace] = stringRule.split(':'); |
| 122 |
const appInfo = GioUnix.DesktopAppInfo.new(id); |
| 123 |
if (appInfo) |
| 124 |
this.#rules.push(new Rule({appInfo, workspace})); |
| 125 |
else |
| 126 |
log(`Invalid ID ${id}`); |
| 127 |
} |
| 128 |
this.items_changed(0, removed, this.#rules.length); |
| 129 |
} |
| 130 |
|
| 131 |
vfunc_get_item_type() { |
| 132 |
return Rule; |
| 133 |
} |
| 134 |
|
| 135 |
vfunc_get_n_items() { |
| 136 |
return this.#rules.length; |
| 137 |
} |
| 138 |
|
| 139 |
vfunc_get_item(pos) { |
| 140 |
return this.#rules[pos] ?? null; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
class AutoMoveSettingsWidget extends Adw.PreferencesGroup { |
| 145 |
static { |
| 146 |
GObject.registerClass(this); |
| 147 |
|
| 148 |
this.install_action('rules.add', null, self => self._addNewRule()); |
| 149 |
this.install_action('rules.remove', 's', |
| 150 |
(self, name, param) => self._rules.remove(param.unpack())); |
| 151 |
this.install_action('rules.change-workspace', '(si)', |
| 152 |
(self, name, param) => self._rules.changeWorkspace(...param.deepUnpack())); |
| 153 |
} |
| 154 |
|
| 155 |
constructor(settings) { |
| 156 |
super({ |
| 157 |
title: _('Workspace Rules'), |
| 158 |
}); |
| 159 |
|
| 160 |
this._settings = settings; |
| 161 |
this._rules = new RulesList(this._settings); |
| 162 |
|
| 163 |
const store = new Gio.ListStore({item_type: Gio.ListModel}); |
| 164 |
const listModel = new Gtk.FlattenListModel({model: store}); |
| 165 |
store.append(this._rules); |
| 166 |
store.append(new NewItemModel()); |
| 167 |
|
| 168 |
this._list = new Gtk.ListBox({ |
| 169 |
selection_mode: Gtk.SelectionMode.NONE, |
| 170 |
css_classes: ['boxed-list'], |
| 171 |
}); |
| 172 |
this.add(this._list); |
| 173 |
|
| 174 |
this._list.bind_model(listModel, item => { |
| 175 |
return item instanceof NewItem |
| 176 |
? new NewRuleRow() |
| 177 |
: new RuleRow(item); |
| 178 |
}); |
| 179 |
} |
| 180 |
|
| 181 |
_addNewRule() { |
| 182 |
const dialog = new NewRuleDialog(this.get_root(), this._settings); |
| 183 |
dialog.connect('response', (dlg, id) => { |
| 184 |
const appInfo = id === Gtk.ResponseType.OK |
| 185 |
? dialog.get_widget().get_app_info() : null; |
| 186 |
if (appInfo) |
| 187 |
this._rules.append(appInfo); |
| 188 |
dialog.destroy(); |
| 189 |
}); |
| 190 |
dialog.show(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
class WorkspaceSelector extends Gtk.Widget { |
| 195 |
static [GObject.properties] = { |
| 196 |
'number': GObject.ParamSpec.uint( |
| 197 |
'number', null, null, |
| 198 |
GObject.ParamFlags.READWRITE, |
| 199 |
1, WORKSPACE_MAX, 1), |
| 200 |
}; |
| 201 |
|
| 202 |
static { |
| 203 |
GObject.registerClass(this); |
| 204 |
|
| 205 |
this.set_layout_manager_type(Gtk.BoxLayout); |
| 206 |
} |
| 207 |
|
| 208 |
constructor() { |
| 209 |
super(); |
| 210 |
|
| 211 |
this.layout_manager.spacing = 6; |
| 212 |
|
| 213 |
const label = new Gtk.Label({ |
| 214 |
xalign: 1, |
| 215 |
margin_end: 6, |
| 216 |
}); |
| 217 |
this.bind_property('number', |
| 218 |
label, 'label', |
| 219 |
GObject.BindingFlags.SYNC_CREATE); |
| 220 |
label.set_parent(this); |
| 221 |
|
| 222 |
const buttonProps = { |
| 223 |
css_classes: ['circular'], |
| 224 |
valign: Gtk.Align.CENTER, |
| 225 |
}; |
| 226 |
|
| 227 |
this._decButton = new Gtk.Button({ |
| 228 |
icon_name: 'list-remove-symbolic', |
| 229 |
...buttonProps, |
| 230 |
}); |
| 231 |
this._decButton.set_parent(this); |
| 232 |
this._decButton.connect('clicked', () => this.number--); |
| 233 |
|
| 234 |
this._incButton = new Gtk.Button({ |
| 235 |
icon_name: 'list-add-symbolic', |
| 236 |
...buttonProps, |
| 237 |
}); |
| 238 |
this._incButton.set_parent(this); |
| 239 |
this._incButton.connect('clicked', () => this.number++); |
| 240 |
|
| 241 |
this.connect('notify::number', () => this._syncButtons()); |
| 242 |
this._syncButtons(); |
| 243 |
} |
| 244 |
|
| 245 |
_syncButtons() { |
| 246 |
this._decButton.sensitive = this.number > 1; |
| 247 |
this._incButton.sensitive = this.number < WORKSPACE_MAX; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
class RuleRow extends Adw.ActionRow { |
| 252 |
static { |
| 253 |
GObject.registerClass(this); |
| 254 |
} |
| 255 |
|
| 256 |
constructor(rule) { |
| 257 |
const {appInfo} = rule; |
| 258 |
const id = appInfo.get_id(); |
| 259 |
|
| 260 |
super({ |
| 261 |
activatable: false, |
| 262 |
title: rule.appInfo.get_display_name(), |
| 263 |
}); |
| 264 |
|
| 265 |
const icon = new Gtk.Image({ |
| 266 |
css_classes: ['icon-dropshadow'], |
| 267 |
gicon: appInfo.get_icon(), |
| 268 |
pixel_size: 32, |
| 269 |
}); |
| 270 |
this.add_prefix(icon); |
| 271 |
|
| 272 |
const wsButton = new WorkspaceSelector(); |
| 273 |
rule.bind_property('workspace', |
| 274 |
wsButton, 'number', |
| 275 |
GObject.BindingFlags.SYNC_CREATE); |
| 276 |
this.add_suffix(wsButton); |
| 277 |
|
| 278 |
wsButton.connect('notify::number', () => { |
| 279 |
this.activate_action('rules.change-workspace', |
| 280 |
new GLib.Variant('(si)', [id, wsButton.number])); |
| 281 |
}); |
| 282 |
|
| 283 |
const button = new Gtk.Button({ |
| 284 |
action_name: 'rules.remove', |
| 285 |
action_target: new GLib.Variant('s', id), |
| 286 |
icon_name: 'edit-delete-symbolic', |
| 287 |
has_frame: false, |
| 288 |
valign: Gtk.Align.CENTER, |
| 289 |
}); |
| 290 |
this.add_suffix(button); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
class NewRuleRow extends Gtk.ListBoxRow { |
| 295 |
static { |
| 296 |
GObject.registerClass(this); |
| 297 |
} |
| 298 |
|
| 299 |
constructor() { |
| 300 |
super({ |
| 301 |
action_name: 'rules.add', |
| 302 |
child: new Gtk.Image({ |
| 303 |
icon_name: 'list-add-symbolic', |
| 304 |
pixel_size: 16, |
| 305 |
margin_top: 12, |
| 306 |
margin_bottom: 12, |
| 307 |
margin_start: 12, |
| 308 |
margin_end: 12, |
| 309 |
}), |
| 310 |
}); |
| 311 |
this.update_property( |
| 312 |
[Gtk.AccessibleProperty.LABEL], [_('Add Rule')]); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
class NewRuleDialog extends Gtk.AppChooserDialog { |
| 317 |
static { |
| 318 |
GObject.registerClass(this); |
| 319 |
} |
| 320 |
|
| 321 |
constructor(parent, settings) { |
| 322 |
super({ |
| 323 |
transient_for: parent, |
| 324 |
modal: true, |
| 325 |
}); |
| 326 |
|
| 327 |
this._settings = settings; |
| 328 |
|
| 329 |
this.get_widget().set({ |
| 330 |
show_all: true, |
| 331 |
show_other: true, // hide more button |
| 332 |
}); |
| 333 |
|
| 334 |
this.get_widget().connect('application-selected', |
| 335 |
this._updateSensitivity.bind(this)); |
| 336 |
this._updateSensitivity(); |
| 337 |
} |
| 338 |
|
| 339 |
_updateSensitivity() { |
| 340 |
const rules = this._settings.get_strv(SETTINGS_KEY); |
| 341 |
const appInfo = this.get_widget().get_app_info(); |
| 342 |
this.set_response_sensitive(Gtk.ResponseType.OK, |
| 343 |
appInfo && !rules.some(i => i.startsWith(appInfo.get_id()))); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
export default class AutoMovePrefs extends ExtensionPreferences { |
| 348 |
getPreferencesWidget() { |
| 349 |
return new AutoMoveSettingsWidget(this.getSettings()); |
| 350 |
} |
| 351 |
} |