Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Monarch/Peasant & Make UI single-threaded #18215

Merged
merged 29 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7853853
List of monarchs who lost their thrones in the 21st century
lhecker Nov 19, 2024
3dee5c0
Revert WindowsTerminal.vcxproj
lhecker Nov 19, 2024
16900fd
Fix window singleton for unpackaged builds
lhecker Nov 19, 2024
25d1c71
Dismiss tray icon on exit
lhecker Nov 19, 2024
c7d1393
Fix _quake summoning
lhecker Nov 19, 2024
5faf0f6
Fix formatting
lhecker Nov 19, 2024
82902f2
Merge remote-tracking branch 'origin/main' into dev/lhecker/dethronement
lhecker Nov 22, 2024
06066d5
Fix wt /?
lhecker Nov 22, 2024
1aafccb
Fix tab dragging between windows
lhecker Nov 22, 2024
0862e90
Address feedback
lhecker Nov 22, 2024
c66ad24
Don't include the pch in header files
lhecker Nov 26, 2024
f0449d7
Remove unused code
lhecker Nov 26, 2024
f665c2f
Prevent the CoreWindow from getting destroyed
lhecker Nov 26, 2024
88a5302
Fix the touch input panel
lhecker Nov 26, 2024
8682ae1
A more sensible timeout
lhecker Nov 26, 2024
d84aa79
Fix tab drag window position
lhecker Nov 26, 2024
b353d41
Fix spelling
lhecker Dec 2, 2024
26c407b
Merge remote-tracking branch 'origin/main' into dev/lhecker/dethronement
lhecker Dec 2, 2024
d7e2a3c
Lazy-init IVirtualDesktopManager
lhecker Dec 5, 2024
0beb916
Address feedback
lhecker Dec 5, 2024
84eb929
Const-correctness
lhecker Dec 5, 2024
717c4c2
Some subjective cleanup
lhecker Dec 5, 2024
c9151e1
Fix WindowEmperor not reacing HWND_BROADCAST messages
lhecker Dec 5, 2024
53ec62d
Merge remote-tracking branch 'origin/main' into dev/lhecker/dethronement
lhecker Dec 5, 2024
314d8c2
Merge remote-tracking branch 'origin/main' into dev/lhecker/dethronement
lhecker Dec 10, 2024
5ca8264
Address feedback
lhecker Dec 11, 2024
26877d4
Reduce nesting in the window message handler
lhecker Dec 11, 2024
90cbe94
Fix typo
lhecker Dec 11, 2024
fea2660
Fix the message box
lhecker Dec 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ DEFINE_ENUM_FLAG_OPERATORS(winrt::Microsoft::Terminal::Control::MouseButtonState
static void hideCursorUntilMoved()
{
static CoreCursor previousCursor{ nullptr };
static auto shouldVanish = []() {
static const auto shouldVanish = []() {
BOOL shouldVanish = TRUE;
SystemParametersInfoW(SPI_GETMOUSEVANISH, 0, &shouldVanish, 0);
if (!shouldVanish)
Expand Down
91 changes: 56 additions & 35 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,27 @@ using namespace std::chrono_literals;
static constexpr short KeyPressed{ gsl::narrow_cast<short>(0x8000) };
static constexpr auto FrameUpdateInterval = std::chrono::milliseconds(16);

static winrt::com_ptr<IVirtualDesktopManager> s_desktopManager;
winrt::com_ptr<IVirtualDesktopManager> getDesktopManager()
{
static til::shared_mutex<winrt::com_ptr<IVirtualDesktopManager>> s_desktopManager;

if (const auto manager = *s_desktopManager.lock_shared())
{
return manager;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this will still return a dead RPC object if we had one stored, right? there's no way to communicate failure and invalidate it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it by killing explorer.exe but it still worked for me. Does the client perhaps reconnect automatically?

}

const auto guard = s_desktopManager.lock();
if (!*guard)
{
*guard = winrt::try_create_instance<IVirtualDesktopManager>(__uuidof(VirtualDesktopManager));
}
return *guard;
}

AppHost::AppHost(WindowEmperor* manager, const winrt::TerminalApp::AppLogic& logic, winrt::TerminalApp::WindowRequestedArgs args) noexcept :
_appLogic{ logic },
_windowManager{ manager }
{
if (!s_desktopManager)
{
s_desktopManager = winrt::try_create_instance<IVirtualDesktopManager>(__uuidof(VirtualDesktopManager));
}

_HandleCommandlineArgs(args);

// _HandleCommandlineArgs will create a _windowLogic
Expand Down Expand Up @@ -327,8 +337,9 @@ winrt::Windows::Foundation::IAsyncOperation<winrt::guid> AppHost::GetVirtualDesk
const auto dispatcher = _windowLogic.GetRoot().Dispatcher();
const auto hwnd = _window->GetHandle();
const auto weakThis = weak_from_this();
const auto desktopManager = getDesktopManager();

if (!hwnd || !s_desktopManager)
if (!hwnd || !desktopManager)
{
co_return null_guid;
}
Expand All @@ -339,7 +350,7 @@ winrt::Windows::Foundation::IAsyncOperation<winrt::guid> AppHost::GetVirtualDesk
co_await winrt::resume_background();

GUID id;
if (FAILED_LOG(s_desktopManager->GetWindowDesktopId(hwnd, &id)))
if (FAILED_LOG(desktopManager->GetWindowDesktopId(hwnd, &id)))
{
co_return null_guid;
}
Expand Down Expand Up @@ -811,37 +822,47 @@ void AppHost::_WindowActivated(bool activated)
}
}

void AppHost::HandleSummon(const winrt::TerminalApp::SummonWindowBehavior args) const
safe_void_coroutine AppHost::HandleSummon(const winrt::TerminalApp::SummonWindowBehavior args) const
{
_window->SummonWindow(args);

if (args != nullptr && args.MoveToCurrentDesktop())
if (!args || !args.MoveToCurrentDesktop())
{
if (s_desktopManager)
{
// First thing - make sure that we're not on the current desktop. If
// we are, then don't call MoveWindowToDesktop. This is to mitigate
// MSFT:33035972
BOOL onCurrentDesktop{ false };
if (SUCCEEDED(s_desktopManager->IsWindowOnCurrentVirtualDesktop(_window->GetHandle(), &onCurrentDesktop)) && onCurrentDesktop)
{
// If we succeeded, and the window was on the current desktop, then do nothing.
}
else
{
// Here, we either failed to check if the window is on the
// current desktop, or it wasn't on that desktop. In both those
// cases, just move the window.
co_return;
}

GUID currentlyActiveDesktop{ 0 };
if (VirtualDesktopUtils::GetCurrentVirtualDesktopId(&currentlyActiveDesktop))
{
LOG_IF_FAILED(s_desktopManager->MoveWindowToDesktop(_window->GetHandle(), currentlyActiveDesktop));
}
// If GetCurrentVirtualDesktopId failed, then just leave the window
// where it is. Nothing else to be done :/
}
const auto desktopManager = getDesktopManager();
if (!desktopManager)
{
co_return;
}

// Just like AppHost::GetVirtualDesktopId:
// IVirtualDesktopManager is cross-process COM into explorer.exe,
// and we can't use that on the UI thread.
co_await winrt::resume_background();

// First thing - make sure that we're not on the current desktop. If
// we are, then don't call MoveWindowToDesktop. This is to mitigate
// MSFT:33035972
BOOL onCurrentDesktop{ false };
if (SUCCEEDED(desktopManager->IsWindowOnCurrentVirtualDesktop(_window->GetHandle(), &onCurrentDesktop)) && onCurrentDesktop)
{
// If we succeeded, and the window was on the current desktop, then do nothing.
}
else
{
// Here, we either failed to check if the window is on the
// current desktop, or it wasn't on that desktop. In both those
// cases, just move the window.

GUID currentlyActiveDesktop{ 0 };
if (VirtualDesktopUtils::GetCurrentVirtualDesktopId(&currentlyActiveDesktop))
{
LOG_IF_FAILED(desktopManager->MoveWindowToDesktop(_window->GetHandle(), currentlyActiveDesktop));
}
// If GetCurrentVirtualDesktopId failed, then just leave the window
// where it is. Nothing else to be done :/
}
}

Expand Down Expand Up @@ -1037,12 +1058,12 @@ void AppHost::_WindowSizeChanged(const winrt::Windows::Foundation::IInspectable&
void AppHost::_SummonWindowRequested(const winrt::Windows::Foundation::IInspectable&,
const winrt::Windows::Foundation::IInspectable&)
{
const winrt::TerminalApp::SummonWindowBehavior summonArgs{};
winrt::TerminalApp::SummonWindowBehavior summonArgs;
summonArgs.MoveToCurrentDesktop(false);
summonArgs.DropdownDuration(0);
summonArgs.ToMonitor(winrt::TerminalApp::MonitorBehavior::InPlace);
summonArgs.ToggleVisibility(false); // Do not toggle, just make visible.
HandleSummon(summonArgs);
HandleSummon(std::move(summonArgs));
}

void AppHost::_OpenSystemMenu(const winrt::Windows::Foundation::IInspectable&,
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/WindowsTerminal/AppHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AppHost : public std::enable_shared_from_this<AppHost>

bool OnDirectKeyEvent(uint32_t vkey, uint8_t scanCode, bool down);
void SetTaskbarProgress(const winrt::Windows::Foundation::IInspectable& sender, const winrt::Windows::Foundation::IInspectable& args);
void HandleSummon(winrt::TerminalApp::SummonWindowBehavior args) const;
safe_void_coroutine HandleSummon(winrt::TerminalApp::SummonWindowBehavior args) const;
void DispatchCommandline(winrt::TerminalApp::CommandlineArgs args);

private:
Expand Down
30 changes: 15 additions & 15 deletions src/cascadia/WindowsTerminal/WindowEmperor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,20 +642,17 @@ static WindowEmperor* GetThisFromHandle(HWND const window) noexcept

[[nodiscard]] LRESULT __stdcall WindowEmperor::_wndProc(HWND const window, UINT const message, WPARAM const wparam, LPARAM const lparam) noexcept
{
WINRT_ASSERT(window);
if (const auto that = GetThisFromHandle(window))
{
return that->_messageHandler(window, message, wparam, lparam);
}

if (WM_NCCREATE == message)
{
const auto cs = reinterpret_cast<CREATESTRUCT*>(lparam);
WindowEmperor* that = static_cast<WindowEmperor*>(cs->lpCreateParams);
WINRT_ASSERT(that);
WINRT_ASSERT(!that->_window);
that->_window = wil::unique_hwnd(window);
SetWindowLongPtr(that->_window.get(), GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that));
}
else if (WindowEmperor* that = GetThisFromHandle(window))
{
return that->_messageHandler(window, message, wparam, lparam);
const auto that = static_cast<WindowEmperor*>(cs->lpCreateParams);
that->_window.reset(window);
SetWindowLongPtrW(window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that));
}

return DefWindowProcW(window, message, wparam, lparam);
Expand All @@ -674,6 +671,12 @@ void WindowEmperor::_createMessageWindow(const wchar_t* className)
};
RegisterClassW(&wc);

WM_TASKBARCREATED = RegisterWindowMessageW(L"TaskbarCreated");

// NOTE: This cannot be a HWND_MESSAGE window as otherwise we don't
// receive any HWND_BROADCAST messages, like WM_QUERYENDSESSION.
// NOTE: Before CreateWindowExW() returns it invokes our WM_NCCREATE
// message handler, which then stores the HWND in this->_window.
WINRT_VERIFY(CreateWindowExW(
/* dwExStyle */ 0,
/* lpClassName */ className,
Expand All @@ -683,7 +686,7 @@ void WindowEmperor::_createMessageWindow(const wchar_t* className)
/* Y */ 0,
/* nWidth */ 0,
/* nHeight */ 0,
/* hWndParent */ HWND_MESSAGE,
/* hWndParent */ nullptr,
/* hMenu */ nullptr,
/* hInstance */ instance,
/* lpParam */ this));
Expand Down Expand Up @@ -724,10 +727,6 @@ safe_void_coroutine WindowEmperor::_showMessageBox(winrt::hstring message, bool

LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM const wParam, LPARAM const lParam) noexcept
{
// use C++11 magic statics to make sure we only do this once.
// This won't change over the lifetime of the application
static const UINT WM_TASKBARCREATED = []() { return RegisterWindowMessageW(L"TaskbarCreated"); }();

try
{
switch (message)
Expand Down Expand Up @@ -943,6 +942,7 @@ LRESULT WindowEmperor::_messageHandler(HWND window, UINT const message, WPARAM c
// message at runtime.
if (message == WM_TASKBARCREATED)
{
_notificationIconShown = false;
_checkWindowsForNotificationIcon();
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/WindowEmperor.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class WindowEmperor
std::vector<std::shared_ptr<::AppHost>> _windows;
std::vector<winrt::Microsoft::Terminal::Settings::Model::GlobalSummonArgs> _hotkeys;
NOTIFYICONDATA _notificationIcon{};
UINT WM_TASKBARCREATED = 0;
HMENU _currentWindowMenu = nullptr;
bool _notificationIconShown = false;
bool _forcePersistence = false;
Expand Down
Loading