From bbcce141bed3c09a99085b113f3cd50709c08612 Mon Sep 17 00:00:00 2001 From: fanlumaster <1730976608@qq.com> Date: Wed, 31 Dec 2025 15:36:18 +0800 Subject: [PATCH] [ftb]: Adjust initial position --- server/src/utils/window_utils.cpp | 53 +++++++++++++++++++++++++++++++ server/src/utils/window_utils.h | 2 ++ server/src/window/ime_windows.cpp | 53 ++++++++++++++++++++++++++----- 3 files changed, 100 insertions(+), 8 deletions(-) diff --git a/server/src/utils/window_utils.cpp b/server/src/utils/window_utils.cpp index 43b174d9..ba0ceaba 100644 --- a/server/src/utils/window_utils.cpp +++ b/server/src/utils/window_utils.cpp @@ -57,6 +57,59 @@ MonitorCoordinates GetMonitorCoordinates() return coordinates; } +/** + * @brief Get the Main Monitor Coordinates + * + * @return MonitorCoordinates + */ +MonitorCoordinates GetMainMonitorCoordinates() +{ + MonitorCoordinates coordinates{}; + + HMONITOR hPrimary = MonitorFromPoint({0, 0}, MONITOR_DEFAULTTOPRIMARY); + + MONITORINFO mi{}; + mi.cbSize = sizeof(mi); + + if (GetMonitorInfo(hPrimary, &mi)) + { + coordinates.left = mi.rcMonitor.left; + coordinates.top = mi.rcMonitor.top; + coordinates.right = mi.rcMonitor.right; + coordinates.bottom = mi.rcMonitor.bottom; + } + + return coordinates; +} + +/** + * @brief Get the Taskbar Height + * + * @return int + */ +int GetTaskbarHeight() +{ + APPBARDATA abd{}; + abd.cbSize = sizeof(abd); + + if (!SHAppBarMessage(ABM_GETTASKBARPOS, &abd)) + return 0; + + RECT &r = abd.rc; + + switch (abd.uEdge) + { + case ABE_BOTTOM: + case ABE_TOP: + return r.bottom - r.top; // 高度 + case ABE_LEFT: + case ABE_RIGHT: + return r.right - r.left; // 宽度(竖向任务栏) + } + + return 0; +} + int AdjustCandidateWindowPosition( // const POINT *point, // const std::pair &containerSize, // diff --git a/server/src/utils/window_utils.h b/server/src/utils/window_utils.h index 43f9deaa..7fb73deb 100644 --- a/server/src/utils/window_utils.h +++ b/server/src/utils/window_utils.h @@ -7,6 +7,8 @@ FLOAT GetWindowScale(HWND); FLOAT GetForegroundWindowScale(); MonitorCoordinates GetMonitorCoordinates(); +MonitorCoordinates GetMainMonitorCoordinates(); +int GetTaskbarHeight(); int AdjustCandidateWindowPosition( // const POINT *point, // diff --git a/server/src/window/ime_windows.cpp b/server/src/window/ime_windows.cpp index 8e598302..c010c665 100644 --- a/server/src/window/ime_windows.cpp +++ b/server/src/window/ime_windows.cpp @@ -22,7 +22,8 @@ #pragma comment(lib, "dwmapi.lib") -constexpr UINT_PTR TIMER_ID_INIT_WEBVIEW = 1; +constexpr UINT_PTR TIMER_ID_INIT_WEBVIEW_MENU = 1; +constexpr UINT_PTR TIMER_ID_MOVE_WEBVIEW_FTB = 2; int FineTuneWindow(HWND hwnd); int FineTuneWindow(HWND hwnd, UINT firstFlag, UINT secondFlag); @@ -213,7 +214,7 @@ int CreateCandidateWindow(HINSTANCE hInstance) lpWindowNameFtb, // WS_POPUP, // 2216, // - 1254, // + -12540, // (::FTB_WND_WIDTH + ::FTB_WND_SHADOW_WIDTH) * scale, // (::FTB_WND_HEIGHT + ::FTB_WND_SHADOW_WIDTH) * scale, // nullptr, // @@ -255,7 +256,10 @@ int CreateCandidateWindow(HINSTANCE hInstance) InitWebviewFtbWnd(hwnd_ftb); /* 调整菜单窗口 size */ - SetTimer(hwnd_menu, TIMER_ID_INIT_WEBVIEW, 200, nullptr); + SetTimer(hwnd_menu, TIMER_ID_INIT_WEBVIEW_MENU, 200, nullptr); + + /* 调整 floating toolbar 窗口 position */ + SetTimer(hwnd_ftb, TIMER_ID_MOVE_WEBVIEW_FTB, 200, nullptr); // // 注册一下全局钩子 @@ -449,15 +453,15 @@ LRESULT CALLBACK WndProcMenuWindow(HWND hwnd, UINT message, WPARAM wParam, LPARA } case WM_TIMER: { - if (wParam == TIMER_ID_INIT_WEBVIEW) + if (wParam == TIMER_ID_INIT_WEBVIEW_MENU) { - KillTimer(hwnd, TIMER_ID_INIT_WEBVIEW); - if (webviewMenuWnd) // 确保 webview 已初始化 + KillTimer(hwnd, TIMER_ID_INIT_WEBVIEW_MENU); + if (::webviewMenuWnd) // 确保 webview 已初始化 { GetContainerSizeMenu(webviewMenuWnd, [hwnd](std::pair containerSize) { if (hwnd == ::global_hwnd_menu) { - UINT flag = SWP_NOZORDER | SWP_NOMOVE; + UINT flag = SWP_NOZORDER | SWP_NOMOVE | SWP_HIDEWINDOW; FLOAT scale = GetForegroundWindowScale(); int newWidth = (containerSize.first) * scale; int newHeight = (containerSize.second) * scale; @@ -480,7 +484,7 @@ LRESULT CALLBACK WndProcMenuWindow(HWND hwnd, UINT message, WPARAM wParam, LPARA else { // 如果 webview 还没准备好,再等一会 - SetTimer(hwnd, TIMER_ID_INIT_WEBVIEW, 100, nullptr); + SetTimer(hwnd, TIMER_ID_INIT_WEBVIEW_MENU, 100, nullptr); } } break; @@ -526,6 +530,39 @@ LRESULT CALLBACK WndProcFtbWindow(HWND hwnd, UINT message, WPARAM wParam, LPARAM { switch (message) { + case WM_TIMER: { + if (wParam == TIMER_ID_MOVE_WEBVIEW_FTB) + { + KillTimer(hwnd, TIMER_ID_MOVE_WEBVIEW_FTB); + if (::webviewFtbWnd) + { + // 放在屏幕右下角 + // 获取主屏幕尺寸 + MonitorCoordinates coordinates = GetMainMonitorCoordinates(); + // 获取窗口尺寸 + RECT rect; + GetWindowRect(hwnd, &rect); + // 获取任务栏高度 + int taskbarHeight = GetTaskbarHeight(); + // 移动窗口 + SetWindowPos( // + hwnd, // + 0, // + coordinates.right - (rect.right - rect.left) - 10, // + coordinates.bottom - (rect.bottom - rect.top) - taskbarHeight - 10, // + 0, // + 0, // + SWP_NOSIZE); + break; + } + else + { + // 如果 webview 还没准备好,再等一会 + SetTimer(hwnd, TIMER_ID_MOVE_WEBVIEW_FTB, 100, nullptr); + } + } + break; + } default: { return DefWindowProc(hwnd, message, wParam, lParam); }