Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ You can find its changes [documented below](#070---2021-01-01).
- Add #[data(eq)] shorthand attribute for Data derive macro ([#1884] by [@Maan2003])
- X11: detect keyboard layout ([#1779] by [@Maan2003])
- WindowDesc::with_config ([#1929] by [@Maan2003])
- `Notification::route` ([#1978] by [@xarvic])

### Changed

Expand Down Expand Up @@ -793,8 +794,9 @@ Last release without a changelog :(
[#1919]: https://github.com/linebender/druid/pull/1919
[#1929]: https://github.com/linebender/druid/pull/1929
[#1947]: https://github.com/linebender/druid/pull/1947
[#1967]: https://github.com/linebender/druid/pull/1967
[#1953]: https://github.com/linebender/druid/pull/1953
[#1967]: https://github.com/linebender/druid/pull/1967
[#1978]: https://github.com/linebender/druid/pull/1978

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
30 changes: 30 additions & 0 deletions druid/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub struct Notification {
symbol: SelectorSymbol,
payload: Arc<dyn Any>,
source: WidgetId,
route: WidgetId,
}

/// A wrapper type for [`Command`] payloads that should only be used once.
Expand Down Expand Up @@ -407,6 +408,7 @@ impl Command {
symbol: self.symbol,
payload: self.payload,
source,
route: source,
}
}

Expand Down Expand Up @@ -528,6 +530,34 @@ impl Notification {
pub fn source(&self) -> WidgetId {
self.source
}

/// Change the route id
pub(crate) fn with_route(mut self, widget_id: WidgetId) -> Self {
self.route = widget_id;
self
}

/// The [`WidgetId`] of the last [`Widget`] that this [`Notification`] was passed through.
///
/// # Example
///
/// ```ignore
/// fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut (), env: &Env) {
/// if let Event::Notification(notification) = event {
/// if notification.route() == self.widget1.id() {
/// // the notification came from inside of widget1
/// }
/// if notification.route() == self.widget2.id() {
/// // the notification came from inside of widget2
/// }
/// }
/// }
/// ```
///
/// [`Widget`]: crate::Widget
pub fn route(&self) -> WidgetId {
self.route
}
}

impl<T: Any> SingleUse<T> {
Expand Down
4 changes: 3 additions & 1 deletion druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,9 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
inner_ctx.is_handled = false;
} else if let Event::Notification(notification) = event {
// we will try again with the next parent
inner_ctx.notifications.push_back(notification);
inner_ctx
.notifications
.push_back(notification.with_route(self_id));
} else {
// could be unchecked but we avoid unsafe in druid :shrug:
unreachable!()
Expand Down