Skip to content

Commit

Permalink
Add a view inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Nov 1, 2023
1 parent 40e1700 commit 9f293eb
Show file tree
Hide file tree
Showing 16 changed files with 733 additions and 79 deletions.
14 changes: 13 additions & 1 deletion examples/counter/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_signal,
unit::UnitExt,
Expand All @@ -8,7 +10,7 @@ use floem::{

fn app_view() -> impl View {
let (counter, set_counter) = create_signal(0);
stack((
let view = stack((
label(move || format!("Value: {}", counter.get())).style(|s| s.padding(10.0)),
stack((
text("Increment")
Expand Down Expand Up @@ -72,6 +74,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
14 changes: 12 additions & 2 deletions examples/widget-gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn app_view() -> impl View {
let (tabs, _set_tabs) = create_signal(tabs);

let (active_tab, set_active_tab) = create_signal(0);
stack({
let view = stack({
(
container({
scroll({
Expand Down Expand Up @@ -144,7 +144,17 @@ fn app_view() -> impl View {
)
})
.style(|s| s.size(100.pct(), 100.pct()))
.window_title(|| "Widget Gallery".to_owned())
.window_title(|| "Widget Gallery".to_owned());

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
})
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Img<'a> {
}

pub trait Renderer {
fn begin(&mut self);
fn begin(&mut self, capture: bool);

fn transform(&mut self, transform: Affine);

Expand Down Expand Up @@ -49,5 +49,5 @@ pub trait Renderer {

fn draw_img(&mut self, img: Img<'_>, rect: Rect);

fn finish(&mut self);
fn finish(&mut self) -> Option<DynamicImage>;
}
10 changes: 9 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cell::RefCell, sync::Arc};

use floem_reactive::WriteSignal;
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use winit::{
Expand All @@ -8,7 +9,10 @@ use winit::{
window::WindowId,
};

use crate::{action::Timer, app_handle::ApplicationHandle, view::View, window::WindowConfig};
use crate::{
action::Timer, app_handle::ApplicationHandle, inspector::Capture, view::View,
window::WindowConfig,
};

type AppEventCallback = dyn Fn(AppEvent);

Expand Down Expand Up @@ -42,6 +46,10 @@ pub(crate) enum AppUpdateEvent {
CloseWindow {
window_id: WindowId,
},
CaptureWindow {
window_id: WindowId,
capture: WriteSignal<Option<Capture>>,
},
RequestTimer {
timer: Timer,
},
Expand Down
10 changes: 10 additions & 0 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
action::{Timer, TimerToken},
app::{AppUpdateEvent, UserEvent, APP_UPDATE_EVENTS},
ext_event::EXT_EVENT_HANDLER,
inspector::Capture,
view::View,
window::WindowConfig,
window_handle::WindowHandle,
Expand Down Expand Up @@ -64,6 +65,9 @@ impl ApplicationHandle {
AppUpdateEvent::RequestTimer { timer } => {
self.request_timer(timer, event_loop);
}
AppUpdateEvent::CaptureWindow { window_id, capture } => {
capture.set(self.capture_window(window_id));
}

Check warning on line 70 in src/app_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/app_handle.rs#L68-L70

Added lines #L68 - L70 were not covered by tests
#[cfg(target_os = "linux")]
AppUpdateEvent::MenuAction {
window_id,
Expand Down Expand Up @@ -234,6 +238,12 @@ impl ApplicationHandle {
}
}

fn capture_window(&mut self, window_id: WindowId) -> Option<Capture> {
self.window_handles
.get_mut(&window_id)
.map(|handle| handle.capture())
}

Check warning on line 245 in src/app_handle.rs

View check run for this annotation

Codecov / codecov/patch

src/app_handle.rs#L241-L245

Added lines #L241 - L245 were not covered by tests

pub(crate) fn idle(&mut self) {
while let Some(trigger) = { EXT_EVENT_HANDLER.queue.lock().pop_front() } {
trigger.notify();
Expand Down
4 changes: 4 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ pub struct AppState {
pub(crate) keyboard_navigation: bool,
pub(crate) window_menu: HashMap<usize, Box<dyn Fn()>>,
pub(crate) context_menu: HashMap<usize, Box<dyn Fn()>>,

/// This is set if we're currently capturing the window for the inspector.
pub(crate) capture: Option<()>,
}

impl Default for AppState {
Expand Down Expand Up @@ -298,6 +301,7 @@ impl AppState {
grid_bps: GridBreakpoints::default(),
window_menu: HashMap::new(),
context_menu: HashMap::new(),
capture: None,

Check warning on line 304 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L304

Added line #L304 was not covered by tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
pointer::{PointerInputEvent, PointerMoveEvent, PointerWheelEvent},
};

#[derive(Hash, PartialEq, Eq)]
#[derive(Debug, Hash, PartialEq, Eq)]

Check warning on line 12 in src/event.rs

View check run for this annotation

Codecov / codecov/patch

src/event.rs#L12

Added line #L12 was not covered by tests
pub enum EventListener {
KeyDown,
KeyUp,
Expand Down
4 changes: 4 additions & 0 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ impl Id {
self.add_update_message(UpdateMessage::PopoutMenu { id: *self, menu });
}

pub fn inspect(&self) {
self.add_update_message(UpdateMessage::Inspect);
}

Check warning on line 219 in src/id.rs

View check run for this annotation

Codecov / codecov/patch

src/id.rs#L217-L219

Added lines #L217 - L219 were not covered by tests

fn add_update_message(&self, msg: UpdateMessage) {
CENTRAL_UPDATE_MESSAGES.with(|msgs| {
msgs.borrow_mut().push((*self, msg));
Expand Down
Loading

0 comments on commit 9f293eb

Please sign in to comment.