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§

Creates a new Canvas around the given view.

By default, forwards all calls to the inner view.

Creates a new, empty Canvas.

Gets a mutable reference to the inner state.

Sets the closure for draw(&Printer).

Sets the closure for draw(&Printer).

Chainable variant.

Sets the closure for on_event(Event).

Sets the closure for on_event(Event).

Chainable variant.

Sets the closure for required_size(Vec2).

Sets the closure for required_size(Vec2).

Chainable variant.

Sets the closure for layout(Vec2).

Sets the closure for layout(Vec2).

Chainable variant.

Sets the closure for take_focus(Direction).

Sets the closure for take_focus(Direction).

Chainable variant.

Sets the closure for needs_relayout().

Sets the closure for needs_relayout().

Chainable variant.

Sets the closure for call_on_any().

Sets the closure for call_on_any().

Chainable variant.

Sets the closure for important_area().

Sets the closure for important_area().

Chainable variant.

Sets the closure for focus_view().

Sets the closure for focus_view().

Chainable variant.

Trait Implementations§

Draws the view with the given printer (includes bounds) and focus. Read more
Called when an event is received (key press, mouse event, …). Read more
Returns the minimum size the view requires with the given restrictions. Read more
Called once the size for this view has been decided. Read more
Attempt to give this view the focus. Read more
Should return true if the view content changed since the last call to layout(). Read more
Moves the focus to the view identified by the given selector. Read more
What part of the view is important and should be visible? Read more
Runs a closure on the view identified by the given selector. Read more
Returns the type of this view. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.