Add &Backend to Printer struct

Makes `::B` public but undocumented.
Backend is currently an immutable ref, may need to become
mutable to be more useful (will need to change the View trait)
This commit is contained in:
Alexandre Bury 2016-09-22 22:00:58 -07:00
parent 05a862d98b
commit 829dd77deb
3 changed files with 29 additions and 12 deletions

View File

@ -132,6 +132,8 @@ pub struct Cursive {
running: bool, running: bool,
backend: B,
cb_source: mpsc::Receiver<Box<Fn(&mut Cursive) + Send>>, cb_source: mpsc::Receiver<Box<Fn(&mut Cursive) + Send>>,
cb_sink: mpsc::Sender<Box<Fn(&mut Cursive) + Send>>, cb_sink: mpsc::Sender<Box<Fn(&mut Cursive) + Send>>,
} }
@ -140,7 +142,8 @@ new_default!(Cursive);
// Use the Ncurses backend. // Use the Ncurses backend.
// TODO: make this feature-driven // TODO: make this feature-driven
type B = NcursesBackend; #[doc(hidden)]
pub type B = NcursesBackend;
impl Cursive { impl Cursive {
/// Creates a new Cursive root, and initialize ncurses. /// Creates a new Cursive root, and initialize ncurses.
@ -163,6 +166,7 @@ impl Cursive {
running: true, running: true,
cb_source: rx, cb_source: rx,
cb_sink: tx, cb_sink: tx,
backend: NcursesBackend,
}; };
res.screens.push(views::StackView::new()); res.screens.push(views::StackView::new());
@ -441,7 +445,7 @@ impl Cursive {
fn draw(&mut self) { fn draw(&mut self) {
// TODO: don't clone the theme // TODO: don't clone the theme
// Reference it or something // Reference it or something
let printer = Printer::new(self.screen_size(), self.theme.clone()); let printer = Printer::new(self.screen_size(), self.theme.clone(), &mut self.backend);
// Draw the currently active screen // Draw the currently active screen
// If the menubar is active, nothing else can be. // If the menubar is active, nothing else can be.
@ -462,7 +466,8 @@ impl Cursive {
let printer = let printer =
printer.sub_printer(Vec2::new(0, offset), printer.size, !selected); printer.sub_printer(Vec2::new(0, offset), printer.size, !selected);
self.screen_mut().draw(&printer); let id = self.active_screen;
self.screens.get_mut(id).unwrap().draw(&printer);
B::refresh(); B::refresh();
} }

View File

@ -12,8 +12,7 @@ use theme::{BorderStyle, ColorStyle, Effect, Theme};
use vec::Vec2; use vec::Vec2;
/// Convenient interface to draw on a subset of the screen. /// Convenient interface to draw on a subset of the screen.
#[derive(Clone)] pub struct Printer<'a> {
pub struct Printer {
/// Offset into the window this printer should start drawing at. /// Offset into the window this printer should start drawing at.
pub offset: Vec2, pub offset: Vec2,
/// Size of the area we are allowed to draw on. /// Size of the area we are allowed to draw on.
@ -22,16 +21,22 @@ pub struct Printer {
pub focused: bool, pub focused: bool,
/// Currently used theme /// Currently used theme
pub theme: Theme, pub theme: Theme,
backend: &'a B,
} }
impl Printer { impl <'a> Printer<'a> {
/// Creates a new printer on the given window. /// Creates a new printer on the given window.
pub fn new<T: Into<Vec2>>(size: T, theme: Theme) -> Self { ///
/// But nobody needs to know that.
#[doc(hidden)]
pub fn new<T: Into<Vec2>>(size: T, theme: Theme, backend: &'a B) -> Self {
Printer { Printer {
offset: Vec2::zero(), offset: Vec2::zero(),
size: size.into(), size: size.into(),
focused: true, focused: true,
theme: theme, theme: theme,
backend: backend,
} }
} }
@ -90,7 +95,9 @@ impl Printer {
/// ```no_run /// ```no_run
/// # use cursive::Printer; /// # use cursive::Printer;
/// # use cursive::theme; /// # use cursive::theme;
/// # let printer = Printer::new((6,4), theme::load_default()); /// # use cursive::B;
/// # let b = B{};
/// # let printer = Printer::new((6,4), theme::load_default(), &b);
/// printer.with_color(theme::ColorStyle::Highlight, |printer| { /// printer.with_color(theme::ColorStyle::Highlight, |printer| {
/// printer.print((0,0), "This text is highlighted!"); /// printer.print((0,0), "This text is highlighted!");
/// }); /// });
@ -121,7 +128,9 @@ impl Printer {
/// ```no_run /// ```no_run
/// # use cursive::Printer; /// # use cursive::Printer;
/// # use cursive::theme; /// # use cursive::theme;
/// # let printer = Printer::new((6,4), theme::load_default()); /// # use cursive::B;
/// # let b = B{};
/// # let printer = Printer::new((6,4), theme::load_default(), &b);
/// printer.print_box((0,0), (6,4), false); /// printer.print_box((0,0), (6,4), false);
/// ``` /// ```
pub fn print_box<T: Into<Vec2>, S: Into<Vec2>>(&self, start: T, size: S, pub fn print_box<T: Into<Vec2>, S: Into<Vec2>>(&self, start: T, size: S,
@ -213,9 +222,9 @@ impl Printer {
} }
/// Returns a printer on a subset of this one's area. /// Returns a printer on a subset of this one's area.
pub fn sub_printer<S: Into<Vec2>, T: Into<Vec2>>(&self, offset: S, pub fn sub_printer<S: Into<Vec2>, T: Into<Vec2>>(&'a self, offset: S,
size: T, focused: bool) size: T, focused: bool)
-> Printer { -> Printer<'a> {
let size = size.into(); let size = size.into();
let offset = offset.into().or_min(self.size); let offset = offset.into().or_min(self.size);
Printer { Printer {
@ -224,6 +233,7 @@ impl Printer {
size: Vec2::min(self.size - offset, size), size: Vec2::min(self.size - offset, size),
focused: self.focused && focused, focused: self.focused && focused,
theme: self.theme.clone(), theme: self.theme.clone(),
backend: self.backend,
} }
} }

View File

@ -134,8 +134,10 @@ impl ScrollBase {
/// # use cursive::view::ScrollBase; /// # use cursive::view::ScrollBase;
/// # use cursive::Printer; /// # use cursive::Printer;
/// # use cursive::theme; /// # use cursive::theme;
/// # use cursive::B;
/// # let scrollbase = ScrollBase::new(); /// # let scrollbase = ScrollBase::new();
/// # let printer = Printer::new((5,1), theme::load_default()); /// # let b = B{};
/// # let printer = Printer::new((5,1), theme::load_default(), &b);
/// # let printer = &printer; /// # let printer = &printer;
/// let lines = ["Line 1", "Line number 2"]; /// let lines = ["Line 1", "Line number 2"];
/// scrollbase.draw(printer, |printer, i| { /// scrollbase.draw(printer, |printer, i| {