@@ -321,12 +321,7 @@ impl CosmicStack {
321
321
swap : Option < NodeDesc > ,
322
322
) -> bool {
323
323
let ( result, update) = self . 0 . with_program ( |p| {
324
- let last_mod_serial = seat. last_modifier_change ( ) ;
325
- let mut prev_idx = p. previous_index . lock ( ) . unwrap ( ) ;
326
- if !prev_idx. is_some_and ( |( serial, _) | Some ( serial) == last_mod_serial) {
327
- * prev_idx = last_mod_serial. map ( |s| ( s, p. active . load ( Ordering :: SeqCst ) ) ) ;
328
- }
329
-
324
+ let prev_idx = p. set_previous_index ( Some ( seat) ) ;
330
325
match direction {
331
326
FocusDirection :: Left => {
332
327
if !p. group_focused . load ( Ordering :: SeqCst ) {
@@ -340,9 +335,8 @@ impl CosmicStack {
340
335
p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
341
336
( true , true )
342
337
} else {
343
- let new = prev_idx. unwrap ( ) . 1 ;
344
- let old = p. active . swap ( new, Ordering :: SeqCst ) ;
345
- if old != new {
338
+ let old = p. active . swap ( 0 , Ordering :: SeqCst ) ;
339
+ if old != 0 {
346
340
p. previous_keyboard . store ( old, Ordering :: SeqCst ) ;
347
341
p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
348
342
( false , true )
@@ -371,9 +365,8 @@ impl CosmicStack {
371
365
p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
372
366
( true , true )
373
367
} else {
374
- let new = prev_idx. unwrap ( ) . 1 ;
375
- let old = p. active . swap ( new, Ordering :: SeqCst ) ;
376
- if old != new {
368
+ let old = p. active . swap ( max - 1 , Ordering :: SeqCst ) ;
369
+ if old != max - 1 {
377
370
p. previous_keyboard . store ( old, Ordering :: SeqCst ) ;
378
371
p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
379
372
( false , true )
@@ -415,12 +408,9 @@ impl CosmicStack {
415
408
}
416
409
FocusDirection :: Up | FocusDirection :: Down => {
417
410
if !p. group_focused . load ( Ordering :: SeqCst ) {
418
- let new = prev_idx. unwrap ( ) . 1 ;
419
- let old = p. active . swap ( new, Ordering :: SeqCst ) ;
420
- if old != new {
421
- p. previous_keyboard . store ( old, Ordering :: SeqCst ) ;
422
- p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
423
- }
411
+ p. previous_keyboard
412
+ . store ( prev_idx. unwrap ( ) , Ordering :: SeqCst ) ;
413
+ p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
424
414
( false , true )
425
415
} else {
426
416
( false , false )
@@ -441,19 +431,17 @@ impl CosmicStack {
441
431
pub fn handle_move ( & self , direction : Direction ) -> MoveResult {
442
432
let loop_handle = self . 0 . loop_handle ( ) ;
443
433
let result = self . 0 . with_program ( |p| {
444
- let prev_idx = p. previous_index . lock ( ) . unwrap ( ) ;
445
-
446
434
if p. group_focused . load ( Ordering :: SeqCst ) {
447
435
return MoveResult :: Default ;
448
436
}
449
437
450
438
let active = p. active . load ( Ordering :: SeqCst ) ;
451
439
let mut windows = p. windows . lock ( ) . unwrap ( ) ;
452
440
453
- let next = match direction {
454
- Direction :: Left => active. checked_sub ( 1 ) ,
455
- Direction :: Right => ( active + 1 < windows. len ( ) ) . then_some ( active + 1 ) ,
456
- Direction :: Down | Direction :: Up => None ,
441
+ let ( next, moved_horizontally ) = match direction {
442
+ Direction :: Left => ( active. checked_sub ( 1 ) , true ) ,
443
+ Direction :: Right => ( ( active + 1 < windows. len ( ) ) . then_some ( active + 1 ) , true ) ,
444
+ Direction :: Down | Direction :: Up => ( None , false ) ,
457
445
} ;
458
446
459
447
if let Some ( val) = next {
@@ -466,20 +454,20 @@ impl CosmicStack {
466
454
if windows. len ( ) == 1 {
467
455
return MoveResult :: Default ;
468
456
}
457
+
469
458
let window = windows. remove ( active) ;
470
- if let Some ( prev_idx) = prev_idx
471
- . map ( |( _, idx) | idx)
472
- . filter ( |idx| * idx < windows. len ( ) )
473
- {
474
- p. active . store ( prev_idx, Ordering :: SeqCst ) ;
475
- p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
476
- } else if active == windows. len ( ) {
477
- p. active . store ( active - 1 , Ordering :: SeqCst ) ;
478
- p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
479
- }
480
459
window. try_force_undecorated ( false ) ;
481
460
window. set_tiled ( false ) ;
482
461
462
+ let active = moved_horizontally
463
+ . then ( || if active == 0 { 0 } else { windows. len ( ) - 1 } )
464
+ . unwrap_or_else ( || active. checked_sub ( 1 ) . unwrap_or ( 0 ) ) ;
465
+
466
+ p. active . store ( active, Ordering :: SeqCst ) ;
467
+ p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
468
+ let mut prev_idx = p. previous_index . lock ( ) . unwrap ( ) ;
469
+ * prev_idx = prev_idx. map ( |( s, _) | ( s, active) ) ;
470
+
483
471
MoveResult :: MoveOut ( window, loop_handle)
484
472
}
485
473
} ) ;
0 commit comments