Skip to content

Commit 56ae6e9

Browse files
committed
Add getId method for retrieving window ID on macOS and Windows
1 parent f94b618 commit 56ae6e9

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

packages/window_manager/example/lib/pages/home.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
141141
PreferenceListSection(
142142
title: const Text('METHODS'),
143143
children: [
144+
if (Platform.isWindows || Platform.isMacOS)
145+
PreferenceListItem(
146+
title: const Text('getId'),
147+
onTap: () async {
148+
final result = await windowManager.getId();
149+
BotToast.showText(text: 'Window ID:$result');
150+
},
151+
),
144152
PreferenceListItem(
145153
title: const Text('setAsFrameless'),
146154
onTap: () async {
@@ -180,14 +188,6 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
180188
print('isFocused: ${await windowManager.isFocused()}');
181189
},
182190
),
183-
if(Platform.isWindows)
184-
PreferenceListItem(
185-
title: const Text('getWindowHandle'),
186-
onTap: () async {
187-
final result = await windowManager.getWindowHandle();
188-
print('HWND:$result');
189-
},
190-
),
191191
PreferenceListItem(
192192
title: const Text('show / hide'),
193193
onTap: () async {

packages/window_manager/lib/src/window_manager.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,14 @@ class WindowManager {
104104
await _channel.invokeMethod('ensureInitialized');
105105
}
106106

107-
Future<int> getWindowHandle()async{
108-
return await _channel.invokeMethod('getWindowHandle') as int;
107+
/// Returns `int` - The ID of the window.
108+
///
109+
/// For macOS, the ID is the window number.
110+
/// For Windows, the ID is the window handle.
111+
///
112+
/// @platforms macos,windows
113+
Future<int> getId() async {
114+
return await _channel.invokeMethod('getId') as int;
109115
}
110116

111117
/// You can call this to remove the window frame (title bar, outline border, etc), which is basically everything except the Flutter view, also can call setTitleBarStyle(TitleBarStyle.normal) or setTitleBarStyle(TitleBarStyle.hidden) to restore it.

packages/window_manager/macos/window_manager/Sources/window_manager/WindowManager.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public class WindowManager: NSObject, NSWindowDelegate {
7676
public func waitUntilReadyToShow() {
7777
// nothing
7878
}
79+
80+
public func getId() -> Int {
81+
return mainWindow.windowNumber
82+
}
7983

8084
public func setAsFrameless() {
8185
mainWindow.styleMask.insert(.fullSizeContentView)

packages/window_manager/macos/window_manager/Sources/window_manager/WindowManagerPlugin.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public class WindowManagerPlugin: NSObject, FlutterPlugin {
5050
windowManager.waitUntilReadyToShow()
5151
result(true)
5252
break
53+
case "getId":
54+
result(windowManager.getId())
55+
break
5356
case "setAsFrameless":
5457
windowManager.setAsFrameless()
5558
result(true)

packages/window_manager/windows/window_manager_plugin.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ class WindowManagerPlugin : public flutter::Plugin {
4141

4242
private:
4343
std::unique_ptr<
44-
flutter::MethodChannel<flutter::EncodableValue>,
45-
std::default_delete<flutter::MethodChannel<flutter::EncodableValue>>>
44+
flutter::MethodChannel<flutter::EncodableValue>,
45+
std::default_delete<flutter::MethodChannel<flutter::EncodableValue>>>
4646
channel = nullptr;
4747

48-
4948
WindowManager* window_manager;
5049
flutter::PluginRegistrarWindows* registrar;
5150

@@ -69,8 +68,8 @@ class WindowManagerPlugin : public flutter::Plugin {
6968

7069
// HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
7170
// Don't use `MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST)` above.
72-
// Because if the window is restored from minimized state, the window is not in the correct monitor.
73-
// The monitor is always the left-most monitor.
71+
// Because if the window is restored from minimized state, the window is not
72+
// in the correct monitor. The monitor is always the left-most monitor.
7473
// https://github.com/leanflutter/window_manager/issues/489
7574
HMONITOR monitor = MonitorFromRect(&sz->rgrc[0], MONITOR_DEFAULTTONEAREST);
7675
if (monitor != NULL) {
@@ -113,10 +112,9 @@ WindowManagerPlugin::WindowManagerPlugin(
113112
registrar->messenger(), "window_manager",
114113
&flutter::StandardMethodCodec::GetInstance());
115114

116-
channel->SetMethodCallHandler(
117-
[this](const auto& call, auto result) {
118-
HandleMethodCall(call, std::move(result));
119-
});
115+
channel->SetMethodCallHandler([this](const auto& call, auto result) {
116+
HandleMethodCall(call, std::move(result));
117+
});
120118
}
121119

122120
WindowManagerPlugin::~WindowManagerPlugin() {
@@ -350,13 +348,12 @@ void WindowManagerPlugin::HandleMethodCall(
350348
window_manager->native_window =
351349
::GetAncestor(registrar->GetView()->GetNativeWindow(), GA_ROOT);
352350
result->Success(flutter::EncodableValue(true));
353-
}
354-
else if (method_name.compare("getWindowHandle") == 0) {
355-
result->Success(flutter::EncodableValue(reinterpret_cast<__int64>(window_manager->GetMainWindow())));
356-
}
357-
else if (method_name.compare("waitUntilReadyToShow") == 0) {
351+
} else if (method_name.compare("waitUntilReadyToShow") == 0) {
358352
window_manager->WaitUntilReadyToShow();
359353
result->Success(flutter::EncodableValue(true));
354+
} else if (method_name.compare("getId") == 0) {
355+
result->Success(flutter::EncodableValue(
356+
reinterpret_cast<__int64>(window_manager->GetMainWindow())));
360357
} else if (method_name.compare("setAsFrameless") == 0) {
361358
window_manager->SetAsFrameless();
362359
result->Success(flutter::EncodableValue(true));

0 commit comments

Comments
 (0)