@@ -2616,6 +2616,12 @@ export const Spaces = class Spaces extends Map {
26162616 let space = this . monitors . get ( newMonitor ) ;
26172617
26182618 if ( move && focus ) {
2619+ const customIndex = getMoveWindowPositionIndex ( space , direction ) ;
2620+ if ( customIndex !== null ) {
2621+ // namespaced prop to avoid possible future collisions
2622+ focus . paperwm_openAtIndex = customIndex ;
2623+ }
2624+
26192625 let metaWindow = focus . get_transient_for ( ) || focus ;
26202626
26212627 if ( currentSpace && currentSpace . indexOf ( metaWindow ) !== - 1 ) {
@@ -4200,7 +4206,13 @@ Opening "${metaWindow?.title}" on current space.`);
42004206 }
42014207 ok && clone . set_position ( x , y ) ;
42024208
4203- if ( ! space . addWindow ( metaWindow , getOpenWindowPositionIndex ( space ) ) )
4209+ // When moving a window from another monitor, we may request a certain index
4210+ const openAtIndex = metaWindow . paperwm_openAtIndex ?? getOpenWindowPositionIndex ( space ) ;
4211+ if ( metaWindow . paperwm_openAtIndex ) {
4212+ delete metaWindow . paperwm_openAtIndex ;
4213+ }
4214+
4215+ if ( ! space . addWindow ( metaWindow , openAtIndex ) )
42044216 return ;
42054217
42064218 metaWindow . unmake_above ( ) ;
@@ -4303,6 +4315,43 @@ Opening "${metaWindow?.title}" on current space.`);
43034315 }
43044316}
43054317
4318+ /**
4319+ * When we're moving a window from an existing monitor, we want to insert with
4320+ * minimal disruption. E.g. if we're moving window E to the left,
4321+ *
4322+ * ([a b] c d) ([E f])
4323+ * (a [b E] c d) ([f])
4324+ *
4325+ * so that visually it looks like this:
4326+ *
4327+ * [a b] [E f]
4328+ * [b E] [f g]
4329+ *
4330+ * regardless of the setting for inserting new windows.
4331+ */
4332+ function getMoveWindowPositionIndex ( space , direction ) {
4333+ const visibleColumns = space . filter ( ( [ mw ] ) => space . isVisible ( mw ) ) ;
4334+
4335+ if ( visibleColumns . length === 0 ) {
4336+ return null ;
4337+ }
4338+
4339+ switch ( direction ) {
4340+ case Meta . DisplayDirection . LEFT : {
4341+ const windowAtTarget = visibleColumns [ visibleColumns . length - 1 ] [ 0 ] ;
4342+ return space . indexOf ( windowAtTarget ) + 1 ;
4343+ break ;
4344+ }
4345+ case Meta . DisplayDirection . RIGHT : {
4346+ const windowAtTarget = visibleColumns [ 0 ] [ 0 ] ;
4347+ return space . indexOf ( windowAtTarget ) ;
4348+ }
4349+ default :
4350+ // No special handling yet for moving up/down
4351+ return null ;
4352+ }
4353+ }
4354+
43064355/**
43074356 * Gets the window index to add a new window in the space:
43084357 * { RIGHT: 0, LEFT: 1, START: 2, END: 3 };
0 commit comments