Skip to content

Commit fd678fe

Browse files
committed
[windows] Update getBounds & setBounds methods
1 parent e4ca104 commit fd678fe

File tree

9 files changed

+54
-144
lines changed

9 files changed

+54
-144
lines changed

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ packages:
122122
name: screen_retriever
123123
url: "https://pub.dartlang.org"
124124
source: hosted
125-
version: "0.1.1"
125+
version: "0.1.2"
126126
shortid:
127127
dependency: transitive
128128
description:

lib/src/window_manager.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,6 @@ class WindowManager {
617617
);
618618
}
619619

620-
Future<Map<String, dynamic>> _getPrimaryDisplay() async {
621-
final Map<String, dynamic> arguments = {
622-
'devicePixelRatio': window.devicePixelRatio,
623-
};
624-
final Map<dynamic, dynamic> resultData =
625-
await _channel.invokeMethod('getPrimaryDisplay', arguments);
626-
return Map<String, dynamic>.from(resultData);
627-
}
628-
629620
Future<bool> isSubWindow() async {
630621
return await _channel.invokeMethod('isSubWindow');
631622
}

linux/window_manager_plugin.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,6 @@ static void window_manager_plugin_handle_method_call(
642642
response = start_dragging(self);
643643
} else if (strcmp(method, "startResizing") == 0) {
644644
response = start_resizing(self, args);
645-
} else if (strcmp(method, "getPrimaryDisplay") == 0) {
646-
response = get_primary_display(self, args);
647645
} else {
648646
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
649647
}

macos/Classes/WindowManager.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -447,28 +447,6 @@ public class WindowManager: NSObject, NSWindowDelegate {
447447
}
448448
}
449449

450-
public func getPrimaryDisplay() -> NSDictionary {
451-
let screen = NSScreen.main!
452-
let size: NSDictionary = [
453-
"width": screen.frame.width,
454-
"height": screen.frame.height,
455-
]
456-
let visiblePosition: NSDictionary = [
457-
"x": screen.visibleFrame.topLeft.x,
458-
"y": screen.visibleFrame.topLeft.y,
459-
]
460-
let visibleSize: NSDictionary = [
461-
"width": screen.visibleFrame.width,
462-
"height": screen.visibleFrame.height,
463-
]
464-
let dict: NSDictionary = [
465-
"size": size,
466-
"visiblePosition": visiblePosition,
467-
"visibleSize": visibleSize,
468-
]
469-
return dict;
470-
}
471-
472450
public func isSubWindow() -> Bool {
473451
let identifier: String = mainWindow.identifier?.rawValue ?? "";
474452
return identifier == "subwindow"

macos/Classes/WindowManagerPlugin.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,6 @@ public class WindowManagerPlugin: NSObject, FlutterPlugin {
231231
windowManager.startDragging()
232232
result(true)
233233
break
234-
case "getPrimaryDisplay":
235-
result(windowManager.getPrimaryDisplay())
236-
break
237234
case "isSubWindow":
238235
result(windowManager.isSubWindow())
239236
break

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ packages:
9494
name: screen_retriever
9595
url: "https://pub.dartlang.org"
9696
source: hosted
97-
version: "0.1.1"
97+
version: "0.1.2"
9898
sky_engine:
9999
dependency: transitive
100100
description: flutter

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ dependencies:
1717
sdk: flutter
1818

1919
path: ^1.8.0
20-
screen_retriever: ^0.1.1
20+
screen_retriever: ^0.1.2
2121

2222
dev_dependencies:
2323
flutter_test:

windows/window_manager.cpp

Lines changed: 47 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
#define DWMWA_USE_IMMERSIVE_DARK_MODE 19
2626

2727
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+
2838
class WindowManager {
2939
public:
3040
WindowManager();
@@ -74,12 +84,9 @@ class WindowManager {
7484
void WindowManager::SetFullScreen(const flutter::EncodableMap& args);
7585
void WindowManager::SetAspectRatio(const flutter::EncodableMap& args);
7686
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(
8188
const flutter::EncodableMap& args);
82-
void WindowManager::SetSize(const flutter::EncodableMap& args);
89+
void WindowManager::SetBounds(const flutter::EncodableMap& args);
8390
void WindowManager::SetMinimumSize(const flutter::EncodableMap& args);
8491
void WindowManager::SetMaximumSize(const flutter::EncodableMap& args);
8592
bool WindowManager::IsResizable();
@@ -107,8 +114,6 @@ class WindowManager {
107114
void WindowManager::PopUpWindowMenu(const flutter::EncodableMap& args);
108115
void WindowManager::StartDragging();
109116
void WindowManager::StartResizing(const flutter::EncodableMap& args);
110-
flutter::EncodableMap WindowManager::GetPrimaryDisplay(
111-
const flutter::EncodableMap& args);
112117

113118
private:
114119
bool g_is_window_fullscreen = false;
@@ -444,60 +449,64 @@ void WindowManager::SetBackgroundColor(const flutter::EncodableMap& args) {
444449
}
445450
}
446451

447-
flutter::EncodableMap WindowManager::GetPosition(
452+
flutter::EncodableMap WindowManager::GetBounds(
448453
const flutter::EncodableMap& args) {
454+
HWND hwnd = GetMainWindow();
449455
double devicePixelRatio =
450456
std::get<double>(args.at(flutter::EncodableValue("devicePixelRatio")));
451457

452458
flutter::EncodableMap resultMap = flutter::EncodableMap();
453459
RECT rect;
454-
if (GetWindowRect(GetMainWindow(), &rect)) {
460+
if (GetWindowRect(hwnd, &rect)) {
455461
double x = rect.left / devicePixelRatio * 1.0f;
456462
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;
457465

458466
resultMap[flutter::EncodableValue("x")] = flutter::EncodableValue(x);
459467
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);
460472
}
461473
return resultMap;
462474
}
463475

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();
473478

474-
flutter::EncodableMap WindowManager::GetSize(
475-
const flutter::EncodableMap& args) {
476479
double devicePixelRatio =
477480
std::get<double>(args.at(flutter::EncodableValue("devicePixelRatio")));
478481

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"));
484486

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);
489500
}
490-
return resultMap;
491-
}
492501

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+
}
498508

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);
501510
}
502511

