|
1 | | -import GObject from 'gi://GObject'; |
2 | | -import Gio from 'gi://Gio'; |
3 | | -import St from 'gi://St'; |
4 | | -import Clutter from 'gi://Clutter'; |
5 | | - |
6 | | -import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js'; |
7 | | -import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js'; |
8 | | -import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js'; |
| 1 | +import GObject from 'gi://GObject' |
| 2 | +import Gio from 'gi://Gio' |
| 3 | +import St from 'gi://St' |
| 4 | +import Clutter from 'gi://Clutter' |
| 5 | + |
| 6 | +import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js' |
| 7 | +import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js' |
| 8 | +import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js' |
9 | 9 | import * as Utils from './utils.js' |
10 | 10 |
|
11 | | -import * as Main from 'resource:///org/gnome/shell/ui/main.js'; |
| 11 | +import * as Main from 'resource:///org/gnome/shell/ui/main.js' |
| 12 | +import {phpVersion} from './utils.js' |
12 | 13 |
|
13 | 14 | const PhpLaravelValet = GObject.registerClass( |
14 | 15 | class PhpLaravelValet extends PanelMenu.Button { |
15 | 16 | _init(ext) { |
16 | | - super._init(1.0, null, false); |
| 17 | + super._init(1.0, null, false) |
17 | 18 |
|
18 | | - this._extension = ext; |
19 | | - this._settings = ext.getSettings(); |
20 | | - this._settings.connect('changed', () => this._refreshIndicator()); |
| 19 | + this._extension = ext |
| 20 | + this._settings = ext.getSettings() |
| 21 | + this._settings.connect('changed', () => { |
| 22 | + this._refreshIndicator() |
| 23 | + this._refreshMenu() |
| 24 | + }) |
21 | 25 |
|
22 | | - this._indicatorText = new St.Label({ text: _('Loading...'), y_align: Clutter.ActorAlign.CENTER }); |
23 | | - this.add_child(this._indicatorText); |
| 26 | + this._indicatorText = new St.Label({text: _('Loading...'), y_align: Clutter.ActorAlign.CENTER}) |
| 27 | + this.add_child(this._indicatorText) |
24 | 28 |
|
25 | 29 | // initializing the menu with demo item |
26 | | - this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Loading...'))); |
| 30 | + this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Loading...'))) |
27 | 31 |
|
28 | | - this._refreshIndicator(); |
| 32 | + this._refreshIndicator() |
29 | 33 |
|
30 | 34 | this.menu.connect('open-state-changed', (menu, open) => { |
31 | | - if (open) this._refreshMenu(); |
32 | | - }); |
| 35 | + if (open) { |
| 36 | + this._refreshMenu() |
| 37 | + } |
| 38 | + }) |
33 | 39 | } |
34 | 40 |
|
35 | 41 | _refreshIndicator() { |
36 | | - const phpVersion = Utils.phpVersion(); |
| 42 | + const phpVersion = Utils.phpVersion() |
| 43 | + |
37 | 44 | if (phpVersion) { |
38 | | - this._indicatorText.set_text(phpVersion); |
| 45 | + this._settings.get_boolean('shorten-php-version') ? |
| 46 | + this._indicatorText.set_text(phpVersion.replace(/^(\D*\d+\.\d+).*/, '$1')) : |
| 47 | + this._indicatorText.set_text(phpVersion) |
39 | 48 | } else { |
40 | | - this._indicatorText.set_text(_('PHP not found')); |
| 49 | + this._indicatorText.set_text(_('PHP not found')) |
41 | 50 | } |
42 | 51 | } |
43 | 52 |
|
44 | 53 | _refreshMenu() { |
45 | | - this.menu.removeAll(); |
| 54 | + this.menu.removeAll() |
| 55 | + |
| 56 | + if (this.menuSection1() && (this.shouldShowMenuSection3() || this.shouldShowMenuSection4())) { |
| 57 | + this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); |
| 58 | + } |
| 59 | + |
| 60 | + if (this.menuSection2() && (this.shouldShowMenuSection3() || this.shouldShowMenuSection4())) { |
| 61 | + this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); |
| 62 | + } |
| 63 | + |
| 64 | + this.menuSection3() |
| 65 | + |
| 66 | + if (this.shouldShowMenuSection4()) { |
| 67 | + this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); |
| 68 | + } |
| 69 | + |
| 70 | + this.menuSection4() |
| 71 | + } |
| 72 | + |
| 73 | + _switchPhp(version) { |
| 74 | + const terminal = this._settings.get_string('default-shell').split(' ') |
| 75 | + try { |
| 76 | + let proc = Gio.Subprocess.new( |
| 77 | + terminal.concat(['valet', 'use', version]), |
| 78 | + Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE |
| 79 | + ) |
| 80 | + |
| 81 | + proc.communicate_utf8_async(null, null, (proc, res) => { |
| 82 | + if (proc.get_successful()) { |
| 83 | + this._refreshIndicator() |
| 84 | + } |
| 85 | + }) |
| 86 | + } catch (e) { |
| 87 | + logError(e) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return {boolean} |
| 93 | + */ |
| 94 | + menuSection1() { |
| 95 | + if (! this._settings.get_boolean('show-status')) { |
| 96 | + return false |
| 97 | + } |
| 98 | + |
| 99 | + const valetStatus = Utils.valetStatus() |
46 | 100 |
|
47 | | - // valet status menu |
48 | | - const valetStatus = Utils.valetStatus(); |
49 | 101 | if (valetStatus.length > 0) { |
50 | 102 | valetStatus.forEach(item => { |
51 | | - this.menu.addMenuItem(new PopupMenu.PopupMenuItem(item.replace(/\.\.\./g, ''))); |
| 103 | + this.menu.addMenuItem(new PopupMenu.PopupMenuItem(item.replace(/\.\.\./g, ''))) |
52 | 104 | }) |
53 | 105 | } else { |
54 | | - this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Valet not found'))); |
| 106 | + this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Valet not found'))) |
55 | 107 | } |
56 | 108 |
|
57 | | - // menu separator |
58 | | - this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); |
| 109 | + return true |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @return {boolean} |
| 114 | + */ |
| 115 | + menuSection2() { |
| 116 | + let state = false |
59 | 117 |
|
60 | 118 | // switch php sub menu |
61 | | - const phpSubMenu = new PopupMenu.PopupSubMenuMenuItem(_('Switch PHP')); |
62 | | - const phpList = Utils.phpList(); |
63 | | - if (phpList.length > 0) { |
64 | | - phpList.forEach(item => { |
65 | | - const subMenu = new PopupMenu.PopupMenuItem(_('Switch to ') + item); |
66 | | - subMenu.connect('activate', () => this._switchPhp(item)); |
67 | | - phpSubMenu.menu.addMenuItem(subMenu); |
68 | | - }) |
69 | | - } else { |
70 | | - phpSubMenu.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('PHP not found'))); |
| 119 | + if (this._settings.get_boolean('show-php-switcher')) { |
| 120 | + const phpSubMenu = new PopupMenu.PopupSubMenuMenuItem(_('Switch PHP')) |
| 121 | + const phpList = Utils.phpList() |
| 122 | + |
| 123 | + if (phpList.length > 0) { |
| 124 | + phpList.forEach(item => { |
| 125 | + const subMenu = new PopupMenu.PopupMenuItem(_('Switch to ') + item) |
| 126 | + subMenu.connect('activate', () => this._switchPhp(item)) |
| 127 | + phpSubMenu.menu.addMenuItem(subMenu) |
| 128 | + }) |
| 129 | + } else { |
| 130 | + phpSubMenu.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('PHP not found'))) |
| 131 | + } |
| 132 | + this.menu.addMenuItem(phpSubMenu) |
| 133 | + |
| 134 | + state = true |
71 | 135 | } |
72 | | - this.menu.addMenuItem(phpSubMenu); |
73 | 136 |
|
74 | 137 | // valet start/restart menu |
75 | | - const valetRestart = new PopupMenu.PopupMenuItem(_('Valet start/restart')); |
76 | | - valetRestart.connect('activate', () => Utils.valetRestart()); |
77 | | - this.menu.addMenuItem(valetRestart); |
| 138 | + if (this._settings.get_boolean('show-valet-restart')) { |
| 139 | + const valetRestart = new PopupMenu.PopupMenuItem(_('Valet start/restart')) |
| 140 | + valetRestart.connect('activate', () => Utils.valetRestart()) |
| 141 | + this.menu.addMenuItem(valetRestart) |
| 142 | + |
| 143 | + state = true |
| 144 | + } |
78 | 145 |
|
79 | 146 | // valet stop menu |
80 | | - const valetStop = new PopupMenu.PopupMenuItem(_('Valet stop')); |
81 | | - valetStop.connect('activate', () => Utils.valetStop()); |
82 | | - this.menu.addMenuItem(valetStop); |
| 147 | + if (this._settings.get_boolean('show-valet-stop')) { |
| 148 | + const valetStop = new PopupMenu.PopupMenuItem(_('Valet stop')) |
| 149 | + valetStop.connect('activate', () => Utils.valetStop()) |
| 150 | + this.menu.addMenuItem(valetStop) |
83 | 151 |
|
| 152 | + state = true |
| 153 | + } |
84 | 154 |
|
85 | | - if (this._settings.get_boolean('show-settings')) { |
86 | | - // menu separator |
87 | | - this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); |
| 155 | + return state |
| 156 | + } |
88 | 157 |
|
89 | | - // settings menu |
90 | | - const settings = new PopupMenu.PopupMenuItem(_('Settings')); |
91 | | - settings.connect('activate', () => this._extension.openPreferences()); |
92 | | - this.menu.addMenuItem(settings); |
| 158 | + menuSection3() |
| 159 | + { |
| 160 | + if (! this.shouldShowMenuSection3()) { |
| 161 | + return |
93 | 162 | } |
94 | | - } |
95 | 163 |
|
96 | | - _switchPhp(version) { |
97 | | - const terminal = this._settings.get_string('default-shell').split(' '); |
98 | | - try { |
99 | | - let proc = Gio.Subprocess.new( |
100 | | - terminal.concat(['valet', 'use', version]), |
101 | | - Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE |
102 | | - ); |
| 164 | + const valetLinks = Utils.valetList() |
103 | 165 |
|
104 | | - proc.communicate_utf8_async(null, null, (proc, res) => { |
105 | | - if (proc.get_successful()) this._refreshIndicator(); |
106 | | - }); |
107 | | - } catch (e) { |
108 | | - logError(e); |
| 166 | + if (valetLinks.length < 1) { |
| 167 | + this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('No Links Found'))) |
| 168 | + |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + const linksSubMenu = new PopupMenu.PopupSubMenuMenuItem(_('Valet Sites')) |
| 173 | + |
| 174 | + valetLinks.forEach(site => { |
| 175 | + const label = site.site + ' (PHP ' + site.php + ')' |
| 176 | + const siteItem = new PopupMenu.PopupMenuItem(label) |
| 177 | + siteItem.connect('activate', () => Gio.AppInfo.launch_default_for_uri(site.url, null)) |
| 178 | + |
| 179 | + linksSubMenu.menu.addMenuItem(siteItem) |
| 180 | + }) |
| 181 | + |
| 182 | + // Add submenu to main menu |
| 183 | + this.menu.addMenuItem(linksSubMenu) |
| 184 | + } |
| 185 | + |
| 186 | + menuSection4() { |
| 187 | + if (! this.shouldShowMenuSection4()) { |
| 188 | + return |
109 | 189 | } |
| 190 | + |
| 191 | + // settings menu |
| 192 | + const settings = new PopupMenu.PopupMenuItem(_('Settings')) |
| 193 | + settings.connect('activate', () => this._extension.openPreferences()) |
| 194 | + this.menu.addMenuItem(settings) |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @return {boolean} |
| 199 | + */ |
| 200 | + shouldShowMenuSection3() { |
| 201 | + return this._settings.get_boolean('show-links') |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @return {boolean} |
| 206 | + */ |
| 207 | + shouldShowMenuSection4() { |
| 208 | + return this._settings.get_boolean('show-settings') |
110 | 209 | } |
111 | 210 | } |
112 | | -); |
| 211 | +) |
113 | 212 |
|
114 | 213 | export default class PhpLaravelValetExtension extends Extension { |
115 | 214 | enable() { |
116 | | - this._indicator = new PhpLaravelValet(this); |
117 | | - Main.panel.addToStatusArea(this.uuid, this._indicator); |
| 215 | + this._indicator = new PhpLaravelValet(this) |
| 216 | + Main.panel.addToStatusArea(this.uuid, this._indicator) |
118 | 217 | } |
119 | 218 |
|
120 | 219 | disable() { |
121 | | - this._indicator.destroy(); |
122 | | - this._indicator = null; |
| 220 | + this._indicator.destroy() |
| 221 | + this._indicator = null |
123 | 222 | } |
124 | 223 | } |
0 commit comments