Repository: microsoft/terminal (language: cpp)

Issue:
Context Menu Item for Find
<!-- 
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨

I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING:
1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement.
2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement.
3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number).
4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement.
5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement.

All good? Then proceed!
-->

# Description of the new feature/enhancement
A menu flyout item for the find feature.



Discussion / hints:
Thanks for the offer of help! I think we're trying pretty hard ad the moment to _not_ add additional UI elements to the New Tab flyout we already have. It's really the only UI we have at the moment, but it's a slippery slope towards having just every button in that single UI, and all of a sudden it's a cluttered mess like this:
![image](https://user-images.githubusercontent.com/18356694/80601259-3061de80-89f3-11ea-9d38-b11ebbb428e7.png)

Hopefully once #3789 lands (in 1.1), then we'll be able to add menu entries to the right-click menu on a tab, and I think that's probably a better place to put something like this.

Then there's also #3337 for adding a context menu to the terminal controls themselves, which would also be an appropriate place for this

Then of course, there's always #5400, which is another possible place.


I'm going to leave this open as a potential follow-up task of either #3337 or #3789
Alright, we'll talk about it when context menu is introduced.
> Thanks for the offer of help! I think we're trying pretty hard ad the moment to _not_ add additional UI elements to the New Tab flyout we already have. It's really the only UI we have at the moment, but it's a slippery slope towards having just every button in that single UI, and all of a sudden it's a cluttered mess like this:
> ![image](https://user-images.githubusercontent.com/18356694/80601259-3061de80-89f3-11ea-9d38-b11ebbb428e7.png)

Is that Microsoft saying that their own products are a mess? Lol
It would not be wise to take the opinion of one person at a company with multiple tens of thousands of employees as representative of the entire company.
I understand. It was an innocent joke. 
In regards to the above discussion, has it been determined whether or not this is something that is wanted?
Honestly, I don't remember. I thought I closed this because _eventually_ we were going to make it possible for users to add this on their own. I don't remember why I tagged this easy starter...



...


Okay, now I'm remembering. We thought it was a fine idea for someone to add to the tab context menu
![image](https://user-images.githubusercontent.com/18356694/141859582-0c10390e-32f0-4b3d-af2b-d3e7705a7932.png)

And that should be relatively easy to do. 

Fix patch:
diff --git a/src/cascadia/TerminalApp/Resources/en-US/Resources.resw b/src/cascadia/TerminalApp/Resources/en-US/Resources.resw
index b2133af1816..a0a653927e3 100644
--- a/src/cascadia/TerminalApp/Resources/en-US/Resources.resw
+++ b/src/cascadia/TerminalApp/Resources/en-US/Resources.resw
@@ -709,6 +709,9 @@
   <data name="ExportSuccess" xml:space="preserve">
     <value>Successfully exported terminal content</value>
   </data>
+  <data name="FindText" xml:space="preserve">
+    <value>Find</value>
+  </data>
   <data name="PlainText" xml:space="preserve">
     <value>Plain Text</value>
   </data>
diff --git a/src/cascadia/TerminalApp/TabManagement.cpp b/src/cascadia/TerminalApp/TabManagement.cpp
index d744cb09fd2..47cfa70d7b2 100644
--- a/src/cascadia/TerminalApp/TabManagement.cpp
+++ b/src/cascadia/TerminalApp/TabManagement.cpp
@@ -209,6 +209,16 @@ namespace winrt::TerminalApp::implementation
             }
         });
 
+        newTabImpl->FindRequested([weakTab, weakThis{ get_weak() }]() {
+            auto page{ weakThis.get() };
+            auto tab{ weakTab.get() };
+
+            if (page && tab)
+            {
+                page->_Find();
+            }
+        });
+
         auto tabViewItem = newTabImpl->TabViewItem();
         _tabView.TabItems().Append(tabViewItem);
 
diff --git a/src/cascadia/TerminalApp/TerminalTab.cpp b/src/cascadia/TerminalApp/TerminalTab.cpp
index c5c6cd2f5b2..64cbd16dc4f 100644
--- a/src/cascadia/TerminalApp/TerminalTab.cpp
+++ b/src/cascadia/TerminalApp/TerminalTab.cpp
@@ -1273,6 +1273,23 @@ namespace winrt::TerminalApp::implementation
             exportTabMenuItem.Icon(exportTabSymbol);
         }
 
