From e2cc27889aba9e1ca144dbef17c68321647237ad Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Fri, 24 Feb 2023 16:20:41 -0600 Subject: [PATCH 1/3] POC: We can take it out of the Popup and have it still work (cherry picked from commit 5defde545f6419f333cbdcf624d39616ff5c5ce8) --- .../TerminalApp/SuggestionsControl.cpp | 31 +++++++ src/cascadia/TerminalApp/SuggestionsControl.h | 3 + .../TerminalApp/SuggestionsControl.idl | 1 + src/cascadia/TerminalApp/TerminalPage.cpp | 83 ++++++++++--------- src/cascadia/TerminalApp/TerminalPage.xaml | 14 ++-- 5 files changed, 88 insertions(+), 44 deletions(-) diff --git a/src/cascadia/TerminalApp/SuggestionsControl.cpp b/src/cascadia/TerminalApp/SuggestionsControl.cpp index 8a364ed006c..c3c1841394b 100644 --- a/src/cascadia/TerminalApp/SuggestionsControl.cpp +++ b/src/cascadia/TerminalApp/SuggestionsControl.cpp @@ -1050,4 +1050,35 @@ namespace winrt::TerminalApp::implementation Controls::Grid::SetRow(_searchBox(), 4); } } + + void SuggestionsControl::Anchor(Windows::Foundation::Point anchor, Windows::Foundation::Size space) + { + _anchor = anchor; + _space = space; + + // TODO! do some clamping + + // // Create a thickness for the new margins + auto newMargin = Windows::UI::Xaml::ThicknessHelper::FromLengths(_anchor.X, 0, 0, 0); + + // // SuggestionsPopup().HorizontalOffset(clampedX); + + // // Now, position vertically. + if (_direction == TerminalApp::SuggestionsDirection::TopDown) + { + // // The control should open right below the cursor, with the list + // // extending below. This is easy, we can just use the cursor as the + // // origin (more or less) + // // SuggestionsPopup().VerticalOffset(realCursorPos.y + characterSize.Height); + newMargin.Top = (_anchor.Y + 16); // TODO! 16 is cursor height + } + else + { + // // Position at the cursor. The suggestions UI itself will maintian + // // its own offset such that it's always above its origin + // // SuggestionsPopup().VerticalOffset(realCursorPos.y); + newMargin.Top = (_anchor.Y - ActualHeight()); + } + Margin(newMargin); + } } diff --git a/src/cascadia/TerminalApp/SuggestionsControl.h b/src/cascadia/TerminalApp/SuggestionsControl.h index 83cefbd74c8..3f09754286c 100644 --- a/src/cascadia/TerminalApp/SuggestionsControl.h +++ b/src/cascadia/TerminalApp/SuggestionsControl.h @@ -38,6 +38,7 @@ namespace winrt::TerminalApp::implementation TerminalApp::SuggestionsMode Mode() const; void Mode(TerminalApp::SuggestionsMode mode); + void Anchor(Windows::Foundation::Point anchor, Windows::Foundation::Size space); WINRT_CALLBACK(PropertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler); WINRT_OBSERVABLE_PROPERTY(winrt::hstring, NoMatchesText, _PropertyChangedHandlers); @@ -65,6 +66,8 @@ namespace winrt::TerminalApp::implementation TerminalApp::SuggestionsDirection _direction{ TerminalApp::SuggestionsDirection::TopDown }; bool _lastFilterTextWasEmpty{ true }; + Windows::Foundation::Point _anchor; + Windows::Foundation::Size _space; void _filterTextChanged(const Windows::Foundation::IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& args); diff --git a/src/cascadia/TerminalApp/SuggestionsControl.idl b/src/cascadia/TerminalApp/SuggestionsControl.idl index d48fb6b784b..ccb55b5d058 100644 --- a/src/cascadia/TerminalApp/SuggestionsControl.idl +++ b/src/cascadia/TerminalApp/SuggestionsControl.idl @@ -42,6 +42,7 @@ namespace TerminalApp void SelectNextItem(Boolean moveDown); void Direction(SuggestionsDirection direction); + void Anchor(Windows.Foundation.Point anchor, Windows.Foundation.Size space); event Windows.Foundation.TypedEventHandler DispatchCommandRequested; event Windows.Foundation.TypedEventHandler PreviewAction; diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index 6daf5996c64..cd75d89dfdf 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -293,7 +293,7 @@ namespace winrt::TerminalApp::implementation sxnUi.RegisterPropertyChangedCallback(UIElement::VisibilityProperty(), [this](auto&&, auto&&) { if (SuggestionsUI().Visibility() == Visibility::Collapsed) { - SuggestionsPopup().IsOpen(false); + // SuggestionsPopup().IsOpen(false); _FocusActiveControl(nullptr, nullptr); } }); @@ -4510,7 +4510,7 @@ namespace winrt::TerminalApp::implementation } if (commandsCollection.Size() == 0) { - SuggestionsPopup().IsOpen(false); + // SuggestionsPopup().IsOpen(false); SuggestionsUI().Visibility(Visibility::Collapsed); co_return; @@ -4556,47 +4556,54 @@ namespace winrt::TerminalApp::implementation TerminalApp::SuggestionsDirection::BottomUp; sxnUi.Direction(direction); + sxnUi.Anchor(realCursorPos.to_winrt_point(), windowDimensions.to_winrt_size()); sxnUi.Mode(mode); - SuggestionsPopup().IsOpen(true); + // SuggestionsPopup().IsOpen(true); sxnUi.SetCommands(commandsCollection); sxnUi.Visibility(commandsCollection.Size() > 0 ? Visibility::Visible : Visibility::Collapsed); - // Position the suggestions UI relative to the actual term control. - // - // This needs to be done _after_ it is set to be visible. If not, then - // the control won't have an Actual{Width, Height} yet. - const til::size actualSuggestionsSize{ til::math::rounding, - sxnUi.ActualWidth(), - sxnUi.ActualHeight() }; - - // First, position horizonally. - // - // We want to align the left edge of the text within the control to the - // cursor position. We'll need to scoot a little to the left, to align - // text with cursor - const auto proposedX = realCursorPos.x - 40; - // If the control is too wide to fit in the window, clamp it fit inside - // the window. - const auto maxX = gsl::narrow_cast(windowDimensions.width - actualSuggestionsSize.width); - const auto clampedX = std::clamp(proposedX, 0, maxX); - - SuggestionsPopup().HorizontalOffset(clampedX); - - // Now, position vertically. - if (direction == TerminalApp::SuggestionsDirection::TopDown) - { - // The control should open right below the cursor, with the list - // extending below. This is easy, we can just use the cursor as the - // origin (more or less) - SuggestionsPopup().VerticalOffset(realCursorPos.y + characterSize.Height); - } - else - { - // Position above the cursor. We'll need to make sure - // (origin.y+sxnUi.Height) = cursorPos.y. - SuggestionsPopup().VerticalOffset(realCursorPos.y - actualSuggestionsSize.height); - } + // // Position the suggestions UI relative to the actual term control. + // // + // // This needs to be done _after_ it is set to be visible. If not, then + // // the control won't have an Actual{Width, Height} yet. + // const til::size actualSuggestionsSize{ til::math::rounding, + // sxnUi.ActualWidth(), + // sxnUi.ActualHeight() }; + + // // First, position horizonally. + // // + // // We want to align the left edge of the text within the control to the + // // cursor position. We'll need to scoot a little to the left, to align + // // text with cursor + // const auto proposedX = realCursorPos.x - 40; + // // If the control is too wide to fit in the window, clamp it fit inside + // // the window. + // const auto maxX = gsl::narrow_cast(windowDimensions.width - actualSuggestionsSize.width); + // const auto clampedX = std::clamp(proposedX, 0, maxX); + + // // Create a thickness for the new margins + // auto newMargin = Windows::UI::Xaml::ThicknessHelper::FromLengths(clampedX, 0, 0, 0); + + // // SuggestionsPopup().HorizontalOffset(clampedX); + + // // Now, position vertically. + // if (direction == TerminalApp::SuggestionsDirection::TopDown) + // { + // // The control should open right below the cursor, with the list + // // extending below. This is easy, we can just use the cursor as the + // // origin (more or less) + // // SuggestionsPopup().VerticalOffset(realCursorPos.y + characterSize.Height); + // newMargin.Top = (realCursorPos.y + characterSize.Height); + // } + // else + // { + // // Position at the cursor. The suggestions UI itself will maintian + // // its own offset such that it's always above its origin + // // SuggestionsPopup().VerticalOffset(realCursorPos.y); + // newMargin.Top = (realCursorPos.y); + // } + // sxnUi.Margin(newMargin); } } diff --git a/src/cascadia/TerminalApp/TerminalPage.xaml b/src/cascadia/TerminalApp/TerminalPage.xaml index 93f64ace9ed..f160ab965e9 100644 --- a/src/cascadia/TerminalApp/TerminalPage.xaml +++ b/src/cascadia/TerminalApp/TerminalPage.xaml @@ -195,17 +195,19 @@ PreviewKeyDown="_KeyDownHandler" Visibility="Collapsed" /> - +