@@ -2,6 +2,7 @@ import Meta from 'gi://Meta';
2
2
import { registerGObjectClass } from '@/utils/gjs' ;
3
3
import Mtk from 'gi://Mtk' ;
4
4
import Clutter from 'gi://Clutter' ;
5
+ import St from 'gi://St' ;
5
6
import TilePreview , {
6
7
TilePreviewConstructorProperties ,
7
8
} from '../tilepreview/tilePreview' ;
@@ -11,11 +12,13 @@ import Tile from '../layout/Tile';
11
12
import {
12
13
buildRectangle ,
13
14
buildTileGaps ,
15
+ isPointInsideRect ,
14
16
squaredEuclideanDistance ,
15
17
} from '@utils/ui' ;
16
18
import TileUtils from '@components/layout/TileUtils' ;
17
19
import { logger } from '@utils/shell' ;
18
20
import GlobalState from '@utils/globalState' ;
21
+ import { KeyBindingsDirection } from '@keybindings' ;
19
22
20
23
const debug = logger ( 'TilingLayout' ) ;
21
24
@@ -372,102 +375,104 @@ export default class TilingLayout extends LayoutWidget<DynamicTilePreview> {
372
375
public findNearestTile (
373
376
source : Mtk . Rectangle ,
374
377
) : { rect : Mtk . Rectangle ; tile : Tile } | undefined {
375
- let previewFound : DynamicTilePreview | undefined ;
376
- let bestDistance = - 1 ;
377
-
378
- const sourceCenter = {
378
+ const sourceCoords = {
379
379
x : source . x + source . width / 2 ,
380
- y : source . x + source . height / 2 ,
380
+ y : source . y + source . height / 2 ,
381
381
} ;
382
382
383
- for ( let i = 0 ; i < this . _previews . length ; i ++ ) {
384
- const preview = this . _previews [ i ] ;
385
-
386
- const previewCenter = {
387
- x : preview . innerX + preview . innerWidth / 2 ,
388
- y : preview . innerY + preview . innerHeight / 2 ,
389
- } ;
390
-
391
- const euclideanDistance = squaredEuclideanDistance (
392
- previewCenter ,
393
- sourceCenter ,
394
- ) ;
383
+ // uncomment to show debugging
384
+ /* global.windowGroup
385
+ .get_children()
386
+ .filter((c) => c.get_name() === 'debug-kb')[0]
387
+ ?.destroy();
388
+ const debugWidget = new St.Widget({
389
+ x: sourceCoords.x - 8,
390
+ y: sourceCoords.y - 8,
391
+ height: 16,
392
+ width: 16,
393
+ style: 'border: 2px solid red; border-radius: 8px;',
394
+ name: 'debug-kb',
395
+ });
396
+ global.windowGroup.add_child(debugWidget);*/
395
397
396
- if ( ! previewFound || euclideanDistance < bestDistance ) {
397
- previewFound = preview ;
398
- bestDistance = euclideanDistance ;
398
+ for ( let i = 0 ; i < this . _previews . length ; i ++ ) {
399
+ const previewFound = this . _previews [ i ] ;
400
+ if ( isPointInsideRect ( sourceCoords , previewFound . rect ) ) {
401
+ return {
402
+ rect : buildRectangle ( {
403
+ x : previewFound . innerX ,
404
+ y : previewFound . innerY ,
405
+ width : previewFound . innerWidth ,
406
+ height : previewFound . innerHeight ,
407
+ } ) ,
408
+ tile : previewFound . tile ,
409
+ } ;
399
410
}
400
411
}
401
412
402
- if ( ! previewFound ) return undefined ;
403
-
404
- return {
405
- rect : buildRectangle ( {
406
- x : previewFound . innerX ,
407
- y : previewFound . innerY ,
408
- width : previewFound . innerWidth ,
409
- height : previewFound . innerHeight ,
410
- } ) ,
411
- tile : previewFound . tile ,
412
- } ;
413
+ return undefined ;
413
414
}
414
415
415
416
public findNearestTileDirection (
416
417
source : Mtk . Rectangle ,
417
- direction : Meta . DisplayDirection ,
418
+ direction : KeyBindingsDirection ,
418
419
) : { rect : Mtk . Rectangle ; tile : Tile } | undefined {
419
- const sourceCenter = {
420
+ if ( direction === KeyBindingsDirection . CENTER ) return undefined ;
421
+
422
+ const sourceCoords = {
420
423
x : source . x + source . width / 2 ,
421
- y : source . x + source . height / 2 ,
424
+ y : source . y + source . height / 2 ,
422
425
} ;
423
426
424
- const filtered = this . _previews . filter ( ( preview ) => {
425
- switch ( direction ) {
426
- case Meta . DisplayDirection . RIGHT :
427
- return preview . x >= source . x + source . width ;
428
- case Meta . DisplayDirection . LEFT :
429
- return preview . x + preview . width <= source . x ;
430
- case Meta . DisplayDirection . DOWN :
431
- return preview . y >= source . y + source . height ;
432
- case Meta . DisplayDirection . UP :
433
- return preview . y + preview . height <= source . y ;
434
- default :
435
- return false ;
436
- }
437
- } ) ;
438
-
439
- let previewFound : DynamicTilePreview | undefined ;
440
- let bestDistance = - 1 ;
441
- for ( let i = 0 ; i < filtered . length ; i ++ ) {
442
- const preview = filtered [ i ] ;
443
-
444
- const previewCenter = {
445
- x : preview . innerX + preview . innerWidth / 2 ,
446
- y : preview . innerY + preview . innerHeight / 2 ,
447
- } ;
427
+ // enlarge the side of the direction and search a tile that contains that point
428
+ const enlarge = 64 ;
429
+
430
+ switch ( direction ) {
431
+ case KeyBindingsDirection . RIGHT :
432
+ sourceCoords . x = source . x + source . width + enlarge ;
433
+ break ;
434
+ case KeyBindingsDirection . LEFT :
435
+ sourceCoords . x = source . x - enlarge ;
436
+ break ;
437
+ case KeyBindingsDirection . DOWN :
438
+ sourceCoords . y = source . y + source . height + enlarge ;
439
+ break ;
440
+ case KeyBindingsDirection . UP :
441
+ sourceCoords . y = source . y - enlarge ;
442
+ break ;
443
+ }
448
444
449
- const euclideanDistance = squaredEuclideanDistance (
450
- previewCenter ,
451
- sourceCenter ,
452
- ) ;
445
+ // uncomment to show debugging
446
+ /* global.windowGroup
447
+ .get_children()
448
+ .filter((c) => c.get_name() === 'debug-kb')[0]
449
+ ?.destroy();
450
+ const debugWidget = new St.Widget({
451
+ x: sourceCoords.x - 8,
452
+ y: sourceCoords.y - 8,
453
+ height: 16,
454
+ width: 16,
455
+ style: 'border: 2px solid red; border-radius: 8px;',
456
+ name: 'debug-kb',
457
+ });
458
+ global.windowGroup.add_child(debugWidget);*/
453
459
454
- if ( ! previewFound || euclideanDistance < bestDistance ) {
455
- previewFound = preview ;
456
- bestDistance = euclideanDistance ;
460
+ for ( let i = 0 ; i < this . _previews . length ; i ++ ) {
461
+ const previewFound = this . _previews [ i ] ;
462
+ if ( isPointInsideRect ( sourceCoords , previewFound . rect ) ) {
463
+ return {
464
+ rect : buildRectangle ( {
465
+ x : previewFound . innerX ,
466
+ y : previewFound . innerY ,
467
+ width : previewFound . innerWidth ,
468
+ height : previewFound . innerHeight ,
469
+ } ) ,
470
+ tile : previewFound . tile ,
471
+ } ;
457
472
}
458
473
}
459
474
460
- if ( ! previewFound ) return undefined ;
461
-
462
- return {
463
- rect : buildRectangle ( {
464
- x : previewFound . innerX ,
465
- y : previewFound . innerY ,
466
- width : previewFound . innerWidth ,
467
- height : previewFound . innerHeight ,
468
- } ) ,
469
- tile : previewFound . tile ,
470
- } ;
475
+ return undefined ;
471
476
}
472
477
473
478
public getRightmostTile ( ) : { rect : Mtk . Rectangle ; tile : Tile } {
0 commit comments