Struct cursive::views::OnEventView
source · [−]pub struct OnEventView<T> { /* private fields */ }
Expand description
A wrapper view that can react to events.
This view registers a set of callbacks tied to specific events, to be run in certain conditions.
-
Some callbacks are called only for events ignored by the wrapped view.
(those registered by
on_event
oron_event_inner
) -
Others are processed first, and can control whether the child view should be given the event (those registered by
on_pre_event
oron_pre_event_inner
).
“Inner” callbacks (on_event_inner
and on_pre_event_inner
) are given
a reference to the inner wrapped view (but not to the Cursive
root). They
can then return another callback, taking only a &mut Cursive
root as
argument.
“Simple” callbacks (on_event
and on_pre_event
) skip this first
phase and are only called with a &mut Cursive
.
Examples
let view = OnEventView::new(TextView::new("This view has an event!"))
.on_event('q', |s| s.quit())
.on_event(event::Key::Esc, |s| s.quit());
Implementations
sourceimpl<T> OnEventView<T>
impl<T> OnEventView<T>
sourcepub fn new(view: T) -> OnEventView<T>
pub fn new(view: T) -> OnEventView<T>
Wraps the given view in a new OnEventView.
sourcepub fn clear_event<E>(&mut self, event: E)where
E: Into<Event>,
pub fn clear_event<E>(&mut self, event: E)where
E: Into<Event>,
Remove all callbacks associated with the given event.
sourcepub fn on_event<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
pub fn on_event<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
Registers a callback when the given event is ignored by the child.
Chainable variant.
Examples
let view = OnEventView::new(DummyView)
.on_event('q', |s| s.quit())
.on_event(Key::Esc, |s| {
s.pop_layer();
})
.on_event(EventTrigger::mouse(), |s| {
s.add_layer(DummyView);
});
sourcepub fn on_pre_event<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
pub fn on_pre_event<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
Registers a callback when the given event is received.
The child will never receive this event.
Chainable variant.
sourcepub fn on_pre_event_inner<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
pub fn on_pre_event_inner<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
Registers a callback when the given event is received.
The given callback will be run before the child view sees the event.
- If the result is
None
, then the child view is given the event as usual. - Otherwise, it bypasses the child view and directly processes the result.
Chainable variant.
sourcepub fn on_event_inner<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
pub fn on_event_inner<F, E>(self, trigger: E, cb: F) -> OnEventView<T>where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
Registers a callback when the given event is ignored by the child.
This is an advanced method to get more control.
on_event
may be easier to use.
If the child view ignores the event, cb
will be called with the
child view as argument.
If the result is not None
, it will be processed as well.
Chainable variant.
Examples
let view = OnEventView::new(DummyView)
.on_event_inner(
EventTrigger::mouse(),
|d: &mut DummyView, e: &Event| {
if let &Event::Mouse { event: MouseEvent::Press(_), .. } = e {
// Do something on mouse press
Some(EventResult::with_cb(|s| {
s.pop_layer();
}))
} else {
// Otherwise, don't do anything
None
}
}
);
sourcepub fn set_on_event<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
pub fn set_on_event<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
Registers a callback when the given event is ignored by the child.
sourcepub fn set_on_pre_event<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
pub fn set_on_pre_event<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut Cursive),
Registers a callback when the given event is received.
The child will never receive this event.
sourcepub fn set_on_pre_event_inner<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
pub fn set_on_pre_event_inner<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
Registers a callback when the given event is received.
The given callback will be run before the child view sees the event.
- If the result is
None
, then the child view is given the event as usual. - Otherwise, it bypasses the child view and directly processes the result.
sourcepub fn set_on_event_inner<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
pub fn set_on_event_inner<F, E>(&mut self, trigger: E, cb: F)where
E: Into<EventTrigger>,
F: 'static + Fn(&mut T, &Event) -> Option<EventResult>,
Registers a callback when the given event is ignored by the child.
If the child view ignores the event, cb
will be called with the
child view as argument.
If the result is not None
, it will be processed as well.
sourcepub fn clear_callbacks(&mut self)
pub fn clear_callbacks(&mut self)
Remove any callbacks defined for this view.
sourcepub fn get_inner_mut(&mut self) -> &mut T
pub fn get_inner_mut(&mut self) -> &mut T
Gets mutable access to the inner view.
Trait Implementations
sourceimpl<T> Default for OnEventView<T>where
T: Default,
impl<T> Default for OnEventView<T>where
T: Default,
sourcefn default() -> OnEventView<T>
fn default() -> OnEventView<T>
Returns the “default value” for a type. Read more
sourceimpl<T> ViewWrapper for OnEventView<T>where
T: View,
impl<T> ViewWrapper for OnEventView<T>where
T: View,
type V = T
type V = T
Type that this view wraps.
sourcefn with_view<F, R>(&self, f: F) -> Option<R>where
F: FnOnce(&<OnEventView<T> as ViewWrapper>::V) -> R,
fn with_view<F, R>(&self, f: F) -> Option<R>where
F: FnOnce(&<OnEventView<T> as ViewWrapper>::V) -> R,
Runs a function on the inner view, returning the result. Read more
sourcefn with_view_mut<F, R>(&mut self, f: F) -> Option<R>where
F: FnOnce(&mut <OnEventView<T> as ViewWrapper>::V) -> R,
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>where
F: FnOnce(&mut <OnEventView<T> as ViewWrapper>::V) -> R,
Runs a function on the inner view, returning the result. Read more
sourcefn into_inner(
self
) -> Result<<OnEventView<T> as ViewWrapper>::V, OnEventView<T>>where
<OnEventView<T> as ViewWrapper>::V: Sized,
fn into_inner(
self
) -> Result<<OnEventView<T> as ViewWrapper>::V, OnEventView<T>>where
<OnEventView<T> as ViewWrapper>::V: Sized,
Attempts to retrieve the inner view.
sourcefn wrap_on_event(&mut self, event: Event) -> EventResult
fn wrap_on_event(&mut self, event: Event) -> EventResult
Wraps the on_event
method.
sourcefn wrap_layout(&mut self, size: XY<usize>)
fn wrap_layout(&mut self, size: XY<usize>)
Wraps the layout
method.
sourcefn wrap_take_focus(
&mut self,
source: Direction
) -> Result<EventResult, CannotFocus>
fn wrap_take_focus(
&mut self,
source: Direction
) -> Result<EventResult, CannotFocus>
Wraps the take_focus
method.
sourcefn wrap_call_on_any(
&mut self,
selector: &Selector<'_>,
callback: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)
)
fn wrap_call_on_any(
&mut self,
selector: &Selector<'_>,
callback: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)
)
Wraps the find
method.
sourcefn wrap_focus_view(
&mut self,
selector: &Selector<'_>
) -> Result<EventResult, ViewNotFound>
fn wrap_focus_view(
&mut self,
selector: &Selector<'_>
) -> Result<EventResult, ViewNotFound>
Wraps the focus_view
method.
sourcefn wrap_needs_relayout(&self) -> bool
fn wrap_needs_relayout(&self) -> bool
Wraps the needs_relayout
method.
sourcefn wrap_important_area(&self, size: XY<usize>) -> Rect
fn wrap_important_area(&self, size: XY<usize>) -> Rect
Wraps the important_area
method.
Auto Trait Implementations
impl<T> !RefUnwindSafe for OnEventView<T>
impl<T> !Send for OnEventView<T>
impl<T> !Sync for OnEventView<T>
impl<T> Unpin for OnEventView<T>where
T: Unpin,
impl<T> !UnwindSafe for OnEventView<T>
Blanket Implementations
sourceimpl<T> AnyView for Twhere
T: View,
impl<T> AnyView for Twhere
T: View,
sourcefn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Downcast self to a mutable Any
.
sourcefn as_boxed_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn as_boxed_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Returns a boxed any from a boxed self. Read more
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Finder for Twhere
T: View,
impl<T> Finder for Twhere
T: View,
sourcefn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)where
V: View,
F: FnMut(&mut V),
fn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)where
V: View,
F: FnMut(&mut V),
Runs a callback on all views identified by sel
. Read more
sourcefn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>where
V: View,
F: FnOnce(&mut V) -> R,
fn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>where
V: View,
F: FnOnce(&mut V) -> R,
Runs a callback on the view identified by sel
. Read more
sourceimpl<T> IntoBoxedView for Twhere
T: View,
impl<T> IntoBoxedView for Twhere
T: View,
sourcefn into_boxed_view(self) -> Box<dyn View + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
fn into_boxed_view(self) -> Box<dyn View + 'static, Global>ⓘNotable traits for Box<F, A>impl<F, A> Future for Box<F, A>where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<I, A> Iterator for Box<I, A>where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;
Returns a Box<View>
.
sourceimpl<T> Resizable for Twhere
T: View,
impl<T> Resizable for Twhere
T: View,
sourcefn resized(
self,
width: SizeConstraint,
height: SizeConstraint
) -> ResizedView<Self>
fn resized(
self,
width: SizeConstraint,
height: SizeConstraint
) -> ResizedView<Self>
Wraps self
in a ResizedView
with the given size constraints.
sourcefn fixed_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
fn fixed_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
Wraps self
into a fixed-size ResizedView
.
sourcefn fixed_width(self, width: usize) -> ResizedView<Self>
fn fixed_width(self, width: usize) -> ResizedView<Self>
Wraps self
into a fixed-width ResizedView
.
sourcefn fixed_height(self, height: usize) -> ResizedView<Self>
fn fixed_height(self, height: usize) -> ResizedView<Self>
Wraps self
into a fixed-width ResizedView
.
sourcefn full_screen(self) -> ResizedView<Self>
fn full_screen(self) -> ResizedView<Self>
Wraps self
into a full-screen ResizedView
.
sourcefn full_width(self) -> ResizedView<Self>
fn full_width(self) -> ResizedView<Self>
Wraps self
into a full-width ResizedView
.
sourcefn full_height(self) -> ResizedView<Self>
fn full_height(self) -> ResizedView<Self>
Wraps self
into a full-height ResizedView
.
sourcefn max_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
fn max_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
Wraps self
into a limited-size ResizedView
.
sourcefn max_width(self, max_width: usize) -> ResizedView<Self>
fn max_width(self, max_width: usize) -> ResizedView<Self>
Wraps self
into a limited-width ResizedView
.
sourcefn max_height(self, max_height: usize) -> ResizedView<Self>
fn max_height(self, max_height: usize) -> ResizedView<Self>
Wraps self
into a limited-height ResizedView
.
sourcefn min_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
fn min_size<S>(self, size: S) -> ResizedView<Self>where
S: Into<XY<usize>>,
Wraps self
into a ResizedView
at least sized size
.
sourcefn min_width(self, min_width: usize) -> ResizedView<Self>
fn min_width(self, min_width: usize) -> ResizedView<Self>
Wraps self
in a ResizedView
at least min_width
wide.
sourcefn min_height(self, min_height: usize) -> ResizedView<Self>
fn min_height(self, min_height: usize) -> ResizedView<Self>
Wraps self
in a ResizedView
at least min_height
tall.
sourceimpl<T> Scrollable for Twhere
T: View,
impl<T> Scrollable for Twhere
T: View,
sourcefn scrollable(self) -> ScrollView<Self>
fn scrollable(self) -> ScrollView<Self>
Wraps self
in a ScrollView
.
sourceimpl<T> View for Twhere
T: ViewWrapper,
impl<T> View for Twhere
T: ViewWrapper,
sourcefn draw(&self, printer: &Printer<'_, '_>)
fn draw(&self, printer: &Printer<'_, '_>)
Draws the view with the given printer (includes bounds) and focus. Read more
sourcefn required_size(&mut self, req: XY<usize>) -> XY<usize>
fn required_size(&mut self, req: XY<usize>) -> XY<usize>
Returns the minimum size the view requires with the given restrictions. Read more
sourcefn on_event(&mut self, ch: Event) -> EventResult
fn on_event(&mut self, ch: Event) -> EventResult
Called when an event is received (key press, mouse event, …). Read more
sourcefn layout(&mut self, size: XY<usize>)
fn layout(&mut self, size: XY<usize>)
Called once the size for this view has been decided. Read more
sourcefn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>
fn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>
Attempt to give this view the focus. Read more
sourcefn call_on_any(
&mut self,
selector: &Selector<'_>,
callback: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)
)
fn call_on_any(
&mut self,
selector: &Selector<'_>,
callback: &'a mut (dyn FnMut(&mut (dyn View + 'static)) + 'a)
)
Runs a closure on the view identified by the given selector. Read more
sourcefn needs_relayout(&self) -> bool
fn needs_relayout(&self) -> bool
Should return true
if the view content changed since the last call
to layout()
. Read more
sourcefn focus_view(
&mut self,
selector: &Selector<'_>
) -> Result<EventResult, ViewNotFound>
fn focus_view(
&mut self,
selector: &Selector<'_>
) -> Result<EventResult, ViewNotFound>
Moves the focus to the view identified by the given selector. Read more