Files

89 lines
3.2 KiB
JavaScript

import Meta from 'gi://Meta';
import Shell from 'gi://Shell';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
// ignore the conky window as alignment target if the tile would get narrower
const MIN_FRACTION = 0.5;
export default class WideTileExtension extends Extension {
enable() {
this._settings = this.getSettings();
this._handlers = new Map();
for (const [binding, dir] of [['toggle-tiled-left', -1],
['toggle-tiled-right', 1]]) {
const handler = () => this._tileSide(dir);
Main.wm.setCustomKeybindingHandler(binding,
Shell.ActionMode.NORMAL, handler);
this._handlers.set(binding, handler);
}
this._prevRects = new WeakMap();
}
disable() {
for (const [binding] of this._handlers)
Main.wm.setCustomKeybindingHandler(binding,
Shell.ActionMode.NORMAL, null);
this._handlers = null;
this._prevRects = null;
this._settings = null;
}
_tileSide(dir) {
const win = global.display.focus_window;
if (!win || !win.allows_move() || !win.allows_resize())
return;
const wa = win.get_work_area_current_monitor();
const mon = global.display.get_monitor_geometry(win.get_monitor());
// target width: conky-aligned or a fraction of the work area
const fraction = this._settings.get_double('fraction');
let width = Math.floor(wa.width * fraction);
if (this._settings.get_string('mode') === 'conky') {
const monIndex = win.get_monitor();
for (const actor of global.get_window_actors()) {
const mw = actor.meta_window;
if (mw.get_wm_class() === 'conky' &&
mw.get_monitor() === monIndex) {
const w = mw.get_frame_rect().x - wa.x;
if (w >= wa.width * MIN_FRACTION)
width = w;
break;
}
}
}
// reach the monitor's bottom edge even if the work area is a few
// pixels short
const height = mon.y + mon.height - wa.y;
const target = {
x: dir < 0 ? wa.x : wa.x + wa.width - width,
y: wa.y,
width,
height,
};
// toggle: second press restores the pre-tile geometry
const frame = win.get_frame_rect();
if (frame.x === target.x && frame.y === target.y &&
frame.width === target.width && frame.height === target.height) {
const prev = this._prevRects.get(win);
if (prev)
win.move_resize_frame(false, prev.x, prev.y,
prev.width, prev.height);
this._prevRects.delete(win);
return;
}
this._prevRects.set(win, frame);
if (win.maximized_horizontally || win.maximized_vertically)
win.unmaximize(Meta.MaximizeFlags.HORIZONTAL |
Meta.MaximizeFlags.VERTICAL);
win.move_resize_frame(false, target.x, target.y,
target.width, target.height);
}
}