From 0fd3bc113f8cbac0f2e858fb0e82ddea025277eb Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 2 Jul 2016 19:46:23 -0700 Subject: [PATCH] Fix layout bug in StackView Only skip padding for non-centered views --- src/view/position.rs | 2 ++ src/view/shadow_view.rs | 29 +++++++++++++---------------- src/view/stack_view.rs | 9 ++++++--- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/view/position.rs b/src/view/position.rs index 057c04c..fae6246 100644 --- a/src/view/position.rs +++ b/src/view/position.rs @@ -2,6 +2,7 @@ use std::cmp::min; use vec::{ToVec2, Vec2}; /// Location of the view on screen +#[derive(PartialEq,Debug,Clone)] pub struct Position { pub x: Offset, pub y: Offset, @@ -32,6 +33,7 @@ impl Position { } } +#[derive(PartialEq,Debug,Clone)] pub enum Offset { /// In the center of the screen Center, diff --git a/src/view/shadow_view.rs b/src/view/shadow_view.rs index cb0b6aa..ea797a7 100644 --- a/src/view/shadow_view.rs +++ b/src/view/shadow_view.rs @@ -1,5 +1,3 @@ -use std::borrow::Cow; - use view::{View, ViewWrapper}; use printer::Printer; use vec::Vec2; @@ -10,7 +8,8 @@ use theme::ColorStyle; /// It reserves a 1 pixel border on each side. pub struct ShadowView { view: T, - topleft_padding: bool, + top_padding: bool, + left_padding: bool, } impl ShadowView { @@ -18,20 +17,22 @@ impl ShadowView { pub fn new(view: T) -> Self { ShadowView { view: view, - topleft_padding: true, + top_padding: true, + left_padding: true, } } fn padding(&self) -> (usize, usize) { - if self.topleft_padding { - (2, 2) - } else { - (1, 1) - } + (1 + self.left_padding as usize, + 1 + self.top_padding as usize) } - pub fn no_topleft_padding(mut self) -> Self { - self.topleft_padding = false; + pub fn left_padding(mut self, value: bool) -> Self { + self.left_padding = value; + self + } + pub fn top_padding(mut self, value: bool) -> Self { + self.top_padding = value; self } @@ -53,11 +54,7 @@ impl ViewWrapper for ShadowView { fn wrap_draw(&mut self, printer: &Printer) { // Skip the first row/column - let printer = if self.topleft_padding { - Cow::Owned(printer.sub_printer(Vec2::new(1, 1), printer.size, true)) - } else { - Cow::Borrowed(printer) - }; + let printer = &printer.sub_printer(Vec2::new(self.left_padding as usize, self.top_padding as usize), printer.size, true); // Draw the view background for y in 0..printer.size.y - 1 { diff --git a/src/view/stack_view.rs b/src/view/stack_view.rs index d6c330e..ce8487c 100644 --- a/src/view/stack_view.rs +++ b/src/view/stack_view.rs @@ -1,7 +1,7 @@ use std::any::Any; use vec::Vec2; -use view::{Position, Selector, ShadowView, View}; +use view::{Offset, Position, Selector, ShadowView, View}; use event::{Event, EventResult}; use printer::Printer; use theme::ColorStyle; @@ -41,7 +41,10 @@ impl StackView { pub fn add_layer_at(&mut self, position: Position, view: T) { self.layers.push(Layer { - view: Box::new(ShadowView::new(view).no_topleft_padding()), + // Skip padding for absolute/parent-placed views + view: Box::new(ShadowView::new(view) + .top_padding(position.y == Offset::Center) + .left_padding(position.x == Offset::Center)), size: Vec2::new(0, 0), position: position, virgin: true, @@ -62,7 +65,7 @@ impl View for StackView { for (i, v) in self.layers.iter_mut().enumerate() { // Place the view // Center the view - let mut offset = v.position + let offset = v.position .compute_offset(v.size, printer.size, previous); previous = offset;