Skip to main content

MouseRegion

Struct MouseRegion 

Source
pub struct MouseRegion {
    pub key: Option<KeyRef>,
    pub on_enter: Option<PointerEnterEventListener>,
    pub on_hover: Option<PointerHoverEventListener>,
    pub on_exit: Option<PointerExitEventListener>,
    pub cursor: MouseCursorRef,
    pub opaque: bool,
    pub hit_test_behavior: Option<HitTestBehavior>,
    pub child: Option<WidgetRef>,
}
Expand description

A widget that tracks the movement of mice.

MouseRegion is used when it is needed to compare the list of objects that a mouse pointer is hovering over between this frame and the last frame. This means entering events, exiting events, and mouse cursors.

To listen to general pointer events, use Listener, or more preferably, GestureDetector.

§Layout behavior

See BoxConstraints for an introduction to box layout models.

If it has a child, this widget defers to the child for sizing behavior. If it does not have a child, it grows to fit the parent instead.

See also:

  • Listener, a similar widget that tracks pointer events when the pointer has buttons pressed.

By default, all callbacks are empty, cursor is MouseCursor.defer, and opaque is true.

Fields§

§key: Option<KeyRef>§on_enter: Option<PointerEnterEventListener>

Triggered when a mouse pointer has entered this widget.

This callback is triggered when the pointer, with or without buttons pressed, has started to be contained by the region of this widget. More specifically, the callback is triggered by the following cases:

  • This widget has appeared under a pointer.
  • This widget has moved to under a pointer.
  • A new pointer has been added to somewhere within this widget.
  • An existing pointer has moved into this widget.

This callback is not always matched by an on_exit. If the MouseRegion is unmounted while being hovered by a pointer, the on_exit of the widget callback will never called. For more details, see on_exit.

The time that this callback is triggered is always between frames: either during the post-frame callbacks, or during the callback of a pointer event.

See also:

  • on_exit, which is triggered when a mouse pointer exits the region.
  • MouseTrackerAnnotation.onEnter, which is how this callback is internally implemented.
§on_hover: Option<PointerHoverEventListener>

Triggered when a pointer moves into a position within this widget without buttons pressed.

Usually this is only fired for pointers which report their location when not down (e.g. mouse pointers). Certain devices also fire this event on single taps in accessibility mode.

This callback is not triggered by the movement of the widget.

The time that this callback is triggered is during the callback of a pointer event, which is always between frames.

See also:

§on_exit: Option<PointerExitEventListener>

Triggered when a mouse pointer has exited this widget when the widget is still mounted.

This callback is triggered when the pointer, with or without buttons pressed, has stopped being contained by the region of this widget, except when the exit is caused by the disappearance of this widget. More specifically, this callback is triggered by the following cases:

  • A pointer that is hovering this widget has moved away.
  • A pointer that is hovering this widget has been removed.
  • This widget, which is being hovered by a pointer, has moved away.

And is not triggered by the following case:

  • This widget, which is being hovered by a pointer, has disappeared.

This means that a MouseRegion::on_exit might not be matched by a MouseRegion::on_enter.

This restriction aims to prevent a common misuse: if State::set_state is called during MouseRegion::on_exit without checking whether the widget is still mounted, an exception will occur. This is because the callback is triggered during the post-frame phase, at which point the widget has been unmounted. Since State::set_state is exclusive to widgets, the restriction is specific to MouseRegion, and does not apply to its lower-level counterparts, RenderMouseRegion and MouseTrackerAnnotation.

There are a few ways to mitigate this restriction:

  • If the hover state is completely contained within a widget that unconditionally creates this MouseRegion, then this will not be a concern, since after the MouseRegion is unmounted the state is no longer used.
  • Otherwise, the outer widget very likely has access to the variable that controls whether this MouseRegion is present. If so, call on_exit at the event that turns the condition from true to false.
  • In cases where the solutions above won’t work, you can always override State::dispose and call on_exit, or create your own widget using RenderMouseRegion.

See also:

  • on_enter, which is triggered when a mouse pointer enters the region.
  • RenderMouseRegion and MouseTrackerAnnotation.onExit, which are how this callback is internally implemented, but without the restriction.
§cursor: MouseCursorRef

The mouse cursor for mouse pointers that are hovering over the region.

When a mouse enters the region, its cursor will be changed to the cursor. When the mouse leaves the region, the cursor will be decided by the region found at the new location.

The cursor defaults to MouseCursor.defer, deferring the choice of cursor to the next region behind it in hit-test order.

§opaque: bool

Whether this widget should prevent other MouseRegions visually behind it from detecting the pointer.

This changes the list of regions that a pointer hovers, thus affecting how their on_hover, on_enter, on_exit, and cursor behave.

If opaque is true, this widget will absorb the mouse pointer and prevent this widget’s siblings (or any other widgets that are not ancestors or descendants of this widget) from detecting the mouse pointer even when the pointer is within their areas.

If opaque is false, this object will not affect how MouseRegions behind it behave, which will detect the mouse pointer as long as the pointer is within their areas.

