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.
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 {
let offset = offset.into().or_min(self.size);
Printer {
@ -184,4 +184,9 @@ impl Printer {
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 {
fn draw(&mut self, printer: &Printer) {
fn draw(&self, printer: &Printer) {
let style = if !printer.focused {
ColorStyle::Primary
} else {

View File

@ -111,7 +111,7 @@ impl 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.
let mut buttons_height = 0;
@ -133,7 +133,7 @@ impl View for Dialog {
.get_offset(width, printer.size.x - overhead.horizontal());
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;
// Add some special effect to the focused button
button.draw(&printer.sub_printer(Vec2::new(offset, y),

View File

@ -79,7 +79,7 @@ impl EditView {
}
impl View for EditView {
fn draw(&mut self, printer: &Printer) {
fn draw(&self, printer: &Printer) {
assert!(printer.size.x == self.last_length);
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 {
fn draw(&mut self, printer: &Printer) {
fn draw(&self, printer: &Printer) {
// Use pre-computed sizes
let mut offset = Vec2::zero();
for (i, child) in self.children.iter_mut().enumerate() {
for (i, child) in self.children.iter().enumerate() {
let printer =
&printer.sub_printer(offset, child.size, i == self.focus);
child.view.draw(printer);

View File

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

View File

@ -127,7 +127,7 @@ pub trait View {
fn layout(&mut self, Vec2) {}
/// 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.
///

View File

@ -132,7 +132,7 @@ impl SelectView<String> {
}
impl<T: 'static> View for SelectView<T> {
fn draw(&mut self, printer: &Printer) {
fn draw(&self, printer: &Printer) {
let h = self.items.len();
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);
}
fn wrap_draw(&mut self, printer: &Printer) {
fn wrap_draw(&self, printer: &Printer) {
// Skip the first row/column
let printer =

View File

@ -61,11 +61,11 @@ impl StackView {
}
impl View for StackView {
fn draw(&mut self, printer: &Printer) {
fn draw(&self, printer: &Printer) {
let last = self.layers.len();
let mut previous = Vec2::zero();
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
// Center the view
let offset = v.position

View File

@ -194,7 +194,7 @@ impl<'a> Iterator for LinesIterator<'a> {
}
impl View for TextView {
fn draw(&mut self, printer: &Printer) {
fn draw(&self, printer: &Printer) {
let h = self.rows.len();
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 Printer;
use vec::Vec2;
@ -7,7 +9,14 @@ pub struct TrackedView<T: View> {
/// Wrapped view.
pub view: T,
/// 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> {
@ -15,7 +24,7 @@ impl<T: View> TrackedView<T> {
pub fn new(view: T) -> Self {
TrackedView {
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> {
wrap_impl!(&self.view);
fn wrap_draw(&mut self, printer: &Printer) {
self.offset = printer.offset;
fn wrap_draw(&self, printer: &Printer) {
self.offset.set(printer.offset);
self.view.draw(printer);
}
}

View File

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