Skip to content

Commit

Permalink
feat: enable/disable window menu override
Browse files Browse the repository at this point in the history
  • Loading branch information
domferr committed Jul 14, 2024
1 parent a84b13c commit 130872a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@
<summary>Drag agains top edge to maximize window</summary>
<description>Drag windows against the top edge to maximize them</description>
</key>

<key name="override-window-menu" type="b">
<default>true</default>
<summary>Add snap assistant and auto-tile buttons to window menu</summary>
<description>Add snap assistant and auto-tile buttons in the menu that shows up when you right click on a window title.</description>
</key>

<!-- keybindings -->
<key type="as" name="move-window-right">
<default><![CDATA[['<Super>Right']]]></default>
<summary>Move focused window to the tile to its right</summary>
Expand All @@ -116,4 +122,4 @@
</key>
</schema>

</schemalist>
</schemalist>
92 changes: 53 additions & 39 deletions src/components/window_menu/overriddenWindowMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import GlobalState from '@utils/globalState';
import Settings from '@settings/settings';
import { registerGObjectClass } from '@utils/gjs';
import Tile from '@components/layout/Tile';
import { buildMarginOf, getWindows } from '@utils/ui';
import { getWindows } from '@utils/ui';
import ExtendedWindow from '@components/tilingsystem/extendedWindow';
import TileUtils from '@components/layout/TileUtils';
import LayoutIcon from './layoutIcon';
import LayoutTileButtons from './layoutTileButtons';
import { buildMarginOf } from '@utils/ui';
import LayoutIcon from './layoutIcon';

const LAYOUT_ICON_WIDTH = 46;
const LAYOUT_ICON_HEIGHT = 32;

function buildMenuWithLayoutIcon(
export function buildMenuWithLayoutIcon(
title: string,
popupMenu: PopupMenu.PopupBaseMenuItem,
importantTiles: Tile[],
Expand All @@ -36,8 +40,8 @@ function buildMenuWithLayoutIcon(
tiles,
buildMarginOf(innerGaps),
buildMarginOf(4),
46,
32,
LAYOUT_ICON_WIDTH,
LAYOUT_ICON_HEIGHT,
);
layoutIcon.set_x_align(Clutter.ActorAlign.END);
}
Expand All @@ -55,6 +59,7 @@ export default class OverriddenWindowMenu extends GObject.Object {

private static _instance: OverriddenWindowMenu | null = null;
private static _old_buildMenu: ((window: Meta.Window) => void) | null;
private static _enabled: boolean = false;

static get(): OverriddenWindowMenu {
if (this._instance === null)
Expand All @@ -63,17 +68,32 @@ export default class OverriddenWindowMenu extends GObject.Object {
}

static enable() {
// if it is already enabled, do not enable again
if (this._enabled) return;

const owm = this.get();

OverriddenWindowMenu._old_buildMenu =
windowMenu.WindowMenu.prototype._buildMenu;
const owm = this.get();
windowMenu.WindowMenu.prototype._buildMenu = owm.newBuildMenu;

this._enabled = true;
}

static disable() {
this._instance = null;
// if it is not enabled, do not disable
if (!this._enabled) return;

windowMenu.WindowMenu.prototype._buildMenu =
OverriddenWindowMenu._old_buildMenu;
this._old_buildMenu = null;

this._enabled = false;
}

static destroy() {
this.disable();
this._instance = null;
}

// the function will be treated as a method of class WindowMenu
Expand Down Expand Up @@ -127,7 +147,11 @@ export default class OverriddenWindowMenu extends GObject.Object {
hasGaps ? 2 : 0,
);
vacantPopupMenu.connect('activate', () => {
owm.emit('tile-clicked', vacantTiles[middleTileIndex], window);
OverriddenWindowMenu.get().emit(
'tile-clicked',
vacantTiles[middleTileIndex],
window,
);
});
}

Expand All @@ -143,7 +167,11 @@ export default class OverriddenWindowMenu extends GObject.Object {
hasGaps ? 2 : 0,
);
vacantLeftPopupMenu.connect('activate', () => {
owm.emit('tile-clicked', vacantTiles[0], window);
OverriddenWindowMenu.get().emit(
'tile-clicked',
vacantTiles[0],
window,
);
});

const tilesFromRightToLeft = vacantTiles
Expand All @@ -160,7 +188,11 @@ export default class OverriddenWindowMenu extends GObject.Object {
hasGaps ? 2 : 0,
);
vacantRightPopupMenu.connect('activate', () => {
owm.emit('tile-clicked', tilesFromRightToLeft[0], window);
OverriddenWindowMenu.get().emit(
'tile-clicked',
tilesFromRightToLeft[0],
window,
);
});
}

Expand Down Expand Up @@ -194,7 +226,6 @@ export default class OverriddenWindowMenu extends GObject.Object {

const layoutHeight: number = 30;
const layoutWidth: number = 52; // 16:9 ratio. -> (16*layoutHeight) / 9 and then rounded to int
const owm = OverriddenWindowMenu.get();
layouts.forEach((lay, ind) => {
const row = rows[Math.floor(ind / layoutsPerRow)];
const layoutWidget = new LayoutTileButtons(
Expand All @@ -208,7 +239,11 @@ export default class OverriddenWindowMenu extends GObject.Object {
layoutWidget.set_x_align(Clutter.ActorAlign.END);
layoutWidget.buttons.forEach((btn) => {
btn.connect('clicked', () => {
owm.emit('tile-clicked', btn.tile, window);
OverriddenWindowMenu.get().emit(
'tile-clicked',
btn.tile,
window,
);
layoutsPopupMenu.activate(Clutter.get_current_event());
});
});
Expand Down Expand Up @@ -255,33 +290,12 @@ export default class OverriddenWindowMenu extends GObject.Object {
});
});*/
}
}

/* Main.panel.statusArea.appMenu.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
static connect(key: string, func: (...arg: unknown[]) => void): number {
return this.get().connect(key, func) || -1;
}

const layouts = GlobalState.get().layouts;
const rowsBoxLayout: St.BoxLayout[] = [];
const layoutsPerRow = 2;
for (let i = 0; i < layouts.length / layoutsPerRow; i++) {
const item = new PopupMenu.PopupBaseMenuItem({ styleClass: 'indicator-menu-item' });
const box = new St.BoxLayout({
x_align: Clutter.ActorAlign.CENTER,
y_align: Clutter.ActorAlign.CENTER,
xExpand: true,
vertical: false, // horizontal box layout
styleClass: "layouts-box-layout",
});
rowsBoxLayout.push(box);
item.add_actor(box);
Main.panel.statusArea.appMenu.menu.addMenuItem(item);
static disconnect(id: number) {
this.get().disconnect(id);
}
}
const hasGaps = Settings.get_inner_gaps(1).top > 0;
const layoutHeight: number = 36;
const layoutWidth: number = 64; // 16:9 ratio. -> (16*layoutHeight) / 9 and then rounded to int
const layoutsButtons: St.Widget[] = layouts.map((lay, ind) => {
const btn = new St.Button({xExpand: false, styleClass: "layout-button button"});
btn.child = new LayoutSelectionWidget(lay, hasGaps ? 1:0, 1, layoutHeight, layoutWidth);
rowsBoxLayout[Math.floor(ind / layoutsPerRow)].add_child(btn);
return btn;
});*/
32 changes: 22 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,7 @@ export default class TilingShellExtension extends Extension {
this._dbus = new DBus();
this._dbus.enable(this);

OverriddenWindowMenu.enable();
OverriddenWindowMenu.get().connect(
'tile-clicked',
(_, tile: Tile, window: Meta.Window) => {
const monitorIndex = window.get_monitor();
const manager = this._tilingManagers[monitorIndex];
if (manager) manager.onTileFromWindowMenu(tile, window);
},
);
if (Settings.get_override_window_menu()) OverriddenWindowMenu.enable();

debug('extension is enabled');
}
Expand Down Expand Up @@ -218,6 +210,26 @@ export default class TilingShellExtension extends Extension {
);
},
);

