From 9849b09cd5f0920ab66752fa72345e3ccabbfe26 Mon Sep 17 00:00:00 2001 From: fanlumaster <1730976608@qq.com> Date: Wed, 17 Sep 2025 17:18:40 +0800 Subject: [PATCH] [ui]: Adjust size and position in some cases --- server/src/global/globals.h | 7 +- server/src/utils/window_utils.cpp | 7 ++ server/src/utils/window_utils.h | 1 + server/src/window/candidate_window.cpp | 144 +++++++------------------ 4 files changed, 52 insertions(+), 107 deletions(-) diff --git a/server/src/global/globals.h b/server/src/global/globals.h index d530a1ea..480c1e77 100644 --- a/server/src/global/globals.h +++ b/server/src/global/globals.h @@ -41,4 +41,9 @@ inline std::unordered_set PUNC_SET = { namespace CandidateUi { inline std::string NumHanSeparator = " "; // Number and Hanzi separator -} // namespace CandidateUi \ No newline at end of file +} // namespace CandidateUi + +namespace Global +{ +inline LONG INVALID_Y = -100000; +} \ No newline at end of file diff --git a/server/src/utils/window_utils.cpp b/server/src/utils/window_utils.cpp index 80cc264b..d978a2d8 100644 --- a/server/src/utils/window_utils.cpp +++ b/server/src/utils/window_utils.cpp @@ -10,6 +10,13 @@ FLOAT GetWindowScale(HWND hwnd) return scale; } +FLOAT GetForegroundWindowScale() +{ + HWND hwnd = GetForegroundWindow(); + FLOAT scale = GetWindowScale(hwnd); + return scale; +} + //+--------------------------------------------------------------------------- // // GetMonitorCoordinates diff --git a/server/src/utils/window_utils.h b/server/src/utils/window_utils.h index a0bd48e3..43f9deaa 100644 --- a/server/src/utils/window_utils.h +++ b/server/src/utils/window_utils.h @@ -4,6 +4,7 @@ #include FLOAT GetWindowScale(HWND); +FLOAT GetForegroundWindowScale(); MonitorCoordinates GetMonitorCoordinates(); diff --git a/server/src/window/candidate_window.cpp b/server/src/window/candidate_window.cpp index 2d70829f..eb0e43e2 100644 --- a/server/src/window/candidate_window.cpp +++ b/server/src/window/candidate_window.cpp @@ -1,3 +1,4 @@ +#include "global/globals.h" #include "ipc/ipc.h" #include "candidate_window.h" #include "defines/defines.h" @@ -18,6 +19,7 @@ #pragma comment(lib, "dwmapi.lib") +int FineTuneWindow(HWND hwnd); int FineTuneWindow(HWND hwnd, UINT firstFlag, UINT secondFlag); LRESULT RegisterCandidateWindowMessage() @@ -140,8 +142,6 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) if (message == WM_SHOW_MAIN_WINDOW) { - int caretX = Global::Point[0]; - int caretY = Global::Point[1]; /* Read candidate string */ ::ReadDataFromSharedMemory(0b1000000); std::wstring embeded_pinyin = string_to_wstring( // @@ -150,46 +150,14 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) std::wstring str = embeded_pinyin + L"," + Global::CandidateString; InflateCandidateWindow(str); - if (caretY < -900) - { - std::shared_ptr> properPos = std::make_shared>(); - GetContainerSize(webview, [caretX, caretY, properPos, hwnd](std::pair containerSize) { - POINT pt = {caretX, caretY}; - AdjustCandidateWindowPosition(&pt, containerSize, properPos); - SetWindowPos( // - hwnd, // - nullptr, // - caretX, // - caretY, // - (containerSize.first + ::SHADOW_WIDTH) * 1.5, // - (containerSize.second + ::SHADOW_WIDTH) * 1.5, // - SWP_NOZORDER | SWP_SHOWWINDOW // - ); - }); - } - else - { - std::shared_ptr> properPos = std::make_shared>(); - GetContainerSize(webview, [caretX, caretY, properPos, hwnd](std::pair containerSize) { - POINT pt = {caretX, caretY}; - AdjustCandidateWindowPosition(&pt, containerSize, properPos); - SetWindowPos( // - hwnd, // - nullptr, // - 0, // - 0, // - (containerSize.first + ::SHADOW_WIDTH) * 1.5, // - (containerSize.second + ::SHADOW_WIDTH) * 1.5, // - SWP_NOMOVE | SWP_NOZORDER | SWP_SHOWWINDOW // - ); - }); - } + FineTuneWindow(hwnd); + return 0; } if (message == WM_HIDE_MAIN_WINDOW) { - ShowWindow(hwnd, SW_HIDE); + SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_HIDEWINDOW); UpdateHtmlContentWithJavaScript(webview, L""); std::wstring str = L"n,那,年,女,难,内,你,男,哪"; InflateCandidateWindow(str); @@ -198,37 +166,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) if (message == WM_MOVE_CANDIDATE_WINDOW) { - int caretX = Global::Point[0]; - int caretY = Global::Point[1]; - if (caretY < -900) - { - SetWindowPos( // - hwnd, // - nullptr, // - caretX, // - caretY, // - 0, // - 0, // - SWP_NOSIZE | SWP_NOZORDER // - ); - } - else - { - std::shared_ptr> properPos = std::make_shared>(); - GetContainerSize(webview, [caretX, caretY, properPos, hwnd](std::pair containerSize) { - POINT pt = {caretX, caretY}; - AdjustCandidateWindowPosition(&pt, containerSize, properPos); - SetWindowPos( // - hwnd, // - nullptr, // - properPos->first, // - properPos->second, // - 0, // - 0, // - SWP_NOSIZE | SWP_NOZORDER // - ); - }); - } + FineTuneWindow(hwnd); return 0; } @@ -264,47 +202,41 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) return 0; } -int FineTuneWindow(HWND hwnd, UINT firstFlag, UINT secondFlag) +int FineTuneWindow(HWND hwnd) { + UINT flag = SWP_NOZORDER | SWP_SHOWWINDOW; + + FLOAT scale = GetForegroundWindowScale(); + int caretX = Global::Point[0]; int caretY = Global::Point[1]; - - int newWidth = (::CANDIDATE_WINDOW_WIDTH + ::SHADOW_WIDTH); - int newHeight = (::DEFAULT_WINDOW_HEIGHT + ::SHADOW_WIDTH); - - if (caretY < -900) - { - SetWindowPos( // - hwnd, // - nullptr, // - caretX, // - caretY, // - newWidth, // - newHeight, // - firstFlag // + std::shared_ptr> properPos = std::make_shared>(); + GetContainerSize(webview, [flag, // + scale, // + caretX, // + caretY, // + properPos, // + hwnd](std::pair containerSize) { + POINT pt = {caretX, caretY}; + /* Whether need to adjust candidate window position */ + if (caretX == Global::INVALID_Y) + { + properPos->first = caretX; + properPos->second = caretY; + } + else + { + AdjustCandidateWindowPosition(&pt, containerSize, properPos); + } + SetWindowPos( // + hwnd, // + nullptr, // + properPos->first, // + properPos->second, // + (containerSize.first + ::SHADOW_WIDTH) * scale, // + (containerSize.second + ::SHADOW_WIDTH) * scale, // + flag // ); - } - else - { - int point[2] = {0, 0}; - AdjustWndPosition( // - hwnd, // - caretX, // - caretY, // - newWidth, // - newHeight, // - point // - ); - - SetWindowPos( // - hwnd, // - nullptr, // - point[0], // - point[1], // - newWidth, // - newHeight, // - secondFlag // - ); - } + }); return 0; } \ No newline at end of file