Skip to content

Commit

Permalink
Merge pull request #1526 from Maan2003/delegatectx-get-external-handle
Browse files Browse the repository at this point in the history
Provide get_external_handle on DelegateCtx
  • Loading branch information
maan2003 authored Jan 11, 2021
2 parents d8041c2 + 8b37b44 commit 5fbbeb4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
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,
{
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

0 comments on commit 5fbbeb4

Please sign in to comment.