Make View::draw take a &self (instead of a &mut self)

TrackedView, the only user of the mutability, now uses internal
mutability with a Cell.
This commit is contained in:
Alexandre Bury 2016-07-15 23:44:38 -07:00
parent ef6f83751c
commit 2b54d6df28
13 changed files with 35 additions and 21 deletions

View File

@ -173,7 +173,7 @@ 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>>(&self, offset: S, size: S, focused: bool) pub fn sub_printer<S: Into<Vec2>, T: Into<Vec2>>(&self, offset: S, size: T, focused: bool)
-> Printer { -> Printer {
let offset = offset.into().or_min(self.size); let offset = offset.into().or_min(self.size);
Printer { Printer {
@ -184,4 +184,9 @@ impl Printer {
theme: self.theme.clone(), theme: self.theme.clone(),
} }
} }
/// Returns a sub-printer with the given offset.
pub fn offset<S: Into<Vec2>>(&self, offset: S, focused: bool) -> Printer {
self.sub_printer(offset, self.size, focused)
}
} }

View File

@ -29,7 +29,7 @@ impl Button {
} }
impl View for Button { impl View for Button {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
let style = if !printer.focused { let style = if !printer.focused {
ColorStyle::Primary ColorStyle::Primary
} else { } else {

View File

@ -111,7 +111,7 @@ impl Dialog {
} }
impl View for Dialog { impl View for Dialog {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
// This will be the buttons_height used by the buttons. // This will be the buttons_height used by the buttons.
let mut buttons_height = 0; let mut buttons_height = 0;
@ -133,7 +133,7 @@ impl View for Dialog {
.get_offset(width, printer.size.x - overhead.horizontal()); .get_offset(width, printer.size.x - overhead.horizontal());
let y = printer.size.y - self.padding.bottom - self.borders.bottom - 1; let y = printer.size.y - self.padding.bottom - self.borders.bottom - 1;
for (i, button) in self.buttons.iter_mut().enumerate() { for (i, button) in self.buttons.iter().enumerate() {
let size = button.size; let size = button.size;
// Add some special effect to the focused button // Add some special effect to the focused button
button.draw(&printer.sub_printer(Vec2::new(offset, y), button.draw(&printer.sub_printer(Vec2::new(offset, y),

View File

@ -79,7 +79,7 @@ impl EditView {
} }
impl View for EditView { impl View for EditView {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
assert!(printer.size.x == self.last_length); assert!(printer.size.x == self.last_length);
let width = self.content.width(); let width = self.content.width();

View File

@ -162,10 +162,10 @@ fn try_focus((i, child): (usize, &mut Child), source: direction::Direction)
} }
impl View for LinearLayout { impl View for LinearLayout {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
// Use pre-computed sizes // Use pre-computed sizes
let mut offset = Vec2::zero(); let mut offset = Vec2::zero();
for (i, child) in self.children.iter_mut().enumerate() { for (i, child) in self.children.iter().enumerate() {
let printer = let printer =
&printer.sub_printer(offset, child.size, i == self.focus); &printer.sub_printer(offset, child.size, i == self.focus);
child.view.draw(printer); child.view.draw(printer);

View File

@ -133,7 +133,7 @@ impl MenuPopup {
} }
impl View for MenuPopup { impl View for MenuPopup {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
let h = self.menu.len(); let h = self.menu.len();
let offset = self.align.v.get_offset(h, printer.size.y); let offset = self.align.v.get_offset(h, printer.size.y);
let printer = let printer =

View File

@ -127,7 +127,7 @@ pub trait View {
fn layout(&mut self, Vec2) {} fn layout(&mut self, Vec2) {}
/// Draws the view with the given printer (includes bounds) and focus. /// Draws the view with the given printer (includes bounds) and focus.
fn draw(&mut self, printer: &Printer); fn draw(&self, printer: &Printer);
/// Finds the view pointed to by the given path. /// Finds the view pointed to by the given path.
/// ///

View File

@ -132,7 +132,7 @@ impl SelectView<String> {
} }
impl<T: 'static> View for SelectView<T> { impl<T: 'static> View for SelectView<T> {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
let h = self.items.len(); let h = self.items.len();
let offset = self.align.v.get_offset(h, printer.size.y); let offset = self.align.v.get_offset(h, printer.size.y);

View File

@ -56,7 +56,7 @@ impl<T: View> ViewWrapper for ShadowView<T> {
self.view.layout(size - offset); self.view.layout(size - offset);
} }
fn wrap_draw(&mut self, printer: &Printer) { fn wrap_draw(&self, printer: &Printer) {
// Skip the first row/column // Skip the first row/column
let printer = let printer =

View File

@ -61,11 +61,11 @@ impl StackView {
} }
impl View for StackView { impl View for StackView {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
let last = self.layers.len(); let last = self.layers.len();
let mut previous = Vec2::zero(); let mut previous = Vec2::zero();
printer.with_color(ColorStyle::Primary, |printer| { printer.with_color(ColorStyle::Primary, |printer| {
for (i, v) in self.layers.iter_mut().enumerate() { for (i, v) in self.layers.iter().enumerate() {
// Place the view // Place the view
// Center the view // Center the view
let offset = v.position let offset = v.position

View File

@ -194,7 +194,7 @@ impl<'a> Iterator for LinesIterator<'a> {
} }
impl View for TextView { impl View for TextView {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
let h = self.rows.len(); let h = self.rows.len();
let offset = self.align.v.get_offset(h, printer.size.y); let offset = self.align.v.get_offset(h, printer.size.y);

View File

@ -1,3 +1,5 @@
use std::cell::Cell;
use view::{IdView, View, ViewWrapper}; use view::{IdView, View, ViewWrapper};
use Printer; use Printer;
use vec::Vec2; use vec::Vec2;
@ -7,7 +9,14 @@ pub struct TrackedView<T: View> {
/// Wrapped view. /// Wrapped view.
pub view: T, pub view: T,
/// Last position the view was located. /// Last position the view was located.
pub offset: Vec2, offset: Cell<Vec2>,
}
impl<T: View> TrackedView<T> {
/// Return the last offset at which the view was drawn.
pub fn offset(&self) -> Vec2 {
self.offset.get()
}
} }
impl<T: View> TrackedView<T> { impl<T: View> TrackedView<T> {
@ -15,7 +24,7 @@ impl<T: View> TrackedView<T> {
pub fn new(view: T) -> Self { pub fn new(view: T) -> Self {
TrackedView { TrackedView {
view: view, view: view,
offset: Vec2::zero(), offset: Cell::new(Vec2::zero()),
} }
} }
@ -28,8 +37,8 @@ impl<T: View> TrackedView<T> {
impl<T: View> ViewWrapper for TrackedView<T> { impl<T: View> ViewWrapper for TrackedView<T> {
wrap_impl!(&self.view); wrap_impl!(&self.view);
fn wrap_draw(&mut self, printer: &Printer) { fn wrap_draw(&self, printer: &Printer) {
self.offset = printer.offset; self.offset.set(printer.offset);
self.view.draw(printer); self.view.draw(printer);
} }
} }

View File

@ -18,8 +18,8 @@ pub trait ViewWrapper {
fn get_view_mut(&mut self) -> &mut View; fn get_view_mut(&mut self) -> &mut View;
/// Wraps the `draw` method. /// Wraps the `draw` method.
fn wrap_draw(&mut self, printer: &Printer) { fn wrap_draw(&self, printer: &Printer) {
self.get_view_mut().draw(printer); self.get_view().draw(printer);
} }
/// Wraps the `get_min_size` method. /// Wraps the `get_min_size` method.
@ -54,7 +54,7 @@ pub trait ViewWrapper {
} }
impl<T: ViewWrapper> View for T { impl<T: ViewWrapper> View for T {
fn draw(&mut self, printer: &Printer) { fn draw(&self, printer: &Printer) {
self.wrap_draw(printer); self.wrap_draw(printer);
} }