Skip to content

Commit

Permalink
version 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSidious007 committed Dec 7, 2024
1 parent 028b049 commit b867e92
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 66 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"esversion": 6}
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
.PHONY: all install clean

UUID := $(shell jq -r .uuid metadata.json)
VERSION := $(shell jq .version metadata.json)
ZIP_NAME := $(UUID).shell-extension.zip

all: $(ZIP_NAME)

$(ZIP_NAME):
rm -f schemas/gschemas.compiled
glib-compile-schemas ./schemas
zip -r $(ZIP_NAME) metadata.json icons/ schemas/ prefs.js extension.js README.md LICENSE
zip -r $(ZIP_NAME) metadata.json icons/ schemas/ prefs.js extension.js gsettingsManager.js README.md LICENSE

install: $(ZIP_NAME)
gnome-extensions install -f $(ZIP_NAME)
Expand Down
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ A GNOME Shell extension that allows you to toggle the **AmneziaWG** (via awg-qui

![Screenshot](images/screenshot-prefs.png)

## Gnome Versions Support
## Versions

- 40|41|42|43|44 - `master` branch, `v3` tag
> [!NOTE]
> `Main` branch always has latest `Version Name`.
| Version Name | Version | Gnome Shell Support | Tag |
|--------------|---------|---------------------|--------|
| 1.4.0 | 4 | 40-44 | v1.4.0 |

## Manual Installation

Expand All @@ -46,16 +51,17 @@ git clone https://github.com/amnezia-vpn/amneziawg-gnome-extension.git ~/.local/
```

> [!TIP]
> add `--branch <version tag>` if you need exact version
> add `--branch <version tag>` if you need exact version. Version from `main` may not work with your gnome shell version.
### Get extension from ZIP:
```bash
wget https://github.com/amnezia-vpn/amneziawg-gnome-extension/releases/download/v3/[email protected]
version_tag=1.4.0 # Latest version
wget https://github.com/amnezia-vpn/amneziawg-gnome-extension/releases/download/v${version_tag}/[email protected]
gnome-extensions install [email protected] --force
```

> [!TIP]
> Replace `v3` in url with `<version tag>` if you need another version
> Replace `version_tag` variable value if you need another version
### Apply extension

Expand Down
44 changes: 17 additions & 27 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ const PanelMenu = imports.ui.panelMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Util = imports.misc.util;
const { GsettingsManager } = Me.imports.gsettingsManager;

class ToggleAWG {
constructor(settings) {
constructor(gsettingsManager) {
this._isActive = false;
this.settings = settings;
this.gsettingsManager = gsettingsManager;
}

_getIconPath(isActive) {
const manualTheme = this.settings.get_string('manual-theme');
const manualTheme = this.gsettingsManager.getValue('manual-theme');
const iconType = manualTheme === 'dark' ? 'dark' : 'light';
return `${Me.path}/icons/${isActive ? 'active-' : 'inactive-'}${iconType}.png`;
}

toggleService() {
const iface = this.settings.get_string('interface');
const iface = this.gsettingsManager.getValue('interface');
if (!iface) {
Main.notify('Toggle AWG', 'No interface set in settings!');
return;
Expand All @@ -34,10 +35,10 @@ class ToggleAWG {

var ToggleAWGButton = GObject.registerClass(
class ToggleAWGButton extends PanelMenu.Button {
_init(toggleAWG, settings) {
_init(toggleAWG, gsettingsManager) {
super._init(0.0, 'Toggle AWG Button');
this.toggleAWG = toggleAWG;
this.settings = settings;
this.gsettingsManager = gsettingsManager;

this._icon = this._createIcon(false);
this.add_child(this._icon);
Expand All @@ -47,47 +48,33 @@ var ToggleAWGButton = GObject.registerClass(
this._updateIcon();
});

this.settings.connect('changed', () => this._updateIcon());
this.gsettingsManager.settings.connect('changed', () => this._updateIcon());
}

_createIcon(isActive) {
return new St.Icon({
gicon: Gio.icon_new_for_string(this.toggleAWG._getIconPath(isActive)),
icon_size: this.settings.get_int('icon-size'),
icon_size: this.gsettingsManager.getValue('icon-size'),
style_class: 'system-status-icon',
});
}

_updateIcon() {
const isActive = this.toggleAWG._isActive;
this._icon.gicon = Gio.icon_new_for_string(this.toggleAWG._getIconPath(isActive));
this._icon.icon_size = this.settings.get_int('icon-size');
this._icon.icon_size = this.gsettingsManager.getValue('icon-size');
}
}
);

class Extension {
constructor() {
}

_initializeDefaults() {
if (!this._settings.get_string('interface')) {
this._settings.set_string('interface', 'awg0');
}
if (!this._settings.get_int('icon-size')) {
this._settings.set_int('icon-size', 24);
}
if (!this._settings.get_string('manual-theme')) {
this._settings.set_string('manual-theme', 'dark');
}
}
constructor() {}

enable() {
this._button = null;
this._settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.amneziawg');
this._initializeDefaults();
const toggleAWG = new ToggleAWG(this._settings);
this._button = new ToggleAWGButton(toggleAWG, this._settings);
this._gsettingsManager = new GsettingsManager();
const toggleAWG = new ToggleAWG(this._gsettingsManager);
this._button = new ToggleAWGButton(toggleAWG, this._gsettingsManager);
Main.panel.addToStatusArea('toggle-awg-button', this._button);
}

Expand All @@ -96,6 +83,9 @@ class Extension {
this._button.destroy();
this._button = null;
}
if (this._gsettingsManager) {
this._gsettingsManager = null;
}
}
}

Expand Down
57 changes: 57 additions & 0 deletions gsettingsManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();

var GsettingsManager = class {
constructor(schemaId = null) {
if (!schemaId) {
const meta = ExtensionUtils.getCurrentExtension().metadata;
schemaId = meta['settings-schema'];
}

this.settings = ExtensionUtils.getSettings(schemaId);

if (!this.settings) {
throw new Error(`Failed to load schema: ${schemaId}`);
}
}


resetKey(key) {
if (!this.settings.list_keys().includes(key)) {
throw new Error(`Key '${key}' does not exist in the schema.`);
}
this.settings.reset(key);
log(`Key '${key}' has been reset to its default value.`);
}

listKeys() {
return this.settings.list_keys();
}

getValue(key) {
if (!this.settings.list_keys().includes(key)) {
throw new Error(`Key '${key}' does not exist in the schema.`);
}
return this.settings.get_value(key).unpack();
}

setValue(key, value) {
if (!this.settings.list_keys().includes(key)) {
throw new Error(`Key '${key}' does not exist in the schema.`);
}

const currentType = this.settings.get_value(key).get_type_string();
const variant = new imports.gi.GLib.Variant(currentType, value);

this.settings.set_value(key, variant);
log(`Key '${key}' has been set to: ${value}`);
}

resetAllKeys() {
const keys = this.listKeys();
for (const key of keys) {
this.resetKey(key);
}
log("All keys have been reset to their default values.");
}
};
3 changes: 2 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"shell-version": ["40", "41", "42", "43", "44"],
"url": "https://github.com/amnezia-vpn/amneziawg-gnome-extension",
"settings-schema": "org.gnome.shell.extensions.amneziawg",
"version": 3
"version": 4,
"version-name": "1.4.0"
}
84 changes: 53 additions & 31 deletions prefs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { GObject, Gtk } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const { GsettingsManager } = Me.imports.gsettingsManager;

let settings;

function init() {
settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.amneziawg');
}
function init() {}

/**
* Creates a labeled widget (label + child) inside a horizontal box.
Expand Down Expand Up @@ -35,35 +33,67 @@ function _createLabeledWidget(labelText, widget) {
* @param {Object[]} options - Array of { id, label } objects for ComboBox options.
* @returns {Gtk.Box} - The labeled ComboBox container.
*/
function _createLabeledComboBox(labelText, settingKey, options) {
function _createLabeledComboBox(gsettingsManager, labelText, settingKey, options) {
const comboBox = new Gtk.ComboBoxText();

options.forEach(option => comboBox.append(option.id, option.label));
comboBox.set_active_id(settings.get_string(settingKey));
comboBox.set_active_id(gsettingsManager.getValue(settingKey));

comboBox.connect('changed', () => {
settings.set_string(settingKey, comboBox.get_active_id());
gsettingsManager.setValue(settingKey, comboBox.get_active_id());
});

return _createLabeledWidget(labelText, comboBox);
}

function _createResetBox(gsettingsManager) {
const box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: 10 });
const resetButton = new Gtk.Button({
label: 'Reset Settings',
halign: Gtk.Align.CENTER
});
resetButton.connect('clicked', () => {
gsettingsManager.resetAllKeys();
gsettingsManager.settings.sync();
});
box.append(resetButton);
return box;
}

/**
* Creates a labeled Entry widget bound to a setting.
* @param {string} labelText - The text for the label.
* @param {string} settingKey - The key for the setting to bind.
* @returns {Object} - { box: Gtk.Box, entry: Gtk.Entry }.
*/
function _createLabeledEntry(labelText, settingKey) {
function _createLabeledEntry(gsettingsManager, labelText, settingKey) {
const box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: 10 });

const label = new Gtk.Label({
label: labelText,
halign: Gtk.Align.START,
hexpand: true
});

const entry = new Gtk.Entry({
text: settings.get_string(settingKey),
text: gsettingsManager.getValue(settingKey),
hexpand: true
});

return {
box: _createLabeledWidget(labelText, entry),
entry
};
const saveButton = new Gtk.Button({
label: 'Save',
});

saveButton.connect('clicked', () => {
gsettingsManager.setValue('interface', entry.text);
gsettingsManager.settings.sync();
});

box.append(label);
box.append(entry);
box.append(saveButton);

return box;
}

/**
Expand All @@ -75,7 +105,7 @@ function _createLabeledEntry(labelText, settingKey) {
* @param {number} step - Step increment.
* @returns {Object} - { box: Gtk.Box, spinButton: Gtk.SpinButton }.
*/
function _createLabeledSpinButton(labelText, settingKey, min, max, step) {
function _createLabeledSpinButton(gsettingsManager, labelText, settingKey, min, max, step) {
const adjustment = new Gtk.Adjustment({
lower: min,
upper: max,
Expand All @@ -84,12 +114,12 @@ function _createLabeledSpinButton(labelText, settingKey, min, max, step) {

const spinButton = new Gtk.SpinButton({
adjustment,
value: settings.get_int(settingKey),
value: gsettingsManager.getValue(settingKey),
hexpand: true
});

spinButton.connect('value-changed', () => {
settings.set_int(settingKey, spinButton.get_value_as_int());
gsettingsManager.setValue(settingKey, spinButton.get_value_as_int());
});

return {
Expand All @@ -103,6 +133,7 @@ function _createLabeledSpinButton(labelText, settingKey, min, max, step) {
* @returns {Gtk.Box} - The main container with all preferences.
*/
function buildPrefsWidget() {
const gsettingsManager = new GsettingsManager();
const widget = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 10,
Expand All @@ -113,14 +144,16 @@ function buildPrefsWidget() {
});

// Interface Name Entry
const { box: interfaceBox, entry: interfaceEntry } = _createLabeledEntry(
const interfaceBox = _createLabeledEntry(
gsettingsManager,
'Interface Name:',
'interface'
);
widget.append(interfaceBox);

// Icon Size Spin Button
const { box: iconSizeBox, spinButton: iconSizeSpinButton } = _createLabeledSpinButton(
gsettingsManager,
'Icon Size:',
'icon-size',
16, // Minimum size
Expand All @@ -131,6 +164,7 @@ function buildPrefsWidget() {

// Icon Theme ComboBox
const iconThemeBox = _createLabeledComboBox(
gsettingsManager,
'Icon Theme:',
'manual-theme',
[
Expand All @@ -139,19 +173,7 @@ function buildPrefsWidget() {
]
);
widget.append(iconThemeBox);

// Save Button
const saveButton = new Gtk.Button({
label: 'Save',
halign: Gtk.Align.CENTER
});

saveButton.connect('clicked', () => {
settings.set_string('interface', interfaceEntry.text);
settings.sync(); // Ensure settings are saved immediately
});

widget.append(saveButton);
widget.append(_createResetBox(gsettingsManager));

return widget;
}

0 comments on commit b867e92

Please sign in to comment.