Skip to content

Commit

Permalink
feat: Move mouse focus position config to gnome extension options
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunarmagpie authored Oct 11, 2022
1 parent aaf86e4 commit 886a069
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 50 deletions.
5 changes: 5 additions & 0 deletions schemas/org.gnome.shell.extensions.pop-shell.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<summary>Move cursor to active window when navigating with keyboard shortcuts or touchpad gestures</summary>
</key>

<key type="u" name="mouse-cursor-focus-location">
<default>0</default>
<summary>The location the mouse cursor focuses when selecting a window</summary>
</key>

<!-- Tiling Options -->
<key type="u" name="column-size">
<default>64</default>
Expand Down
12 changes: 0 additions & 12 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ export interface FloatRule {
title?: string;
};

export enum DefaultPointerPosition {
TopLeft = "TOP_LEFT",
TopRight = "TOP_RIGHT",
BottomLeft = "BOTTOM_LEFT",
BottomRight = "BOTTOM_RIGHT",
Center = "CENTER",
};

export class Config {
/** List of windows that should float, regardless of their WM hints */
float: Array<FloatRule> = [];
Expand All @@ -104,9 +96,6 @@ export class Config {
/** Logs window details on focus of window */
log_on_focus: boolean = false;

/** Specify default pointer position when you're switching windows */
default_pointer_position: DefaultPointerPosition = DefaultPointerPosition.TopLeft;

/** Add a floating exception which matches by wm_class */
add_app_exception(wmclass: string) {
for (const r of this.float) {
Expand Down Expand Up @@ -179,7 +168,6 @@ export class Config {
let c = conf.value;
this.float = c.float;
this.log_on_focus = c.log_on_focus;
this.default_pointer_position = c.default_pointer_position;
} else {
log(`error loading conf: ${conf.why}`)
}
Expand Down
8 changes: 8 additions & 0 deletions src/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import * as Geom from 'geom';
import type { ShellWindow } from 'window';
import type { Ext } from './extension';

export enum FocusPosition {
TopLeft = "Top Left",
TopRight = "Top Right",
BottomLeft = "Bottom Left",
BottomRight = "Bottom Right",
Center = "Center",
}

export class FocusSelector {
select(
ext: Ext,
Expand Down
69 changes: 44 additions & 25 deletions src/prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { Settings } = imports.gi.Gio;

import * as settings from 'settings';
import * as log from 'log';
import * as focus from 'focus';

interface AppWidgets {
fullscreen_launcher: any,
Expand All @@ -20,6 +21,8 @@ interface AppWidgets {
smart_gaps: any,
snap_to_grid: any,
window_titles: any,
mouse_cursor_focus_position: any,
log_level: any,
}

// @ts-ignore
Expand Down Expand Up @@ -66,6 +69,12 @@ function settings_dialog_new(): Gtk.Container {
}
});

app.log_level.set_active(ext.log_level());
app.log_level.connect("changed", () => {
let active_id = app.log_level.get_active_id();
ext.set_log_level(active_id);
});

app.show_skip_taskbar.set_active(ext.show_skiptaskbar());
app.show_skip_taskbar.connect('state-set', (_widget: any, state: boolean) => {
ext.set_show_skiptaskbar(state);
Expand All @@ -78,6 +87,12 @@ function settings_dialog_new(): Gtk.Container {
Settings.sync();
});

app.mouse_cursor_focus_position.set_active(ext.mouse_cursor_focus_location());
app.mouse_cursor_focus_position.connect("changed", () => {
let active_id = app.mouse_cursor_focus_position.get_active_id();
ext.set_mouse_cursor_focus_location(active_id);
});

app.fullscreen_launcher.set_active(ext.fullscreen_launcher())
app.fullscreen_launcher.connect('state-set', (_widget: any, state: boolean) => {
ext.set_fullscreen_launcher(state)
Expand Down Expand Up @@ -128,7 +143,7 @@ function settings_dialog_view(): [AppWidgets, Gtk.Container] {
xalign: 0.0
})

const [inner_gap, outer_gap] = gaps_section(grid, 7);
const [inner_gap, outer_gap] = gaps_section(grid, 8);

const settings = {
inner_gap,
Expand All @@ -138,7 +153,19 @@ function settings_dialog_view(): [AppWidgets, Gtk.Container] {
snap_to_grid: new Gtk.Switch({ halign: Gtk.Align.END }),
window_titles: new Gtk.Switch({ halign: Gtk.Align.END }),
show_skip_taskbar: new Gtk.Switch({ halign: Gtk.Align.END }),
mouse_cursor_follows_active_window: new Gtk.Switch({ halign: Gtk.Align.END })
mouse_cursor_follows_active_window: new Gtk.Switch({ halign: Gtk.Align.END }),
mouse_cursor_focus_position: build_combo(
grid,
6,
focus.FocusPosition,
'Mouse Cursor Focus Position',
),
log_level: build_combo(
grid,
7,
log.LOG_LEVELS,
'Log Level',
)
}

grid.attach(win_label, 0, 0, 1, 1)
Expand All @@ -159,8 +186,6 @@ function settings_dialog_view(): [AppWidgets, Gtk.Container] {
grid.attach(mouse_cursor_follows_active_window_label, 0, 5, 1, 1)
grid.attach(settings.mouse_cursor_follows_active_window, 1, 5, 1, 1)

logging_combo(grid, 6)

return [settings, grid]
}

Expand Down Expand Up @@ -199,35 +224,29 @@ function number_entry(): Gtk.Widget {
return new Gtk.Entry({ input_purpose: Gtk.InputPurpose.NUMBER });
}

function logging_combo(grid: any, top_index: number) {
let log_label = new Gtk.Label({
label: `Log Level`,
function build_combo(
grid: any,
top_index: number,
iter_enum: any,
label: string,
) {
let label_ = new Gtk.Label({
label: label,
halign: Gtk.Align.START
});

grid.attach(log_label, 0, top_index, 1, 1);
grid.attach(label_, 0, top_index, 1, 1);

let log_combo = new Gtk.ComboBoxText();
let combo = new Gtk.ComboBoxText();

for (const key in log.LOG_LEVELS) {
// since log level loop will contain key and level,
// then cherry-pick the number, key will be the text value
if (typeof log.LOG_LEVELS[key] === 'number') {
log_combo.append(`${log.LOG_LEVELS[key]}`, key);
for (const [index, key] of Object.keys(iter_enum).entries()) {
if (typeof iter_enum[key] == 'string') {
combo.append(`${index}`, iter_enum[key]);
}
}

let current_log_level = log.log_level();

log_combo.set_active_id(`${current_log_level}`);
log_combo.connect("changed", () => {
let activeId = log_combo.get_active_id();

let settings = ExtensionUtils.getSettings();
settings.set_uint('log-level', activeId);
});

grid.attach(log_combo, 1, top_index, 1, 1);
grid.attach(combo, 1, top_index, 1, 1);
return combo
}

// @ts-ignore
Expand Down
9 changes: 9 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const DEFAULT_RGBA_COLOR = "rgba(251, 184, 108, 1)"; //pop-orange
const LOG_LEVEL = "log-level";
const SHOW_SKIPTASKBAR = "show-skip-taskbar";
const MOUSE_CURSOR_FOLLOWS_ACTIVE_WINDOW = "mouse-cursor-follows-active-window"
const MOUSE_CURSOR_FOCUS_LOCATION = "mouse-cursor-focus-location";

export class ExtensionSettings {
ext: Settings = settings_new_schema(Me.metadata["settings-schema"]);
Expand Down Expand Up @@ -167,6 +168,10 @@ export class ExtensionSettings {
return this.ext.get_boolean(MOUSE_CURSOR_FOLLOWS_ACTIVE_WINDOW);
}

mouse_cursor_focus_location(): number {
return this.ext.get_uint(MOUSE_CURSOR_FOCUS_LOCATION);
}

// Setters

set_active_hint(set: boolean) {
Expand Down Expand Up @@ -238,4 +243,8 @@ export class ExtensionSettings {
set_mouse_cursor_follows_active_window(set: boolean) {
this.ext.set_boolean(MOUSE_CURSOR_FOLLOWS_ACTIVE_WINDOW, set);
}

set_mouse_cursor_focus_location(set: number) {
this.ext.set_uint(MOUSE_CURSOR_FOCUS_LOCATION, set);
}
}
29 changes: 16 additions & 13 deletions src/window.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-ignore
const Me = imports.misc.extensionUtils.getCurrentExtension();

import * as Config from 'config';
import * as lib from 'lib';
import * as log from 'log';
import * as once_cell from 'once_cell';
Expand All @@ -13,8 +12,8 @@ import type { Entity } from './ecs';
import type { Ext } from './extension';
import type { Rectangle } from './rectangle';
import * as scheduler from 'scheduler';
import * as focus from 'focus';

const { DefaultPointerPosition } = Config;
const { Gdk, Meta, Shell, St, GLib } = imports.gi;

const { OnceCell } = once_cell;
Expand Down Expand Up @@ -124,7 +123,7 @@ export class ShellWindow {
}

activate(move_mouse: boolean = true): void {
activate(this.ext, move_mouse, this.ext.conf.default_pointer_position, this.meta);
activate(this.ext, move_mouse, this.meta);
}

actor_exists(): boolean {
Expand Down Expand Up @@ -380,7 +379,8 @@ export class ShellWindow {
let br = other.rect().clone();

other.move(ext, ar);
this.move(ext, br, () => place_pointer_on(this.ext.conf.default_pointer_position, this.meta));
this.move(ext, br, () => place_pointer_on(this.ext, this.meta)
);
}

title(): string {
Expand Down Expand Up @@ -661,7 +661,7 @@ export class ShellWindow {
}

/// Activates a window, and moves the mouse point.
export function activate(ext: Ext, move_mouse: boolean, default_pointer_position: Config.DefaultPointerPosition, win: Meta.Window) {
export function activate(ext: Ext, move_mouse: boolean, win: Meta.Window) {
try {
if (win.is_override_redirect()) return

Expand All @@ -681,7 +681,7 @@ export function activate(ext: Ext, move_mouse: boolean, default_pointer_position
&& pointer_in_work_area()

if (pointer_placement_permitted) {
place_pointer_on(default_pointer_position, win)
place_pointer_on(ext, win)
}
} catch (error) {
log.error(`failed to activate window: ${error}`)
Expand All @@ -698,29 +698,32 @@ function pointer_in_work_area(): boolean {
return mon ? cursor.intersects(mon) : false
}

export function place_pointer_on(default_pointer_position: Config.DefaultPointerPosition, win: Meta.Window) {
function place_pointer_on(ext: Ext, win: Meta.Window) {
const rect = win.get_frame_rect();
let x = rect.x;
let y = rect.y;

switch (default_pointer_position) {
case DefaultPointerPosition.TopLeft:
let key = Object.keys(focus.FocusPosition)[ext.settings.mouse_cursor_focus_location()]
let pointer_position_ = focus.FocusPosition[key as keyof typeof focus.FocusPosition]

switch (pointer_position_) {
case focus.FocusPosition.TopLeft:
x += 8;
y += 8;
break;
case DefaultPointerPosition.BottomLeft:
case focus.FocusPosition.BottomLeft:
x += 8;
y += (rect.height - 16);
break;
case DefaultPointerPosition.TopRight:
case focus.FocusPosition.TopRight:
x += (rect.width - 16);
y += 8;
break;
case DefaultPointerPosition.BottomRight:
case focus.FocusPosition.BottomRight:
x += (rect.width - 16);
y += (rect.height - 16);
break;
case DefaultPointerPosition.Center:
case focus.FocusPosition.Center:
x += (rect.width / 2) + 8;
y += (rect.height / 2) + 8;
break;
Expand Down

0 comments on commit 886a069

Please sign in to comment.