Skip to content

Commit 33da387

Browse files
committed
Add will show/hide hooks and update window manager API
Introduces 'will show' and 'will hide' hooks to the window manager, allowing Dart-side interception before native window show/hide actions. Updates the API to use bool-returning callbacks for these hooks, and adds FFI bindings for checking hook presence and calling original native show/hide implementations. Removes the unused native_window_manager_destroy binding.
1 parent a555ad1 commit 33da387

File tree

4 files changed

+77
-21
lines changed

4 files changed

+77
-21
lines changed

examples/multiple_window_example/lib/main.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ void main() {
2424
break;
2525
}
2626
}
27+
return true;
28+
});
29+
WindowManager.instance.setWillHideHook((windowId) {
30+
print('[Dart] will hide hook $windowId');
31+
return true;
2732
});
2833
runWidget(
2934
ViewCollection(

packages/cnativeapi/lib/src/bindings_generated.dart

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,20 +3616,6 @@ class CNativeApiBindings {
36163616
_native_window_manager_get_currentPtr
36173617
.asFunction<native_window_t Function()>();
36183618

3619-
/// Destroy a window by its ID
3620-
/// @param window_id The window ID to destroy
3621-
/// @return true if window was found and destroyed, false otherwise
3622-
bool native_window_manager_destroy(int window_id) {
3623-
return _native_window_manager_destroy(window_id);
3624-
}
3625-
3626-
late final _native_window_manager_destroyPtr =
3627-
_lookup<ffi.NativeFunction<ffi.Bool Function(native_window_id_t)>>(
3628-
'native_window_manager_destroy',
3629-
);
3630-
late final _native_window_manager_destroy = _native_window_manager_destroyPtr
3631-
.asFunction<bool Function(int)>();
3632-
36333619
/// Register a callback for window events
36343620
/// @param callback The callback function to register
36353621
/// @param user_data User data to pass to the callback
@@ -3740,6 +3726,64 @@ class CNativeApiBindings {
37403726
ffi.Pointer<ffi.Void>,
37413727
)
37423728
>();
3729+
3730+
/// Check if the "will show" hook is set.
3731+
/// @return true if hook is set, false otherwise.
3732+
bool native_window_manager_has_will_show_hook() {
3733+
return _native_window_manager_has_will_show_hook();
3734+
}
3735+
3736+
late final _native_window_manager_has_will_show_hookPtr =
3737+
_lookup<ffi.NativeFunction<ffi.Bool Function()>>(
3738+
'native_window_manager_has_will_show_hook',
3739+
);
3740+
late final _native_window_manager_has_will_show_hook =
3741+
_native_window_manager_has_will_show_hookPtr
3742+
.asFunction<bool Function()>();
3743+
3744+
/// Check if the "will hide" hook is set.
3745+
/// @return true if hook is set, false otherwise.
3746+
bool native_window_manager_has_will_hide_hook() {
3747+
return _native_window_manager_has_will_hide_hook();
3748+
}
3749+
3750+
late final _native_window_manager_has_will_hide_hookPtr =
3751+
_lookup<ffi.NativeFunction<ffi.Bool Function()>>(
3752+
'native_window_manager_has_will_hide_hook',
3753+
);
3754+
late final _native_window_manager_has_will_hide_hook =
3755+
_native_window_manager_has_will_hide_hookPtr
3756+
.asFunction<bool Function()>();
3757+
3758+
/// Call the original native show implementation for the specified window.
3759+
/// This bypasses the swizzled hook path on macOS.
3760+
/// @return true on success, false if the window wasn't found or unsupported.
3761+
bool native_window_manager_call_original_show(int window_id) {
3762+
return _native_window_manager_call_original_show(window_id);
3763+
}
3764+
3765+
late final _native_window_manager_call_original_showPtr =
3766+
_lookup<ffi.NativeFunction<ffi.Bool Function(native_window_id_t)>>(
3767+
'native_window_manager_call_original_show',
3768+
);
3769+
late final _native_window_manager_call_original_show =
3770+
_native_window_manager_call_original_showPtr
3771+
.asFunction<bool Function(int)>();
3772+
3773+
/// Call the original native hide implementation for the specified window.
3774+
/// This bypasses the swizzled hook path on macOS.
3775+
/// @return true on success, false if the window wasn't found or unsupported.
3776+
bool native_window_manager_call_original_hide(int window_id) {
3777+
return _native_window_manager_call_original_hide(window_id);
3778+
}
3779+
3780+
late final _native_window_manager_call_original_hidePtr =
3781+
_lookup<ffi.NativeFunction<ffi.Bool Function(native_window_id_t)>>(
3782+
'native_window_manager_call_original_hide',
3783+
);
3784+
late final _native_window_manager_call_original_hide =
3785+
_native_window_manager_call_original_hidePtr
3786+
.asFunction<bool Function(int)>();
37433787
}
37443788

37453789
/// Representation of a point

packages/nativeapi/lib/src/window_manager.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:ffi' hide Size;
2+
import 'package:flutter/widgets.dart';
23
import 'package:nativeapi/src/foundation/cnativeapi_bindings_mixin.dart';
34
import 'package:nativeapi/src/foundation/event_emitter.dart';
45
import 'package:nativeapi/src/foundation/geometry.dart';
@@ -87,8 +88,8 @@ class WindowManager with EventEmitter, CNativeApiBindingsMixin {
8788
_willHideCallback;
8889

8990
// Dart-side hook handlers
90-
void Function(int windowId)? _onWillShowHook;
91-
void Function(int windowId)? _onWillHideHook;
91+
bool Function(int windowId)? _onWillShowHook;
92+
bool Function(int windowId)? _onWillHideHook;
9293

9394
/// Private constructor for singleton pattern.
9495
WindowManager._();
@@ -291,7 +292,7 @@ class WindowManager with EventEmitter, CNativeApiBindingsMixin {
291292

292293
/// Set (or clear) the hook invoked BEFORE a native window is shown.
293294
/// Passing null clears the hook.
294-
void setWillShowHook(void Function(int windowId)? callback) {
295+
void setWillShowHook(bool Function(int windowId)? callback) {
295296
_onWillShowHook = callback;
296297

297298
// Clear current hook if requested
@@ -317,7 +318,7 @@ class WindowManager with EventEmitter, CNativeApiBindingsMixin {
317318

318319
/// Set (or clear) the hook invoked BEFORE a native window is hidden.
319320
/// Passing null clears the hook.
320-
void setWillHideHook(void Function(int windowId)? callback) {
321+
void setWillHideHook(bool Function(int windowId)? callback) {
321322
_onWillHideHook = callback;
322323

323324
if (callback == null) {
@@ -347,7 +348,10 @@ class WindowManager with EventEmitter, CNativeApiBindingsMixin {
347348
) {
348349
final cb = _instance._onWillShowHook;
349350
if (cb != null) {
350-
cb(windowId);
351+
bool result = cb(windowId);
352+
if (result) {
353+
_instance.bindings.native_window_manager_call_original_show(windowId);
354+
}
351355
}
352356
}
353357

@@ -358,7 +362,10 @@ class WindowManager with EventEmitter, CNativeApiBindingsMixin {
358362
) {
359363
final cb = _instance._onWillHideHook;
360364
if (cb != null) {
361-
cb(windowId);
365+
bool result = cb(windowId);
366+
if (result) {
367+
_instance.bindings.native_window_manager_call_original_hide(windowId);
368+
}
362369
}
363370
}
364371
}

0 commit comments

Comments
 (0)