From 7dac50c0e52baef7f5c2f8aca0b134b302e8db57 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 16 May 2023 16:20:39 +0900 Subject: [PATCH] Add layout indicator --- extension.js | 3 ++- icons/deck.svg | 3 +++ icons/floating.svg | 3 +++ icons/monocle.svg | 3 +++ icons/tiling.svg | 20 ++++++++++++++++++++ sources/indicator.js | 21 +++++++++++++++++---- sources/keybinds.js | 4 +++- sources/renderer.js | 19 ++++++++++--------- sources/state.js | 4 +++- 9 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 icons/deck.svg create mode 100644 icons/floating.svg create mode 100644 icons/monocle.svg create mode 100644 icons/tiling.svg diff --git a/extension.js b/extension.js index 0eb97b1..60127a5 100644 --- a/extension.js +++ b/extension.js @@ -12,9 +12,10 @@ class Extension { constructor() { this._state = new State.StateManager(); this._settings = ExtensionUtils.getSettings("org.gnome.shell.extensions.fairy"); + this._renderer = new Renderer.Renderer(this._state, this._settings); - this._keybinds = new Keybinds.KeyboardManager(this._state, this._renderer); this._indicator = new Indicator.Indicator(this._state, this._renderer); + this._keybinds = new Keybinds.KeyboardManager(this._state, this._renderer, this._indicator); } enable() { diff --git a/icons/deck.svg b/icons/deck.svg new file mode 100644 index 0000000..798076a --- /dev/null +++ b/icons/deck.svg @@ -0,0 +1,3 @@ + + + diff --git a/icons/floating.svg b/icons/floating.svg new file mode 100644 index 0000000..92314f7 --- /dev/null +++ b/icons/floating.svg @@ -0,0 +1,3 @@ + + + diff --git a/icons/monocle.svg b/icons/monocle.svg new file mode 100644 index 0000000..de159cd --- /dev/null +++ b/icons/monocle.svg @@ -0,0 +1,3 @@ + + + diff --git a/icons/tiling.svg b/icons/tiling.svg new file mode 100644 index 0000000..c5826ff --- /dev/null +++ b/icons/tiling.svg @@ -0,0 +1,20 @@ + + + + + + + + + + + diff --git a/sources/indicator.js b/sources/indicator.js index 05121e7..f4bec52 100644 --- a/sources/indicator.js +++ b/sources/indicator.js @@ -18,6 +18,13 @@ var Indicator = GObject.registerClass( super._init(); this._state = state; this._renderer = renderer; + + this._layoutIcons = { + tiling: Gio.icon_new_for_string(`${Me.path}/icons/tiling.svg`), + monocle: Gio.icon_new_for_string(`${Me.path}/icons/monocle.svg`), + floating: Gio.icon_new_for_string(`${Me.path}/icons/floating.svg`), + deck: Gio.icon_new_for_string(`${Me.path}/icons/deck.svg`), + }; } enable() { @@ -25,13 +32,13 @@ var Indicator = GObject.registerClass( "org.gnome.shell.extensions.fairy" ); - const indicatorName = `${Me.metadata.name} Indicator`; + const indicatorName = `${Me.metadata.name} Indicator`; this._layoutIndicator = new PanelMenu.Button(0.0, indicatorName); - const icon = new St.Icon({ - gicon: new Gio.ThemedIcon({ name: "face-laugh-symbolic" }), + this._icon = new St.Icon({ + gicon: this._layoutIcons.tiling, style_class: "system-status-icon", }); - this._layoutIndicator.add_child(icon); + this._layoutIndicator.add_child(this._icon); this.settings.bind( "show-layout", @@ -45,8 +52,14 @@ var Indicator = GObject.registerClass( disable() { this._layoutIndicator.destroy(); this._layoutIndicator = null; + this._icon = null; this.settings = null; } + + update() { + const primaryMon = global.display.get_primary_monitor(); + this._icon.gicon = this._layoutIcons[this._state.monitors[primaryMon].layout]; + } } ); diff --git a/sources/keybinds.js b/sources/keybinds.js index 78360be..4c69d43 100644 --- a/sources/keybinds.js +++ b/sources/keybinds.js @@ -8,10 +8,11 @@ const ExtensionUtils = imports.misc.extensionUtils; var KeyboardManager = GObject.registerClass( class KeyboardManager extends GObject.Object { - _init(state, renderer) { + _init(state, renderer, indicator) { super._init(); this._state = state; this._renderer = renderer; + this._indicator = indicator; } /** @@ -197,6 +198,7 @@ var KeyboardManager = GObject.registerClass( else state.layout = mode; state.oldLayout = currentLayout; this._renderer.render(mon); + this._indicator.update(mon); } _focusNext() { diff --git a/sources/renderer.js b/sources/renderer.js index 76e6432..5f490f2 100644 --- a/sources/renderer.js +++ b/sources/renderer.js @@ -139,12 +139,12 @@ var Renderer = GObject.registerClass( global.display.connect("window-created", (_display, window) => this._waitForWindow(window, () => { this.trackWindow(window); - GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => { + // GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, () => { this.focus(window); - // Do not retrigger this idle. - return false; - }); - this.renderForWindow(window); + this.renderForHandle(window); + // // Do not retrigger this idle. + // return false; + // }); }) ), // global.display.connect("window-entered-monitor", (_display, monitor, window) => { @@ -155,8 +155,8 @@ var Renderer = GObject.registerClass( global.workspace_manager.connect("active-workspace-changed", () => { // Convert gnome workspaces to fairy's tags const workspace = global.display - .get_workspace_manager() - .get_active_workspace_index(); + .get_workspace_manager() + .get_active_workspace_index(); const tags = 0b1 << workspace; log("Switch to tags", tags); if (Meta.prefs_get_workspaces_only_on_primary()) { @@ -227,6 +227,7 @@ var Renderer = GObject.registerClass( handle.connect("focus", (handle) => { if (!this._isValidWindow(handle)) return; this._state.monitors[handle.get_monitor()].focused = handle; + this.renderForHandle(handle); }), ]; @@ -309,8 +310,8 @@ var Renderer = GObject.registerClass( } } - renderForWindow(window) { - const mon = window.get_monitor(); + renderForHandle(handle) { + const mon = handle.get_monitor(); this.render(mon); } diff --git a/sources/state.js b/sources/state.js index aa7beb5..45234df 100644 --- a/sources/state.js +++ b/sources/state.js @@ -150,10 +150,12 @@ var StateManager = GObject.registerClass( } _layout({ layout, nmaster, mfact, focused }, windows) { - const focusedW = this.windows.find((x) => x.handle === focused); + const focusedW = windows.find((x) => x.handle === focused) + ?? windows[0]; switch (layout) { case "monocle": + if (!focusedW) return []; return [ { ...focusedW,