Struct Border

Source
pub struct Border<V: View + ViewSize> {
    pub child: V,
    pub title: Option<String>,
    pub padding: BorderPadding,
    pub chars: BorderChars,
    pub foreground_colour: Colour,
    pub background_colour: Colour,
    pub title_colour: Colour,
    pub bold_title: bool,
    pub underline_title: bool,
    pub bold_border: bool,
}
Expand description

Decorate another element with a border. The child element must implement View and ViewSize, and can be accessed via the child field. It’s possible to give the border a title, in which case the text appears in the top-left corner.

Fields§

§child: V§title: Option<String>§padding: BorderPadding§chars: BorderChars§foreground_colour: Colour§background_colour: Colour§title_colour: Colour§bold_title: bool§underline_title: bool§bold_border: bool

Implementations§

Source§

impl<V: View + ViewSize> Border<V>

Source

pub fn new(child: V) -> Self

Examples found in repository?
examples/tetris.rs (line 379)
376    fn new(width: u16, height: u16) -> Self {
377        let context = Context::new().unwrap();
378        let mut model = Model {
379            game_canvas: Border::new(Canvas::new((width, height))),
380            piece_canvas: Border::new(Canvas::new((6, 4))),
381        };
382
383        model.piece_canvas.title = Some("next".to_string());
384        model.piece_canvas.foreground_colour = BORDER_COLOUR;
385        model.piece_canvas.background_colour = BORDER_BACKGROUND;
386        model.game_canvas.foreground_colour = BORDER_COLOUR;
387        model.game_canvas.background_colour = BORDER_BACKGROUND;
388
389        Self {
390            context,
391            model,
392        }
393    }
394    fn display_end_text(&mut self) {
395        self.context.render(&RichText::one_line(
396                vec![("YOU DIED", TextInfo::default().bold().foreground_colour(colours::RED))])).unwrap();
397    }
398    fn render(&mut self, game: &Game) {
399        game.render(&mut self.model.game_canvas.child);
400        game.render_next(&mut self.model.piece_canvas.child);
401        self.context.render(&self.model).unwrap();
402    }
403    fn main_menu(&mut self) -> MainMenuChoice {
404        let mut menu = Border::new(MenuInstance::new(Menu::smallest(vec![
405            ("Play", MainMenuChoice::Play),
406            ("Quit", MainMenuChoice::Quit),
407        ])).unwrap());
408
409        match self.context.run_menu(&mut menu, |b| &mut b.child).unwrap() {
410            MenuChoice::Finalise(x) => x,
411            _ => MainMenuChoice::Quit,
412        }
413    }
More examples
Hide additional examples
examples/menu.rs (line 25)
13fn main() {
14    let mut ctx = Context::new().unwrap();
15
16    let mut menu = Menu::new(vec![
17        ("One", 1),
18        ("Two", 2),
19        ("Three", 3),
20        ("Four", 4),
21    ], (6, 4));
22
23    menu.selected_info.foreground_colour = colours::RED;
24
25    let mut menu_instance = Border::new(MenuInstance::new(menu).unwrap());
26
27    let choice = loop {
28        match ctx.run_menu(&mut menu_instance, |v| &mut v.child).unwrap() {
29            MenuChoice::Quit => return,
30            MenuChoice::Cancel => break None,
31            MenuChoice::Finalise(x) => break Some(x),
32        }
33    };
34
35    if let Some(x) = choice {
36        ctx.render(&Text::one_line(format!("You chose: {}", x))).unwrap();
37    } else {
38        ctx.render(&Text::one_line("You chose nothing!")).unwrap();
39    }
40
41    thread::sleep(Duration::from_millis(1000));
42}
examples/border.rs (line 33)
29fn main() {
30    let mut context = Context::new().unwrap();
31
32    let mut app = App {
33        a: Border::new(Text::new("abcdefghijklmnopqrstuvwxyz", (5, 5))),
34        b: Border::with_title(Text::new("abcdefghijklmnopqrstuvwxyz", (5, 5)), "Foo"),
35    };
36
37    app.b.title_colour = colours::DARK_GREY;
38    app.b.foreground_colour = colours::RED;
39    app.b.background_colour = colours::BLUE;
40
41    app.a.bold_border = true;
42    app.b.bold_title = true;
43
44    app.a.chars.top_left = '#';
45    app.a.chars.bottom_left = '#';
46    app.a.chars.top_right = '#';
47    app.a.chars.bottom_right = '#';
48    app.a.chars.left = '#';
49    app.a.chars.right = '#';
50    app.a.chars.top = '#';
51    app.a.chars.bottom = '#';
52
53    app.a.padding.top = 1;
54    app.a.padding.left = 2;
55    app.a.padding.right = 2;
56    app.a.padding.bottom = 4;
57
58    let mut app = Border::with_title(app, "Hello, World!");
59    app.underline_title = true;
60
61    context.render(&app).unwrap();
62    context.wait_input().unwrap();
63}
Source

