Skip to content

Commit ec91f8a

Browse files
authored
Merge pull request #5 from Muetze42/gnome-shell-46
Add Valet Links and settings options
2 parents 0102084 + be2e105 commit ec91f8a

File tree

8 files changed

+331
-110
lines changed

8 files changed

+331
-110
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
A PHP Laravel Valet status indicator and manager extension (GNOME Panel Applet) for GNOME Shell.
44

5-
![Screenshot](./screenshot.png)
5+
![Screenshot](./screenshot.png)
6+
![Screenshot2](./screenshot2.png)
67

78
## Supports
89

extension.js

Lines changed: 170 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,223 @@
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'
99
import * as Utils from './utils.js'
1010

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'
1213

1314
const PhpLaravelValet = GObject.registerClass(
1415
class PhpLaravelValet extends PanelMenu.Button {
1516
_init(ext) {
16-
super._init(1.0, null, false);
17+
super._init(1.0, null, false)
1718

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+
})
2125

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)
2428

2529
// initializing the menu with demo item
26-
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Loading...')));
30+
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Loading...')))
2731

28-
this._refreshIndicator();
32+
this._refreshIndicator()
2933

3034
this.menu.connect('open-state-changed', (menu, open) => {
31-
if (open) this._refreshMenu();
32-
});
35+
if (open) {
36+
this._refreshMenu()
37+
}
38+
})
3339
}
3440

3541
_refreshIndicator() {
36-
const phpVersion = Utils.phpVersion();
42+
const phpVersion = Utils.phpVersion()
43+
3744
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)
3948
} else {
40-
this._indicatorText.set_text(_('PHP not found'));
49+
this._indicatorText.set_text(_('PHP not found'))
4150
}
4251
}
4352

4453
_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()
46100

47-
// valet status menu
48-
const valetStatus = Utils.valetStatus();
49101
if (valetStatus.length > 0) {
50102
valetStatus.forEach(item => {
51-
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(item.replace(/\.\.\./g, '')));
103+
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(item.replace(/\.\.\./g, '')))
52104
})
53105
} else {
54-
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Valet not found')));
106+
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Valet not found')))
55107
}
56108

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
59117

60118
// 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
71135
}
72-
this.menu.addMenuItem(phpSubMenu);
73136

74137
// 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+
}
78145

79146
// 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)
83151

152+
state = true
153+
}
84154

85-
if (this._settings.get_boolean('show-settings')) {
86-
// menu separator
87-
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
155+
return state
156+
}
88157

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
93162
}
94-
}
95163

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()
103165

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
109189
}
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')
110209
}
111210
}
112-
);
211+
)
113212

114213
export default class PhpLaravelValetExtension extends Extension {
115214
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)
118217
}
119218

120219
disable() {
121-
this._indicator.destroy();
122-
this._indicator = null;
220+
this._indicator.destroy()
221+
this._indicator = null
123222
}
124223
}

metadata.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"name": "PHP Laravel Valet",
2+
"_generated": "Generated by SweetTooth, do not edit",
33
"description": "A PHP Laravel Valet status indicator and manager.",
4-
"uuid": "php-laravel-valet@rahulhaque",
4+
"name": "PHP Laravel Valet",
5+
"settings-schema": "org.gnome.shell.extensions.php-laravel-valet",
56
"shell-version": [
67
"46"
78
],
8-
"version": 8,
99
"url": "https://github.com/rahulhaque/php-laravel-valet-gnome-shell-extension",
10-
"settings-schema": "org.gnome.shell.extensions.php-laravel-valet"
11-
}
10+
"uuid": "php-laravel-valet@rahulhaque",
11+
"version": 8
12+
}

0 commit comments

Comments
 (0)