Skip to content

Commit 536aabd

Browse files
committed
Refactor menu item API to use nullable setters
Removed explicit remove methods for menu item accelerators and submenus in favor of passing NULL to setter functions. Updated documentation and usage to reflect this change, simplifying the API and improving consistency.
1 parent 149388f commit 536aabd

3 files changed

Lines changed: 9 additions & 46 deletions

File tree

packages/cnativeapi/lib/src/bindings_generated.dart

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ class CNativeApiBindings {
836836

837837
/// Set the keyboard accelerator for a menu item
838838
/// @param item The menu item
839-
/// @param accelerator The keyboard accelerator to set
839+
/// @param accelerator The keyboard accelerator to set, or NULL to remove the accelerator
840840
void native_menu_item_set_accelerator(
841841
native_menu_item_t item,
842842
ffi.Pointer<native_keyboard_accelerator_t> accelerator,
@@ -891,20 +891,6 @@ class CNativeApiBindings {
891891
)
892892
>();
893893

894-
/// Remove the keyboard accelerator from a menu item
895-
/// @param item The menu item
896-
void native_menu_item_remove_accelerator(native_menu_item_t item) {
897-
return _native_menu_item_remove_accelerator(item);
898-
}
899-
900-
late final _native_menu_item_remove_acceleratorPtr =
901-
_lookup<ffi.NativeFunction<ffi.Void Function(native_menu_item_t)>>(
902-
'native_menu_item_remove_accelerator',
903-
);
904-
late final _native_menu_item_remove_accelerator =
905-
_native_menu_item_remove_acceleratorPtr
906-
.asFunction<void Function(native_menu_item_t)>();
907-
908894
/// Set the enabled state of a menu item
909895
/// @param item The menu item
910896
/// @param enabled true to enable, false to disable
@@ -1000,7 +986,7 @@ class CNativeApiBindings {
1000986

1001987
/// Set the submenu for a menu item
1002988
/// @param item The menu item
1003-
/// @param submenu The submenu to attach
989+
/// @param submenu The submenu to attach, or NULL to remove the submenu
1004990
void native_menu_item_set_submenu(
1005991
native_menu_item_t item,
1006992
native_menu_t submenu,
@@ -1029,20 +1015,6 @@ class CNativeApiBindings {
10291015
late final _native_menu_item_get_submenu = _native_menu_item_get_submenuPtr
10301016
.asFunction<native_menu_t Function(native_menu_item_t)>();
10311017

1032-
/// Remove the submenu from a menu item
1033-
/// @param item The menu item
1034-
void native_menu_item_remove_submenu(native_menu_item_t item) {
1035-
return _native_menu_item_remove_submenu(item);
1036-
}
1037-
1038-
late final _native_menu_item_remove_submenuPtr =
1039-
_lookup<ffi.NativeFunction<ffi.Void Function(native_menu_item_t)>>(
1040-
'native_menu_item_remove_submenu',
1041-
);
1042-
late final _native_menu_item_remove_submenu =
1043-
_native_menu_item_remove_submenuPtr
1044-
.asFunction<void Function(native_menu_item_t)>();
1045-
10461018
/// Add event listener for a menu item
10471019
/// @param item The menu item
10481020
/// @param event_type The type of event to listen for

packages/nativeapi/lib/src/menu.dart

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,15 @@ class MenuItem
8888
static bool _callbacksInitialized = false;
8989

9090
late final native_menu_item_t _nativeHandle;
91-
91+
9292
// Store listener IDs for cleanup
9393
int? _clickedListenerId;
9494
int? _submenuOpenedListenerId;
9595
int? _submenuClosedListenerId;
9696

9797
MenuItem(String label, [MenuItemType type = MenuItemType.normal]) {
9898
final labelPtr = label.toNativeUtf8().cast<Char>();
99-
_nativeHandle = bindings.native_menu_item_create(
100-
labelPtr,
101-
type.toNative(),
102-
);
99+
_nativeHandle = bindings.native_menu_item_create(labelPtr, type.toNative());
103100
ffi.calloc.free(labelPtr);
104101

105102
// Store instance in static map using handle address as key
@@ -292,7 +289,7 @@ class MenuItem
292289

293290
set submenu(Menu? menu) {
294291
if (menu == null) {
295-
bindings.native_menu_item_remove_submenu(_nativeHandle);
292+
bindings.native_menu_item_set_submenu(_nativeHandle, nullptr);
296293
} else {
297294
bindings.native_menu_item_set_submenu(_nativeHandle, menu.nativeHandle);
298295
}
@@ -329,7 +326,7 @@ class Menu
329326
_closedCallback;
330327

331328
static bool _callbacksInitialized = false;
332-
329+
333330
// Store listener IDs for cleanup
334331
int? _openedListenerId;
335332
int? _closedListenerId;
@@ -375,17 +372,11 @@ class Menu
375372
void stopEventListening() {
376373
// Remove native listeners using stored IDs
377374
if (_openedListenerId != null) {
378-
bindings.native_menu_remove_listener(
379-
_nativeHandle,
380-
_openedListenerId!,
381-
);
375+
bindings.native_menu_remove_listener(_nativeHandle, _openedListenerId!);
382376
_openedListenerId = null;
383377
}
384378
if (_closedListenerId != null) {
385-
bindings.native_menu_remove_listener(
386-
_nativeHandle,
387-
_closedListenerId!,
388-
);
379+
bindings.native_menu_remove_listener(_nativeHandle, _closedListenerId!);
389380
_closedListenerId = null;
390381
}
391382
}

0 commit comments

Comments
 (0)