Struct cursive_core::views::Canvas
source · pub struct Canvas<T> { /* private fields */ }
Expand description
A blank view that forwards calls to closures.
You can use this view to easily draw your own interface.
Examples
use cursive_core::event::{Event, EventResult, Key};
use cursive_core::views::{Canvas, Dialog};
use unicode_width::UnicodeWidthStr; // To get the width of some text.
// Build a canvas around a string.
let state = String::new();
let canvas = Canvas::new(state)
.with_draw(|text: &String, printer| {
// Simply print our string
printer.print((0, 0), text);
})
.with_on_event(|text: &mut String, event| match event {
Event::Char(c) => {
text.push(c);
EventResult::Consumed(None)
}
Event::Key(Key::Enter) => {
let text = text.clone();
EventResult::with_cb(move |s| {
s.add_layer(Dialog::info(&text));
})
}
_ => EventResult::Ignored,
})
.with_required_size(|text, _constraints| (text.width(), 1).into());
Implementations§
source§impl<T> Canvas<T>
impl<T> Canvas<T>
sourcepub fn set_draw<F>(&mut self, f: F)where
F: 'static + Fn(&T, &Printer<'_, '_>),
pub fn set_draw<F>(&mut self, f: F)where
F: 'static + Fn(&T, &Printer<'_, '_>),
Sets the closure for draw(&Printer)
.
sourcepub fn with_draw<F>(self, f: F) -> Selfwhere
F: 'static + Fn(&T, &Printer<'_, '_>),
pub fn with_draw<F>(self, f: F) -> Selfwhere
F: 'static + Fn(&T, &Printer<'_, '_>),
Sets the closure for draw(&Printer)
.
Chainable variant.
sourcepub fn set_on_event<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Event) -> EventResult,
pub fn set_on_event<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Event) -> EventResult,
Sets the closure for on_event(Event)
.
sourcepub fn with_on_event<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Event) -> EventResult,
pub fn with_on_event<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Event) -> EventResult,
Sets the closure for on_event(Event)
.
Chainable variant.
sourcepub fn set_required_size<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Vec2) -> Vec2,
pub fn set_required_size<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Vec2) -> Vec2,
Sets the closure for required_size(Vec2)
.
sourcepub fn with_required_size<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Vec2) -> Vec2,
pub fn with_required_size<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Vec2) -> Vec2,
Sets the closure for required_size(Vec2)
.
Chainable variant.
sourcepub fn set_layout<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Vec2),
pub fn set_layout<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Vec2),
Sets the closure for layout(Vec2)
.
sourcepub fn with_layout<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Vec2),
pub fn with_layout<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Vec2),
Sets the closure for layout(Vec2)
.
Chainable variant.
sourcepub fn set_take_focus<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Direction) -> Result<EventResult, CannotFocus>,
pub fn set_take_focus<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, Direction) -> Result<EventResult, CannotFocus>,
Sets the closure for take_focus(Direction)
.
sourcepub fn with_take_focus<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Direction) -> Result<EventResult, CannotFocus>,
pub fn with_take_focus<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, Direction) -> Result<EventResult, CannotFocus>,
Sets the closure for take_focus(Direction)
.
Chainable variant.
sourcepub fn set_needs_relayout<F>(&mut self, f: F)where
F: 'static + Fn(&T) -> bool,
pub fn set_needs_relayout<F>(&mut self, f: F)where
F: 'static + Fn(&T) -> bool,
Sets the closure for needs_relayout()
.
sourcepub fn with_needs_relayout<F>(self, f: F) -> Selfwhere
F: 'static + Fn(&T) -> bool,
pub fn with_needs_relayout<F>(self, f: F) -> Selfwhere
F: 'static + Fn(&T) -> bool,
Sets the closure for needs_relayout()
.
Chainable variant.
sourcepub fn set_call_on_any<F>(&mut self, f: F)where
F: 'static + for<'a> FnMut(&mut T, &Selector<'_>, AnyCb<'a>),
pub fn set_call_on_any<F>(&mut self, f: F)where
F: 'static + for<'a> FnMut(&mut T, &Selector<'_>, AnyCb<'a>),
Sets the closure for call_on_any()
.
sourcepub fn with_call_on_any<F>(self, f: F) -> Selfwhere
F: 'static + for<'a> FnMut(&mut T, &Selector<'_>, AnyCb<'a>),
pub fn with_call_on_any<F>(self, f: F) -> Selfwhere
F: 'static + for<'a> FnMut(&mut T, &Selector<'_>, AnyCb<'a>),
Sets the closure for call_on_any()
.
Chainable variant.
sourcepub fn set_important_area<F>(&mut self, f: F)where
F: 'static + Fn(&T, Vec2) -> Rect,
pub fn set_important_area<F>(&mut self, f: F)where
F: 'static + Fn(&T, Vec2) -> Rect,
Sets the closure for important_area()
.
sourcepub fn with_important_area<F>(self, f: F) -> Selfwhere
F: 'static + Fn(&T, Vec2) -> Rect,
pub fn with_important_area<F>(self, f: F) -> Selfwhere
F: 'static + Fn(&T, Vec2) -> Rect,
Sets the closure for important_area()
.
Chainable variant.
sourcepub fn set_focus_view<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, &Selector<'_>) -> Result<EventResult, ViewNotFound>,
pub fn set_focus_view<F>(&mut self, f: F)where
F: 'static + FnMut(&mut T, &Selector<'_>) -> Result<EventResult, ViewNotFound>,
Sets the closure for focus_view()
.
sourcepub fn with_focus_view<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, &Selector<'_>) -> Result<EventResult, ViewNotFound>,
pub fn with_focus_view<F>(self, f: F) -> Selfwhere
F: 'static + FnMut(&mut T, &Selector<'_>) -> Result<EventResult, ViewNotFound>,
Sets the closure for focus_view()
.
Chainable variant.