Skip to content

Commit

Permalink
Make style changes which alter inherited style properties request lay…
Browse files Browse the repository at this point in the history
…out for all children
  • Loading branch information
Zoxc committed Nov 3, 2023
1 parent 3e3aa62 commit 5fcffda
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
10 changes: 10 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct ViewState {
pub(crate) node: Node,
pub(crate) children_nodes: Vec<Node>,
pub(crate) request_layout: bool,
/// Layout is requested on all direct and indirect children.
pub(crate) request_layout_recursive: bool,
pub(crate) has_style_selectors: StyleSelectors,
pub(crate) viewport: Option<Rect>,
pub(crate) layout_rect: Rect,
Expand Down Expand Up @@ -78,6 +80,7 @@ impl ViewState {
layout_rect: Rect::ZERO,
layout_props: Default::default(),
request_layout: true,
request_layout_recursive: false,

Check warning on line 83 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L83

Added line #L83 was not covered by tests
has_style_selectors: StyleSelectors::default(),
animation: None,
base_style: None,
Expand Down Expand Up @@ -370,6 +373,13 @@ impl AppState {
}
}

/// Requests layout for a view and all direct and indirect children.
pub(crate) fn request_layout_recursive(&mut self, id: Id) {
let view = self.view_state(id);
view.request_layout_recursive = true;
self.request_layout(id);
}

Check warning on line 381 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L377-L381

Added lines #L377 - L381 were not covered by tests

pub fn request_layout(&mut self, id: Id) {
let view = self.view_state(id);
if view.request_layout {
Expand Down
8 changes: 5 additions & 3 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,12 @@ impl Style {
}
}

pub(crate) fn apply_only_inherited(this: &mut Rc<Style>, over: &Style) {
let any_inherited = over.map.iter().any(|(p, _)| p.info.inherited);
pub(crate) fn any_inherited(&self) -> bool {
!self.classes.is_empty() || self.map.iter().any(|(p, _)| p.info.inherited)
}

Check warning on line 561 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L559-L561

Added lines #L559 - L561 were not covered by tests

if any_inherited || !over.classes.is_empty() {
pub(crate) fn apply_only_inherited(this: &mut Rc<Style>, over: &Style) {
if over.any_inherited() {

Check warning on line 564 in src/style.rs

View check run for this annotation

Codecov / codecov/patch

src/style.rs#L563-L564

Added lines #L563 - L564 were not covered by tests
let inherited = over
.map
.iter()
Expand Down
19 changes: 15 additions & 4 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,30 @@ pub trait View {
fn layout_main(&mut self, cx: &mut LayoutCx) -> Node {
cx.save();

let view_state = cx.app_state_mut().view_state(self.id());

Check warning on line 201 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L200-L201

Added lines #L200 - L201 were not covered by tests
let view_style = self.view_style();
let view_class = self.view_class();
let class = cx.app_state_mut().view_state(self.id()).class;
let class = view_state.class;

Check warning on line 204 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L204

Added line #L204 was not covered by tests
let class_array;
let classes = if let Some(class) = class {
class_array = [class];
&class_array[..]
} else {
&[]
};

// Propagate layout requests to children if needed.
if view_state.request_layout_recursive {
view_state.request_layout_recursive = false;
view_state.request_layout = true;
for child in self.children() {
cx.app_state_mut()
.view_state(child.id())
.request_layout_recursive = true;
}
}

Check warning on line 222 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L214-L222

Added lines #L214 - L222 were not covered by tests

cx.app_state.compute_style(
self.id(),
view_style,
Expand Down Expand Up @@ -253,9 +267,6 @@ pub trait View {

cx.save();

cx.style.direct = cx.app_state_mut().get_computed_style(self.id()).clone();
Style::apply_only_inherited(&mut cx.style.current, &cx.style.direct);

let layout = cx
.app_state()
.get_layout(self.id())
Expand Down
9 changes: 0 additions & 9 deletions src/views/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,6 @@ impl View for Label {
return None;
}

if self.font.read(cx) | self.style.read(cx) {
self.text_layout = None;
self.available_text = None;
self.available_width = None;
self.available_text_layout = None;
self.set_text_layout();
cx.app_state_mut().request_layout(self.id());
}

let layout = cx.get_layout(self.id()).unwrap();
let style = cx.app_state_mut().get_builtin_style(self.id);
let text_overflow = style.text_overflow();
Expand Down
6 changes: 3 additions & 3 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ enum BarHeldState {
style_class!(pub Handle);
style_class!(pub Track);

prop!(pub Rounded: bool { inherited } = cfg!(target_os = "macos"));
prop!(pub Thickness: Px { inherited } = Px(10.0));
prop!(pub Border: Px { inherited } = Px(0.0));
prop!(pub Rounded: bool {} = cfg!(target_os = "macos"));
prop!(pub Thickness: Px {} = Px(10.0));
prop!(pub Border: Px {} = Px(0.0));

Check warning on line 49 in src/views/scroll.rs

View check run for this annotation

Codecov / codecov/patch

src/views/scroll.rs#L47-L49

Added lines #L47 - L49 were not covered by tests

prop_extracter! {
ScrollStyle {
Expand Down
16 changes: 13 additions & 3 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,18 +692,28 @@ impl WindowHandle {
}
UpdateMessage::BaseStyle { id, style } => {
let state = cx.app_state.view_state(id);
let old_any_inherited = state.style.any_inherited();

Check warning on line 695 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L695

Added line #L695 was not covered by tests
state.base_style = Some(style);
cx.request_layout(id);
if state.style.any_inherited() || old_any_inherited {
cx.app_state.request_layout_recursive(id);
} else {
cx.request_layout(id);
}

Check warning on line 701 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L697-L701

Added lines #L697 - L701 were not covered by tests
}
UpdateMessage::Style { id, style } => {
let state = cx.app_state.view_state(id);
let old_any_inherited = state.style.any_inherited();

Check warning on line 705 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L705

Added line #L705 was not covered by tests
state.style = style;
cx.request_layout(id);
if state.style.any_inherited() || old_any_inherited {
cx.app_state.request_layout_recursive(id);

Check warning on line 708 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L707-L708

Added lines #L707 - L708 were not covered by tests
} else {
cx.request_layout(id);
}

Check warning on line 711 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L710-L711

Added lines #L710 - L711 were not covered by tests
}
UpdateMessage::Class { id, class } => {
let state = cx.app_state.view_state(id);
state.class = Some(class);
cx.request_layout(id);
cx.app_state.request_layout_recursive(id);

Check warning on line 716 in src/window_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/window_handle.rs#L716

Added line #L716 was not covered by tests
}
UpdateMessage::StyleSelector {
id,
Expand Down

0 comments on commit 5fcffda

Please sign in to comment.