Skip to main content

RawMenuAnchor

Struct RawMenuAnchor 

Source
pub struct RawMenuAnchor {
    pub key: Option<KeyRef>,
    pub on_open: Option<VoidCallback>,
    pub on_close: Option<VoidCallback>,
    pub on_open_requested: RawMenuAnchorOpenRequestedCallback,
    pub on_close_requested: RawMenuAnchorCloseRequestedCallback,
    pub builder: Option<RawMenuAnchorChildBuilder>,
    pub child: Option<WidgetRef>,
    pub overlay_builder: RawMenuAnchorOverlayBuilder,
    pub use_root_overlay: bool,
    pub child_focus_node: Option<FocusNodeRef>,
    pub consume_outside_taps: bool,
    pub controller: MenuControllerRef,
}
Expand description

A widget that wraps a child and anchors a floating menu.

The child can be any widget, but is typically a button, a text field, or, in the case of context menus, the entire screen.

The menu overlay of a RawMenuAnchor is shown by calling MenuController::open on an attached MenuController.

When a RawMenuAnchor is opened, overlay_builder is called to construct the menu contents within an Overlay. The Overlay allows the menu to “float” on top of other widgets. The info argument passed to overlay_builder provides the anchor’s Rect, the Size of the overlay, the TapRegion::group_id used by members of the menu system, and the position argument passed to MenuController::open.

If MenuController::open is called with a position argument, it will be passed to the info argument of the overlay_builder function.

The RawMenuAnchor does not manage semantics and focus of the menu.

§Adding animations to menus

A RawMenuAnchor has no knowledge of animations, as evident from its APIs, which don’t involve AnimationController at all. It only knows whether the overlay is shown or hidden.

If another widget intends to implement a menu with opening and closing transitions, RawMenuAnchor’s overlay should remain visible throughout both the opening and closing animation durations.

This means that the show_overlay callback passed to on_open_requested should be called before the first frame of the opening animation. Conversely, hide_overlay within on_close_requested should only be called after the closing animation has completed.

This also means that, if MenuController::open is called while the overlay is already visible, RawMenuAnchor has no way of knowing whether the menu is currently opening, closing, or stably displayed. The parent widget will need to manage additional information (such as the state of an AnimationController) to determine how to respond in such scenarios.

To programmatically control a RawMenuAnchor, like opening or closing it, or checking its state, you can get its associated MenuController. Use MenuController::maybe_of to retrieve the controller for the closest RawMenuAnchor ancestor of a given BuildContext. More detailed usage of MenuController is available in its type documentation.

Fields§

§key: Option<KeyRef>§on_open: Option<VoidCallback>

Called when the menu overlay is shown.

When MenuController::open is called, on_open_requested is invoked with a show_overlay callback that, when called, shows the menu overlay and triggers on_open.

The default implementation of on_open_requested calls show_overlay synchronously, thereby calling on_open synchronously. In this case, on_open is called regardless of whether the menu overlay is already showing.

Custom implementations of on_open_requested can delay the call to show_overlay, or not call it at all, in which case on_open will not be called. Calling show_overlay after disposal is a no-op, and will not trigger on_open.

A typical usage is to respond when the menu first becomes interactive, such as by setting focus to a menu item.

§on_close: Option<VoidCallback>

Called when the menu overlay is hidden.

When MenuController::close is called, on_close_requested is invoked with a hide_overlay callback that, when called, hides the menu overlay and triggers on_close.

The default implementation of on_close_requested calls hide_overlay synchronously, thereby calling on_close synchronously. In this case, on_close is called regardless of whether the menu overlay is already hidden.

Custom implementations of on_close_requested can delay the call to hide_overlay or not call it at all, in which case on_close will not be called. Calling hide_overlay after disposal is a no-op, and will not trigger on_close.

§on_open_requested: RawMenuAnchorOpenRequestedCallback

Called when a request is made to open the menu.

This callback is triggered every time MenuController::open is called, even when the menu overlay is already showing. As a result, this callback is a good place to begin menu opening animations, or observe when a menu is repositioned.

After an open request is intercepted, the show_overlay callback should be called when the menu overlay (the widget built by overlay_builder) is ready to be shown. This can occur immediately (the default behavior), or after a delay. Calling show_overlay sets MenuController::is_open to true, builds (or rebuilds) the overlay widget, and shows the menu overlay at the front of the overlay stack.

If show_overlay is not called, the menu will stay hidden. Calling show_overlay after disposal is a no-op, meaning it will not trigger on_open or show the menu overlay.

