[][src]Module unsegen::container

Higher level window manager like functionality using containers as the combination of widget and input concepts.

Compose widgets into multi-widget applications using Containers and ContainerManager as the analogon of a window manager.

Example:

use unsegen::base::*;
use unsegen::container::*;
use unsegen::input::*;
use unsegen::widget::builtin::*;
use unsegen::widget::*;
use std::io::{stdin, stdout};

struct Pager {
    buffer: LogViewer,
}

impl Pager {
    fn new() -> Self {
        Pager {
            buffer: LogViewer::new(),
        }
    }
}

impl Widget for Pager {
    fn space_demand(&self) -> Demand2D {
        self.buffer.space_demand()
    }
    fn draw(&self, window: Window, hints: RenderingHints) {
        self.buffer.draw(window, hints);
    }
}

impl Container<()> for Pager {
    fn input(&mut self, input: Input, _: &mut ()) -> Option<Input> {
        input
            .chain(
                ScrollBehavior::new(&mut self.buffer)
                    .backwards_on(Key::Char('k'))
                    .forwards_on(Key::Char('j')),
            )
            .finish()
    }
}

#[derive(Clone, PartialEq)]
enum Index {
    Left,
    Right,
}

struct App {
    left: Pager,
    right: Pager,
}

impl ContainerProvider for App {
    type Parameters = ();
    type Index = Index;
    fn get<'a, 'b: 'a>(&'b self, index: &'a Self::Index) -> &'b Container<Self::Parameters> {
        match index {
            Index::Left => &self.left,
            Index::Right => &self.right,
        }
    }
    fn get_mut<'a, 'b: 'a>(
        &'b mut self,
        index: &'a Self::Index,
    ) -> &'b mut Container<Self::Parameters> {
        match index {
            Index::Left => &mut self.left,
            Index::Right => &mut self.right,
        }
    }
    const DEFAULT_CONTAINER: Self::Index = Index::Left;
}

fn main() {
    let stdout = stdout();
    let stdin = stdin();
    let stdin = stdin.lock();

    let mut app = App {
        left: Pager::new(),
        right: Pager::new(),
    };
    let mut manager = ContainerManager::<App>::from_layout(Box::new(VSplit::new(vec![
        Box::new(Leaf::new(Index::Left)),
        Box::new(Leaf::new(Index::Right)),
    ])));
    let mut term = Terminal::new(stdout.lock()).unwrap();

    for input in Input::read_all(stdin) {
        let input = input.unwrap();
        input
            .chain(manager.active_container_behavior(&mut app, &mut ()))
            .chain(
                NavigateBehavior::new(&mut manager.navigatable(&mut app))
                    .left_on(Key::Char('h'))
                    .right_on(Key::Char('l')),
            );
        // Put more application logic here...

        {
            let win = term.create_root_window();
            manager.draw(
                win,
                &mut app,
                StyleModifier::new().fg_color(Color::Yellow),
                RenderingHints::default(),
            )
        }
        term.present();
    }
}

Modules

boxdrawing

Utility functions for unicode box characters

Structs

ActiveContainerBehavior

A Behavior which can be used to pass input to the currently active container.

ContainerManager

Stores the layout of containers and manages and has a concept of an active container.

HSplit

A Layout laying out all children horizontally, separated by vertical lines.

HorizontalLine

A single line occupying a number of cells in a row.

LayoutOutput

The result of a layouting operation for containers.

Leaf

A Leaf in a Layout-tree.

NavigatableContainerManager

A wrapper allowing for user defined modification of the currently active container using NavigateBehavior.

Rectangle

A simple rectangle with integer coordinates. Nothing to see here.

VSplit

A Layout laying out all children vertically, separated by Horizontal lines.

VerticalLine

A single line occupying a number of cells in a column.

Enums

Line

An axis aligned line, either vertical or horizontal.

Traits

Container

Extension to the widget trait to enable passing input to (active) widgets. The parameter P can be used to manipulate global application state.

ContainerProvider

A ContainerProvider stores the individual components (Containers) of an application and allows them to be retrieved based on an index.

Layout

A Layouter managing screen real estate for multiple containers