From 57a53279ae03d801e46a07e59dc95509e8518437 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Fri, 24 Feb 2023 16:43:02 -0600 Subject: [PATCH] very confident we can get all the logic into the sxnui --- .../TerminalApp/SuggestionsControl.cpp | 21 +++++++++++++--- src/cascadia/TerminalApp/TerminalPage.cpp | 25 +++++++++++++------ 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/cascadia/TerminalApp/SuggestionsControl.cpp b/src/cascadia/TerminalApp/SuggestionsControl.cpp index 28ca36d0a20..52860e492a4 100644 --- a/src/cascadia/TerminalApp/SuggestionsControl.cpp +++ b/src/cascadia/TerminalApp/SuggestionsControl.cpp @@ -1060,15 +1060,28 @@ namespace winrt::TerminalApp::implementation } } - void SuggestionsControl::Anchor(Windows::Foundation::Point anchor, Windows::Foundation::Size space) + void SuggestionsControl::Anchor(Windows::Foundation::Point anchor, + Windows::Foundation::Size space) { _anchor = anchor; _space = space; // TODO! do some clamping + const til::size actualSize{ til::math::rounding, ActualWidth(), 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 = gsl::narrow_cast(_anchor.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(space.Width - actualSize.width); + const auto clampedX = std::clamp(proposedX, 0, maxX); + // // Create a thickness for the new margins - auto newMargin = Windows::UI::Xaml::ThicknessHelper::FromLengths(_anchor.X, 0, 0, 0); + auto newMargin = Windows::UI::Xaml::ThicknessHelper::FromLengths(clampedX, 0, 0, 0); // // SuggestionsPopup().HorizontalOffset(clampedX); @@ -1079,14 +1092,14 @@ namespace winrt::TerminalApp::implementation // // 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 + newMargin.Top = (_anchor.Y); } 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()); + newMargin.Top = (_anchor.Y - actualSize.height); } Margin(newMargin); } diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index cd75d89dfdf..9eacb0c0275 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -4539,6 +4539,7 @@ namespace winrt::TerminalApp::implementation const auto controlTransform = control.TransformToVisual(this->Root()); const til::point controlOrigin{ til::math::rounding, controlTransform.TransformPoint(Windows::Foundation::Point{ 0, 0 }) }; const til::point realCursorPos = controlOrigin + cursorPos; + // const til::point anchor = realCursorPos + til::point{ 0, characterSize.Height }; const auto currentWindow = CoreWindow::GetForCurrentThread(); const auto currentWindowBounds = currentWindow.Bounds(); @@ -4550,18 +4551,28 @@ namespace winrt::TerminalApp::implementation const til::size windowDimensions{ til::math::rounding, ActualWidth(), ActualHeight() }; // Is there space in the window below the cursor to open the menu downwards? - const bool canOpenDownwards = ((realCursorPos.y) + characterSize.Height + maxSize.Height) < windowDimensions.height; - - const auto direction = canOpenDownwards ? TerminalApp::SuggestionsDirection::TopDown : - TerminalApp::SuggestionsDirection::BottomUp; - - sxnUi.Direction(direction); - sxnUi.Anchor(realCursorPos.to_winrt_point(), windowDimensions.to_winrt_size()); + // const bool canOpenDownwards = ((realCursorPos.y) + characterSize.Height + maxSize.Height) < windowDimensions.height; + + // const auto direction = canOpenDownwards ? TerminalApp::SuggestionsDirection::TopDown : + // TerminalApp::SuggestionsDirection::BottomUp; + const til::size actualSuggestionsSizeBefore{ til::math::rounding, sxnUi.ActualWidth(), sxnUi.ActualHeight() }; + actualSuggestionsSizeBefore; + // sxnUi.Direction(direction); + // sxnUi.Anchor(realCursorPos.to_winrt_point(), windowDimensions.to_winrt_size()); sxnUi.Mode(mode); // SuggestionsPopup().IsOpen(true); sxnUi.SetCommands(commandsCollection); sxnUi.Visibility(commandsCollection.Size() > 0 ? Visibility::Visible : Visibility::Collapsed); + const til::size actualSuggestionsSizeAfter{ til::math::rounding, sxnUi.ActualWidth(), sxnUi.ActualHeight() }; + actualSuggestionsSizeAfter; + + const bool canOpenDownwards = ((realCursorPos.y) + characterSize.Height + actualSuggestionsSizeAfter.height) < windowDimensions.height; + const auto direction = canOpenDownwards ? TerminalApp::SuggestionsDirection::TopDown : + TerminalApp::SuggestionsDirection::BottomUp; + const auto anchor = realCursorPos + til::point{ til::math::rounding, 0.0f, canOpenDownwards ? characterSize.Height : 0 }; + sxnUi.Anchor(anchor.to_winrt_point(), windowDimensions.to_winrt_size()); + sxnUi.Direction(direction); // // Position the suggestions UI relative to the actual term control. // //