|
| 1 | +import '@girs/gjs'; |
| 2 | + |
| 3 | +import St from '@girs/st-18'; |
| 4 | +import Clutter from '@girs/clutter-18'; |
| 5 | +import * as Main from '@girs/gnome-shell/ui/main'; |
| 6 | +import type { Button as PanelMenuButton } from '@girs/gnome-shell/ui/panelMenu'; |
| 7 | +import * as PanelMenu from '@girs/gnome-shell/ui/panelMenu'; |
| 8 | +import * as PopupMenu from '@girs/gnome-shell/ui/popupMenu'; |
| 9 | + |
| 10 | +import type { ExtensionContext } from '~/core/context.ts'; |
| 11 | +import { Module } from '~/module.ts'; |
| 12 | +import { loadIcon } from '~/shared/icons.ts'; |
| 13 | + |
| 14 | +import { TrayIconsDevTool } from './trayIconsDevTool.ts'; |
| 15 | + |
| 16 | +const DEVTOOL_ID = 'aurora-devtool'; |
| 17 | + |
| 18 | +type DevToolSection = { |
| 19 | + key: string; |
| 20 | + title: string; |
| 21 | + iconName: string; |
| 22 | + buildPanel(): St.Widget; |
| 23 | + destroy(): void; |
| 24 | +}; |
| 25 | + |
| 26 | +export class DevTool extends Module { |
| 27 | + private _button: PanelMenu.Button | null = null; |
| 28 | + private _menuOpenStateId = 0; |
| 29 | + private _trayIconsTool: TrayIconsDevTool | null = null; |
| 30 | + private _sections: DevToolSection[] = []; |
| 31 | + private _activeSectionKey = 'tray-icons'; |
| 32 | + |
| 33 | + constructor(context: ExtensionContext) { |
| 34 | + super(context); |
| 35 | + } |
| 36 | + |
| 37 | + override enable(): void { |
| 38 | + this._button = new PanelMenu.Button(1.0, 'Aurora DevTool'); |
| 39 | + this._button.add_child( |
| 40 | + new St.Icon({ |
| 41 | + gicon: loadIcon('applications-engineering-symbolic'), |
| 42 | + icon_size: 16, |
| 43 | + style_class: 'system-status-icon', |
| 44 | + }), |
| 45 | + ); |
| 46 | + |
| 47 | + this._trayIconsTool = new TrayIconsDevTool(() => this._rebuildMenu()); |
| 48 | + this._sections = [this._trayIconsTool]; |
| 49 | + |
| 50 | + const menu = this._getMenu(); |
| 51 | + if (!menu) return; |
| 52 | + menu.setSourceAlignment(1.0); |
| 53 | + |
| 54 | + this._menuOpenStateId = menu.connect('open-state-changed', (_menu, open) => { |
| 55 | + if (open) this._rebuildMenu(); |
| 56 | + return undefined; |
| 57 | + }); |
| 58 | + |
| 59 | + this._rebuildMenu(); |
| 60 | + Main.panel.addToStatusArea(DEVTOOL_ID, this._button as unknown as PanelMenuButton, 1, 'left'); |
| 61 | + } |
| 62 | + |
| 63 | + override disable(): void { |
| 64 | + for (const section of this._sections) { |
| 65 | + section.destroy(); |
| 66 | + } |
| 67 | + this._sections = []; |
| 68 | + this._trayIconsTool = null; |
| 69 | + |
| 70 | + if (this._menuOpenStateId && this._button) { |
| 71 | + this._getMenu()?.disconnect(this._menuOpenStateId); |
| 72 | + this._menuOpenStateId = 0; |
| 73 | + } |
| 74 | + |
| 75 | + (Main.panel.statusArea as Record<string, unknown>)[DEVTOOL_ID] = null; |
| 76 | + this._button?.destroy(); |
| 77 | + this._button = null; |
| 78 | + } |
| 79 | + |
| 80 | + get trayIconsTool(): TrayIconsDevTool | null { |
| 81 | + return this._trayIconsTool; |
| 82 | + } |
| 83 | + |
| 84 | + private _rebuildMenu(): void { |
| 85 | + const menu = this._getMenu(); |
| 86 | + if (!menu) return; |
| 87 | + |
| 88 | + menu.removeAll(); |
| 89 | + const section = new PopupMenu.PopupMenuSection(); |
| 90 | + section.box.add_child(this._buildPanel()); |
| 91 | + menu.addMenuItem(section); |
| 92 | + } |
| 93 | + |
| 94 | + private _buildPanel(): St.Widget { |
| 95 | + const panel = new St.BoxLayout({ |
| 96 | + vertical: true, |
| 97 | + style_class: 'aurora-devtool-panel', |
| 98 | + }); |
| 99 | + |
| 100 | + panel.add_child(this._buildHeader()); |
| 101 | + panel.add_child(this._buildTabs()); |
| 102 | + |
| 103 | + const activeSection = this._activeSection(); |
| 104 | + if (activeSection) { |
| 105 | + panel.add_child(activeSection.buildPanel()); |
| 106 | + } |
| 107 | + |
| 108 | + return panel; |
| 109 | + } |
| 110 | + |
| 111 | + private _buildHeader(): St.Widget { |
| 112 | + const header = new St.BoxLayout({ |
| 113 | + style_class: 'aurora-devtool-header', |
| 114 | + }); |
| 115 | + |
| 116 | + header.add_child( |
| 117 | + new St.Icon({ |
| 118 | + gicon: loadIcon('applications-engineering-symbolic'), |
| 119 | + icon_size: 18, |
| 120 | + style_class: 'aurora-devtool-header-icon', |
| 121 | + }), |
| 122 | + ); |
| 123 | + header.add_child( |
| 124 | + new St.Label({ |
| 125 | + text: 'Aurora DevTool', |
| 126 | + y_align: Clutter.ActorAlign.CENTER, |
| 127 | + style_class: 'aurora-devtool-title', |
| 128 | + }), |
| 129 | + ); |
| 130 | + |
| 131 | + return header; |
| 132 | + } |
| 133 | + |
| 134 | + private _buildTabs(): St.Widget { |
| 135 | + const tabs = new St.BoxLayout({ |
| 136 | + style_class: 'aurora-devtool-tabs', |
| 137 | + }); |
| 138 | + |
| 139 | + for (const section of this._sections) { |
| 140 | + const active = section.key === this._activeSectionKey; |
| 141 | + const tabContent = new St.BoxLayout({ |
| 142 | + style_class: 'aurora-devtool-tab-content', |
| 143 | + }); |
| 144 | + const tab = new St.Button({ |
| 145 | + child: tabContent, |
| 146 | + style_class: active ? 'aurora-devtool-tab active' : 'aurora-devtool-tab', |
| 147 | + can_focus: true, |
| 148 | + x_expand: true, |
| 149 | + accessible_name: section.title, |
| 150 | + }); |
| 151 | + |
| 152 | + tabContent.add_child( |
| 153 | + new St.Icon({ |
| 154 | + icon_name: section.iconName, |
| 155 | + icon_size: 16, |
| 156 | + style_class: 'aurora-devtool-tab-icon', |
| 157 | + }), |
| 158 | + ); |
| 159 | + tabContent.add_child( |
| 160 | + new St.Label({ |
| 161 | + text: section.title, |
| 162 | + y_align: Clutter.ActorAlign.CENTER, |
| 163 | + }), |
| 164 | + ); |
| 165 | + tab.connect('clicked', () => { |
| 166 | + this._activeSectionKey = section.key; |
| 167 | + this._rebuildMenu(); |
| 168 | + }); |
| 169 | + tabs.add_child(tab); |
| 170 | + } |
| 171 | + |
| 172 | + return tabs; |
| 173 | + } |
| 174 | + |
| 175 | + private _activeSection(): DevToolSection | null { |
| 176 | + return this._sections.find((section) => section.key === this._activeSectionKey) ?? null; |
| 177 | + } |
| 178 | + |
| 179 | + private _getMenu(): PopupMenu.PopupMenu | null { |
| 180 | + return (this._button?.menu as PopupMenu.PopupMenu | null | undefined) ?? null; |
| 181 | + } |
| 182 | +} |
0 commit comments