This defaults to true.

§hit_test_behavior: Option<HitTestBehavior>

How to behave during hit testing.

This defaults to HitTestBehavior::Opaque if None.

§child: Option<WidgetRef>

Implementations§

Source§

impl MouseRegion

Source

pub fn new() -> MouseRegion

Creates a MouseRegion; Dart’s named arguments are the setters.

Source

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

Dart MouseRegion(key:).

Source

pub fn on_enter(self, on_enter: PointerEnterEventListener) -> MouseRegion

Dart MouseRegion(on_enter:).

Source

pub fn on_hover(self, on_hover: PointerHoverEventListener) -> MouseRegion

Dart MouseRegion(on_hover:).

Source

pub fn on_exit(self, on_exit: PointerExitEventListener) -> MouseRegion

Dart MouseRegion(on_exit:).

Source

pub fn cursor(self, cursor: MouseCursorRef) -> MouseRegion

Dart MouseRegion(cursor:).

Source

pub fn opaque(self, opaque: bool) -> MouseRegion

Dart MouseRegion(opaque:).

Source

pub fn hit_test_behavior( self, hit_test_behavior: HitTestBehavior, ) -> MouseRegion

Dart MouseRegion(hit_test_behavior:).

Source

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

Dart MouseRegion(child:).

Trait Implementations§

Source§

impl Debug for MouseRegion

Source§

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

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

impl Default for MouseRegion

Source§

fn default() -> MouseRegion

Returns the “default value” for a type. Read more
Source§

impl RenderObjectWidget for MouseRegion

Source§

type RenderObject = RenderMouseRegion

The render object this widget creates; Dart’s covariant RenderObject parameter type.
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_render_object( &self, app: &mut App, _context: BuildContext, ) -> AnyRenderObject

Creates an instance of the RenderObject class that this widget represents, using the configuration described by this widget. Read more
Source§

fn update_render_object( &self, app: &mut App, _context: BuildContext, render_object: RenderHandle<RenderMouseRegion>, )

Copies the configuration described by this widget to the given render object, which will be of the same type as returned by this object’s create_render_object. Read more
Source§

fn to_string_short(&self) -> String

Dart’s toStringShort, for a widget that names itself apart from its type, as SizedBox.expand does; see Widget::to_string_short.
Source§

fn debug_check_constructor(&self)

Source§

fn debug_visit_onstage_children( &self, app: &App, children: &[AnyElement], visitor: &mut dyn FnMut(AnyElement), )

Dart’s debugVisitOnstageChildren of the element this widget makes, for a widget whose Dart element subclass overrides it only to hide children (_OffstageElement, _IndexedStackElement): calls visitor for each of children, in order, that is onstage. Waft’s generic single- and multi-child elements stand in for those subclasses and ask their widget here; an element of a widget’s own overrides Element::debug_visit_onstage_children instead.
Source§

fn did_unmount_render_object( &self, app: &mut App, render_object: RenderHandle<Self::RenderObject>, )

A render object previously associated with this widget has been removed from the tree. The given RenderObject will be of the same type as returned by this object’s create_render_object.
Source§

impl SingleChildRenderObjectWidget for MouseRegion

Source§

fn child(&self) -> Option<&WidgetRef>

The widget below this widget in the tree.
Source§

fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement
where Self: Sized,

Creates the element that manages this widget’s child. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for MouseRegion

§

impl !Send for MouseRegion

§

impl !Sync for MouseRegion

§

impl !UnwindSafe for MouseRegion

§

impl Forget for MouseRegion

§

impl Freeze for MouseRegion
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const RcInner<dyn Fn(&mut App, PointerEnterEvent)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, PointerEnterEvent)>>: Freeze, *const RcInner<dyn Fn(&mut App, PointerHoverEvent)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, PointerHoverEvent)>>: Freeze, *const RcInner<dyn Fn(&mut App, PointerExitEvent)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, PointerExitEvent)>>: Freeze, *const RcInner<dyn MouseCursor> is !null: Freeze, PhantomData<RcInner<dyn MouseCursor>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze,

§

impl Move for MouseRegion

§

impl Unpin for MouseRegion
where Rc<dyn Key>: Unpin, Rc<dyn Fn(&mut App, PointerEnterEvent)>: Unpin, Rc<dyn Fn(&mut App, PointerHoverEvent)>: Unpin, Rc<dyn Fn(&mut App, PointerExitEvent)>: Unpin, Rc<dyn MouseCursor>: Unpin, Rc<dyn Widget>: Unpin,

§

impl UnsafeUnpin for MouseRegion
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, PointerEnterEvent)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, PointerEnterEvent)>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, PointerHoverEvent)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, PointerHoverEvent)>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, PointerExitEvent)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, PointerExitEvent)>>: UnsafeUnpin, *const RcInner<dyn MouseCursor> is !null: UnsafeUnpin, PhantomData<RcInner<dyn MouseCursor>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<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<SingleChildRenderObjectKind> for W

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>