|
25 | 25 | #define DWMWA_USE_IMMERSIVE_DARK_MODE 19 |
26 | 26 |
|
27 | 27 | namespace { |
| 28 | + |
| 29 | +const flutter::EncodableValue* ValueOrNull(const flutter::EncodableMap& map, |
| 30 | + const char* key) { |
| 31 | + auto it = map.find(flutter::EncodableValue(key)); |
| 32 | + if (it == map.end()) { |
| 33 | + return nullptr; |
| 34 | + } |
| 35 | + return &(it->second); |
| 36 | +} |
| 37 | + |
28 | 38 | class WindowManager { |
29 | 39 | public: |
30 | 40 | WindowManager(); |
@@ -74,12 +84,9 @@ class WindowManager { |
74 | 84 | void WindowManager::SetFullScreen(const flutter::EncodableMap& args); |
75 | 85 | void WindowManager::SetAspectRatio(const flutter::EncodableMap& args); |
76 | 86 | void WindowManager::SetBackgroundColor(const flutter::EncodableMap& args); |
77 | | - flutter::EncodableMap WindowManager::GetPosition( |
78 | | - const flutter::EncodableMap& args); |
79 | | - void WindowManager::SetPosition(const flutter::EncodableMap& args); |
80 | | - flutter::EncodableMap WindowManager::GetSize( |
| 87 | + flutter::EncodableMap WindowManager::GetBounds( |
81 | 88 | const flutter::EncodableMap& args); |
82 | | - void WindowManager::SetSize(const flutter::EncodableMap& args); |
| 89 | + void WindowManager::SetBounds(const flutter::EncodableMap& args); |
83 | 90 | void WindowManager::SetMinimumSize(const flutter::EncodableMap& args); |
84 | 91 | void WindowManager::SetMaximumSize(const flutter::EncodableMap& args); |
85 | 92 | bool WindowManager::IsResizable(); |
@@ -107,8 +114,6 @@ class WindowManager { |
107 | 114 | void WindowManager::PopUpWindowMenu(const flutter::EncodableMap& args); |
108 | 115 | void WindowManager::StartDragging(); |
109 | 116 | void WindowManager::StartResizing(const flutter::EncodableMap& args); |
110 | | - flutter::EncodableMap WindowManager::GetPrimaryDisplay( |
111 | | - const flutter::EncodableMap& args); |
112 | 117 |
|
113 | 118 | private: |
114 | 119 | bool g_is_window_fullscreen = false; |
@@ -444,60 +449,64 @@ void WindowManager::SetBackgroundColor(const flutter::EncodableMap& args) { |
444 | 449 | } |
445 | 450 | } |
446 | 451 |
|
447 | | -flutter::EncodableMap WindowManager::GetPosition( |
| 452 | +flutter::EncodableMap WindowManager::GetBounds( |
448 | 453 | const flutter::EncodableMap& args) { |
| 454 | + HWND hwnd = GetMainWindow(); |
449 | 455 | double devicePixelRatio = |
450 | 456 | std::get<double>(args.at(flutter::EncodableValue("devicePixelRatio"))); |
451 | 457 |
|
452 | 458 | flutter::EncodableMap resultMap = flutter::EncodableMap(); |
453 | 459 | RECT rect; |
454 | | - if (GetWindowRect(GetMainWindow(), &rect)) { |
| 460 | + if (GetWindowRect(hwnd, &rect)) { |
455 | 461 | double x = rect.left / devicePixelRatio * 1.0f; |
456 | 462 | double y = rect.top / devicePixelRatio * 1.0f; |
| 463 | + double width = (rect.right - rect.left) / devicePixelRatio * 1.0f; |
| 464 | + double height = (rect.bottom - rect.top) / devicePixelRatio * 1.0f; |
457 | 465 |
|
458 | 466 | resultMap[flutter::EncodableValue("x")] = flutter::EncodableValue(x); |
459 | 467 | resultMap[flutter::EncodableValue("y")] = flutter::EncodableValue(y); |
| 468 | + resultMap[flutter::EncodableValue("width")] = |
| 469 | + flutter::EncodableValue(width); |
| 470 | + resultMap[flutter::EncodableValue("height")] = |
| 471 | + flutter::EncodableValue(height); |
460 | 472 | } |
461 | 473 | return resultMap; |
462 | 474 | } |
463 | 475 |
|
464 | | -void WindowManager::SetPosition(const flutter::EncodableMap& args) { |
465 | | - double devicePixelRatio = |
466 | | - std::get<double>(args.at(flutter::EncodableValue("devicePixelRatio"))); |
467 | | - double x = std::get<double>(args.at(flutter::EncodableValue("x"))); |
468 | | - double y = std::get<double>(args.at(flutter::EncodableValue("y"))); |
469 | | - |
470 | | - SetWindowPos(GetMainWindow(), HWND_TOP, int(x * devicePixelRatio), |
471 | | - int(y * devicePixelRatio), 0, 0, SWP_NOSIZE); |
472 | | -} |
| 476 | +void WindowManager::SetBounds(const flutter::EncodableMap& args) { |
| 477 | + HWND hwnd = GetMainWindow(); |
473 | 478 |
|
474 | | -flutter::EncodableMap WindowManager::GetSize( |
475 | | - const flutter::EncodableMap& args) { |
476 | 479 | double devicePixelRatio = |
477 | 480 | std::get<double>(args.at(flutter::EncodableValue("devicePixelRatio"))); |
478 | 481 |
|
479 | | - flutter::EncodableMap resultMap = flutter::EncodableMap(); |
480 | | - RECT rect; |
481 | | - if (GetWindowRect(GetMainWindow(), &rect)) { |
482 | | - double width = (rect.right - rect.left) / devicePixelRatio * 1.0f; |
483 | | - double height = (rect.bottom - rect.top) / devicePixelRatio * 1.0f; |
| 482 | + auto* null_or_x = std::get_if<double>(ValueOrNull(args, "x")); |
| 483 | + auto* null_or_y = std::get_if<double>(ValueOrNull(args, "y")); |
| 484 | + auto* null_or_width = std::get_if<double>(ValueOrNull(args, "width")); |
| 485 | + auto* null_or_height = std::get_if<double>(ValueOrNull(args, "height")); |
484 | 486 |
|
485 | | - resultMap[flutter::EncodableValue("width")] = |
486 | | - flutter::EncodableValue(width); |
487 | | - resultMap[flutter::EncodableValue("height")] = |
488 | | - flutter::EncodableValue(height); |
| 487 | + int x = 0; |
| 488 | + int y = 0; |
| 489 | + int width = 0; |
| 490 | + int height = 0; |
| 491 | + UINT uFlags = NULL; |
| 492 | + |
| 493 | + if (null_or_x != nullptr && null_or_y != nullptr) { |
| 494 | + x = static_cast<int>(*null_or_x * devicePixelRatio); |
| 495 | + y = static_cast<int>(*null_or_y * devicePixelRatio); |
| 496 | + } |
| 497 | + if (null_or_width != nullptr && null_or_height != nullptr) { |
| 498 | + width = static_cast<int>(*null_or_width * devicePixelRatio); |
| 499 | + height = static_cast<int>(*null_or_height * devicePixelRatio); |
489 | 500 | } |
490 | | - return resultMap; |
491 | | -} |
492 | 501 |
|
493 | | -void WindowManager::SetSize(const flutter::EncodableMap& args) { |
494 | | - double devicePixelRatio = |
495 | | - std::get<double>(args.at(flutter::EncodableValue("devicePixelRatio"))); |
496 | | - double width = std::get<double>(args.at(flutter::EncodableValue("width"))); |
497 | | - double height = std::get<double>(args.at(flutter::EncodableValue("height"))); |
| 502 | + if (null_or_x == nullptr || null_or_y == nullptr) { |
| 503 | + uFlags = SWP_NOMOVE; |
| 504 | + } |
| 505 | + if (null_or_width == nullptr || null_or_height == nullptr) { |
| 506 | + uFlags = SWP_NOSIZE; |
| 507 | + } |
498 | 508 |
|
499 | | - SetWindowPos(GetMainWindow(), HWND_TOP, 0, 0, int(width * devicePixelRatio), |
500 | | - int(height * devicePixelRatio), SWP_NOMOVE); |
| 509 | + SetWindowPos(hwnd, HWND_TOP, x, y, width, height, uFlags); |
501 | 510 | } |
502 | 511 |
|
503 | 512 | void WindowManager::SetMinimumSize(const flutter::EncodableMap& args) { |
@@ -800,52 +809,4 @@ void WindowManager::StartResizing(const flutter::EncodableMap& args) { |
800 | 809 | SendMessage(hWnd, WM_SYSCOMMAND, command, 0); |
801 | 810 | } |
802 | 811 |
|
803 | | -flutter::EncodableMap WindowManager::GetPrimaryDisplay( |
804 | | - const flutter::EncodableMap& args) { |
805 | | - double devicePixelRatio = |
806 | | - std::get<double>(args.at(flutter::EncodableValue("devicePixelRatio"))); |
807 | | - POINT ptZero = {0, 0}; |
808 | | - HMONITOR monitor = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY); |
809 | | - MONITORINFO info; |
810 | | - info.cbSize = sizeof(MONITORINFO); |
811 | | - ::GetMonitorInfo(monitor, &info); |
812 | | - |
813 | | - double width = |
814 | | - (info.rcMonitor.right - info.rcMonitor.left) / devicePixelRatio; |
815 | | - double height = |
816 | | - (info.rcMonitor.bottom - info.rcMonitor.top) / devicePixelRatio; |
817 | | - |
818 | | - double visibleWidth = |
819 | | - (info.rcWork.right - info.rcWork.left) / devicePixelRatio; |
820 | | - double visibleHeight = |
821 | | - (info.rcWork.bottom - info.rcWork.top) / devicePixelRatio; |
822 | | - |
823 | | - double x = (info.rcWork.left) / devicePixelRatio; |
824 | | - double y = (info.rcWork.top) / devicePixelRatio; |
825 | | - |
826 | | - flutter::EncodableMap size = flutter::EncodableMap(); |
827 | | - flutter::EncodableMap visibleSize = flutter::EncodableMap(); |
828 | | - flutter::EncodableMap visiblePosition = flutter::EncodableMap(); |
829 | | - |
830 | | - size[flutter::EncodableValue("width")] = flutter::EncodableValue(width); |
831 | | - size[flutter::EncodableValue("height")] = flutter::EncodableValue(height); |
832 | | - |
833 | | - visibleSize[flutter::EncodableValue("width")] = |
834 | | - flutter::EncodableValue(visibleWidth); |
835 | | - visibleSize[flutter::EncodableValue("height")] = |
836 | | - flutter::EncodableValue(visibleHeight); |
837 | | - |
838 | | - visiblePosition[flutter::EncodableValue("x")] = flutter::EncodableValue(x); |
839 | | - visiblePosition[flutter::EncodableValue("y")] = flutter::EncodableValue(y); |
840 | | - |
841 | | - flutter::EncodableMap display = flutter::EncodableMap(); |
842 | | - display[flutter::EncodableValue("size")] = flutter::EncodableValue(size); |
843 | | - display[flutter::EncodableValue("visibleSize")] = |
844 | | - flutter::EncodableValue(visibleSize); |
845 | | - display[flutter::EncodableValue("visiblePosition")] = |
846 | | - flutter::EncodableValue(visiblePosition); |
847 | | - |
848 | | - return display; |
849 | | -} |
850 | | - |
851 | 812 | } // namespace |
0 commit comments