Skip to content

Commit

Permalink
Add h_stack and v_stack variants of stack. Make static_list d…
Browse files Browse the repository at this point in the history
…efault to `FlexDirection::Column`.
  • Loading branch information
Zoxc committed Nov 5, 2023
1 parent 749db2b commit 4047c47
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
47 changes: 17 additions & 30 deletions src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::id::Id;
use crate::style::{Style, StyleMapValue};
use crate::view::View;
use crate::views::{
dyn_container, empty, img_dynamic, scroll, stack, static_list, text, Decorators, Label,
dyn_container, empty, img_dynamic, scroll, stack, static_list, text, v_stack, Decorators, Label,
};
use crate::window::WindowConfig;
use crate::{new_window, style};
Expand Down Expand Up @@ -253,16 +253,16 @@ fn captured_view_with_children(
});

let list = static_list(children).style(move |s| {
s.flex_col().display(if expanded.get() {
s.display(if expanded.get() {

Check warning on line 256 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L256

Added line #L256 was not covered by tests
style::Display::Flex
} else {
style::Display::None
})
});

let list = stack((line, list)).style(|s| s.flex_col());
let list = v_stack((line, list));

Check warning on line 263 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L263

Added line #L263 was not covered by tests

Box::new(stack((row, list)).style(|s| s.flex_col().min_width_full()))
Box::new(v_stack((row, list)).style(|s| s.min_width_full()))

Check warning on line 265 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L265

Added line #L265 was not covered by tests
}

fn captured_view(
Expand Down Expand Up @@ -339,7 +339,7 @@ fn stats(capture: &Capture) -> impl View {
);
let w = info("Window Width", format!("{}", capture.window_size.width));
let h = info("Window Height", format!("{}", capture.window_size.height));
stack((
v_stack((

Check warning on line 342 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L342

Added line #L342 was not covered by tests
style_time,
layout_time,
taffy_time,
Expand All @@ -349,7 +349,6 @@ fn stats(capture: &Capture) -> impl View {
w,
h,
))
.style(|s| s.flex_col())
}

fn selected_view(capture: &Rc<Capture>, selected: RwSignal<Option<Id>>) -> impl View {
Expand Down Expand Up @@ -528,7 +527,7 @@ fn selected_view(capture: &Rc<Capture>, selected: RwSignal<Option<Id>>) -> impl
text(format!("{transition:?}")),
))
.style(|s| s.items_center());
v = Box::new(stack((v, transition)).style(|s| s.flex_col()));
v = Box::new(v_stack((v, transition)));

Check warning on line 530 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L530

Added line #L530 was not covered by tests
}
stack((
stack((name.style(|s| {
Expand All @@ -546,10 +545,10 @@ fn selected_view(capture: &Rc<Capture>, selected: RwSignal<Option<Id>>) -> impl
.hover(|s| s.background(Color::rgba8(228, 237, 216, 160)))
})
}))
.style(|s| s.flex_col().width_full());
.style(|s| s.width_full());

Check warning on line 548 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L548

Added line #L548 was not covered by tests

Box::new(
stack((
v_stack((

Check warning on line 551 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L551

Added line #L551 was not covered by tests
name,
count,
x,
Expand All @@ -564,7 +563,7 @@ fn selected_view(capture: &Rc<Capture>, selected: RwSignal<Option<Id>>) -> impl
style_header,
style_list,
))
.style(|s| s.flex_col().width_full()),
.style(|s| s.width_full()),

Check warning on line 566 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L566

Added line #L566 was not covered by tests
)
} else {
Box::new(text("No selection".to_string()).style(|s| s.padding(5.0)))
Expand Down Expand Up @@ -666,13 +665,13 @@ fn capture_view(capture: &Rc<Capture>) -> impl View {
let image = stack((image, selected_overlay, highlighted_overlay));

let left_scroll = scroll(
stack((
v_stack((

Check warning on line 668 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L668

Added line #L668 was not covered by tests
header("Selected View"),
selected_view(capture, selected),
header("Stats"),
stats(capture),
))
.style(|s| s.flex_col().width_full()),
.style(|s| s.width_full()),

Check warning on line 674 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L674

Added line #L674 was not covered by tests
)
.style(|s| s.width_full().flex_basis(0).min_height(0).flex_grow(1.0));

Expand All @@ -682,22 +681,16 @@ fn capture_view(capture: &Rc<Capture>) -> impl View {
.background(Color::BLACK.with_alpha_factor(0.2))
});

let left = stack((
let left = v_stack((

Check warning on line 684 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L684

Added line #L684 was not covered by tests
header("Captured Window"),
scroll(image).style(|s| s.max_height_pct(60.0)),
seperator,
left_scroll,
))
.style(|s| s.flex_col().max_width_pct(60.0));
.style(|s| s.max_width_pct(60.0));

Check warning on line 690 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L690

Added line #L690 was not covered by tests

let tree = scroll(captured_view(&capture.root, 0, selected, highlighted))
.style(|s| {
s.flex_col()
.width_full()
.min_height(0)
.flex_basis(0)
.flex_grow(1.0)
})
.style(|s| s.width_full().min_height(0).flex_basis(0).flex_grow(1.0))

Check warning on line 693 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L693

Added line #L693 was not covered by tests
.on_event(EventListener::PointerLeave, move |_| {
highlighted.set(None);
false
Expand All @@ -708,18 +701,12 @@ fn capture_view(capture: &Rc<Capture>) -> impl View {
});

let tree: Box<dyn View> = if capture.root.warnings() {
Box::new(stack((header("Warnings"), header("View Tree"), tree)))
Box::new(v_stack((header("Warnings"), header("View Tree"), tree)))

Check warning on line 704 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L704

Added line #L704 was not covered by tests
} else {
Box::new(stack((header("View Tree"), tree)))
Box::new(v_stack((header("View Tree"), tree)))

Check warning on line 706 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L706

Added line #L706 was not covered by tests
};

let tree = tree.style(|s| {
s.flex_col()
.height_full()
.min_width(0)
.flex_basis(0)
.flex_grow(1.0)
});
let tree = tree.style(|s| s.height_full().min_width(0).flex_basis(0).flex_grow(1.0));

Check warning on line 709 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L709

Added line #L709 was not covered by tests

let seperator = empty().style(move |s| {
s.height_full()
Expand Down
40 changes: 38 additions & 2 deletions src/views/stack.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
use kurbo::Rect;
use taffy::style::FlexDirection;

use crate::{
context::{EventCx, UpdateCx},
id::Id,
style::Style,
view::{ChangeFlags, View},
view_tuple::ViewTuple,
};

pub struct Stack<VT> {
id: Id,
children: VT,
direction: Option<FlexDirection>,
}

pub fn stack<VT: ViewTuple + 'static>(children: VT) -> Stack<VT> {
let id = Id::next();
Stack { id, children }
Stack {
id,
children,
direction: None,
}
}

Check warning on line 25 in src/views/stack.rs

View check run for this annotation

Codecov / codecov/patch

src/views/stack.rs#L20-L25

Added lines #L20 - L25 were not covered by tests

/// A stack which defaults to `FlexDirection::Row`.
pub fn h_stack<VT: ViewTuple + 'static>(children: VT) -> Stack<VT> {
let id = Id::next();
Stack {
id,
children,
direction: Some(FlexDirection::Row),
}
}

Check warning on line 35 in src/views/stack.rs

View check run for this annotation

Codecov / codecov/patch

src/views/stack.rs#L28-L35

Added lines #L28 - L35 were not covered by tests

/// A stack which defaults to `FlexDirection::Column`.
pub fn v_stack<VT: ViewTuple + 'static>(children: VT) -> Stack<VT> {
let id = Id::next();
Stack {
id,
children,
direction: Some(FlexDirection::Column),
}

Check warning on line 44 in src/views/stack.rs

View check run for this annotation

Codecov / codecov/patch

src/views/stack.rs#L38-L44

Added lines #L38 - L44 were not covered by tests
}

impl<VT: ViewTuple + 'static> View for Stack<VT> {
fn id(&self) -> Id {
self.id
}

fn view_style(&self) -> Option<crate::style::Style> {
self.direction
.map(|direction| Style::new().flex_direction(direction))
}

Check warning on line 55 in src/views/stack.rs

View check run for this annotation

Codecov / codecov/patch

src/views/stack.rs#L52-L55

Added lines #L52 - L55 were not covered by tests

fn child(&self, id: Id) -> Option<&dyn View> {
self.children.child(id)
}
Expand All @@ -39,7 +71,11 @@ impl<VT: ViewTuple + 'static> View for Stack<VT> {
}

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"Stack".into()
match self.direction {
Some(FlexDirection::Column) => "Vertical Stack".into(),
Some(FlexDirection::Row) => "Horizontal Stack".into(),
_ => "Stack".into(),

Check warning on line 77 in src/views/stack.rs

View check run for this annotation

Codecov / codecov/patch

src/views/stack.rs#L74-L77

Added lines #L74 - L77 were not covered by tests
}
}

fn update(&mut self, cx: &mut UpdateCx, state: Box<dyn std::any::Any>) -> ChangeFlags {
Expand Down
6 changes: 6 additions & 0 deletions src/views/static_list.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{
context::{EventCx, UpdateCx},
id::Id,
style::Style,
view::{ChangeFlags, View},
};
use kurbo::Rect;
use taffy::style::FlexDirection;

pub struct StaticList<V>
where
Expand All @@ -28,6 +30,10 @@ impl<V: View> View for StaticList<V> {
self.id
}

fn view_style(&self) -> Option<crate::style::Style> {
Some(Style::new().flex_direction(FlexDirection::Column))
}

Check warning on line 35 in src/views/static_list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/static_list.rs#L33-L35

Added lines #L33 - L35 were not covered by tests

fn child(&self, id: Id) -> Option<&dyn View> {
self.children
.iter()
Expand Down

0 comments on commit 4047c47

Please sign in to comment.