Skip to content

Commit

Permalink
Gnome shell 45 support added ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulhaque committed Dec 12, 2023
1 parent a8c656c commit 082b279
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 64 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ A PHP Laravel Valet status indicator and manager extension (GNOME Panel Applet)

## Supports

- Tested on Pop!_OS 21.10 and GNOME Shell V40
|Extension Version|Gnome Shell Version|
|:-:|:-:|
|6|45|
|5|44, 43, 42, 41, 40|

## Prerequisite

Expand Down
79 changes: 47 additions & 32 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
'use strict';
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import St from 'gi://St';
import Clutter from 'gi://Clutter';

const {GObject, GLib, Gio, St, Clutter} = imports.gi;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as Utils from './utils.js'

import * as Main from 'resource:///org/gnome/shell/ui/main.js';

const PhpLaravelValet = GObject.registerClass(
class PhpLaravelValet extends PanelMenu.Button {
_init() {
super._init(0.0, null, false);
_init(ext) {
super._init(1.0, null, false);

this._settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.php-laravel-valet');
this._extension = ext;
this._settings = ext.getSettings();
this._settings.connect('changed', () => this._refreshIndicator());

this._indicatorText = new St.Label({text: 'Loading...', y_align: Clutter.ActorAlign.CENTER});
this._indicatorText = new St.Label({ text: _('Loading...'), y_align: Clutter.ActorAlign.CENTER });
this.add_actor(this._indicatorText);

// Initialising the menu with demo item
this.menu.addMenuItem(new PopupMenu.PopupMenuItem('Loading...'));
// initializing the menu with demo item
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Loading...')));

this._refreshIndicator();

this.menu.connect('open-state-changed', (menu, open) => {
if (open) this._refreshMenu()
if (open) this._refreshMenu();
});
}

Expand All @@ -33,7 +37,7 @@ const PhpLaravelValet = GObject.registerClass(
if (phpVersion) {
this._indicatorText.set_text(phpVersion);
} else {
this._indicatorText.set_text('PHP not found');
this._indicatorText.set_text(_('PHP not found'));
}
}

Expand All @@ -47,35 +51,46 @@ const PhpLaravelValet = GObject.registerClass(
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(item.replace(/\.\.\./g, '')));
})
} else {
this.menu.addMenuItem(new PopupMenu.PopupMenuItem('Valet not found'));
this.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('Valet not found')));
}

// menu separator
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());

// switch php sub menu
const phpSubMenu = new PopupMenu.PopupSubMenuMenuItem('Switch PHP');
const phpSubMenu = new PopupMenu.PopupSubMenuMenuItem(_('Switch PHP'));
const phpList = Utils.phpList();
if (phpList.length > 0) {
phpList.forEach(item => {
const subMenu = new PopupMenu.PopupMenuItem('Switch to ' + item);
const subMenu = new PopupMenu.PopupMenuItem(_('Switch to ') + item);
subMenu.connect('activate', () => this._switchPhp(item));
phpSubMenu.menu.addMenuItem(subMenu);
})
} else {
phpSubMenu.menu.addMenuItem(new PopupMenu.PopupMenuItem('PHP not found'));
phpSubMenu.menu.addMenuItem(new PopupMenu.PopupMenuItem(_('PHP not found')));
}
this.menu.addMenuItem(phpSubMenu);

// valet start/restart menu
const valetRestart = new PopupMenu.PopupMenuItem('Valet start/restart');
const valetRestart = new PopupMenu.PopupMenuItem(_('Valet start/restart'));
valetRestart.connect('activate', () => Utils.valetRestart());
this.menu.addMenuItem(valetRestart);

// valet stop menu
const valetStop = new PopupMenu.PopupMenuItem('Valet stop');
const valetStop = new PopupMenu.PopupMenuItem(_('Valet stop'));
valetStop.connect('activate', () => Utils.valetStop());
this.menu.addMenuItem(valetStop);


if (this._settings.get_boolean('show-settings')) {
// menu separator
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());

// settings menu
const settings = new PopupMenu.PopupMenuItem(_('Settings'));
settings.connect('activate', () => this._extension.openPreferences());
this.menu.addMenuItem(settings);
}
}

_switchPhp(version) {
Expand All @@ -94,16 +109,16 @@ const PhpLaravelValet = GObject.registerClass(
}
}
}
)
);

let phpLaravelValet = null;

