Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite to use new QuickSettings API in Gnome43 #8

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Keyboard Backlight slider for gnome

Set the keyboard backlight brightness with a slider in gnome shell's system menu.
Set the keyboard backlight brightness with a slider in gnome shell's "Quick Settings" menu.

<table>
<td>
Expand All @@ -13,7 +13,11 @@ This extension adds a third slider below the sound and brightness sliders in the

## Changelog

### v6

- Rewrite to support Gnome 43 via QuickSettings API

### v5

- Fixed compatibility with ubuntu
- better logging
- Fixed compatibility with ubuntu
- better logging
138 changes: 52 additions & 86 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,12 @@
/* exported init */
"use strict";

const { Gio, GLib, GObject, St } = imports.gi;

const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Slider = imports.ui.slider;
const Main = imports.ui.main;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Gettext = imports.gettext;
const Domain = Gettext.domain(Me.metadata.uuid);
const _ = Domain.gettext;

function setTimeout(func, delay, ...args) {
return GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, () => {
func(...args);
return GLib.SOURCE_REMOVE;
});
};
const {Gio, GObject} = imports.gi;

const QuickSettings = imports.ui.quickSettings;

function clearTimeout(timeout) { GLib.source_remove(timeout); };
// This is the live instance of the Quick Settings menu
const QuickSettingsMenu = imports.ui.main.panel.statusArea.quickSettings;

class KbdBrightnessProxy {
constructor(callback) {
Expand Down Expand Up @@ -82,88 +67,69 @@ class KbdBrightnessProxy {
}
}

const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.SystemIndicator {

const FeatureIndicator = GObject.registerClass(
class FeatureIndicator extends QuickSettings.SystemIndicator {
_init() {
super._init();
this._proxy = new KbdBrightnessProxy((proxy, error) => {
if (error) throw error;
proxy.connectSignal('BrightnessChanged', this._sync.bind(this));
this._sync();

// Create the slider and associate it with the indicator, being sure to
// destroy it along with the indicator
this.quickSettingsItems.push(new FeatureSlider());

this.connect('destroy', () => {
this.quickSettingsItems.forEach(item => item.destroy());
});

this._item = new PopupMenu.PopupBaseMenuItem({ activate: false });
this.menu.addMenuItem(this._item);
// Add the indicator to the panel
QuickSettingsMenu._indicators.add_child(this);

this._slider = new Slider.Slider(0);
this._sliderChangedId = this._slider.connect('notify::value',
this._sliderChanged.bind(this));
this._slider.accessible_name = _("Keyboard brightness");
// Add the slider to the menu, passing `2` as the second
// argument to ensure the slider spans both columns of the menu
QuickSettingsMenu._addItems(this.quickSettingsItems, 2);
}
});

let icon = new St.Icon({
icon_name: 'keyboard-brightness-symbolic',
style_class: 'popup-menu-icon'
});
this._item.add(icon);
this._item.add_child(this._slider);
this._item.connect('button-press-event', (actor, event) => {
return this._slider.startDragging(event);
});
this._item.connect('key-press-event', (actor, event) => {
return this._slider.emit('key-press-event', event);
});
this._item.connect('scroll-event', (actor, event) => {
return this._slider.emit('scroll-event', event);
const FeatureSlider = GObject.registerClass(
class FeatureSlider extends QuickSettings.QuickSlider {
_init() {
super._init({
iconName: 'keyboard-brightness-symbolic',
});
this.lastChange = Date.now();
this.changeSliderTimeout = null;
}

_sliderChanged() {
this.lastChange = Date.now();
this._proxy.Brightness = this._slider.value;
}
this._sliderChangedId = this.slider.connect('notify::value',
this._onSliderChanged.bind(this));

_changeSlider(value) {
this._slider.block_signal_handler(this._sliderChangedId);
this._slider.value = value;
this._slider.unblock_signal_handler(this._sliderChangedId);
}
this.slider.accessible_name = 'Keyboard Brightness';

_sync() {
let visible = this._proxy.Brightness >= 0;
this._item.visible = visible;
if (visible) {
if (this.changeSliderTimeout) clearTimeout(this.changeSliderTimeout);
let dt = this.lastChange + 1000 - Date.now();
if (dt < 0) dt = 0;
this.changeSliderTimeout = setTimeout(_ => {
this.changeSliderTimeout = null;
this._changeSlider(this._proxy.Brightness)
}, dt);
}
// create instance of KbpBrightnessProxy
this._proxy = new KbdBrightnessProxy((proxy, error) => {
if (error) throw error;
// proxy.connectSignal('BrightnessChanged', this._sync.bind(this));
// this._sync();
});
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main part I'm unsure about. Can you let me know what the purpose of it is? In general this sync function but also the lastChange calculation.


destroy() {
if (this.changeSliderTimeout) clearTimeout(this.changeSliderTimeout);
this.menu.destroy();
super.destroy();
_onSliderChanged() {
this._proxy.Brightness = this.slider.value;
}
});

var _indicator;
class Extension {
constructor() {
this._indicator = null;
}

function init() {
log(`initializing ${Me.metadata.name}`);
ExtensionUtils.initTranslations(Me.metadata.uuid);
}
enable() {
this._indicator = new FeatureIndicator();
}

function enable() {
_indicator = new Indicator();
Main.panel.statusArea.aggregateMenu.menu.addMenuItem(this._indicator.menu, 2);
disable() {
this._indicator.destroy();
this._indicator = null;
}
}

function disable() {
_indicator.destroy();
_indicator = null;
}
function init() {
return new Extension();
}
8 changes: 3 additions & 5 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"version": 5,
"version": 6,
"name": "Keyboard Backlight Slider",
"description": "Allow setting the keyboard backlight brightness with a slider in the main menu",
"description": "Allow setting the keyboard backlight brightness with a slider in Quick Settings",
"uuid": "[email protected]",
"url": "https://github.com/lovasoa/gnome-keyboard-backlight-menu",
"shell-version": [
"40",
"41",
"42"
"43"
]
}