Skip to content

Commit

Permalink
Various tweaks and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Nov 2, 2023
1 parent dd3149d commit 0fcbbb2
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 44 deletions.
1 change: 0 additions & 1 deletion examples/counter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ fn app_view() -> impl View {
.flex_col()
.items_center()
.justify_center()
.background(Color::WHITE)
});

let id = view.id();
Expand Down
14 changes: 13 additions & 1 deletion examples/window-scale/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use floem::{
event::{Event, EventListener},
keyboard::{Key, NamedKey},
peniko::Color,
reactive::{create_rw_signal, create_signal},
unit::UnitExt,
Expand All @@ -9,7 +11,7 @@ use floem::{
fn app_view() -> impl View {
let (counter, set_counter) = create_signal(0);
let window_scale = create_rw_signal(1.0);
stack((
let view = stack((
label(move || format!("Value: {}", counter.get())).style(|s| s.padding(10.0)),
stack({
(
Expand Down Expand Up @@ -121,6 +123,16 @@ fn app_view() -> impl View {
.flex_col()
.items_center()
.justify_center()
});

let id = view.id();
view.on_event(EventListener::KeyUp, move |e| {
if let Event::KeyUp(e) = e {
if e.key.logical_key == Key::Named(NamedKey::F11) {
id.inspect();
}
}
true
})
}

Expand Down
103 changes: 80 additions & 23 deletions src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::context::AppState;
use crate::event::{Event, EventListener};
use crate::id::Id;
use crate::new_window;
use crate::style::TextOverflow;
use crate::view::View;
use crate::views::{
dyn_container, empty, img_dynamic, list, scroll, stack, text, Decorators, Label,
Expand All @@ -15,7 +16,7 @@ use peniko::Color;
use std::cell::Cell;
use std::fmt::Display;
use std::rc::Rc;
use std::time::Instant;
use std::time::{Duration, Instant};
use taffy::style::AlignItems;
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowId;
Expand Down Expand Up @@ -71,7 +72,10 @@ pub struct Capture {
pub start: Instant,
pub post_layout: Instant,
pub end: Instant,
pub taffy_duration: Duration,
pub window: Option<Rc<DynamicImage>>,
pub window_size: Size,
pub scale: f64,
}

impl Capture {}
Expand All @@ -83,14 +87,15 @@ pub fn captured_view(
highlighted: RwSignal<Option<Id>>,
) -> Box<dyn View> {
let offset = depth as f64 * 14.0;
let name = text(view.name.clone());
let name = text(view.name.clone()).style(|s| s.text_overflow(TextOverflow::Ellipsis));
let height = 20.0;
let id = view.id;

if view.children.is_empty() {
return Box::new(
name.style(move |s| {
s.width_full()
.text_overflow(TextOverflow::Ellipsis)
.padding_left(20.0 + offset)
.hover(move |s| {
s.background(Color::rgba8(228, 237, 216, 160))
Expand Down Expand Up @@ -229,35 +234,67 @@ fn header(label: impl Display) -> Label {
})
}

Check warning on line 235 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L226-L235

Added lines #L226 - L235 were not covered by tests

fn info(s: String) -> Label {
text(s).style(|s| s.padding(5.0))
}

Check warning on line 239 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L237-L239

Added lines #L237 - L239 were not covered by tests

fn stats(capture: &Capture) -> impl View {
let layout_time = capture.post_layout.saturating_duration_since(capture.start);
let paint_time = capture.end.saturating_duration_since(capture.post_layout);
let layout_time = text(format!(
let layout_time = info(format!(
"Layout time: {:.4} ms",
layout_time.as_secs_f64() * 1000.0
))
.style(|s| s.padding(5.0));
let paint_time = text(format!(
));
let taffy_time = info(format!(
"Taffy time: {:.4} ms",
capture.taffy_duration.as_secs_f64() * 1000.0
));
let paint_time = info(format!(
"Paint time: {:.4} ms",
paint_time.as_secs_f64() * 1000.0
))
.style(|s| s.padding(5.0));
stack((layout_time, paint_time)).style(|s| s.flex_col())
));
let w = info(format!("Window Width: {}", capture.window_size.width));
let h = info(format!("Window Height: {}", capture.window_size.height));
stack((layout_time, taffy_time, paint_time, w, h)).style(|s| s.flex_col())
}

Check warning on line 259 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L241-L259

Added lines #L241 - L259 were not covered by tests

fn selected_view(capture: &Rc<Capture>, selected: RwSignal<Option<Id>>) -> impl View {
let capture = capture.clone();
dyn_container(
move || selected.get(),
move |current| {
let info = |i| text(i).style(|s| s.padding(5.0));
if let Some(view) = current.and_then(|id| capture.root.find(id)) {
let name = info(format!("Type: {}", view.name));
let count = info(format!("Child Count: {}", view.children.len()));
let x = info(format!("X: {}", view.layout.x0));
let y = info(format!("Y: {}", view.layout.y0));
let w = info(format!("Width: {}", view.layout.width()));
let h = info(format!("Height: {}", view.layout.height()));
let beyond = |view: f64, window| {
if view > window {
format!(" ({} after window edge)", view - window)
} else if view < 0.0 {
format!(" ({} before window edge)", -view)

Check warning on line 273 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L261-L273

Added lines #L261 - L273 were not covered by tests
} else {
String::new()

Check warning on line 275 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L275

Added line #L275 was not covered by tests
}
};
let x = info(format!(
"X: {}{}",
view.layout.x0,
beyond(view.layout.x0, capture.window_size.width)
));
let y = info(format!(
"Y: {}{}",
view.layout.y0,
beyond(view.layout.y0, capture.window_size.height)
));
let w = info(format!(
"Width: {}{}",
view.layout.width(),
beyond(view.layout.x1, capture.window_size.width)
));
let h = info(format!(
"Height: {}{}",
view.layout.height(),
beyond(view.layout.y1, capture.window_size.height)
));
let clear = text("Clear selection")
.style(|s| {
s.background(Color::WHITE_SMOKE)
Expand Down Expand Up @@ -291,13 +328,25 @@ fn capture_view(capture: &Rc<Capture>) -> impl View {
let window = capture.window.clone();
let capture_ = capture.clone();
let capture__ = capture.clone();
let (image_width, image_height) = capture
.window
.as_ref()
.map(|img| {
(
img.width() as f64 / capture.scale,
img.height() as f64 / capture.scale,
)
})
.unwrap_or_default();
let image = img_dynamic(move || window.clone())
.style(|s| {
.style(move |s| {
s.margin(5.0)
.border(1.0)
.border_color(Color::BLACK.with_alpha_factor(0.5))
.margin_bottom(25.0)
.margin_right(25.0)
.width(image_width + 2.0)
.height(image_height + 2.0)
.margin_bottom(21.0)
.margin_right(21.0)
})
.on_event(EventListener::PointerMove, move |e| {
if let Event::PointerMove(e) = e {
Expand Down Expand Up @@ -362,13 +411,20 @@ fn capture_view(capture: &Rc<Capture>) -> impl View {

let image = stack((image, selected_overlay, highlighted_overlay));

let left_scroll = scroll(
stack((
header("Selected View"),
selected_view(capture, selected),
header("Stats"),
stats(capture),
))
.style(|s| s.flex_col().width_full()),
);

let left = stack((
header("Captured Window"),
scroll(image).style(|s| s.max_height_pct(60.0)),
header("Selected View"),
scroll(selected_view(capture, selected)),
header("Stats"),
scroll(stats(capture)),
left_scroll,
))
.style(|s| s.flex_col().height_full().max_width_pct(60.0));

Expand Down Expand Up @@ -414,12 +470,13 @@ fn inspector_view(capture: &Option<Rc<Capture>>) -> impl View {
.width_full()
.height_full()
.background(Color::WHITE)
.set(scroll::Thickness, 20.0)
.set(scroll::Thickness, 16.0)
.set(scroll::Rounded, false)
.set(scroll::HandleRadius, 4.0)
.set(scroll::HandleColor, Color::rgba8(166, 166, 166, 140))
.set(scroll::DragColor, Color::rgb8(166, 166, 166))
.set(scroll::HoverColor, Color::rgb8(184, 184, 184))
.set(scroll::BgActiveColor, Color::rgba8(166, 166, 166, 40))
.set(scroll::BgActiveColor, Color::rgba8(166, 166, 166, 30))
})
}

Check warning on line 481 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L462-L481

Added lines #L462 - L481 were not covered by tests

Expand Down
8 changes: 6 additions & 2 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,8 @@ impl View for Box<dyn View> {
(**self).children_mut()
}

fn compute_layout(&mut self, cx: &mut LayoutCx) -> Option<Rect> {
(**self).compute_layout(cx)
fn debug_name(&self) -> std::borrow::Cow<'static, str> {
(**self).debug_name()

Check warning on line 1065 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L1064-L1065

Added lines #L1064 - L1065 were not covered by tests
}

fn update(&mut self, cx: &mut UpdateCx, state: Box<dyn Any>) -> ChangeFlags {
Expand All @@ -1073,6 +1073,10 @@ impl View for Box<dyn View> {
(**self).layout(cx)
}

fn compute_layout(&mut self, cx: &mut LayoutCx) -> Option<Rect> {
(**self).compute_layout(cx)
}

Check warning on line 1078 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L1076-L1078

Added lines #L1076 - L1078 were not covered by tests

fn event(&mut self, cx: &mut EventCx, id_path: Option<&[Id]>, event: Event) -> bool {
(**self).event(cx, id_path, event)
}
Expand Down
8 changes: 5 additions & 3 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ prop!(pub DragColor: Color { inherited } = Color::rgba8(0, 0, 0, 160));
prop!(pub Rounded: bool { inherited } = cfg!(target_os = "macos"));
prop!(pub Thickness: Px { inherited } = Px(10.0));
prop!(pub EdgeWidth: Px { inherited } = Px(0.0));
prop!(pub HandleRadius: Px { inherited } = 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#L49

Added line #L49 was not covered by tests
prop!(pub BgActiveColor: Color { inherited } = Color::rgba8(0, 0, 0, 25));

prop_extracter! {
ScrollStyle {
handle_color: HandleColor,
hover_color: Option<HoverColor>,
drag_color: Option<DragColor>,
handle_radius: HandleRadius,
rounded: Rounded,
thickness: Thickness,
edge_width: EdgeWidth,
Expand Down Expand Up @@ -329,7 +331,7 @@ impl<V: View> Scroll<V> {
(rect.y1 - rect.y0) / 2.
}
} else {
0.
self.style.handle_radius().0

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

View check run for this annotation

Codecov / codecov/patch

src/views/scroll.rs#L334

Added line #L334 was not covered by tests
}
};

Expand All @@ -350,7 +352,7 @@ impl<V: View> Scroll<V> {
self.style.handle_color()
};
if self.vbar_whole_hover || matches!(self.held, BarHeldState::Vertical(..)) {
let mut bounds = bounds;
let mut bounds = bounds - scroll_offset;

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

View check run for this annotation

Codecov / codecov/patch

src/views/scroll.rs#L355

Added line #L355 was not covered by tests
bounds.y0 = self.actual_rect.y0;
bounds.y1 = self.actual_rect.y1;
if let Some(color) = self.style.bg_active_color() {
Expand Down Expand Up @@ -383,7 +385,7 @@ impl<V: View> Scroll<V> {
self.style.handle_color()
};
if self.hbar_whole_hover || matches!(self.held, BarHeldState::Horizontal(..)) {
let mut bounds = bounds;
let mut bounds = bounds - scroll_offset;

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

View check run for this annotation

Codecov / codecov/patch

src/views/scroll.rs#L388

Added line #L388 was not covered by tests
bounds.x0 = self.actual_rect.x0;
bounds.x1 = self.actual_rect.x1;
if let Some(color) = self.style.bg_active_color() {
Expand Down
Loading

0 comments on commit 0fcbbb2

Please sign in to comment.