Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/settings.json
307 changes: 151 additions & 156 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,188 +1,183 @@
"use strict";
// extension.js - GNOME 46 compatible version
'use strict';

const { Shell, Meta, Gio, GObject } = imports.gi;
const Main = imports.ui.main;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import Shell from 'gi://Shell';
import Meta from 'gi://Meta';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';

const SETTINGS_ID = "org.gnome.shell.extensions.focus-window";
const SETTINGS_KEY = "app-settings";
const SETTINGS_VARIANT = "aa{sv}";
const SETTINGS_ID = 'org.gnome.shell.extensions.focus-window';
const SETTINGS_KEY = 'app-settings';
const SETTINGS_VARIANT = 'aa{sv}';

const appSys = Shell.AppSystem.get_default();
const appWin = Shell.WindowTracker.get_default();

const KeyboardShortcuts = GObject.registerClass(
{},
class KeyboardShortcuts extends GObject.Object {
constructor(params = {}) {
super(params);
this.shortcuts = {};

this.displayConnection = global.display.connect(
"accelerator-activated",
(__, action) => {
const grabber = this.shortcuts[action];
if (grabber) grabber.callback();
{},
class KeyboardShortcuts extends GObject.Object {
constructor(params = {}) {
super(params);
this.shortcuts = {};

this.displayConnection = global.display.connect(
'accelerator-activated',
(__, action) => {
const grabber = this.shortcuts[action];
if (grabber) grabber.callback();
}
);
}
);
}

reset() {
for (let action in this.shortcuts) {
this.unbind(action);
}
}
reset() {
for (let action in this.shortcuts) {
this.unbind(action);
}
}

destroy() {
global.display.disconnect(this.displayConnection);
destroy() {
if (this.displayConnection)
global.display.disconnect(this.displayConnection);

for (let action in this.shortcuts) {
this.unbind(action);
}
for (let action in this.shortcuts) {
this.unbind(action);
}

this.shortcuts = {};
this.displayConnection = null;
}
this.shortcuts = {};
this.displayConnection = null;
}

bind(accelerator, callback) {
const action = global.display.grab_accelerator(
accelerator,
Meta.KeyBindingFlags.NONE
);
bind(accelerator, callback) {
const action = global.display.grab_accelerator(
accelerator,
Meta.KeyBindingFlags.NONE
);

if (action === Meta.KeyBindingAction.NONE) return;
if (action === Meta.KeyBindingAction.NONE) return;

const name = Meta.external_binding_name_for_action(action);
Main.wm.allowKeybinding(name, Shell.ActionMode.ALL);
const name = Meta.external_binding_name_for_action(action);
Main.wm.allowKeybinding(name, Shell.ActionMode.ALL);

this.shortcuts[action] = { name, accelerator, callback };
}
this.shortcuts[action] = { name, accelerator, callback };
}

unbind(action) {
const grabber = this.shortcuts[action];
unbind(action) {
const grabber = this.shortcuts[action];

if (grabber) {
global.display.ungrab_accelerator(action);
Main.wm.allowKeybinding(grabber.name, Shell.ActionMode.NONE);
delete this.shortcuts[action];
}
if (grabber) {
global.display.ungrab_accelerator(action);
Main.wm.allowKeybinding(grabber.name, Shell.ActionMode.NONE);
delete this.shortcuts[action];
}
}
}
}
);

class Extension {
constructor() {
this.shortcuts = null;
this.settingsListener = null;
this.settings = null;
}
export default class FocusWindowExtension extends Extension {
enable() {
this.shortcuts = new KeyboardShortcuts();

enable() {
this.shortcuts = new KeyboardShortcuts();
this.settings = this.getSettings(SETTINGS_ID);

this.settings = ExtensionUtils.getSettings(SETTINGS_ID);
this.settingsListener = this.settings.connect(
`changed::${SETTINGS_KEY}`,
() => {
this.setupShortcuts(
this.settings.get_value(SETTINGS_KEY).recursiveUnpack()
);
}
);

this.settingsListener = this.settings.connect(
`changed::${SETTINGS_KEY}`,
() => {
this.setupShortcuts(
this.settings.get_value(SETTINGS_KEY).recursiveUnpack()
this.settings.get_value(SETTINGS_KEY).recursiveUnpack()
);
}
);

this.setupShortcuts(
this.settings.get_value(SETTINGS_KEY).recursiveUnpack()
);
}

setupShortcuts(settings) {
this.shortcuts.reset();

settings.forEach((setting) => {
if (setting.keyboardShortcut && setting.applicationToFocus) {
this.shortcuts.bind(setting.keyboardShortcut, () => {
try {
// get application
const application = appSys.lookup_app(setting.applicationToFocus);
if (!application) return false;

// get application windows and filter appropriately
const appWindows = application.get_windows().filter((window) => {
if (!setting.titleToMatch) return true;
if (setting.exactTitleMatch)
return window.get_title() === setting.titleToMatch;

if (typeof window.get_title() !== "string") return false;

return window
.get_title()
.toLowerCase()
.includes(setting.titleToMatch.toLowerCase());
});

// get the currently focused window
const focusedWindow = global.display.get_focus_window().get_id();

// launch the application
if (!appWindows.length && setting.launchApplication) {
// launch the application normally
if (!setting.commandLineArguments) {
return application.open_new_window(-1);
}

// launch the application with the overriden command line arguments
const context = global.create_app_launch_context(0, -1);
const newApplication = Gio.AppInfo.create_from_commandline(
application.get_app_info().get_executable() +
" " +
setting.commandLineArguments,
null,
Gio.AppInfoCreateFlags.NONE
);

newApplication.launch([], context);
}

// cycle through open windows if there are multiple
if (appWindows.length > 1) {
return Main.activateWindow(appWindows[appWindows.length - 1]);
}
}

// Minimize window if it is already focused and there is only 1 window
if (
appWindows.length === 1 &&
focusedWindow === appWindows[0].get_id()
) {
return appWindows[0].minimize();
setupShortcuts(settings) {
this.shortcuts.reset();

settings.forEach((setting) => {
if (setting.keyboardShortcut && setting.applicationToFocus) {
this.shortcuts.bind(setting.keyboardShortcut, () => {
try {
const application = appSys.lookup_app(setting.applicationToFocus);
if (!application) return false;

const appWindows = application.get_windows().filter((window) => {
if (!setting.titleToMatch) return true;
if (setting.exactTitleMatch)
return window.get_title() === setting.titleToMatch;

if (typeof window.get_title() !== 'string') return false;

return window
.get_title()
.toLowerCase()
.includes(setting.titleToMatch.toLowerCase());
});

const focused = global.display.get_focus_window();
const focusedWindow = focused ? focused.get_id() : null;

if (!appWindows.length && setting.launchApplication) {
if (!setting.commandLineArguments) {
return application.open_new_window(-1);
}

const context = global.create_app_launch_context(0, -1);
const newApplication = Gio.AppInfo.create_from_commandline(
application.get_app_info().get_executable() +
' ' +
setting.commandLineArguments,
null,
Gio.AppInfoCreateFlags.NONE
);

newApplication.launch([], context);
return true;
}

if (appWindows.length > 1) {
return Main.activateWindow(appWindows[appWindows.length - 1]);
}

if (
appWindows.length === 1 &&
focusedWindow !== null &&
focusedWindow === appWindows[0].get_id()
) {
return appWindows[0].minimize();
}

if (appWindows.length === 1) {
return Main.activateWindow(appWindows[0]);
}

return false;
} catch (error) {
console.log('setting trigger failed: ');
console.log(error);
}
});
}
});
}

// Draw focus to the window if it is not already focused
if (appWindows.length === 1) {
return Main.activateWindow(appWindows[0]);
}
disable() {
if (this.shortcuts) {
this.shortcuts.destroy();
}

return false;
} catch (error) {
log("setting trigger failed: ");
log(error);
}
});
}
});
}

disable() {
this.shortcuts.destroy();
this.settings.disconnect(this.settingsListener);

this.settingsListener = null;
this.settings = null;
this.shortcuts = null;
}
}
if (this.settings && this.settingsListener) {
try {
this.settings.disconnect(this.settingsListener);
} catch (e) {}
}

function init() {
return new Extension();
this.settingsListener = null;
this.settings = null;
this.shortcuts = null;
}
}
14 changes: 6 additions & 8 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"name": "Focus Window",
"description": "This extension allows one to create various shortcuts for applications, enabling the ability to have one shortcut that triggers both the launch and focus of an application window.",
"uuid": "focus-window@chris.al",
"shell-version": [
"42"
],
"url": "https://github.com/pcbowers/focus-window",
"version": 1
"name": "Focus Window",
"description": "This extension allows one to create various shortcuts for applications, enabling the ability to have one shortcut that triggers both the launch and focus of an application window.",
"uuid": "focus-window@chris.al",
"shell-version": ["46"],
"url": "https://github.com/pcbowers/focus-window",
"version": 1
}
Loading