+        Controls::MenuFlyoutItem findMenuItem;
+        {
+            // "Split Tab"
+            Controls::FontIcon findSymbol;
+            findSymbol.FontFamily(Media::FontFamily{ L"Segoe Fluent Icons, Segoe MDL2 Assets" });
+            findSymbol.Glyph(L"\xF78B"); // SearchMedium
+
+            findMenuItem.Click([weakThis](auto&&, auto&&) {
+                if (auto tab{ weakThis.get() })
+                {
+                    tab->_FindRequestedHandlers();
+                }
+            });
+            findMenuItem.Text(RS_(L"FindText"));
+            findMenuItem.Icon(findSymbol);
+        }
+
         // Build the menu
         Controls::MenuFlyout contextMenuFlyout;
         Controls::MenuFlyoutSeparator menuSeparator;
@@ -1281,6 +1298,7 @@ namespace winrt::TerminalApp::implementation
         contextMenuFlyout.Items().Append(duplicateTabMenuItem);
         contextMenuFlyout.Items().Append(splitTabMenuItem);
         contextMenuFlyout.Items().Append(exportTabMenuItem);
+        contextMenuFlyout.Items().Append(findMenuItem);
         contextMenuFlyout.Items().Append(menuSeparator);
 
         // GH#5750 - When the context menu is dismissed with ESC, toss the focus
@@ -1291,7 +1309,7 @@ namespace winrt::TerminalApp::implementation
                 // GH#10112 - if we're opening the tab renamer, don't
                 // immediately toss focus to the control. We don't want to steal
                 // focus from the tab renamer.
-                if (!tab->_headerControl.InRename())
+                if (!tab->_headerControl.InRename() && !tab->GetActiveTerminalControl().SearchBoxEditInFocus())
                 {
                     tab->_RequestFocusActiveControlHandlers();
                 }
diff --git a/src/cascadia/TerminalApp/TerminalTab.h b/src/cascadia/TerminalApp/TerminalTab.h
index 09922f2f1ad..f5ea3b0cd41 100644
--- a/src/cascadia/TerminalApp/TerminalTab.h
+++ b/src/cascadia/TerminalApp/TerminalTab.h
@@ -102,6 +102,7 @@ namespace winrt::TerminalApp::implementation
         WINRT_CALLBACK(TabRaiseVisualBell, winrt::delegate<>);
         WINRT_CALLBACK(DuplicateRequested, winrt::delegate<>);
         WINRT_CALLBACK(SplitTabRequested, winrt::delegate<>);
+        WINRT_CALLBACK(FindRequested, winrt::delegate<>);
         WINRT_CALLBACK(ExportTabRequested, winrt::delegate<>);
         TYPED_EVENT(TaskbarProgressChanged, IInspectable, IInspectable);
 
diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp
index 3aeeb35786c..83121125365 100644
--- a/src/cascadia/TerminalControl/TermControl.cpp
+++ b/src/cascadia/TerminalControl/TermControl.cpp
@@ -197,6 +197,20 @@ namespace winrt::Microsoft::Terminal::Control::implementation
         }
     }
 
+    // Method Description:
+    // Find if search box text edit currently is in focus
+    // Return Value:
+    // - true, if search box text edit is in focus
+    bool TermControl::SearchBoxEditInFocus() const
+    {
+        if (!_searchBox)
+        {
+            return false;
+        }
+
+        return _searchBox->TextBox().FocusState() == FocusState::Keyboard;
+    }
+
     // Method Description:
     // - Search text in text buffer. This is triggered if the user click
     //   search button or press enter.
diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h
index fdb10e9b978..bc772f8b0ac 100644
--- a/src/cascadia/TerminalControl/TermControl.h
+++ b/src/cascadia/TerminalControl/TermControl.h
@@ -89,6 +89,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
 
         void SearchMatch(const bool goForward);
 
+        bool SearchBoxEditInFocus() const;
+
         bool OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down);
 
         bool OnMouseWheel(const Windows::Foundation::Point location, const int32_t delta, const bool leftButtonDown, const bool midButtonDown, const bool rightButtonDown);
diff --git a/src/cascadia/TerminalControl/TermControl.idl b/src/cascadia/TerminalControl/TermControl.idl
index b221cc85cd4..81925f405a7 100644
--- a/src/cascadia/TerminalControl/TermControl.idl
+++ b/src/cascadia/TerminalControl/TermControl.idl
@@ -61,6 +61,7 @@ namespace Microsoft.Terminal.Control
         void ScrollViewport(Int32 viewTop);
 
         void CreateSearchBoxControl();
+        Boolean SearchBoxEditInFocus();
 
         void SearchMatch(Boolean goForward);
 


Test patch: