@@ -321,12 +321,7 @@ impl CosmicStack {
321321 swap : Option < NodeDesc > ,
322322 ) -> bool {
323323 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) ) ;
330325 match direction {
331326 FocusDirection :: Left => {
332327 if !p. group_focused . load ( Ordering :: SeqCst ) {
@@ -340,9 +335,8 @@ impl CosmicStack {
340335 p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
341336 ( true , true )
342337 } 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 {
346340 p. previous_keyboard . store ( old, Ordering :: SeqCst ) ;
347341 p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
348342 ( false , true )
@@ -371,9 +365,8 @@ impl CosmicStack {
371365 p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
372366 ( true , true )
373367 } 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 {
377370 p. previous_keyboard . store ( old, Ordering :: SeqCst ) ;
378371 p. scroll_to_focus . store ( true , Ordering :: SeqCst ) ;
379372 ( false , true )
@@ -415,12 +408,9 @@ impl CosmicStack {
415408 }
416409 FocusDirection :: Up | FocusDirection :: Down => {
417410 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 ) ;
424414 ( false , true )
425415 } else {
426416 ( false , false )
@@ -441,19 +431,17 @@ impl CosmicStack {
441431 pub fn handle_move ( & self , direction : Direction ) -> MoveResult {
442432 let loop_handle = self . 0 . loop_handle ( ) ;
443433 let result = self . 0 . with_program ( |p| {
444- let prev_idx = p. previous_index . lock ( ) . unwrap ( ) ;
445-
446434 if p. group_focused . load ( Ordering :: SeqCst ) {
447435 return MoveResult :: Default ;
448436 }
449437
450438 let active = p. active . load ( Ordering :: SeqCst ) ;
451439 let mut windows = p. windows . lock ( ) . unwrap ( ) ;
452440
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 ) ,
457445 } ;
458446
459447 if let Some ( val) = next {
@@ -466,20 +454,20 @@ impl CosmicStack {
466454 if windows. len ( ) == 1 {
467455 return MoveResult :: Default ;
468456 }
457+
469458 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- }
480459 window. try_force_undecorated ( false ) ;
481460 window. set_tiled ( false ) ;
482461
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+
483471 MoveResult :: MoveOut ( window, loop_handle)
484472 }
485473 } ) ;
0 commit comments