diff --git a/druid/src/contexts.rs b/druid/src/contexts.rs index 707d89244c..f59d5d1688 100644 --- a/druid/src/contexts.rs +++ b/druid/src/contexts.rs @@ -72,6 +72,7 @@ pub struct UpdateCtx<'a> { // invalidations, which would mean a structure very much like // `EventCtx` (and possibly using the same structure). But for // now keep it super-simple. + pub(crate) command_queue: &'a mut CommandQueue, pub(crate) window_id: WindowId, pub(crate) base_state: &'a mut BaseState, } @@ -503,6 +504,23 @@ impl<'a> UpdateCtx<'a> { self.base_state.needs_inval = true; } + /// Submit a [`Command`] to be run after layout and paint finish. + /// + /// **Note:** + /// + /// Commands submited during an `update` call are handled *after* update, + /// layout, and paint have completed; this will trigger a new event cycle. + /// + /// [`Command`]: struct.Command.html + pub fn submit_command( + &mut self, + command: impl Into, + target: impl Into>, + ) { + let target = target.into().unwrap_or_else(|| self.window_id.into()); + self.command_queue.push_back((target, command.into())) + } + /// Get an object which can create text layouts. pub fn text(&mut self) -> Text { self.window.text() diff --git a/druid/src/core.rs b/druid/src/core.rs index 7a043abd2a..79140ea5a5 100644 --- a/druid/src/core.rs +++ b/druid/src/core.rs @@ -714,6 +714,7 @@ impl> WidgetPod { window: ctx.window, base_state: &mut self.state, window_id: ctx.window_id, + command_queue: ctx.command_queue, }; self.inner diff --git a/druid/src/window.rs b/druid/src/window.rs index 04e0bc8fe7..73223206db 100644 --- a/druid/src/window.rs +++ b/druid/src/window.rs @@ -264,6 +264,7 @@ impl Window { let mut base_state = BaseState::new(self.root.id()); let mut update_ctx = UpdateCtx { base_state: &mut base_state, + command_queue: queue, window: &self.handle, window_id: self.id, };