diff --git a/Cargo.lock b/Cargo.lock index 91594dca..e1cd57e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2459,8 +2459,10 @@ dependencies = [ name = "iced_devtools" version = "0.14.0-dev" dependencies = [ + "iced_debug", "iced_program", "iced_widget", + "smol", ] [[package]] diff --git a/devtools/Cargo.toml b/devtools/Cargo.toml index 1d43566b..927398bf 100644 --- a/devtools/Cargo.toml +++ b/devtools/Cargo.toml @@ -16,3 +16,7 @@ workspace = true [dependencies] iced_program.workspace = true iced_widget.workspace = true +iced_debug.workspace = true + +# TODO: Use program executor? +smol.workspace = true diff --git a/devtools/src/lib.rs b/devtools/src/lib.rs index 7ef39ead..cec392ff 100644 --- a/devtools/src/lib.rs +++ b/devtools/src/lib.rs @@ -1,15 +1,20 @@ #![allow(missing_docs)] -use crate::runtime::futures; +use iced_debug as debug; use iced_program as program; +use iced_widget as widget; use iced_widget::core; use iced_widget::runtime; +use iced_widget::runtime::futures; use crate::core::Element; -use crate::core::theme; +use crate::core::keyboard; +use crate::core::theme::{self, Base, Theme}; +use crate::core::time::seconds; use crate::core::window; use crate::futures::Subscription; use crate::program::Program; use crate::runtime::Task; +use crate::widget::{bottom_right, container, stack, text, themer}; use std::fmt; @@ -33,9 +38,10 @@ pub fn attach(program: impl Program + 'static) -> impl Program { } fn boot(&self) -> (Self::State, Task) { - let (state, task) = self.program.boot(); + let (state, boot) = self.program.boot(); + let (devtools, task) = DevTools::new(state); - (DevTools { state }, task.map(Message::Program)) + (devtools, Task::batch([boot.map(Message::Program), task])) } fn update( @@ -94,12 +100,25 @@ where P: Program, { state: P::State, + show_notification: bool, } impl

DevTools

where P: Program + 'static, { + pub fn new(state: P::State) -> (Self, Task>) { + ( + Self { + state, + show_notification: true, + }, + Task::perform(smol::Timer::after(seconds(2)), |_| { + Message::HideNotification + }), + ) + } + pub fn title(&self, program: &P, window: window::Id) -> String { program.title(&self.state, window) } @@ -110,6 +129,16 @@ where message: Message

, ) -> Task> { match message { + Message::HideNotification => { + self.show_notification = false; + + Task::none() + } + Message::ToggleComet => { + debug::toggle_comet(); + + Task::none() + } Message::Program(message) => program .update(&mut self.state, message) .map(Message::Program), @@ -121,11 +150,39 @@ where program: &P, window: window::Id, ) -> Element<'_, Message

, P::Theme, P::Renderer> { - program.view(&self.state, window).map(Message::Program) + let view = program.view(&self.state, window).map(Message::Program); + let theme = program.theme(&self.state, window); + + let notification = themer( + theme + .palette() + .map(|palette| Theme::custom("DevTools".to_owned(), palette)) + .unwrap_or_default(), + bottom_right( + container(text("Press F12 to open debug metrics")) + .padding(10) + .style(container::dark), + ), + ); + + stack![view] + .push_maybe(self.show_notification.then_some(notification)) + .into() } pub fn subscription(&self, program: &P) -> Subscription> { - program.subscription(&self.state).map(Message::Program) + let subscription = + program.subscription(&self.state).map(Message::Program); + + let hotkeys = + futures::keyboard::on_key_press(|key, _modifiers| match key { + keyboard::Key::Named(keyboard::key::Named::F12) => { + Some(Message::ToggleComet) + } + _ => None, + }); + + Subscription::batch([subscription, hotkeys]) } pub fn theme(&self, program: &P, window: window::Id) -> P::Theme { @@ -146,6 +203,8 @@ enum Message

where P: Program, { + HideNotification, + ToggleComet, Program(P::Message), } @@ -155,6 +214,10 @@ where { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + Message::HideNotification => { + f.write_str("DevTools(HideNotification)") + } + Message::ToggleComet => f.write_str("DevTools(ToggleComet)"), Message::Program(message) => message.fmt(f), } } diff --git a/winit/src/lib.rs b/winit/src/lib.rs index bafab33b..bb9500e8 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -40,7 +40,6 @@ pub use clipboard::Clipboard; pub use error::Error; pub use proxy::Proxy; -use crate::core::keyboard; use crate::core::mouse; use crate::core::renderer; use crate::core::theme; @@ -892,20 +891,6 @@ async fn run_instance

( window.state.scale_factor(), window.state.modifiers(), ) { - if matches!( - event, - core::Event::Keyboard( - keyboard::Event::KeyPressed { - modified_key: keyboard::Key::Named( - keyboard::key::Named::F12 - ), - .. - } - ) - ) { - debug::toggle_comet(); - } - events.push((id, event)); } }