Skip to content

Commit 3e9437d

Browse files
committed
Add window-relative positioning strategy support
Introduces native_positioning_strategy_relative_to_window to bindings and updates PositioningStrategy to use this function when a window is provided. This enables positioning strategies that are relative to a window's bounds, improving flexibility for menu and UI placement.
1 parent 47f6c2e commit 3e9437d

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

packages/cnativeapi/lib/src/bindings_generated.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,46 @@ class CNativeApiBindings {
15401540
)
15411541
>();
15421542

1543+
/// Create a positioning strategy for positioning relative to a window
1544+
/// @param window Window to position relative to
1545+
/// @param offset Offset point to apply to the position, or NULL for no offset
1546+
/// @return Positioning strategy handle, or NULL if window is invalid
1547+
///
1548+
/// This function obtains the window's bounds using native_window_get_bounds()
1549+
/// and creates a Relative positioning strategy based on those bounds.
1550+
///
1551+
/// @example
1552+
/// ```c
1553+
/// native_window_t window = native_window_manager_create(&options);
1554+
/// native_point_t offset = {0, 10};
1555+
/// native_positioning_strategy_t strategy = native_positioning_strategy_relative_to_window(window,
1556+
/// &offset); native_menu_open(menu, strategy); native_positioning_strategy_free(strategy);
1557+
/// ```
1558+
native_positioning_strategy_t native_positioning_strategy_relative_to_window(
1559+
native_window_t window,
1560+
ffi.Pointer<native_point_t> offset,
1561+
) {
1562+
return _native_positioning_strategy_relative_to_window(window, offset);
1563+
}
1564+
1565+
late final _native_positioning_strategy_relative_to_windowPtr =
1566+
_lookup<
1567+
ffi.NativeFunction<
1568+
native_positioning_strategy_t Function(
1569+
native_window_t,
1570+
ffi.Pointer<native_point_t>,
1571+
)
1572+
>
1573+
>('native_positioning_strategy_relative_to_window');
1574+
late final _native_positioning_strategy_relative_to_window =
1575+
_native_positioning_strategy_relative_to_windowPtr
1576+
.asFunction<
1577+
native_positioning_strategy_t Function(
1578+
native_window_t,
1579+
ffi.Pointer<native_point_t>,
1580+
)
1581+
>();
1582+
15431583
/// Free a positioning strategy handle
15441584
/// @param strategy The positioning strategy to free
15451585
void native_positioning_strategy_free(

packages/nativeapi/lib/src/positioning_strategy.dart

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class PositioningStrategy with CNativeApiBindingsMixin {
152152
/// window's current bounds (obtained dynamically).
153153
Rect? get relativeRectangle {
154154
if (_relativeWindow != null) {
155-
return _relativeWindow!.bounds;
155+
return _relativeWindow.bounds;
156156
}
157157
return _relativeRectangle;
158158
}
@@ -195,7 +195,30 @@ class PositioningStrategy with CNativeApiBindingsMixin {
195195
return bindings.native_positioning_strategy_cursor_position();
196196

197197
case PositioningStrategyType.relative:
198-
final rect = relativeRectangle; // This will get window bounds if needed
198+
// If this strategy was created with a Window, use the window-specific native function
199+
if (_relativeWindow != null) {
200+
final offset = _relativeOffset;
201+
final offsetPtr = offset != null
202+
? (ffi.calloc<native_point_t>()
203+
..ref.x = offset.dx
204+
..ref.y = offset.dy)
205+
: nullptr;
206+
207+
final strategy = bindings
208+
.native_positioning_strategy_relative_to_window(
209+
_relativeWindow.nativeHandle,
210+
offsetPtr.cast<native_point_t>(),
211+
);
212+
213+
if (offsetPtr != nullptr) {
214+
ffi.calloc.free(offsetPtr);
215+
}
216+
217+
return strategy;
218+
}
219+
220+
// Otherwise, use the rectangle-based positioning
221+
final rect = relativeRectangle;
199222
if (rect == null) {
200223
throw StateError(
201224
'Relative rectangle is required for relative strategy',

0 commit comments

Comments
 (0)