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

Provide get_external_handle on DelegateCtx #1526

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ You can find its changes [documented below](#070---2021-01-01).
### Added
- Sub windows: Allow opening windows that share state with arbitrary parts of the widget hierarchy ([#1254] by [@rjwittams])
- WindowCloseRequested/WindowDisconnected event when a window is closing ([#1254] by [@rjwittams])
- RichTextBuilder ([#1520] by [@Maan2003])
- `get_external_handle` on `DelegateCtx` ([#1526] by [@Maan2003])

### Changed

Expand All @@ -19,7 +21,7 @@ You can find its changes [documented below](#070---2021-01-01).

### Fixed

- Fixed docs of derived Lens ([(#1523)] by [@Maan2003])
- Fixed docs of derived Lens ([#1523] by [@Maan2003])

### Visual

Expand Down Expand Up @@ -592,7 +594,9 @@ Last release without a changelog :(
[#1448]: https://github.com/linebender/druid/pull/1448
[#1463]: https://github.com/linebender/druid/pull/1463
[#1452]: https://github.com/linebender/druid/pull/1452
[#1520]: https://github.com/linebender/druid/pull/1520
[#1523]: https://github.com/linebender/druid/pull/1523
[#1526]: https://github.com/linebender/druid/pull/1523

[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
Expand Down
13 changes: 11 additions & 2 deletions druid/src/app_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
use std::any::{Any, TypeId};

use crate::{
commands, core::CommandQueue, Command, Data, Env, Event, Handled, MenuDesc, SingleUse, Target,
WindowDesc, WindowId,
commands, core::CommandQueue, ext_event::ExtEventHost, Command, Data, Env, Event, ExtEventSink,
Handled, MenuDesc, SingleUse, Target, WindowDesc, WindowId,
};

/// A context passed in to [`AppDelegate`] functions.
///
/// [`AppDelegate`]: trait.AppDelegate.html
pub struct DelegateCtx<'a> {
pub(crate) command_queue: &'a mut CommandQueue,
pub(crate) ext_event_host: &'a ExtEventHost,
pub(crate) app_data_type: TypeId,
}

Expand All @@ -45,6 +46,14 @@ impl<'a> DelegateCtx<'a> {
.push_back(command.into().default_to(Target::Global))
}

/// Returns an [`ExtEventSink`] that can be moved between threads,
/// and can be used to submit commands back to the application.
///
/// [`ExtEventSink`]: struct.ExtEventSink.html
pub fn get_external_handle(&self) -> ExtEventSink {
self.ext_event_host.make_sink()
}

/// Create a new window.
/// `T` must be the application's root `Data` type (the type provided to [`AppLauncher::launch`]).
///
Expand Down
6 changes: 4 additions & 2 deletions druid/src/win_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,22 @@ impl<T: Data> Inner<T> {
/// is configured.
fn with_delegate<R, F>(&mut self, f: F) -> Option<R>
where
F: FnOnce(&mut Box<dyn AppDelegate<T>>, &mut T, &Env, &mut DelegateCtx) -> R,
F: FnOnce(&mut dyn AppDelegate<T>, &mut T, &Env, &mut DelegateCtx) -> R,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
let Inner {
ref mut delegate,
ref mut command_queue,
ref mut data,
ref ext_event_host,
ref env,
..
} = self;
let mut ctx = DelegateCtx {
command_queue,
app_data_type: TypeId::of::<T>(),
ext_event_host,
};
if let Some(delegate) = delegate {
if let Some(delegate) = delegate.as_deref_mut() {
Some(f(delegate, data, env, &mut ctx))
} else {
None
Expand Down