forked from yilozt/rounded-window-corners
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor the dbus directory
- Move the dbus directory to preferences/window_picker since it's only used for window picking within preferences. - Simplify some of the code. - Add documentation comments and explain the purpose of the dbus service.
- Loading branch information
Showing
8 changed files
with
170 additions
and
152 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# `window_picker` | ||
|
||
The extensions preferences window runs in a separate isolated process which has | ||
no access to GNOME Shell methods. However, the window picking functionality is | ||
based on GNOME Shell's Looking Glass. | ||
|
||
To allow the preferences window to communicate with the main process, the code | ||
in this directory creates a DBus service, which has a method to open the window | ||
picker and a signal to transmit the class of the selected window. | ||
|
||
## `iface.xml` | ||
|
||
Defines the DBus interface for the window picker. | ||
|
||
## `service.ts` | ||
|
||
Contains the implementation of the DBus interface. | ||
|
||
## `client.ts` | ||
|
||
Provides wrapper JavaScript functions around the DBus method calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @file This file provides wrapper functions around the DBus window picker | ||
* interface. | ||
*/ | ||
|
||
import Gio from 'gi://Gio'; | ||
|
||
const connection = Gio.DBus.session; | ||
const busName = 'org.gnome.Shell'; | ||
const interfaceName = 'org.gnome.Shell.Extensions.RoundedWindowCorners'; | ||
const objectPath = '/org/gnome/shell/extensions/RoundedWindowCorners'; | ||
|
||
/** Open the window picker and select a window. */ | ||
export function pick() { | ||
connection.call( | ||
busName, | ||
objectPath, | ||
interfaceName, | ||
'pick', | ||
null, | ||
null, | ||
Gio.DBusCallFlags.NO_AUTO_START, | ||
-1, | ||
null, | ||
null, | ||
); | ||
} | ||
|
||
/** | ||
* Connect a callback to the `picked` signal, which is emitted when a window | ||
* is picked. | ||
* | ||
* @param callback - The function to execute when the window is picked. | ||
*/ | ||
export function onPicked(callback: (wmInstanceClass: string) => void) { | ||
const id = connection.signal_subscribe( | ||
busName, | ||
interfaceName, | ||
'picked', | ||
objectPath, | ||
null, | ||
Gio.DBusSignalFlags.NONE, | ||
(_conn, _sender, _objectPath, _iface, _signal, params) => { | ||
const val = params.get_child_value(0); | ||
callback(val.get_string()[0]); | ||
connection.signal_unsubscribe(id); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
<node> | ||
<interface name="org.gnome.Shell.Extensions.RoundedWindowCorners"> | ||
<!-- This method is called in preferences to pick a window --> | ||
<method name="pick" /> | ||
<!-- If window is picked, send a signal to preferences --> | ||
<signal name="picked"> | ||
<arg name="window" type="s"/> | ||
<arg name="window" type="s" /> | ||
</signal> | ||
</interface> | ||
</node> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @file This file contains the implementation of the DBus interface for the | ||
* window picker. See the {@link WindowPicker} class for more information. | ||
*/ | ||
|
||
import GLib from 'gi://GLib'; | ||
import Gio from 'gi://Gio'; | ||
import Meta from 'gi://Meta'; | ||
|
||
import {Inspector} from 'resource:///org/gnome/shell/ui/lookingGlass.js'; | ||
import * as Main from 'resource:///org/gnome/shell/ui/main.js'; | ||
|
||
import {loadFile} from '../../utils/io.js'; | ||
import {_log} from '../../utils/log.js'; | ||
|
||
const iface = loadFile(import.meta.url, 'iface.xml'); | ||
|
||
/** | ||
* This class provides the implementation of the DBus interface for the window | ||
* picker. It implements a single method - `pick` - which opens the window picker | ||
* and allows the user to select a window. | ||
*/ | ||
export class WindowPicker { | ||
#dbus = Gio.DBusExportedObject.wrapJSObject(iface, this); | ||
|
||
/** Emit the wm_class of the picked window to the `picked` signal. */ | ||
#sendPickedWindow(wmClass: string) { | ||
this.#dbus.emit_signal('picked', new GLib.Variant('(s)', [wmClass])); | ||
} | ||
|
||
/** | ||
* Open the window picker and select a window. | ||
* | ||
* This uses the window picker from GNOME's Looking Glass. This is the | ||
* easiest way to pick a window, and this is also what's used by other | ||
* extensions such as Blur my Shell. | ||
*/ | ||
pick() { | ||
const lookingGlass = Main.createLookingGlass(); | ||
const inspector = new Inspector(lookingGlass); | ||
|
||
inspector.connect('target', (me, target, x, y) => { | ||
_log(`${me}: pick ${target} in ${x}, ${y}`); | ||
|
||
// Remove the red border effect when the window is picked. | ||
const effectName = 'lookingGlass_RedBorderEffect'; | ||
for (const effect of target.get_effects()) { | ||
if (effect.toString().includes(effectName)) { | ||
target.remove_effect(effect); | ||
} | ||
} | ||
|
||
let actor = target; | ||
|
||
// If the picked actor is not a Meta.WindowActor, which happens | ||
// often since it's usually a Meta.SurfaceActor, try to find its | ||
// parent which is a Meta.WindowActor. | ||
for (let i = 0; i < 2; i++) { | ||
if (actor == null || actor instanceof Meta.WindowActor) { | ||
break; | ||
} | ||
actor = actor.get_parent(); | ||
} | ||
|
||
if (!(actor instanceof Meta.WindowActor)) { | ||
this.#sendPickedWindow('window-not-found'); | ||
return; | ||
} | ||
|
||
this.#sendPickedWindow( | ||
actor.metaWindow.get_wm_class_instance() ?? 'window-not-found', | ||
); | ||
}); | ||
|
||
inspector.connect('closed', () => { | ||
lookingGlass.close(); | ||
}); | ||
} | ||
|
||
export() { | ||
this.#dbus.export( | ||
Gio.DBus.session, | ||
'/org/gnome/shell/extensions/RoundedWindowCorners', | ||
); | ||
_log('DBus Service exported'); | ||
} | ||
|
||
unexport() { | ||
this.#dbus.unexport(); | ||
} | ||
} |