Repository: microsoft/terminal (language: cpp)

Issue:
Duplicated tab should be next to original tab
Currently the duplicated tab is added on the far right, IMO it should be next to the original tab.


Discussion / hints:
We have a setting for that! You can find it here:
![image](https://user-images.githubusercontent.com/2256941/199561893-4820d41d-fe35-4461-878b-45e1b3b73ecf.png)

Let me know if I misunderstood you. 🙂
When was that setting added? My 1.15 doesn't have it.
Also, duplicated tabs should be created next to the one being duplicated, while new tabs should be created after the last tab. How would that work?
This was introduced in #13421 which is part of 1.16 (currently in Windows Terminal Preview).

> Also, duplicated tabs should be created next to the one being duplicated, while new tabs should be created after the last tab. How would that work?

Ah now I got it!

Fix patch:
diff --git a/src/cascadia/TerminalApp/TabManagement.cpp b/src/cascadia/TerminalApp/TabManagement.cpp
index 6e6b6dfbe70..97eb581e2fc 100644
--- a/src/cascadia/TerminalApp/TabManagement.cpp
+++ b/src/cascadia/TerminalApp/TabManagement.cpp
@@ -95,17 +95,22 @@ namespace winrt::TerminalApp::implementation
     // - Sets up state, event handlers, etc on a tab object that was just made.
     // Arguments:
     // - newTabImpl: the uninitialized tab.
-    void TerminalPage::_InitializeTab(winrt::com_ptr<TerminalTab> newTabImpl)
+    // - insertPosition: Optional parameter to indicate the position of tab.
+    void TerminalPage::_InitializeTab(winrt::com_ptr<TerminalTab> newTabImpl, uint32_t insertPosition)
     {
         newTabImpl->Initialize();
 
-        uint32_t insertPosition = _tabs.Size();
-        if (_settings.GlobalSettings().NewTabPosition() == NewTabPosition::AfterCurrentTab)
+        // If insert position is not passed, calculate it
+        if (insertPosition == -1)
         {
-            auto currentTabIndex = _GetFocusedTabIndex();
-            if (currentTabIndex.has_value())
+            insertPosition = _tabs.Size();
+            if (_settings.GlobalSettings().NewTabPosition() == NewTabPosition::AfterCurrentTab)
             {
-                insertPosition = currentTabIndex.value() + 1;
+                auto currentTabIndex = _GetFocusedTabIndex();
+                if (currentTabIndex.has_value())
+                {
+                    insertPosition = currentTabIndex.value() + 1;
+                }
             }
         }
 
@@ -259,12 +264,13 @@ namespace winrt::TerminalApp::implementation
     // - Create a new tab using a specified pane as the root.
     // Arguments:
     // - pane: The pane to use as the root.
-    void TerminalPage::_CreateNewTabFromPane(std::shared_ptr<Pane> pane)
+    // - insertPosition: Optional parameter to indicate the position of tab.
+    void TerminalPage::_CreateNewTabFromPane(std::shared_ptr<Pane> pane, uint32_t insertPosition)
     {
         if (pane)
         {
             auto newTabImpl = winrt::make_self<TerminalTab>(pane);
-            _InitializeTab(newTabImpl);
+            _InitializeTab(newTabImpl, insertPosition);
         }
     }
 
@@ -337,7 +343,7 @@ namespace winrt::TerminalApp::implementation
             // In the future, it may be preferable to just duplicate the
             // current control's live settings (which will include changes
             // made through VT).
-            _CreateNewTabFromPane(_MakePane(nullptr, tab, nullptr));
+            _CreateNewTabFromPane(_MakePane(nullptr, tab, nullptr), tab.TabViewIndex() + 1);
 
             const auto runtimeTabText{ tab.GetTabText() };
             if (!runtimeTabText.empty())
diff --git a/src/cascadia/TerminalApp/TerminalPage.h b/src/cascadia/TerminalApp/TerminalPage.h
index 5a0ad22f183..03c7a0f2383 100644
--- a/src/cascadia/TerminalApp/TerminalPage.h
+++ b/src/cascadia/TerminalApp/TerminalPage.h
@@ -246,7 +246,7 @@ namespace winrt::TerminalApp::implementation
 
         void _OpenNewTabDropdown();
         HRESULT _OpenNewTab(const Microsoft::Terminal::Settings::Model::NewTerminalArgs& newTerminalArgs, winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection existingConnection = nullptr);
-        void _CreateNewTabFromPane(std::shared_ptr<Pane> pane);
+        void _CreateNewTabFromPane(std::shared_ptr<Pane> pane, uint32_t insertPosition = -1);
         winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection _CreateConnectionFromSettings(Microsoft::Terminal::Settings::Model::Profile profile, Microsoft::Terminal::Settings::Model::TerminalSettings settings);
 
         winrt::fire_and_forget _OpenNewWindow(const Microsoft::Terminal::Settings::Model::NewTerminalArgs newTerminalArgs);
@@ -288,7 +288,7 @@ namespace winrt::TerminalApp::implementation
         void _RemoveTab(const winrt::TerminalApp::TabBase& tab);
         winrt::fire_and_forget _RemoveTabs(const std::vector<winrt::TerminalApp::TabBase> tabs);
 
-        void _InitializeTab(winrt::com_ptr<TerminalTab> newTabImpl);
+        void _InitializeTab(winrt::com_ptr<TerminalTab> newTabImpl, uint32_t insertPosition = -1);
         void _RegisterTerminalEvents(Microsoft::Terminal::Control::TermControl term);
         void _RegisterTabEvents(TerminalTab& hostingTab);
 


Test patch: