Skip to content

Commit

Permalink
fix: some corrections to translations and how the keybindings directi…
Browse files Browse the repository at this point in the history
…ons are represented
  • Loading branch information
domferr committed Oct 26, 2024
1 parent 86bc333 commit a594cd2
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 403 deletions.
157 changes: 81 additions & 76 deletions src/components/tilingsystem/tilingLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Meta from 'gi://Meta';
import { registerGObjectClass } from '@/utils/gjs';
import Mtk from 'gi://Mtk';
import Clutter from 'gi://Clutter';
import St from 'gi://St';
import TilePreview, {
TilePreviewConstructorProperties,
} from '../tilepreview/tilePreview';
Expand All @@ -11,11 +12,13 @@ import Tile from '../layout/Tile';
import {
buildRectangle,
buildTileGaps,
isPointInsideRect,
squaredEuclideanDistance,
} from '@utils/ui';
import TileUtils from '@components/layout/TileUtils';
import { logger } from '@utils/shell';
import GlobalState from '@utils/globalState';
import { KeyBindingsDirection } from '@keybindings';

const debug = logger('TilingLayout');

Expand Down Expand Up @@ -372,102 +375,104 @@ export default class TilingLayout extends LayoutWidget<DynamicTilePreview> {
public findNearestTile(
source: Mtk.Rectangle,
): { rect: Mtk.Rectangle; tile: Tile } | undefined {
let previewFound: DynamicTilePreview | undefined;
let bestDistance = -1;

const sourceCenter = {
const sourceCoords = {
x: source.x + source.width / 2,
y: source.x + source.height / 2,
y: source.y + source.height / 2,
};

for (let i = 0; i < this._previews.length; i++) {
const preview = this._previews[i];

const previewCenter = {
x: preview.innerX + preview.innerWidth / 2,
y: preview.innerY + preview.innerHeight / 2,
};

const euclideanDistance = squaredEuclideanDistance(
previewCenter,
sourceCenter,
);
// uncomment to show debugging
/* global.windowGroup
.get_children()
.filter((c) => c.get_name() === 'debug-kb')[0]
?.destroy();
const debugWidget = new St.Widget({
x: sourceCoords.x - 8,
y: sourceCoords.y - 8,
height: 16,
width: 16,
style: 'border: 2px solid red; border-radius: 8px;',
name: 'debug-kb',
});
global.windowGroup.add_child(debugWidget);*/

if (!previewFound || euclideanDistance < bestDistance) {
previewFound = preview;
bestDistance = euclideanDistance;
for (let i = 0; i < this._previews.length; i++) {
const previewFound = this._previews[i];
if (isPointInsideRect(sourceCoords, previewFound.rect)) {
return {
rect: buildRectangle({
x: previewFound.innerX,
y: previewFound.innerY,
width: previewFound.innerWidth,
height: previewFound.innerHeight,
}),
tile: previewFound.tile,
};
}
}

if (!previewFound) return undefined;

return {
rect: buildRectangle({
x: previewFound.innerX,
y: previewFound.innerY,
width: previewFound.innerWidth,
height: previewFound.innerHeight,
}),
tile: previewFound.tile,
};
return undefined;
}

public findNearestTileDirection(
source: Mtk.Rectangle,
direction: Meta.DisplayDirection,
direction: KeyBindingsDirection,
): { rect: Mtk.Rectangle; tile: Tile } | undefined {
const sourceCenter = {
if (direction === KeyBindingsDirection.CENTER) return undefined;

const sourceCoords = {
x: source.x + source.width / 2,
y: source.x + source.height / 2,
y: source.y + source.height / 2,
};

const filtered = this._previews.filter((preview) => {
switch (direction) {
case Meta.DisplayDirection.RIGHT:
return preview.x >= source.x + source.width;
case Meta.DisplayDirection.LEFT:
return preview.x + preview.width <= source.x;
case Meta.DisplayDirection.DOWN:
return preview.y >= source.y + source.height;
case Meta.DisplayDirection.UP:
return preview.y + preview.height <= source.y;
default:
return false;
}
});

let previewFound: DynamicTilePreview | undefined;
let bestDistance = -1;
for (let i = 0; i < filtered.length; i++) {
const preview = filtered[i];

const previewCenter = {
x: preview.innerX + preview.innerWidth / 2,
y: preview.innerY + preview.innerHeight / 2,
};
// enlarge the side of the direction and search a tile that contains that point
const enlarge = 64;

switch (direction) {
case KeyBindingsDirection.RIGHT:
sourceCoords.x = source.x + source.width + enlarge;
break;
case KeyBindingsDirection.LEFT:
sourceCoords.x = source.x - enlarge;
break;
case KeyBindingsDirection.DOWN:
sourceCoords.y = source.y + source.height + enlarge;
break;
case KeyBindingsDirection.UP:
sourceCoords.y = source.y - enlarge;
break;
}

const euclideanDistance = squaredEuclideanDistance(
previewCenter,
sourceCenter,
);
// uncomment to show debugging
/* global.windowGroup
.get_children()
.filter((c) => c.get_name() === 'debug-kb')[0]
?.destroy();
const debugWidget = new St.Widget({
x: sourceCoords.x - 8,
y: sourceCoords.y - 8,
height: 16,
width: 16,
style: 'border: 2px solid red; border-radius: 8px;',
name: 'debug-kb',
});
global.windowGroup.add_child(debugWidget);*/

if (!previewFound || euclideanDistance < bestDistance) {
previewFound = preview;
bestDistance = euclideanDistance;
for (let i = 0; i < this._previews.length; i++) {
const previewFound = this._previews[i];
if (isPointInsideRect(sourceCoords, previewFound.rect)) {
return {
rect: buildRectangle({
x: previewFound.innerX,
y: previewFound.innerY,
width: previewFound.innerWidth,
height: previewFound.innerHeight,
}),
tile: previewFound.tile,
};
}
}

if (!previewFound) return undefined;

return {
rect: buildRectangle({
x: previewFound.innerX,
y: previewFound.innerY,
width: previewFound.innerWidth,
height: previewFound.innerHeight,
}),
tile: previewFound.tile,
};
return undefined;
}

public getRightmostTile(): { rect: Mtk.Rectangle; tile: Tile } {
Expand Down
29 changes: 15 additions & 14 deletions src/components/tilingsystem/tilingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Monitor } from 'resource:///org/gnome/shell/ui/layout.js';
import ExtendedWindow from './extendedWindow';
import EdgeTilingManager from './edgeTilingManager';
import TouchPointer from './touchPointer';
import { KeyBindingsDirection } from '@keybindings';

const MINIMUM_DISTANCE_TO_RESTORE_ORIGINAL_SIZE = 90;

Expand Down Expand Up @@ -190,27 +191,27 @@ export class TilingManager {

public onKeyboardMoveWindow(
window: Meta.Window,
direction: Meta.DisplayDirection | undefined, // direction is undefined -> move to the center of the screen
direction: KeyBindingsDirection,
force: boolean,
spanFlag: boolean,
): boolean {
let destination: { rect: Mtk.Rectangle; tile: Tile } | undefined;
if (window.get_maximized()) {
if (spanFlag) return false;
if (spanFlag && window.get_maximized()) return false;

if (window.get_maximized()) {
switch (direction) {
case undefined:
case KeyBindingsDirection.CENTER:
window.unmaximize(Meta.MaximizeFlags.BOTH);
break;
case Meta.DisplayDirection.DOWN:
case KeyBindingsDirection.DOWN:
window.unmaximize(Meta.MaximizeFlags.BOTH);
return true;
case Meta.DisplayDirection.UP:
case KeyBindingsDirection.UP:
return false;
case Meta.DisplayDirection.LEFT:
case KeyBindingsDirection.LEFT:
destination = this._tilingLayout.getLeftmostTile();
break;
case Meta.DisplayDirection.RIGHT:
case KeyBindingsDirection.RIGHT:
destination = this._tilingLayout.getRightmostTile();
break;
}
Expand All @@ -220,7 +221,7 @@ export class TilingManager {
const windowRectCopy = window.get_frame_rect().copy();
if (!destination) {
// if the window is not tiled, find the nearest tile in any direction
if (!direction) {
if (direction === KeyBindingsDirection.CENTER) {
// direction is undefined -> move to the center of the screen
const rect = buildRectangle({
x:
Expand Down Expand Up @@ -255,7 +256,7 @@ export class TilingManager {

// handle maximize of window
if (
direction === Meta.DisplayDirection.UP &&
direction === KeyBindingsDirection.UP &&
window.can_maximize()
) {
window.maximize(Meta.MaximizeFlags.BOTH);
Expand All @@ -275,14 +276,14 @@ export class TilingManager {
);
}

if (window.get_maximized()) window.unmaximize(Meta.MaximizeFlags.BOTH);

this._easeWindowRect(window, destination.rect, false, force);

// ensure the assigned tile is a COPY
(window as ExtendedWindow).assignedTile = new Tile({
...destination.tile,
});

if (window.get_maximized()) window.unmaximize(Meta.MaximizeFlags.BOTH);

this._easeWindowRect(window, destination.rect, false, force);
return true;
}

Expand Down
Loading

0 comments on commit a594cd2

Please sign in to comment.