If a RawMenuAnchor is used in a themed menu that plays an opening animation, the themed menu should show the overlay before starting the opening animation, since the animation plays on the overlay itself.

The position argument is the position that MenuController::open was called with.

A typical on_open_requested consists of the following steps:

  1. Optional delay.
  2. Call show_overlay (whose call chain eventually invokes on_open).
  3. Optionally start the opening animation.

Defaults to a callback that immediately shows the menu.

§on_close_requested: RawMenuAnchorCloseRequestedCallback

Called when a request is made to close the menu.

This callback can be used to add a delay or a closing animation before the menu is hidden.

This callback is triggered every time MenuController::close is called, even when the menu overlay is already hidden.

This callback is also triggered when a sibling RawMenuAnchor is opened. In this case, the callback can be used to add a delay or a closing animation while the sibling menu opens. When implementing this behavior, consider disabling interactions so that the closing menu does not interfere with the opening sibling menu. Also consider disabling semantics, focus, and hit testing for the closing menu for the duration of the closing animation.

Pending timers or animations started in a previous call to on_close_requested should be canceled when this callback is triggered to prevent them from closing the menu at an unintended time.

If the menu is not closed, this callback will also be called when the root menu anchor is scrolled and when the screen is resized.

After a close request is intercepted and closing behaviors have completed, the hide_overlay callback should be called. This callback sets MenuController::is_open to false and hides the menu overlay widget. If the RawMenuAnchor is used in a themed menu that plays a closing animation, hide_overlay should be called after the closing animation has ended, since the animation plays on the overlay itself. This means that MenuController::is_open will stay true while closing animations are running.

Calling hide_overlay after disposal is a no-op, meaning it will not trigger on_close or hide the menu overlay.

Typically, on_close_requested consists of the following steps:

  1. Optionally start the closing animation and wait for it to complete.
  2. Call hide_overlay (whose call chain eventually invokes on_close).

Throughout the closing sequence, menus should typically not be focusable or interactive.

Defaults to a callback that immediately hides the menu.

§builder: Option<RawMenuAnchorChildBuilder>

A builder that builds the widget that this RawMenuAnchor surrounds.

Typically, this is a button used to open the menu by calling MenuController::open on the controller passed to the builder.

If not supplied, then the RawMenuAnchor will be the size that its parent allocates for it.

§child: Option<WidgetRef>

The optional child to be passed to the builder.

Supply this child if there is a portion of the widget tree built in builder that doesn’t depend on the controller or context supplied to the builder. It will be more efficient, since the framework doesn’t then need to rebuild this child when those change.

§overlay_builder: RawMenuAnchorOverlayBuilder

Called to build and position the menu overlay.

The overlay_builder function is passed a RawMenuOverlayInfo object that defines the anchor’s Rect, the Size of the overlay, the TapRegion::group_id for the menu system, and the position Offset passed to MenuController::open.

To ensure taps are properly consumed, the RawMenuOverlayInfo::tap_region_group_id should be passed to a TapRegion widget that wraps the menu panel.

§use_root_overlay: bool

Whether the menu panel should be rendered in the root Overlay.

When true, the menu is mounted in the root overlay. Rendering the menu in the root overlay prevents the menu from being obscured by other widgets.

When false, the menu is rendered in the nearest ancestor Overlay.

Submenus will always use the same overlay as their top-level ancestor, so setting a use_root_overlay value on a submenu will have no effect.

Defaults to false on overlay menus.

§child_focus_node: Option<FocusNodeRef>

The focus node attached to the widget that takes focus when the menu is opened or closed.

If not supplied, the anchor will not retain focus when the menu is opened.

§consume_outside_taps: bool

Whether a tap event that closes the menu will be permitted to continue on to the gesture arena.

If false, then tapping outside of a menu when the menu is open will both close the menu, and allow the tap to participate in the gesture arena.

If true, then it will only close the menu, and the tap event will be consumed.

Defaults to false.

§controller: MenuControllerRef

A MenuController that allows opening and closing of the menu from other widgets.

Implementations§

Source§

impl RawMenuAnchor

Source

