Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proof of concept widget inspector #291

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions crates/masonry/src/render_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tracing::{debug, info_span, warn};
use vello::peniko::{Color, Fill};
use vello::Scene;
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
use winit::event::MouseButton;
use winit::keyboard::{KeyCode, PhysicalKey};
use winit::window::CursorIcon;

Expand Down Expand Up @@ -41,6 +42,8 @@ pub struct RenderRoot {
// TODO - Add "access_tree_active" to detect when you don't need to update the
// access tree
pub(crate) rebuild_access_tree: bool,
pub(crate) inspector_is_on: bool,
pub(crate) selected_widget: Option<WidgetId>,
}

pub(crate) struct RenderRootState {
Expand Down Expand Up @@ -102,6 +105,8 @@ impl RenderRoot {
font_context: FontContext::default(),
},
rebuild_access_tree: true,
inspector_is_on: false,
selected_widget: None,
};

// We send WidgetAdded to all widgets right away
Expand Down Expand Up @@ -230,6 +235,10 @@ impl RenderRoot {
}

fn root_on_pointer_event(&mut self, event: PointerEvent) -> Handled {
if self.inspector_is_on {
return self.inspector_handle_event(event);
}

let mut widget_state =
WidgetState::new(self.root.id(), Some(self.get_kurbo_size()), "<root>");

Expand Down Expand Up @@ -311,6 +320,13 @@ impl RenderRoot {
self.state.next_focused_widget = self.widget_from_focus_chain(false);
}
}

if handled == Handled::No && key.physical_key == PhysicalKey::Code(KeyCode::F1) {
self.inspector_is_on = !self.inspector_is_on;
self.state
.signal_queue
.push_back(RenderRootSignal::RequestRedraw);
}
}

self.post_event_processing(&mut widget_state);
Expand Down Expand Up @@ -448,6 +464,19 @@ impl RenderRoot {
&empty_path,
);

if let Some(id) = self.selected_widget {
if let Some(widget) = self.root.as_dyn().find_widget_by_id(id) {
let rect = widget.state().window_layout_rect();
scene.fill(
Fill::NonZero,
Affine::IDENTITY,
Color::rgba8(60, 60, 255, 128),
None,
&rect,
);
}
}

scene
}

Expand Down Expand Up @@ -608,6 +637,42 @@ impl RenderRoot {
pub(crate) fn focus_chain(&self) -> &[WidgetId] {
&self.root.state().focus_chain
}

fn inspector_handle_event(&mut self, event: PointerEvent) -> Handled {
match event {
PointerEvent::PointerDown(MouseButton::Left, state) => {
let pos = Point::new(state.position.x, state.position.y);
let selected_widget = self.root.as_dyn().find_widget_at_pos(pos);
if let Some(widget) = selected_widget {
println!(
"Widget '{}' #{} info: {:#?}",
widget.short_type_name(),
widget.state().id.to_raw(),
widget.state()
);
}
self.inspector_is_on = false;
self.selected_widget = None;
Handled::Yes
}
PointerEvent::PointerMove(state) => {
let pos = Point::new(state.position.x, state.position.y);
let selected_widget = self.root.as_dyn().find_widget_at_pos(pos);
if let Some(widget) = selected_widget {
self.state
.signal_queue
.push_back(RenderRootSignal::RequestRedraw);
self.selected_widget = Some(widget.state().id);
}
Handled::Yes
}
PointerEvent::PointerLeave(_) => {
self.selected_widget = None;
Handled::Yes
}
_ => Handled::No,
}
}
}

/*
Expand Down