mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-12-13 11:35:56 +00:00
Fix window positioning on Windows when the taskbar is on the top or left (#12933)
* Added win32_desktop.cpp/.h defining a method Win32Desktop::GetWorkArea. Added code to wWinMain in main.cpp to position the window relative to the work area, which may not be at (0, 0) depending on the user's configuration. * Corrected the constraint on the size value calculated by main.cpp. * Fixed references to min to use std::min. * Reworked GetWorkArea in win32_desktop.cpp to treat the supplied origin and size as containing an existing window rectangle, and to find the monitor that contains or is closest to that window. Added function FitToWorkArea to win32_desktop.cpp/.h. Updated main.cpp to use Win32Desktop::FitToWorkArea instead of explicitly constraining the size.
This commit is contained in:
77
flutter/windows/runner/win32_desktop.cpp
Normal file
77
flutter/windows/runner/win32_desktop.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include "win32_desktop.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Win32Desktop
|
||||
{
|
||||
void GetWorkArea(Win32Window::Point& origin, Win32Window::Size& size)
|
||||
{
|
||||
RECT windowRect;
|
||||
|
||||
windowRect.left = origin.x;
|
||||
windowRect.top = origin.y;
|
||||
windowRect.right = origin.x + size.width;
|
||||
windowRect.bottom = origin.y + size.height;
|
||||
|
||||
HMONITOR hMonitor = MonitorFromRect(&windowRect, MONITOR_DEFAULTTONEAREST);
|
||||
|
||||
if (hMonitor == NULL)
|
||||
hMonitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
|
||||
|
||||
RECT workAreaRect;
|
||||
bool haveWorkAreaRect = false;
|
||||
|
||||
if (hMonitor != NULL)
|
||||
{
|
||||
MONITORINFO monitorInfo = {0};
|
||||
|
||||
monitorInfo.cbSize = sizeof(monitorInfo);
|
||||
|
||||
if (GetMonitorInfoW(hMonitor, &monitorInfo))
|
||||
{
|
||||
workAreaRect = monitorInfo.rcWork;
|
||||
haveWorkAreaRect = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!haveWorkAreaRect)
|
||||
{
|
||||
// I don't think this is possible, but just in case, some
|
||||
// reasonably sane fallbacks.
|
||||
workAreaRect.left = 0;
|
||||
workAreaRect.top = 0;
|
||||
workAreaRect.right = 1280;
|
||||
workAreaRect.bottom = 1024 - 40; // default Windows 10 task bar height
|
||||
}
|
||||
|
||||
origin.x = workAreaRect.left;
|
||||
origin.y = workAreaRect.top;
|
||||
|
||||
size.width = workAreaRect.right - workAreaRect.left;
|
||||
size.height = workAreaRect.bottom - workAreaRect.top;
|
||||
}
|
||||
|
||||
void FitToWorkArea(Win32Window::Point& origin, Win32Window::Size& size)
|
||||
{
|
||||
// Retrieve the work area of the monitor that contains or
|
||||
// is closed to the supplied window bounds.
|
||||
Win32Window::Point workarea_origin = origin;
|
||||
Win32Window::Size workarea_size = size;
|
||||
|
||||
GetWorkArea(workarea_origin, workarea_size);
|
||||
|
||||
// Translate the window so that its top/left is inside the work area.
|
||||
origin.x = std::max(origin.x, workarea_origin.x);
|
||||
origin.y = std::max(origin.y, workarea_origin.y);
|
||||
|
||||
// Crop the window if it extends past the bottom/right of the work area.
|
||||
Win32Window::Point workarea_bottom_right(
|
||||
workarea_origin.x + workarea_size.width,
|
||||
workarea_origin.y + workarea_size.height);
|
||||
|
||||
size.width = std::min(size.width, workarea_bottom_right.x - origin.x);
|
||||
size.height = std::min(size.height, workarea_bottom_right.y - origin.y);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user