Fix important_area for ShadowView and PaddedView

This commit is contained in:
Alexandre Bury 2020-06-11 10:22:03 -07:00
parent 72ded36093
commit 7d833d5386
4 changed files with 27 additions and 2 deletions

View File

@ -1,5 +1,13 @@
# Changelog
## (Next version) cursive-core=0.1.1
### Bugfixes
- More hygienic `wrap_impl!` macro using fully-qualified paths.
- Fixed `LinearLayout` giving children incorrect sizes.
- More accurate "important area" for `ShadowView` and `PaddedView`.
## 0.15.0
### Breaking changes

View File

@ -754,6 +754,8 @@ impl View for Dialog {
}
fn important_area(&self, _: Vec2) -> Rect {
// Only the content is important.
// TODO: if a button is focused, return the button position instead.
self.content.important_area(self.content.size)
+ self.borders.top_left()
+ self.padding.top_left()

View File

@ -1,4 +1,5 @@
use crate::event::{Event, EventResult};
use crate::rect::Rect;
use crate::view::{Margins, View, ViewWrapper};
use crate::Printer;
use crate::Vec2;
@ -43,7 +44,7 @@ impl<V: View> PaddedView<V> {
/// Sets the margins for this view.
pub fn set_margins(&mut self, margins: Margins) {
// TODO: invalidate?
// TODO: invalidate? wrap_needs_relayout?
self.margins = margins;
}
@ -74,4 +75,9 @@ impl<V: View> ViewWrapper for PaddedView<V> {
let printer = &printer.offset(top_left).shrinked(bot_right);
self.view.draw(printer);
}
fn wrap_important_area(&self, view_size: Vec2) -> Rect {
let inner_size = view_size.saturating_sub(self.margins.combined());
self.view.important_area(inner_size) + self.margins.top_left()
}
}

View File

@ -1,4 +1,5 @@
use crate::event::{Event, EventResult};
use crate::rect::Rect;
use crate::theme::ColorStyle;
use crate::view::{View, ViewWrapper};
use crate::Printer;
@ -11,7 +12,7 @@ pub struct ShadowView<T: View> {
view: T,
top_padding: bool,
left_padding: bool,
// TODO: invalidate if we change the padding?
// TODO: invalidate if we change the padding? wrap_needs_relayout?
}
impl<T: View> ShadowView<T> {
@ -24,7 +25,9 @@ impl<T: View> ShadowView<T> {
}
}
/// Return the total padding for this view (include both sides)
fn padding(&self) -> Vec2 {
// We always need (1, 1) for the shadow.
self.top_left_padding() + (1, 1)
}
@ -100,4 +103,10 @@ impl<T: View> ViewWrapper for ShadowView<T> {
let printer = printer.shrinked((1, 1));
self.view.draw(&printer);
}
fn wrap_important_area(&self, view_size: Vec2) -> Rect {
self.view
.important_area(view_size.saturating_sub(self.padding()))
+ self.top_left_padding()
}
}