@@ -471,6 +471,7 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageContext<'_>> for Portfolio
471471
472472 let Some ( source_state) = self . workspace_panel_layout . panel_group ( source_group) else { return } ;
473473 let tabs: Vec < PanelType > = source_state. tabs . clone ( ) ;
474+ let source_active_tab_index = source_state. active_tab_index ;
474475 if tabs. is_empty ( ) {
475476 return ;
476477 }
@@ -489,13 +490,13 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageContext<'_>> for Portfolio
489490 source. active_tab_index = 0 ;
490491 }
491492
492- // Insert all tabs into the target group
493+ // Insert all tabs into the target group, preserving which tab was active in the source
493494 if let Some ( target) = self . workspace_panel_layout . panel_group_mut ( target_group) {
494495 let index = insert_index. min ( target. tabs . len ( ) ) ;
495496 for ( i, panel_type) in tabs. iter ( ) . enumerate ( ) {
496497 target. tabs . insert ( index + i, * panel_type) ;
497498 }
498- target. active_tab_index = index;
499+ target. active_tab_index = index + source_active_tab_index . min ( tabs . len ( ) . saturating_sub ( 1 ) ) ;
499500 }
500501
501502 self . workspace_panel_layout . prune ( ) ;
@@ -1270,7 +1271,12 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageContext<'_>> for Portfolio
12701271 }
12711272 }
12721273 }
1273- PortfolioMessage :: SplitPanelGroup { target_group, direction, tabs } => {
1274+ PortfolioMessage :: SplitPanelGroup {
1275+ target_group,
1276+ direction,
1277+ tabs,
1278+ active_tab_index,
1279+ } => {
12741280 // Destroy layouts for the dragged tabs and the target group's active panel (it may get remounted by the frontend)
12751281 for & panel_type in & tabs {
12761282 Self :: destroy_panel_layouts ( panel_type, responses) ;
@@ -1285,7 +1291,7 @@ impl MessageHandler<PortfolioMessage, PortfolioMessageContext<'_>> for Portfolio
12851291 }
12861292
12871293 // Create the new panel group adjacent to the target, then prune empty groups
1288- let new_id = self . workspace_panel_layout . split_panel_group ( target_group, direction, tabs. clone ( ) ) ;
1294+ let new_id = self . workspace_panel_layout . split_panel_group ( target_group, direction, tabs. clone ( ) , active_tab_index ) ;
12891295 self . workspace_panel_layout . prune ( ) ;
12901296
12911297 responses. add ( MenuBarMessage :: SendLayout ) ;
0 commit comments