From 667b6586d2ae91e1903c7b00396e3c398629bd21 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Wed, 15 Feb 2023 15:33:36 -0600 Subject: [PATCH] I'm committing everything just in case, but I'm gonna revert and just do the part that fixed it --- src/cascadia/TerminalApp/TabManagement.cpp | 2 +- src/cascadia/TerminalApp/TerminalPage.cpp | 77 ++++++++++++------- src/cascadia/TerminalApp/TerminalPage.h | 10 ++- src/cascadia/TerminalApp/TerminalWindow.cpp | 56 ++++++++------ src/cascadia/TerminalApp/TerminalWindow.h | 2 +- src/cascadia/WindowsTerminal/AppHost.cpp | 2 +- .../WindowsTerminal/WindowEmperor.cpp | 7 +- src/cascadia/WindowsTerminal/WindowThread.cpp | 8 ++ 8 files changed, 101 insertions(+), 63 deletions(-) diff --git a/src/cascadia/TerminalApp/TabManagement.cpp b/src/cascadia/TerminalApp/TabManagement.cpp index f1134efbd36..779cb491e2c 100644 --- a/src/cascadia/TerminalApp/TabManagement.cpp +++ b/src/cascadia/TerminalApp/TabManagement.cpp @@ -226,7 +226,7 @@ namespace winrt::TerminalApp::implementation } } - tabViewItem.PointerReleased({ this, &TerminalPage::_OnTabClick }); + tabViewItem.PointerReleased({ get_weak(), &TerminalPage::_OnTabClick }); // When the tab requests close, try to close it (prompt for approval, if required) newTabImpl->CloseRequested([weakTab, weakThis{ get_weak() }](auto&& /*s*/, auto&& /*e*/) { diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index b1ab627b8f1..81438e96c23 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -39,7 +39,7 @@ using namespace ::Microsoft::Console; using namespace ::Microsoft::Terminal::Core; using namespace std::chrono_literals; -#define HOOKUP_ACTION(action) _actionDispatch->action({ this, &TerminalPage::_Handle##action }); +#define HOOKUP_ACTION(action) _actionDispatch->action({ get_weak(), &TerminalPage::_Handle##action }); namespace winrt { @@ -60,6 +60,25 @@ namespace winrt::TerminalApp::implementation InitializeComponent(); } + TerminalPage::~TerminalPage() + { + // Dummy line to get a breakpoint somewnere. We can remove this. + _WindowProperties = nullptr; + } + + void TerminalPage::BigRedButton() + { + _windowIdToast = nullptr; + _windowRenameFailedToast = nullptr; + + _actionDispatch = nullptr; + _tabView = nullptr; + _tabRow = nullptr; + _tabContent = nullptr; + _newTabButton = nullptr; + _tabColorPicker = nullptr; + _settings = nullptr; + } // Method Description: // - implements the IInitializeWithWindow interface from shobjidl_core. // - We're going to use this HWND as the owner for the ConPTY windows, via @@ -222,7 +241,7 @@ namespace winrt::TerminalApp::implementation _RegisterActionCallbacks(); // Hook up inbound connection event handler - ConptyConnection::NewConnection({ this, &TerminalPage::_OnNewConnection }); + ConptyConnection::NewConnection({ get_weak(), &TerminalPage::_OnNewConnection }); //Event Bindings (Early) _newTabButton.Click([weakThis{ get_weak() }](auto&&, auto&&) { @@ -237,9 +256,9 @@ namespace winrt::TerminalApp::implementation page->NewTerminalByDrop(e); } }); - _tabView.SelectionChanged({ this, &TerminalPage::_OnTabSelectionChanged }); - _tabView.TabCloseRequested({ this, &TerminalPage::_OnTabCloseRequested }); - _tabView.TabItemsChanged({ this, &TerminalPage::_OnTabItemsChanged }); + _tabView.SelectionChanged({ get_weak(), &TerminalPage::_OnTabSelectionChanged }); + _tabView.TabCloseRequested({ get_weak(), &TerminalPage::_OnTabCloseRequested }); + _tabView.TabItemsChanged({ get_weak(), &TerminalPage::_OnTabItemsChanged }); _CreateNewTabFlyout(); @@ -248,16 +267,16 @@ namespace winrt::TerminalApp::implementation // When the visibility of the command palette changes to "collapsed", // the palette has been closed. Toss focus back to the currently active // control. - CommandPalette().RegisterPropertyChangedCallback(UIElement::VisibilityProperty(), [this](auto&&, auto&&) { - if (CommandPalette().Visibility() == Visibility::Collapsed) - { - _FocusActiveControl(nullptr, nullptr); - } - }); - CommandPalette().DispatchCommandRequested({ this, &TerminalPage::_OnDispatchCommandRequested }); - CommandPalette().CommandLineExecutionRequested({ this, &TerminalPage::_OnCommandLineExecutionRequested }); - CommandPalette().SwitchToTabRequested({ this, &TerminalPage::_OnSwitchToTabRequested }); - CommandPalette().PreviewAction({ this, &TerminalPage::_PreviewActionHandler }); + // CommandPalette().RegisterPropertyChangedCallback(UIElement::VisibilityProperty(), [this](auto&&, auto&&) { + // if (CommandPalette().Visibility() == Visibility::Collapsed) + // { + // _FocusActiveControl(nullptr, nullptr); + // } + // }); + CommandPalette().DispatchCommandRequested({ get_weak(), &TerminalPage::_OnDispatchCommandRequested }); + CommandPalette().CommandLineExecutionRequested({ get_weak(), &TerminalPage::_OnCommandLineExecutionRequested }); + CommandPalette().SwitchToTabRequested({ get_weak(), &TerminalPage::_OnSwitchToTabRequested }); + CommandPalette().PreviewAction({ get_weak(), &TerminalPage::_PreviewActionHandler }); // Settings AllowDependentAnimations will affect whether animations are // enabled application-wide, so we don't need to check it each time we @@ -269,7 +288,7 @@ namespace winrt::TerminalApp::implementation // window will be, so they can subdivide that space. // // _OnFirstLayout will remove this handler so it doesn't get called more than once. - _layoutUpdatedRevoker = _tabContent.LayoutUpdated(winrt::auto_revoke, { this, &TerminalPage::_OnFirstLayout }); + _layoutUpdatedRevoker = _tabContent.LayoutUpdated(winrt::auto_revoke, { get_weak(), &TerminalPage::_OnFirstLayout }); _isAlwaysOnTop = _settings.GlobalSettings().AlwaysOnTop(); @@ -788,7 +807,7 @@ namespace winrt::TerminalApp::implementation ico.Symbol(WUX::Controls::Symbol::Setting); settingsItem.Icon(ico); - settingsItem.Click({ this, &TerminalPage::_SettingsButtonOnClick }); + settingsItem.Click({ get_weak(), &TerminalPage::_SettingsButtonOnClick }); newTabFlyout.Items().Append(settingsItem); auto actionMap = _settings.ActionMap(); @@ -811,7 +830,7 @@ namespace winrt::TerminalApp::implementation commandPaletteIcon.FontFamily(Media::FontFamily{ L"Segoe Fluent Icons, Segoe MDL2 Assets" }); commandPaletteFlyout.Icon(commandPaletteIcon); - commandPaletteFlyout.Click({ this, &TerminalPage::_CommandPaletteButtonOnClick }); + commandPaletteFlyout.Click({ get_weak(), &TerminalPage::_CommandPaletteButtonOnClick }); newTabFlyout.Items().Append(commandPaletteFlyout); const auto commandPaletteKeyChord{ actionMap.GetKeyBindingForAction(ShortcutAction::ToggleCommandPalette) }; @@ -833,7 +852,7 @@ namespace winrt::TerminalApp::implementation aboutIcon.Symbol(WUX::Controls::Symbol::Help); aboutFlyout.Icon(aboutIcon); - aboutFlyout.Click({ this, &TerminalPage::_AboutButtonOnClick }); + aboutFlyout.Click({ get_weak(), &TerminalPage::_AboutButtonOnClick }); newTabFlyout.Items().Append(aboutFlyout); } @@ -845,9 +864,9 @@ namespace winrt::TerminalApp::implementation // Since the previous focus location might be discarded in the background, // e.g., the command palette will be dismissed by the menu, // and then closing the fly-out will move the focus to wrong location. - newTabFlyout.Opening([this](auto&&, auto&&) { - _FocusCurrentTab(true); - }); + // newTabFlyout.Opening([this](auto&&, auto&&) { + // _FocusCurrentTab(true); + // }); _newTabButton.Flyout(newTabFlyout); } @@ -1554,16 +1573,16 @@ namespace winrt::TerminalApp::implementation // - term: The newly created TermControl to connect the events for void TerminalPage::_RegisterTerminalEvents(TermControl term) { - term.RaiseNotice({ this, &TerminalPage::_ControlNoticeRaisedHandler }); + term.RaiseNotice({ get_weak(), &TerminalPage::_ControlNoticeRaisedHandler }); // Add an event handler when the terminal's selection wants to be copied. // When the text buffer data is retrieved, we'll copy the data into the Clipboard - term.CopyToClipboard({ this, &TerminalPage::_CopyToClipboardHandler }); + term.CopyToClipboard({ get_weak(), &TerminalPage::_CopyToClipboardHandler }); // Add an event handler when the terminal wants to paste data from the Clipboard. - term.PasteFromClipboard({ this, &TerminalPage::_PasteFromClipboardHandler }); + term.PasteFromClipboard({ get_weak(), &TerminalPage::_PasteFromClipboardHandler }); - term.OpenHyperlink({ this, &TerminalPage::_OpenHyperlinkHandler }); + term.OpenHyperlink({ get_weak(), &TerminalPage::_OpenHyperlinkHandler }); term.HidePointerCursor({ get_weak(), &TerminalPage::_HidePointerCursorHandler }); term.RestorePointerCursor({ get_weak(), &TerminalPage::_RestorePointerCursorHandler }); @@ -3405,7 +3424,7 @@ namespace winrt::TerminalApp::implementation } // GH#8767 - let unhandled keys in the SUI try to run commands too. - sui.KeyDown({ this, &TerminalPage::_KeyDownHandler }); + sui.KeyDown({ get_weak(), &TerminalPage::_KeyDownHandler }); sui.OpenJson([weakThis{ get_weak() }](auto&& /*s*/, winrt::Microsoft::Terminal::Settings::Model::SettingsTarget e) { if (auto page{ weakThis.get() }) @@ -3436,7 +3455,7 @@ namespace winrt::TerminalApp::implementation // Update the state of the close button to match the current theme _updateTabCloseButton(tabViewItem); - tabViewItem.PointerPressed({ this, &TerminalPage::_OnTabClick }); + tabViewItem.PointerPressed({ get_weak(), &TerminalPage::_OnTabClick }); // When the tab requests close, try to close it (prompt for approval, if required) newTabImpl->CloseRequested([weakTab, weakThis{ get_weak() }](auto&& /*s*/, auto&& /*e*/) { @@ -3850,7 +3869,7 @@ namespace winrt::TerminalApp::implementation else if (key == Windows::System::VirtualKey::Escape) { // User wants to discard the changes they made - WindowRenamerTextBox().Text(WindowProperties().WindowName()); + // WindowRenamerTextBox().Text(WindowProperties().WindowName()); WindowRenamer().IsOpen(false); _renamerPressedEnter = false; } diff --git a/src/cascadia/TerminalApp/TerminalPage.h b/src/cascadia/TerminalApp/TerminalPage.h index 7f2d0e13b4f..5028647af8a 100644 --- a/src/cascadia/TerminalApp/TerminalPage.h +++ b/src/cascadia/TerminalApp/TerminalPage.h @@ -54,6 +54,8 @@ namespace winrt::TerminalApp::implementation { public: TerminalPage(); + void BigRedButton(); + ~TerminalPage(); // This implements shobjidl's IInitializeWithWindow, but due to a XAML Compiler bug we cannot // put it in our inheritance graph. https://github.com/microsoft/microsoft-ui-xaml/issues/3331 @@ -116,10 +118,10 @@ namespace winrt::TerminalApp::implementation const winrt::hstring cwd = L""); // For the sake of XAML binding: - winrt::hstring WindowName() const noexcept { return _WindowProperties.WindowName(); }; - uint64_t WindowId() const noexcept { return _WindowProperties.WindowId(); }; - winrt::hstring WindowIdForDisplay() const noexcept { return _WindowProperties.WindowIdForDisplay(); }; - winrt::hstring WindowNameForDisplay() const noexcept { return _WindowProperties.WindowNameForDisplay(); }; + winrt::hstring WindowName() const noexcept { return L""; /*return _WindowProperties.WindowName();*/ }; + uint64_t WindowId() const noexcept { return 1u; /*return _WindowProperties.WindowId();*/ }; + winrt::hstring WindowIdForDisplay() const noexcept { return L""; /*return _WindowProperties.WindowIdForDisplay();*/ }; + winrt::hstring WindowNameForDisplay() const noexcept { return L""; /*return _WindowProperties.WindowNameForDisplay();*/ }; void SetNumberOfOpenWindows(const uint64_t value); diff --git a/src/cascadia/TerminalApp/TerminalWindow.cpp b/src/cascadia/TerminalApp/TerminalWindow.cpp index a6fcd3f8e80..c2fd6c325eb 100644 --- a/src/cascadia/TerminalApp/TerminalWindow.cpp +++ b/src/cascadia/TerminalApp/TerminalWindow.cpp @@ -132,12 +132,18 @@ namespace winrt::TerminalApp::implementation _isElevated = ::Microsoft::Console::Utils::IsElevated(); } + TerminalWindow::~TerminalWindow() + { + _root->BigRedButton(); + _root = nullptr; + } + // Method Description: // - Implements the IInitializeWithWindow interface from shobjidl_core. HRESULT TerminalWindow::Initialize(HWND hwnd) { _root = winrt::make_self(); - _root->WindowProperties(*this); + // _root->WindowProperties(*this); _dialog = ContentDialog{}; // Pass commandline args into the TerminalPage. If we were supposed to @@ -237,30 +243,30 @@ namespace winrt::TerminalApp::implementation } _root->SetSettings(_settings, false); - _root->Loaded({ this, &TerminalWindow::_OnLoaded }); - _root->Initialized([this](auto&&, auto&&) { - // GH#288 - When we finish initialization, if the user wanted us - // launched _fullscreen_, toggle fullscreen mode. This will make sure - // that the window size is _first_ set up as something sensible, so - // leaving fullscreen returns to a reasonable size. - const auto launchMode = this->GetLaunchMode(); - if (IsQuakeWindow() || WI_IsFlagSet(launchMode, LaunchMode::FocusMode)) - { - _root->SetFocusMode(true); - } - - // The IslandWindow handles (creating) the maximized state - // we just want to record it here on the page as well. - if (WI_IsFlagSet(launchMode, LaunchMode::MaximizedMode)) - { - _root->Maximized(true); - } - - if (WI_IsFlagSet(launchMode, LaunchMode::FullscreenMode) && !IsQuakeWindow()) - { - _root->SetFullscreen(true); - } - }); + _root->Loaded({ get_weak(), &TerminalWindow::_OnLoaded }); + // _root->Initialized([this](auto&&, auto&&) { + // // GH#288 - When we finish initialization, if the user wanted us + // // launched _fullscreen_, toggle fullscreen mode. This will make sure + // // that the window size is _first_ set up as something sensible, so + // // leaving fullscreen returns to a reasonable size. + // const auto launchMode = this->GetLaunchMode(); + // if (IsQuakeWindow() || WI_IsFlagSet(launchMode, LaunchMode::FocusMode)) + // { + // _root->SetFocusMode(true); + // } + + // // The IslandWindow handles (creating) the maximized state + // // we just want to record it here on the page as well. + // if (WI_IsFlagSet(launchMode, LaunchMode::MaximizedMode)) + // { + // _root->Maximized(true); + // } + + // if (WI_IsFlagSet(launchMode, LaunchMode::FullscreenMode) && !IsQuakeWindow()) + // { + // _root->SetFullscreen(true); + // } + // }); _root->Create(); _RefreshThemeRoutine(); diff --git a/src/cascadia/TerminalApp/TerminalWindow.h b/src/cascadia/TerminalApp/TerminalWindow.h index 5bec441ed99..04bb63b289c 100644 --- a/src/cascadia/TerminalApp/TerminalWindow.h +++ b/src/cascadia/TerminalApp/TerminalWindow.h @@ -38,7 +38,7 @@ namespace winrt::TerminalApp::implementation { public: TerminalWindow(const TerminalApp::SettingsLoadEventArgs& settingsLoadedResult); - ~TerminalWindow() = default; + ~TerminalWindow(); STDMETHODIMP Initialize(HWND hwnd); diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index 63eb0e7693d..eeb9df0b755 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -108,8 +108,8 @@ AppHost::~AppHost() _revokers = {}; _showHideWindowThrottler.reset(); - _window = nullptr; + _windowLogic = nullptr; } bool AppHost::OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down) diff --git a/src/cascadia/WindowsTerminal/WindowEmperor.cpp b/src/cascadia/WindowsTerminal/WindowEmperor.cpp index 92cabd3375a..6e7338739fb 100644 --- a/src/cascadia/WindowsTerminal/WindowEmperor.cpp +++ b/src/cascadia/WindowsTerminal/WindowEmperor.cpp @@ -62,9 +62,12 @@ WindowEmperor::~WindowEmperor() // for this thread. It's a real thinker. // // I need someone to help take a look at this with me. - + _dispatcher = nullptr; + _getWindowLayoutThrottler.reset(); + _manager = nullptr; _app.Close(); _app = nullptr; + // Sleep(20000); } void _buildArgsFromCommandline(std::vector& args) @@ -142,7 +145,7 @@ void WindowEmperor::CreateNewWindowThread(Remoting::WindowRequestedArgs args, co // Add a callback to the window's logic to let us know when the window's // quake mode state changes. We'll use this to check if we need to add // or remove the notification icon. - sender->Logic().IsQuakeWindowChanged([this](auto&&, auto&&) -> winrt::fire_and_forget { + sender->Logic().IsQuakeWindowChanged([this](auto&&, auto &&) -> winrt::fire_and_forget { co_await wil::resume_foreground(this->_dispatcher); this->_checkWindowsForNotificationIcon(); }); diff --git a/src/cascadia/WindowsTerminal/WindowThread.cpp b/src/cascadia/WindowsTerminal/WindowThread.cpp index 6cc6093c986..c7c8b038dda 100644 --- a/src/cascadia/WindowsTerminal/WindowThread.cpp +++ b/src/cascadia/WindowsTerminal/WindowThread.cpp @@ -29,6 +29,14 @@ void WindowThread::Start() const auto exitCode = WindowProc(); _host = nullptr; + { + MSG msg = {}; + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) + { + ::DispatchMessageW(&msg); + } + } + _ExitedHandlers(_peasant.GetID()); return exitCode; });