function enable() {
phpLaravelValet = new PhpLaravelValet();
Main.panel.addToStatusArea('php-laravel-valet', phpLaravelValet);
}
export default class PhpLaravelValetExtension extends Extension {
enable() {
this._indicator = new PhpLaravelValet(this);
Main.panel.addToStatusArea(this.uuid, this._indicator);
}

function disable() {
phpLaravelValet.destroy();
phpLaravelValet = null;
disable() {
this._indicator.destroy();
this._indicator = null;
}
}
11 changes: 4 additions & 7 deletions metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
"description": "A PHP Laravel Valet status indicator and manager.",
"uuid": "php-laravel-valet@rahulhaque",
"shell-version": [
"40",
"41",
"42",
"43",
"44"
"45"
],
"version": 5,
"url": "https://github.com/rahulhaque/php-laravel-valet-gnome-shell-extension"
"version": 6,
"url": "https://github.com/rahulhaque/php-laravel-valet-gnome-shell-extension",
"settings-schema": "org.gnome.shell.extensions.php-laravel-valet"
}
34 changes: 34 additions & 0 deletions prefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Gio from 'gi://Gio';
import Adw from 'gi://Adw';
import { ExtensionPreferences, gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';

export default class PhpLaravelValetPreferences extends ExtensionPreferences {
fillPreferencesWindow(window) {
// create a preferences page, with a single group
const page = new Adw.PreferencesPage({
title: _('General'),
icon_name: 'dialog-information-symbolic',
});
window.add(page);

// create a preferences group, add to page
const group = new Adw.PreferencesGroup({
title: _('Settings'),
description: _('Configure the settings of the extension'),
});
page.add(group);

// create a new preferences row
const show_settings = new Adw.SwitchRow({
title: _('Show Settings'),
subtitle: _('Whether to show the settings in menu'),
});
group.add(show_settings);

// create a settings object and bind inputs
window._settings = this.getSettings();
window._settings.bind('show-settings', show_settings, 'active', Gio.SettingsBindFlags.DEFAULT);

window.set_default_size(620, 300);
}
}
Binary file modified schemas/gschemas.compiled
Binary file not shown.
14 changes: 7 additions & 7 deletions schemas/org.gnome.shell.extensions.php-laravel-valet.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
<key type="s" name="position-in-panel">
<default>'right'</default>
<summary>Position in Panel</summary>
<description>Position in Panel ('left', 'center', 'right')</description>
<description>Position in Panel ('left', 'center', 'right').</description>
</key>
<key type="i" name="panel-box-index">
<default>0</default>
<summary>Index in panel box</summary>
<description>Index within the selected panel box (0: first, 1: second, ..., -1: last)</description>
</key>
<key type="s" name="default-shell">
<default>'x-terminal-emulator -e'</default>
<summary>Default shell to execute extension commands</summary>
<description>Default shell to execute extension commands</description>
<description>Index within the selected panel box (0: first, 1: second, ..., -1: last).</description>
</key>
<key type="b" name="show-settings">
<default>true</default>
<summary>Show the settings button</summary>
<description>Whether to show the settings button as last entry in the extensions list.</description>
</key>
</schema>
</schemalist>
1 change: 1 addition & 0 deletions stylesheet.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Add your custom extension styling here */
28 changes: 11 additions & 17 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,41 @@
'use strict';
import GLib from 'gi://GLib';
import Bytes from 'gi://GLib/Bytes';

const Bytes = imports.byteArray;
const GLib = imports.gi.GLib;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();

const _settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.php-laravel-valet');

function safeSpawn(cmd) {
export function safeSpawn(cmd) {
try {
return GLib.spawn_command_line_sync(cmd);
} catch (e) {
return [false, Bytes.fromString(''), null, null];
}
}

function shellSpawn(cmd) {
const terminal = _settings.get_string('default-shell');
export function shellSpawn(cmd) {
const terminal = 'x-terminal-emulator -e';
GLib.spawn_command_line_async(`${terminal} ${cmd}`);
}

function phpVersion() {
export function phpVersion() {
const res = safeSpawn('/bin/bash -c "php -v | grep -Po \'PHP\\s+\\d+.\\d+(?:(.\\d+))?\'"');
if (res[3] == 0) return Bytes.toString(res[1]).replace(/\n$/, '');
return false;
}

function phpList() {
export function phpList() {
const res = safeSpawn('ls /etc/php');
if (res[3] == 0) return Bytes.toString(res[1]).split('\n').filter(item => !!item).reverse();
return false;
}

function valetStatus() {
export function valetStatus() {
const res = safeSpawn('/bin/bash -c "valet --version && valet status"');
if (res[3] == 0) return Bytes.toString(res[1]).split('\n').filter(item => !!item);
return false;
}

function valetRestart() {
export function valetRestart() {
shellSpawn('valet restart');
}

function valetStop() {
export function valetStop() {
shellSpawn('valet stop');
}
}

0 comments on commit 082b279

Please sign in to comment.