Skip to main content

MenuAcceleratorLabel

Struct MenuAcceleratorLabel 

Source
pub struct MenuAcceleratorLabel {
    pub key: Option<KeyRef>,
    pub label: String,
    pub builder: MenuAcceleratorChildBuilder,
}
Expand description

A widget that draws the label text for a menu item (typically a MenuItemButton or SubmenuButton) and renders its child with information about the currently active keyboard accelerator.

On platforms other than macOS and iOS, this widget listens for the Alt key to be pressed, and when it is down, will update the label by calling the builder again with the position of the accelerator in the label string. While the Alt key is pressed, it registers a shortcut with the ShortcutRegistry mapped to a VoidCallbackIntent containing the callback defined by the nearest MenuAcceleratorCallbackBinding.

Because the accelerators are registered with the ShortcutRegistry, any other shortcuts in the widget tree between the primary focus and the ShortcutRegistry that define Alt-based shortcuts using the same keys will take precedence over the accelerators.

Because accelerators aren’t used on macOS and iOS, the label ignores the Alt key on those platforms, and the builder is always given -1 as an accelerator index. Accelerator labels are still stripped of their accelerator markers.

The built-in menu items MenuItemButton and SubmenuButton already provide the appropriate MenuAcceleratorCallbackBinding, so unless you are creating your own custom menu item type that takes a MenuAcceleratorLabel, it is not necessary to provide one.

Fields§

§key: Option<KeyRef>

See Widget::key.

§label: String

The label string that should be displayed.

The label string provides the label text, as well as the possible characters which could be used as accelerators in the menu system.

To indicate which letters in the label are to be used as accelerators, add an “&” character before the character in the string. If more than one character has an “&” in front of it, then the characters appearing earlier in the string are preferred. To represent a literal “&”, insert “&&” into the string. All other ampersands will be removed from the string before calling MenuAcceleratorLabel::builder. Bare ampersands at the end of the string or before whitespace are stripped and ignored.

See also:

  • display_label, which returns the label with all of the ampersands stripped out of it, and double ampersands converted to ampersands.
  • strip_accelerator_markers, which returns the supplied string with all of the ampersands stripped out of it, and double ampersands converted to ampersands, and optionally calls a callback with the index of the accelerator character found.
§builder: MenuAcceleratorChildBuilder

The optional MenuAcceleratorChildBuilder which is used to build the widget that displays the label itself.

The default_label_builder function serves as the default value for builder, rendering the label as a RichText widget with appropriate TextSpans for rendering the label with an underscore under the selected accelerator for the label when accelerators have been activated.

When writing the builder function, it’s not necessary to take the current platform into account. On platforms which don’t support accelerators (e.g. macOS and iOS), the passed accelerator index will always be -1, and the accelerator markers will already be stripped.

Implementations§

Source§

impl MenuAcceleratorLabel

Source

pub fn new(label: impl Into<String>) -> MenuAcceleratorLabel

Creates a const MenuAcceleratorLabel.

The label parameter is required.

Source

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

Dart MenuAcceleratorLabel(key:).

Source

pub fn builder( self, builder: impl Fn(&mut App, BuildContext, &str, isize) -> WidgetRef + 'static, ) -> MenuAcceleratorLabel

Dart MenuAcceleratorLabel(builder:).

Source

pub fn display_label(&self) -> String

Returns the label with any accelerator markers removed.

This getter just calls strip_accelerator_markers with the label.

Source

pub fn has_accelerator(&self) -> bool

Whether label contains an accelerator definition.

Dart’s RegExp(r'&(?!([&\s]|$))'): an ampersand followed by a character that is neither an ampersand nor whitespace.

Source

pub fn default_label_builder( app: &mut App, context: BuildContext, label: &str, index: isize, ) -> WidgetRef

Serves as the default value for builder, rendering the label as a RichText widget with appropriate TextSpans for rendering the label with an underscore under the selected accelerator for the label when the index is non-negative, and a Text widget when the index is negative.

Source

pub fn strip_accelerator_markers( label: &str, set_index: Option<&mut dyn FnMut(isize)>, ) -> String

Strips out any accelerator markers from the given label, and unescapes any escaped ampersands.

If set_index is supplied, it will be called before this function returns with the index in the returned string of the accelerator character.

See label for the markers.

Trait Implementations§

Source§

impl Debug for MenuAcceleratorLabel

Source§

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

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

impl StatefulWidget for MenuAcceleratorLabel

Source§

type State = MenuAcceleratorLabelState

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) -> MenuAcceleratorLabelState

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 + 'static)>

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 MenuAcceleratorLabel

§

impl !Send for MenuAcceleratorLabel

§

impl !Sync for MenuAcceleratorLabel

§

impl !UnwindSafe for MenuAcceleratorLabel

§

impl Forget for MenuAcceleratorLabel

§

impl Freeze for MenuAcceleratorLabel
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze, *const RcInner<dyn Fn(&mut App, AnyElement, &str, isize) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, &str, isize) -> Rc<dyn Widget>>>: Freeze,

§

impl Move for MenuAcceleratorLabel

§

impl Unpin for MenuAcceleratorLabel
where Rc<dyn Key>: Unpin, *const u8 is !null: Unpin, Rc<dyn Fn(&mut App, AnyElement, &str, isize) -> Rc<dyn Widget>>: Unpin,

§

impl UnsafeUnpin for MenuAcceleratorLabel
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement, &str, isize) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, &str, isize) -> Rc<dyn Widget>>>: 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>