@@ -2051,14 +2051,14 @@ export const Spaces = class Spaces extends Map {
2051
2051
} ) ;
2052
2052
2053
2053
this . signals . connect ( display , 'window-created' ,
2054
- ( display , metaWindow , user_data ) => this . window_created ( metaWindow ) ) ;
2054
+ ( display , metaWindow , _user_data ) => this . window_created ( metaWindow ) ) ;
2055
2055
2056
2056
this . signals . connect ( display , 'grab-op-begin' , ( display , mw , type ) => grabBegin ( mw , type ) ) ;
2057
2057
this . signals . connect ( display , 'grab-op-end' , ( display , mw , type ) => grabEnd ( mw , type ) ) ;
2058
2058
2059
2059
2060
2060
this . signals . connect ( global . window_manager , 'switch-workspace' ,
2061
- ( wm , from , to , direction ) => this . switchWorkspace ( wm , from , to ) ) ;
2061
+ ( wm , from , to , _direction ) => this . switchWorkspace ( wm , from , to ) ) ;
2062
2062
2063
2063
this . signals . connect ( this . overrideSettings , 'changed::workspaces-only-on-primary' , ( ) => {
2064
2064
displayConfig . upgradeGnomeMonitors ( ( ) => this . monitorsChanged ( ) ) ;
@@ -2286,7 +2286,7 @@ export const Spaces = class Spaces extends Map {
2286
2286
this . signals = null ;
2287
2287
2288
2288
// remove spaces
2289
- for ( let [ workspace , space ] of this ) {
2289
+ for ( let [ , space ] of this ) {
2290
2290
this . removeSpace ( space ) ;
2291
2291
}
2292
2292
@@ -2312,7 +2312,7 @@ export const Spaces = class Spaces extends Map {
2312
2312
}
2313
2313
2314
2314
let nextUnusedWorkspaceIndex = nWorkspaces ;
2315
- for ( let [ workspace , space ] of this ) {
2315
+ for ( let [ , space ] of this ) {
2316
2316
if ( workspaces [ space . workspace ] !== true ) {
2317
2317
this . removeSpace ( space ) ;
2318
2318
@@ -2331,6 +2331,20 @@ export const Spaces = class Spaces extends Map {
2331
2331
}
2332
2332
}
2333
2333
2334
+ /**
2335
+ * Return true if there are less-than-or-equal-to spaces than monitors.
2336
+ */
2337
+ lteSpacesThanMonitors ( onFalseCallback ) {
2338
+ const cb = onFalseCallback ?? function ( _nSpaces , _nMonitors ) { } ;
2339
+ const nSpaces = [ ...this ] . length ;
2340
+ const nMonitors = Main . layoutManager . monitors . length ;
2341
+
2342
+ if ( nSpaces <= nMonitors ) {
2343
+ cb ( nSpaces , nMonitors ) ;
2344
+ }
2345
+ return nSpaces <= nMonitors ;
2346
+ }
2347
+
2334
2348
switchMonitor ( direction , move , warp = true ) {
2335
2349
let focus = display . focus_window ;
2336
2350
let monitor = focusMonitor ( ) ;
@@ -2372,12 +2386,91 @@ export const Spaces = class Spaces extends Map {
2372
2386
}
2373
2387
}
2374
2388
2375
- swapMonitor ( direction , backDirection ) {
2389
+ moveToMonitor ( direction , backDirection ) {
2390
+ const monitor = focusMonitor ( ) ;
2391
+ const i = display . get_monitor_neighbor_index ( monitor . index , direction ) ;
2392
+ if ( i === - 1 )
2393
+ return ;
2394
+
2395
+ if ( this . lteSpacesThanMonitors (
2396
+ ( s , m ) => Main . notify (
2397
+ `PaperWM (cannot move workspace)` ,
2398
+ `You need at least ${ m + 1 } workspaces to complete this operation.`
2399
+ )
2400
+ ) ) {
2401
+ return ;
2402
+ }
2403
+
2404
+ // check how many spaces are on this monitor
2405
+ const numSpaces = [ ...this ] . filter ( ( [ _monitor , space ] ) => space ?. monitor === monitor ) . length ;
2406
+ // if last space on this monitor, then swap
2407
+ if ( numSpaces <= 1 ) {
2408
+ this . swapMonitor ( direction , backDirection ) ;
2409
+ return ;
2410
+ }
2411
+
2412
+ let navFinish = ( ) => Navigator . getNavigator ( ) . finish ( ) ;
2413
+ // action on current monitor
2414
+ this . selectStackSpace ( Meta . MotionDirection . DOWN ) ;
2415
+ navFinish ( ) ;
2416
+ // switch to target monitor and action mru
2417
+ this . switchMonitor ( direction , false , true ) ;
2418
+ this . selectStackSpace ( Meta . MotionDirection . DOWN ) ;
2419
+ navFinish ( ) ;
2420
+
2421
+ // /**
2422
+ // * Fullscreen monitor workaround.
2423
+ // * see https://github.com/paperwm/PaperWM/issues/638
2424
+ // */
2425
+ // this.forEach(space => {
2426
+ // space.getWindows().filter(w => w.fullscreen).forEach(w => {
2427
+ // animateWindow(w);
2428
+ // w.unmake_fullscreen();
2429
+ // w.make_fullscreen();
2430
+ // showWindow(w);
2431
+ // });
2432
+ // });
2433
+
2434
+ // ensure after swapping that the space elements are shown correctly
2435
+ this . setSpaceTopbarElementsVisible ( true , { force : true } ) ;
2436
+ }
2437
+
2438
+ swapMonitor ( direction , backDirection , options = { } ) {
2439
+ const checkIfLast = options . checkIfLast ?? true ;
2440
+ const warpIfLast = options . warpIfLast ?? true ;
2441
+
2376
2442
const monitor = focusMonitor ( ) ;
2377
2443
const i = display . get_monitor_neighbor_index ( monitor . index , direction ) ;
2378
2444
if ( i === - 1 )
2379
2445
return ;
2380
2446
2447
+ if ( this . lteSpacesThanMonitors (
2448
+ ( s , m ) => Main . notify (
2449
+ `PaperWM (cannot swap workspaces)` ,
2450
+ `You need at least ${ m + 1 } workspaces to complete this operation.`
2451
+ )
2452
+ ) ) {
2453
+ return ;
2454
+ }
2455
+
2456
+ if ( checkIfLast ) {
2457
+ // check how many spaces are on this monitor
2458
+ const numSpaces = [ ...this ] . filter ( ( [ _monitor , space ] ) => space ?. monitor === monitor ) . length ;
2459
+ // if last space on this monitor, then swap
2460
+ if ( numSpaces <= 1 ) {
2461
+ // focus other monitor for a switchback
2462
+ this . switchMonitor ( direction , false , false ) ;
2463
+ this . swapMonitor ( backDirection , direction , {
2464
+ checkIfLast : false ,
2465
+ warpIfLast : false ,
2466
+ } ) ;
2467
+
2468
+ // now switch monitor with warp since we back-flipped
2469
+ this . switchMonitor ( direction , false , true ) ;
2470
+ return ;
2471
+ }
2472
+ }
2473
+
2381
2474
let navFinish = ( ) => Navigator . getNavigator ( ) . finish ( ) ;
2382
2475
// action on current monitor
2383
2476
this . selectStackSpace ( Meta . MotionDirection . DOWN ) ;
@@ -2391,20 +2484,20 @@ export const Spaces = class Spaces extends Map {
2391
2484
this . selectStackSpace ( Meta . MotionDirection . DOWN ) ;
2392
2485
navFinish ( ) ;
2393
2486
// final switch with warp
2394
- this . switchMonitor ( direction ) ;
2395
-
2396
- /**
2397
- * Fullscreen monitor workaround.
2398
- * see https://github.com/paperwm/PaperWM/issues/638
2399
- */
2400
- this . forEach ( space => {
2401
- space . getWindows ( ) . filter ( w => w . fullscreen ) . forEach ( w => {
2402
- animateWindow ( w ) ;
2403
- w . unmake_fullscreen ( ) ;
2404
- w . make_fullscreen ( ) ;
2405
- showWindow ( w ) ;
2406
- } ) ;
2407
- } ) ;
2487
+ this . switchMonitor ( direction , false , warpIfLast ) ;
2488
+
2489
+ // / **
2490
+ // * Fullscreen monitor workaround.
2491
+ // * see https://github.com/paperwm/PaperWM/issues/638
2492
+ // */
2493
+ // this.forEach(space => {
2494
+ // space.getWindows().filter(w => w.fullscreen).forEach(w => {
2495
+ // animateWindow(w);
2496
+ // w.unmake_fullscreen();
2497
+ // w.make_fullscreen();
2498
+ // showWindow(w);
2499
+ // });
2500
+ // });
2408
2501
2409
2502
// ensure after swapping that the space elements are shown correctly
2410
2503
this . setSpaceTopbarElementsVisible ( true , { force : true } ) ;
@@ -2484,11 +2577,18 @@ export const Spaces = class Spaces extends Map {
2484
2577
let out = [ ] ;
2485
2578
for ( let i = 0 ; i < nWorkspaces ; i ++ ) {
2486
2579
let space = this . spaceOf ( workspaceManager . get_workspace_by_index ( i ) ) ;
2487
- if ( space . monitor === monitor ||
2488
- ( space . length === 0 && this . monitors . get ( space . monitor ) !== space ) ) {
2489
- // include workspace if it is the current one
2490
- // or if it is empty and not active on another monitor
2580
+
2581
+ if ( space . monitor === monitor ) {
2491
2582
out . push ( space ) ;
2583
+ continue ;
2584
+ }
2585
+
2586
+ // include workspace if it is the current one
2587
+ // or if it is empty and not active on another monitor
2588
+ if ( space . length === 0 &&
2589
+ this . monitors . get ( space . monitor ) !== space ) {
2590
+ out . push ( space ) ;
2591
+ continue ;
2492
2592
}
2493
2593
}
2494
2594
return out ;
@@ -2852,7 +2952,7 @@ export const Spaces = class Spaces extends Map {
2852
2952
}
2853
2953
2854
2954
let visible = new Map ( ) ;
2855
- for ( let [ monitor , space ] of this . monitors ) {
2955
+ for ( let [ , space ] of this . monitors ) {
2856
2956
visible . set ( space , true ) ;
2857
2957
}
2858
2958
0 commit comments