pub fn with_title<S: Into<String>>(child: V, title: S) -> Self

Examples found in repository?
examples/border.rs (line 34)
29fn main() {
30    let mut context = Context::new().unwrap();
31
32    let mut app = App {
33        a: Border::new(Text::new("abcdefghijklmnopqrstuvwxyz", (5, 5))),
34        b: Border::with_title(Text::new("abcdefghijklmnopqrstuvwxyz", (5, 5)), "Foo"),
35    };
36
37    app.b.title_colour = colours::DARK_GREY;
38    app.b.foreground_colour = colours::RED;
39    app.b.background_colour = colours::BLUE;
40
41    app.a.bold_border = true;
42    app.b.bold_title = true;
43
44    app.a.chars.top_left = '#';
45    app.a.chars.bottom_left = '#';
46    app.a.chars.top_right = '#';
47    app.a.chars.bottom_right = '#';
48    app.a.chars.left = '#';
49    app.a.chars.right = '#';
50    app.a.chars.top = '#';
51    app.a.chars.bottom = '#';
52
53    app.a.padding.top = 1;
54    app.a.padding.left = 2;
55    app.a.padding.right = 2;
56    app.a.padding.bottom = 4;
57
58    let mut app = Border::with_title(app, "Hello, World!");
59    app.underline_title = true;
60
61    context.render(&app).unwrap();
62    context.wait_input().unwrap();
63}

Trait Implementations§

Source§

impl<V: Clone + View + ViewSize> Clone for Border<V>

Source§

fn clone(&self) -> Border<V>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V: Debug + View + ViewSize> Debug for Border<V>

Source§

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

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

impl<'de, V> Deserialize<'de> for Border<V>
where V: Deserialize<'de> + View + ViewSize,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<V> Serialize for Border<V>
where V: Serialize + View + ViewSize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<V: View + ViewSize> View for Border<V>

Source§

fn view<G: ViewGrid>(&self, offset: Vector2<i16>, depth: i16, grid: &mut G)

Update the cells in grid to describe how a type should be rendered. Implementations of view for low level ui components will typically involve updating cells directly. Implementations for higer level components, such as an entire application’s ui, will typically call the view methed of lower level components which make up the ui.
Source§

impl<V: View + ViewSize> ViewSize for Border<V>

Source§

fn size(&self) -> Vector2<u16>

Returns the size in cells of the rectangle containing a ui element. This allows for the implementation of decorator ui components that render a border around some inner element.

Auto Trait Implementations§

§

impl<V> Freeze for Border<V>
where V: Freeze,

§

impl<V> RefUnwindSafe for Border<V>
where V: RefUnwindSafe,

§

impl<V> Send for Border<V>
where V: Send,

§

impl<V> Sync for Border<V>
where V: Sync,

§

impl<V> Unpin for Border<V>
where V: Unpin,

§

impl<V> UnwindSafe for Border<V>
where V: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,