pub fn new( controller: MenuControllerRef, overlay_builder: impl Fn(&mut App, BuildContext, RawMenuOverlayInfo) -> WidgetRef + 'static, ) -> RawMenuAnchor

A RawMenuAnchor that delegates overlay construction to an overlay_builder; Dart’s optional named arguments are the setters.

Source

pub fn key(self, key: KeyRef) -> RawMenuAnchor

Dart RawMenuAnchor(key:).

Source

pub fn child_focus_node(self, child_focus_node: FocusNodeRef) -> RawMenuAnchor

Dart RawMenuAnchor(childFocusNode:).

Source

pub fn consume_outside_taps(self, consume_outside_taps: bool) -> RawMenuAnchor

Dart RawMenuAnchor(consumeOutsideTaps:).

Source

pub fn on_open(self, on_open: VoidCallback) -> RawMenuAnchor

Dart RawMenuAnchor(onOpen:).

Source

pub fn on_close(self, on_close: VoidCallback) -> RawMenuAnchor

Dart RawMenuAnchor(onClose:).

Source

pub fn on_open_requested( self, on_open_requested: RawMenuAnchorOpenRequestedCallback, ) -> RawMenuAnchor

Dart RawMenuAnchor(onOpenRequested:).

Source

pub fn on_close_requested( self, on_close_requested: RawMenuAnchorCloseRequestedCallback, ) -> RawMenuAnchor

Dart RawMenuAnchor(onCloseRequested:).

Source

pub fn use_root_overlay(self, use_root_overlay: bool) -> RawMenuAnchor

Dart RawMenuAnchor(useRootOverlay:).

Source

pub fn builder( self, builder: impl Fn(&mut App, BuildContext, AnyMenuController, Option<&WidgetRef>) -> WidgetRef + 'static, ) -> RawMenuAnchor

Dart RawMenuAnchor(builder:).

Source

pub fn child<K>(self, child: impl IntoWidget<K>) -> RawMenuAnchor

Dart RawMenuAnchor(child:).

Trait Implementations§

Source§

impl Debug for RawMenuAnchor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl StatefulWidget for RawMenuAnchor

Source§

type State = RawMenuAnchorState

The state this widget creates.
Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s debugFillProperties: the properties a dump shows for this widget, in Dart’s order. Dart’s Widget base only sets the dense tree style, which the framework does.
Source§

fn create_state(&self) -> RawMenuAnchorState

Creates the mutable state for this widget at a given location in the tree. Read more
Source§

fn debug_check_constructor(&self)

Source§

fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>

Source§

fn type_name(&self) -> &'static str

See Widget::type_name. A stateful form another widget kind becomes (an animated or status transition widget) names the widget it wraps.

Auto Trait Implementations§

§

impl !RefUnwindSafe for RawMenuAnchor

§

impl !Send for RawMenuAnchor

§

impl !Sync for RawMenuAnchor

§

impl !UnwindSafe for RawMenuAnchor

§

impl Forget for RawMenuAnchor

§

impl Freeze for RawMenuAnchor
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const RcInner<dyn Fn(&mut App)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App)>>: Freeze, *const RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)>>: Freeze, *const RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)>>: Freeze, *const RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze, *const RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>>>: Freeze, NonZero<u32>: Freeze, &'static FocusNodeVTable: Freeze, *const RcInner<Keep> is !null: Freeze, PhantomData<RcInner<Keep>>: Freeze, &'static MenuControllerVTable: Freeze,

§

impl Move for RawMenuAnchor

§

impl Unpin for RawMenuAnchor
where Rc<dyn Key>: Unpin, Rc<dyn Fn(&mut App)>: Unpin, Rc<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)>: Unpin, Rc<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)>: Unpin, Rc<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>: Unpin, Rc<dyn Widget>: Unpin, Rc<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>>: Unpin, NonZero<u32>: Unpin, &'static FocusNodeVTable: Unpin, Rc<Keep>: Unpin, &'static MenuControllerVTable: Unpin,

§

impl UnsafeUnpin for RawMenuAnchor
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App)>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, Option<Offset>, Rc<dyn Fn(&mut App)>)>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, Rc<dyn Fn(&mut App)>)>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, AnyMenuController, Option<&Rc<dyn Widget>>) -> Rc<dyn Widget>>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Widget>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, RawMenuOverlayInfo) -> Rc<dyn Widget>>>: UnsafeUnpin, NonZeroU32Inner: UnsafeUnpin, &'static FocusNodeVTable: UnsafeUnpin, *const RcInner<Keep> is !null: UnsafeUnpin, PhantomData<RcInner<Keep>>: UnsafeUnpin, &'static MenuControllerVTable: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<W> IntoWidget<StatefulKind> for W
where W: StatefulWidget,

Source§

fn into_widget(self) -> Rc<dyn Widget>

The widget as a tree node, made where this is called: its creation_location.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>