Fix layout bug in StackView

Only skip padding for non-centered views
This commit is contained in:
Alexandre Bury 2016-07-02 19:46:23 -07:00
parent 8220fe529e
commit 0fd3bc113f
3 changed files with 21 additions and 19 deletions

View File

@ -2,6 +2,7 @@ use std::cmp::min;
use vec::{ToVec2, Vec2}; use vec::{ToVec2, Vec2};
/// Location of the view on screen /// Location of the view on screen
#[derive(PartialEq,Debug,Clone)]
pub struct Position { pub struct Position {
pub x: Offset, pub x: Offset,
pub y: Offset, pub y: Offset,
@ -32,6 +33,7 @@ impl Position {
} }
} }
#[derive(PartialEq,Debug,Clone)]
pub enum Offset { pub enum Offset {
/// In the center of the screen /// In the center of the screen
Center, Center,

View File

@ -1,5 +1,3 @@
use std::borrow::Cow;
use view::{View, ViewWrapper}; use view::{View, ViewWrapper};
use printer::Printer; use printer::Printer;
use vec::Vec2; use vec::Vec2;
@ -10,7 +8,8 @@ use theme::ColorStyle;
/// It reserves a 1 pixel border on each side. /// It reserves a 1 pixel border on each side.
pub struct ShadowView<T: View> { pub struct ShadowView<T: View> {
view: T, view: T,
topleft_padding: bool, top_padding: bool,
left_padding: bool,
} }
impl<T: View> ShadowView<T> { impl<T: View> ShadowView<T> {
@ -18,20 +17,22 @@ impl<T: View> ShadowView<T> {
pub fn new(view: T) -> Self { pub fn new(view: T) -> Self {
ShadowView { ShadowView {
view: view, view: view,
topleft_padding: true, top_padding: true,
left_padding: true,
} }
} }
fn padding(&self) -> (usize, usize) { fn padding(&self) -> (usize, usize) {
if self.topleft_padding { (1 + self.left_padding as usize,
(2, 2) 1 + self.top_padding as usize)
} else {
(1, 1)
}
} }
pub fn no_topleft_padding(mut self) -> Self { pub fn left_padding(mut self, value: bool) -> Self {
self.topleft_padding = false; self.left_padding = value;
self
}
pub fn top_padding(mut self, value: bool) -> Self {
self.top_padding = value;
self self
} }
@ -53,11 +54,7 @@ impl<T: View> ViewWrapper for ShadowView<T> {
fn wrap_draw(&mut self, printer: &Printer) { fn wrap_draw(&mut self, printer: &Printer) {
// Skip the first row/column // Skip the first row/column
let printer = if self.topleft_padding { let printer = &printer.sub_printer(Vec2::new(self.left_padding as usize, self.top_padding as usize), printer.size, true);
Cow::Owned(printer.sub_printer(Vec2::new(1, 1), printer.size, true))
} else {
Cow::Borrowed(printer)
};
// Draw the view background // Draw the view background
for y in 0..printer.size.y - 1 { for y in 0..printer.size.y - 1 {

View File

@ -1,7 +1,7 @@
use std::any::Any; use std::any::Any;
use vec::Vec2; use vec::Vec2;
use view::{Position, Selector, ShadowView, View}; use view::{Offset, Position, Selector, ShadowView, View};
use event::{Event, EventResult}; use event::{Event, EventResult};
use printer::Printer; use printer::Printer;
use theme::ColorStyle; use theme::ColorStyle;
@ -41,7 +41,10 @@ impl StackView {
pub fn add_layer_at<T: 'static + View>(&mut self, position: Position, pub fn add_layer_at<T: 'static + View>(&mut self, position: Position,
view: T) { view: T) {
self.layers.push(Layer { 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), size: Vec2::new(0, 0),
position: position, position: position,
virgin: true, virgin: true,
@ -62,7 +65,7 @@ impl View for StackView {
for (i, v) in self.layers.iter_mut().enumerate() { for (i, v) in self.layers.iter_mut().enumerate() {
// Place the view // Place the view
// Center the view // Center the view
let mut offset = v.position let offset = v.position
.compute_offset(v.size, printer.size, previous); .compute_offset(v.size, printer.size, previous);
previous = offset; previous = offset;