Struct arkham::prelude::ViewContext

source ·
pub struct ViewContext {
    pub view: View,
    pub container: Rc<RefCell<Container>>,
    /* private fields */
}
Expand description

ViewContext represents the display context for a given area. it maintains the drawing state for the region internally and is used to generate a final view that is eventually rendered.

Fields§

§view: View§container: Rc<RefCell<Container>>

Implementations§

source§

impl ViewContext

source

pub fn new(container: Rc<RefCell<Container>>, size: Size) -> Self

Constructs a new ViewConext for a given area. A container reference must also be passedo, so that component functions called from the context are injectable.

source

pub fn render(&mut self)

Notify the application to rerender the view. This is useful after a state change that might affect other views.

Examples found in repository?
examples/navigation.rs (line 34)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
fn main_route(ctx: &mut ViewContext, kb: Res<Keyboard>, route: State<Route>) {
    ctx.insert(
        0,
        "Welcome to the main screen, press 2 to goto the secondary screen",
    );
    if kb.char() == Some('2') {
        *route.get_mut() = Route::Secondary;
        ctx.render();
    }
}

fn secondary_route(ctx: &mut ViewContext, kb: Res<Keyboard>, route: State<Route>) {
    ctx.insert(
        0,
        "Welcome to the secondary screen, press 1 to goto the main screen",
    );

    if kb.char() == Some('1') {
        *route.get_mut() = Route::Main;
        ctx.render();
    }
}
source

pub fn vertical_stack<S>(&self, size: S) -> Stack
where S: Into<Size>,

Examples found in repository?
examples/stack.rs (line 8)
7
8
9
10
11
12
13
fn root(ctx: &mut ViewContext) {
    let mut stack = ctx.vertical_stack((100, 100));
    for _ in 0..10 {
        stack.component((ctx.size().width, 1), list_item);
    }
    ctx.component((0, (100, 100)), stack);
}
source

pub fn horizontal_stack<S>(&self, size: S) -> Stack
where S: Into<Size>,

Examples found in repository?
examples/stack.rs (line 17)
15
16
17
18
19
20
21
fn list_item(ctx: &mut ViewContext) {
    let size = ctx.size();
    let mut hstack = ctx.horizontal_stack((ctx.size().width, 1));
    hstack.insert("> ");
    hstack.insert("line 1");
    ctx.component(size, hstack);
}
source

pub fn component<F, Args, R>(&mut self, rect: R, f: F)
where F: Callable<Args>, Args: FromContainer, R: Into<Rect>,

Execute a component function. The passed function will receive a new ViewContext for its size and can be injected with arguments. The context given to the component function will then be applied to the parent ViewContext at a given position.

Examples found in repository?
examples/stack.rs (line 12)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn root(ctx: &mut ViewContext) {
    let mut stack = ctx.vertical_stack((100, 100));
    for _ in 0..10 {
        stack.component((ctx.size().width, 1), list_item);
    }
    ctx.component((0, (100, 100)), stack);
}

fn list_item(ctx: &mut ViewContext) {
    let size = ctx.size();
    let mut hstack = ctx.horizontal_stack((ctx.size().width, 1));
    hstack.insert("> ");
    hstack.insert("line 1");
    ctx.component(size, hstack);
}
More examples
Hide additional examples
examples/component_functions.rs (line 10)
7
8
9
10
11
12
fn root(ctx: &mut ViewContext) {
    let size = ctx.size();
    ctx.fill(size, Rune::new().bg(Color::DarkGrey));
    ctx.component(Rect::new((10, 10), (20, 1)), hello_world);
    ctx.component(Rect::new(0, (size.width, 1)), quit_nag);
}
examples/component_params.rs (line 10)
7
8
9
10
11
12
fn root(ctx: &mut ViewContext) {
    let size = ctx.size();
    ctx.fill(size, Rune::new().bg(Color::DarkGrey));
    ctx.component(Rect::new((10, 10), (20, 1)), say_hello("Alice"));
    ctx.component(Rect::new(0, (size.width, 1)), quit_nag);
}
examples/keyboard.rs (line 10)
7
8
9
10
11
12
13
fn root(ctx: &mut ViewContext) {
    let size = ctx.size();
    ctx.fill(size, Rune::new().bg(Color::DarkGrey));
    ctx.component(((10, 10), (30, 1)), hello_world);
    ctx.component(((10, 11), (20, 1)), show_key_press);
    ctx.component((0, (size.width, 1)), quit_nag);
}
examples/navigation.rs (line 19)
13
14
15
16
17
18
19
20
21
22
23
24
25
fn root(ctx: &mut ViewContext, route: State<Route>) {
    let size = ctx.size();
    let r = *route.get();
    ctx.insert((0, 0), "Press Q to quit");
    match r {
        Route::Main => {
            ctx.component(Rect::new((0, 1), size), main_route);
        }
        Route::Secondary => {
            ctx.component(Rect::new((0, 1), size), secondary_route);
        }
    }
}
source

pub fn set_rune<P>(&mut self, pos: P, rune: Rune)
where P: Into<Pos>,

Set a specific rune to a specific position. This function can be used to set a signle character. To set multiple runes at a time see the View::insert function.

Trait Implementations§

source§

impl Deref for ViewContext

§

type Target = View

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl DerefMut for ViewContext

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

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<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<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.