Skip to content

Commit c0ce7ed

Browse files
authored
Allow running callbacks when an item's highlight status changes (#4)
* Adding support for running callbacks when an item receives or loses highlight * Setting min and max menu item id values to make win32 integration easier
1 parent fc96ca7 commit c0ce7ed

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

lib/src/menu_item.dart

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
import 'dart:math' as math;
2+
13
import 'menu.dart';
24

3-
int _lastAssignMenuItemId = 0;
5+
// Max value for a 16-bit unsigned integer. Chosen because it is the lowest
6+
// common denominator for menu item ids between Linux, Windows, and macOS.
7+
const int _maxMenuItemId = 65535;
8+
// Some parts of the win32 API pass data that is ambiguous about whether it is
9+
// the id of a menu item or its index in the menu. This sets a reasonable floor
10+
// to distinguish between the two by assuming that no menu will have more than
11+
// 1024 items in it.
12+
const int _minMenuItemId = 1024;
13+
int _nextMenuItemId = _minMenuItemId;
414

515
_generateMenuItemId() {
6-
_lastAssignMenuItemId++;
7-
return _lastAssignMenuItemId;
16+
final newId = _nextMenuItemId;
17+
_nextMenuItemId = math.max(
18+
_minMenuItemId,
19+
(_nextMenuItemId + 1) % _maxMenuItemId,
20+
);
21+
return newId;
822
}
923

1024
class MenuItem {
@@ -20,6 +34,8 @@ class MenuItem {
2034
Menu? submenu;
2135

2236
void Function(MenuItem menuItem)? onClick;
37+
void Function(MenuItem menuItem)? onHighlight;
38+
void Function(MenuItem menuItem)? onLoseHighlight;
2339

2440
MenuItem.separator()
2541
: id = _generateMenuItemId(),
@@ -35,6 +51,8 @@ class MenuItem {
3551
this.disabled = false,
3652
this.submenu,
3753
this.onClick,
54+
this.onHighlight,
55+
this.onLoseHighlight,
3856
}) : id = _generateMenuItemId(),
3957
type = 'submenu';
4058

@@ -47,6 +65,8 @@ class MenuItem {
4765
required this.checked,
4866
this.disabled = false,
4967
this.onClick,
68+
this.onHighlight,
69+
this.onLoseHighlight,
5070
}) : id = _generateMenuItemId(),
5171
type = 'checkbox';
5272

@@ -61,6 +81,8 @@ class MenuItem {
6181
this.disabled = false,
6282
this.submenu,
6383
this.onClick,
84+
this.onHighlight,
85+
this.onLoseHighlight,
6486
}) : id = _generateMenuItemId();
6587

6688
Map<String, dynamic> toJson() {

0 commit comments

Comments
 (0)