this._signals.connect(
Settings,
Settings.SETTING_OVERRIDE_WINDOW_MENU,
() => {
if (Settings.get_override_window_menu())
OverriddenWindowMenu.enable();
else OverriddenWindowMenu.disable();
},
);

this._signals.connect(
OverriddenWindowMenu,
'tile-clicked',
(_, tile: Tile, window: Meta.Window) => {
const monitorIndex = window.get_monitor();
const manager = this._tilingManagers[monitorIndex];
if (manager) manager.onTileFromWindowMenu(tile, window);
},
);
}

private _onKeyboardMoveWin(
Expand Down Expand Up @@ -323,7 +335,7 @@ export default class TilingShellExtension extends Extension {

this._fractionalScalingEnabled = false;

OverriddenWindowMenu.disable();
OverriddenWindowMenu.destroy();
debug('extension is disabled');
}
}
7 changes: 7 additions & 0 deletions src/prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ export default class TilingShellExtensionPreferences extends ExtensionPreference
);
behaviourGroup.add(restoreToOriginalSizeRow);

const overrideWindowMenuRow = this._buildSwitchRow(
Settings.SETTING_OVERRIDE_WINDOW_MENU,
'Add snap assistant and auto-tile buttons to window menu',
'Add snap assistant and auto-tile buttons in the menu that shows up when you right click on a window title',
);
behaviourGroup.add(overrideWindowMenuRow);

// Screen Edges section
const activeScreenEdgesGroup = new Adw.PreferencesGroup({
title: 'Screen Edges',
Expand Down
8 changes: 8 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class Settings {
static SETTING_ENABLE_MOVE_KEYBINDINGS = 'enable-move-keybindings';
static SETTING_ACTIVE_SCREEN_EDGES = 'active-screen-edges';
static SETTING_TOP_EDGE_MAXIMIZE = 'top-edge-maximize';
static SETTING_OVERRIDE_WINDOW_MENU = 'override-window-menu';

static SETTING_MOVE_WINDOW_RIGHT = 'move-window-right';
static SETTING_MOVE_WINDOW_LEFT = 'move-window-left';
Expand Down Expand Up @@ -228,6 +229,13 @@ export default class Settings {
);
}

static get_override_window_menu(): boolean {
return (
this._settings?.get_boolean(this.SETTING_OVERRIDE_WINDOW_MENU) ??
false
);
}

static set_last_version_installed(version: string) {
this._settings?.set_string(
this.SETTING_LAST_VERSION_NAME_INSTALLED,
Expand Down

0 comments on commit 130872a

Please sign in to comment.