503512
void WindowManager::SetMinimumSize(const flutter::EncodableMap& args) {
@@ -800,52 +809,4 @@ void WindowManager::StartResizing(const flutter::EncodableMap& args) {
800809
SendMessage(hWnd, WM_SYSCOMMAND, command, 0);
801810
}
802811

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-
851812
} // namespace

windows/window_manager_plugin.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -375,25 +375,15 @@ void WindowManagerPlugin::HandleMethodCall(
375375
std::get<flutter::EncodableMap>(*method_call.arguments());
376376
window_manager->SetBackgroundColor(args);
377377
result->Success(flutter::EncodableValue(true));
378-
} else if (method_name.compare("getPosition") == 0) {
378+
} else if (method_name.compare("getBounds") == 0) {
379379
const flutter::EncodableMap& args =
380380
std::get<flutter::EncodableMap>(*method_call.arguments());
381-
flutter::EncodableMap value = window_manager->GetPosition(args);
381+
flutter::EncodableMap value = window_manager->GetBounds(args);
382382
result->Success(flutter::EncodableValue(value));
383-
} else if (method_name.compare("setPosition") == 0) {
383+
} else if (method_name.compare("setBounds") == 0) {
384384
const flutter::EncodableMap& args =
385385
std::get<flutter::EncodableMap>(*method_call.arguments());
386-
window_manager->SetPosition(args);
387-
result->Success(flutter::EncodableValue(true));
388-
} else if (method_name.compare("getSize") == 0) {
389-
const flutter::EncodableMap& args =
390-
std::get<flutter::EncodableMap>(*method_call.arguments());
391-
flutter::EncodableMap value = window_manager->GetSize(args);
392-
result->Success(flutter::EncodableValue(value));
393-
} else if (method_name.compare("setSize") == 0) {
394-
const flutter::EncodableMap& args =
395-
std::get<flutter::EncodableMap>(*method_call.arguments());
396-
window_manager->SetSize(args);
386+
window_manager->SetBounds(args);
397387
result->Success(flutter::EncodableValue(true));
398388
} else if (method_name.compare("setMinimumSize") == 0) {
399389
const flutter::EncodableMap& args =
@@ -510,11 +500,6 @@ void WindowManagerPlugin::HandleMethodCall(
510500
std::get<flutter::EncodableMap>(*method_call.arguments());
511501
window_manager->StartResizing(args);
512502
result->Success(flutter::EncodableValue(true));
513-
} else if (method_name.compare("getPrimaryDisplay") == 0) {
514-
const flutter::EncodableMap& args =
515-
std::get<flutter::EncodableMap>(*method_call.arguments());
516-
flutter::EncodableMap value = window_manager->GetPrimaryDisplay(args);
517-
result->Success(flutter::EncodableValue(value));
518503
} else {
519504
result->NotImplemented();
520505
}

0 commit comments

Comments
 (0)