Skip to content

Commit ae5950f

Browse files
feat: add setWillCloseHook to WindowManager Dart API
Exposes the native WindowWillCloseHook through the Dart FFI layer, matching the existing setWillShowHook/setWillHideHook API surface. Changes: - cnativeapi/bindings_generated.dart: FFI bindings for native_window_manager_set_will_close_hook, has_will_close_hook, handle_will_close, call_original_close, plus the callback typedef. - nativeapi/window_manager.dart: Dart methods setWillCloseHook, hasWillCloseHook, handleWillClose, callOriginalClose. Depends on libnativeapi/nativeapi#51 (C++ SetWillCloseHook).
1 parent c758afc commit ae5950f

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

packages/cnativeapi/lib/src/bindings_generated.dart

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4358,6 +4358,67 @@ class CNativeApiBindings {
43584358
_native_window_manager_call_original_hidePtr
43594359
.asFunction<bool Function(int)>();
43604360

4361+
void native_window_manager_set_will_close_hook(
4362+
native_window_manager_set_will_close_hook_callback_t hook,
4363+
ffi.Pointer<ffi.Void> hook_user_data,
4364+
) {
4365+
return _native_window_manager_set_will_close_hook(hook, hook_user_data);
4366+
}
4367+
4368+
late final _native_window_manager_set_will_close_hookPtr =
4369+
_lookup<
4370+
ffi.NativeFunction<
4371+
ffi.Void Function(
4372+
native_window_manager_set_will_close_hook_callback_t,
4373+
ffi.Pointer<ffi.Void>,
4374+
)
4375+
>
4376+
>('native_window_manager_set_will_close_hook');
4377+
late final _native_window_manager_set_will_close_hook =
4378+
_native_window_manager_set_will_close_hookPtr
4379+
.asFunction<
4380+
void Function(
4381+
native_window_manager_set_will_close_hook_callback_t,
4382+
ffi.Pointer<ffi.Void>,
4383+
)
4384+
>();
4385+
4386+
bool native_window_manager_has_will_close_hook() {
4387+
return _native_window_manager_has_will_close_hook();
4388+
}
4389+
4390+
late final _native_window_manager_has_will_close_hookPtr =
4391+
_lookup<ffi.NativeFunction<ffi.Bool Function()>>(
4392+
'native_window_manager_has_will_close_hook',
4393+
);
4394+
late final _native_window_manager_has_will_close_hook =
4395+
_native_window_manager_has_will_close_hookPtr
4396+
.asFunction<bool Function()>();
4397+
4398+
void native_window_manager_handle_will_close(int id) {
4399+
return _native_window_manager_handle_will_close(id);
4400+
}
4401+
4402+
late final _native_window_manager_handle_will_closePtr =
4403+
_lookup<ffi.NativeFunction<ffi.Void Function(native_window_id_t)>>(
4404+
'native_window_manager_handle_will_close',
4405+
);
4406+
late final _native_window_manager_handle_will_close =
4407+
_native_window_manager_handle_will_closePtr
4408+
.asFunction<void Function(int)>();
4409+
4410+
bool native_window_manager_call_original_close(int id) {
4411+
return _native_window_manager_call_original_close(id);
4412+
}
4413+
4414+
late final _native_window_manager_call_original_closePtr =
4415+
_lookup<ffi.NativeFunction<ffi.Bool Function(native_window_id_t)>>(
4416+
'native_window_manager_call_original_close',
4417+
);
4418+
late final _native_window_manager_call_original_close =
4419+
_native_window_manager_call_original_closePtr
4420+
.asFunction<bool Function(int)>();
4421+
43614422
/// Registers @p callback for every WindowEvent this WindowManager emits.
43624423
/// @return the listener id, or NATIVE_INVALID_LISTENER_ID on failure.
43634424
int native_window_manager_add_listener(
@@ -5399,6 +5460,16 @@ typedef native_window_manager_set_will_hide_hook_callback_tFunction =
53995460
ffi.Void Function(ffi.UnsignedInt arg0, ffi.Pointer<ffi.Void> user_data);
54005461
typedef Dartnative_window_manager_set_will_hide_hook_callback_tFunction =
54015462
void Function(int arg0, ffi.Pointer<ffi.Void> user_data);
5463+
typedef native_window_manager_set_will_close_hook_callback_t =
5464+
ffi.Pointer<
5465+
ffi.NativeFunction<
5466+
native_window_manager_set_will_close_hook_callback_tFunction
5467+
>
5468+
>;
5469+
typedef native_window_manager_set_will_close_hook_callback_tFunction =
5470+
ffi.Void Function(ffi.UnsignedInt arg0, ffi.Pointer<ffi.Void> user_data);
5471+
typedef Dartnative_window_manager_set_will_close_hook_callback_tFunction =
5472+
void Function(int arg0, ffi.Pointer<ffi.Void> user_data);
54025473
typedef native_window_event_callback_t =
54035474
ffi.Pointer<ffi.NativeFunction<native_window_event_callback_tFunction>>;
54045475
typedef native_window_event_callback_tFunction =

packages/nativeapi/lib/src/window_manager.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ class WindowManager {
6969
_bindings.native_window_manager_set_will_hide_hook(hookCallable?.nativeFunction ?? ffi.nullptr, ffi.nullptr);
7070
}
7171

72+
void setWillCloseHook(void Function(int)? hook) {
73+
final hookCallable = hook == null ? null : ffi.NativeCallable<
74+
ffi.Void Function(ffi.UnsignedInt, ffi.Pointer<ffi.Void>)>.isolateLocal(
75+
(int arg0, ffi.Pointer<ffi.Void> _) {
76+
hook(arg0);
77+
},
78+
);
79+
if (hookCallable != null) _listeners.add(hookCallable);
80+
_bindings.native_window_manager_set_will_close_hook(hookCallable?.nativeFunction ?? ffi.nullptr, ffi.nullptr);
81+
}
82+
7283
bool hasWillShowHook() {
7384
return _bindings.native_window_manager_has_will_show_hook();
7485
}
@@ -77,6 +88,10 @@ class WindowManager {
7788
return _bindings.native_window_manager_has_will_hide_hook();
7889
}
7990

91+
bool hasWillCloseHook() {
92+
return _bindings.native_window_manager_has_will_close_hook();
93+
}
94+
8095
void handleWillShow(WindowId id) {
8196
_bindings.native_window_manager_handle_will_show(id);
8297
}
@@ -85,6 +100,10 @@ class WindowManager {
85100
_bindings.native_window_manager_handle_will_hide(id);
86101
}
87102

103+
void handleWillClose(WindowId id) {
104+
_bindings.native_window_manager_handle_will_close(id);
105+
}
106+
88107
bool callOriginalShow(WindowId id) {
89108
return _bindings.native_window_manager_call_original_show(id);
90109
}
@@ -93,6 +112,10 @@ class WindowManager {
93112
return _bindings.native_window_manager_call_original_hide(id);
94113
}
95114

115+
bool callOriginalClose(WindowId id) {
116+
return _bindings.native_window_manager_call_original_close(id);
117+
}
118+
96119
/// Registers [callback] for every `WindowEvent` this `WindowManager` emits.
97120
///
98121
/// The callback runs synchronously on whichever thread the native side

0 commit comments

Comments
 (0)