mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Fix layout bug in StackView
Only skip padding for non-centered views
This commit is contained in:
parent
8220fe529e
commit
0fd3bc113f
@ -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,
|
||||
|
@ -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<T: View> {
|
||||
view: T,
|
||||
topleft_padding: bool,
|
||||
top_padding: bool,
|
||||
left_padding: bool,
|
||||
}
|
||||
|
||||
impl<T: View> ShadowView<T> {
|
||||
@ -18,20 +17,22 @@ impl<T: View> ShadowView<T> {
|
||||
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<T: View> ViewWrapper for ShadowView<T> {
|
||||
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 {
|
||||
|
@ -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<T: 'static + View>(&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;
|
||||
|
Loading…
Reference in New Issue
Block a user