Skip to content

Commit 0223076

Browse files
xarvicChristophmaan2003
authored
Route id (#1978)
* implemented route Signed-off-by: Christoph <xarvix@web.de> * update CHANGELOG.md Signed-off-by: Christoph <xarvix@web.de> * reformat Signed-off-by: Christoph <xarvix@web.de> * Update command.rs * Update druid/src/command.rs Co-authored-by: Manmeet Maan <49202620+Maan2003@users.noreply.github.com> * add send notifications test Signed-off-by: Christoph <xarvix@web.de> * fix Signed-off-by: Christoph <xarvix@web.de> * fix Signed-off-by: Christoph <xarvix@web.de> Co-authored-by: Christoph <xarvix@web.de> Co-authored-by: Manmeet Maan <49202620+Maan2003@users.noreply.github.com>
1 parent 82ec0be commit 0223076

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ You can find its changes [documented below](#070---2021-01-01).
5858
- Add #[data(eq)] shorthand attribute for Data derive macro ([#1884] by [@Maan2003])
5959
- X11: detect keyboard layout ([#1779] by [@Maan2003])
6060
- WindowDesc::with_config ([#1929] by [@Maan2003])
61+
- `Notification::route` ([#1978] by [@xarvic])
6162

6263
### Changed
6364

@@ -793,8 +794,9 @@ Last release without a changelog :(
793794
[#1919]: https://github.com/linebender/druid/pull/1919
794795
[#1929]: https://github.com/linebender/druid/pull/1929
795796
[#1947]: https://github.com/linebender/druid/pull/1947
796-
[#1967]: https://github.com/linebender/druid/pull/1967
797797
[#1953]: https://github.com/linebender/druid/pull/1953
798+
[#1967]: https://github.com/linebender/druid/pull/1967
799+
[#1978]: https://github.com/linebender/druid/pull/1978
798800

799801
[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
800802
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0

druid/src/command.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub struct Notification {
103103
symbol: SelectorSymbol,
104104
payload: Arc<dyn Any>,
105105
source: WidgetId,
106+
route: WidgetId,
106107
}
107108

108109
/// A wrapper type for [`Command`] payloads that should only be used once.
@@ -407,6 +408,7 @@ impl Command {
407408
symbol: self.symbol,
408409
payload: self.payload,
409410
source,
411+
route: source,
410412
}
411413
}
412414

@@ -528,6 +530,34 @@ impl Notification {
528530
pub fn source(&self) -> WidgetId {
529531
self.source
530532
}
533+
534+
/// Change the route id
535+
pub(crate) fn with_route(mut self, widget_id: WidgetId) -> Self {
536+
self.route = widget_id;
537+
self
538+
}
539+
540+
/// The [`WidgetId`] of the last [`Widget`] that this [`Notification`] was passed through.
541+
///
542+
/// # Example
543+
///
544+
/// ```ignore
545+
/// fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut (), env: &Env) {
546+
/// if let Event::Notification(notification) = event {
547+
/// if notification.route() == self.widget1.id() {
548+
/// // the notification came from inside of widget1
549+
/// }
550+
/// if notification.route() == self.widget2.id() {
551+
/// // the notification came from inside of widget2
552+
/// }
553+
/// }
554+
/// }
555+
/// ```
556+
///
557+
/// [`Widget`]: crate::Widget
558+
pub fn route(&self) -> WidgetId {
559+
self.route
560+
}
531561
}
532562

533563
impl<T: Any> SingleUse<T> {

druid/src/core.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,9 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
903903
inner_ctx.is_handled = false;
904904
} else if let Event::Notification(notification) = event {
905905
// we will try again with the next parent
906-
inner_ctx.notifications.push_back(notification);
906+
inner_ctx
907+
.notifications
908+
.push_back(notification.with_route(self_id));
907909
} else {
908910
// could be unchecked but we avoid unsafe in druid :shrug:
909911
unreachable!()
@@ -1376,7 +1378,7 @@ mod tests {
13761378
use super::*;
13771379
use crate::ext_event::ExtEventHost;
13781380
use crate::text::ParseFormatter;
1379-
use crate::widget::{Flex, Scroll, Split, TextBox};
1381+
use crate::widget::{Button, Flex, Scroll, Split, TextBox};
13801382
use crate::{WidgetExt, WindowHandle, WindowId};
13811383
use test_env_log::test;
13821384

@@ -1438,4 +1440,60 @@ mod tests {
14381440
// A textbox is composed of three components with distinct ids
14391441
assert_eq!(ctx.widget_state.children.entry_count(), 15);
14401442
}
1443+
1444+
#[test]
1445+
fn send_notifications() {
1446+
let mut widget = WidgetPod::new(Button::new("test".to_owned())).boxed();
1447+
1448+
let mut command_queue: CommandQueue = VecDeque::new();
1449+
let mut widget_state = WidgetState::new(WidgetId::next(), None);
1450+
let window = WindowHandle::default();
1451+
let ext_host = ExtEventHost::default();
1452+
let ext_handle = ext_host.make_sink();
1453+
let mut state = ContextState::new::<Option<u32>>(
1454+
&mut command_queue,
1455+
&ext_handle,
1456+
&window,
1457+
WindowId::next(),
1458+
None,
1459+
);
1460+
1461+
let mut ctx = EventCtx {
1462+
widget_state: &mut widget_state,
1463+
notifications: &mut Default::default(),
1464+
is_handled: false,
1465+
state: &mut state,
1466+
is_root: false,
1467+
};
1468+
1469+
let ids = [
1470+
WidgetId::next(),
1471+
WidgetId::next(),
1472+
WidgetId::next(),
1473+
WidgetId::next(),
1474+
];
1475+
1476+
let env = Env::with_default_i10n();
1477+
1478+
let notification = Command::new(druid::command::sys::CLOSE_WINDOW, (), Target::Global);
1479+
1480+
let mut notifictions = VecDeque::from(vec![
1481+
notification.clone().into_notification(ids[0]),
1482+
notification.clone().into_notification(ids[1]),
1483+
notification.clone().into_notification(ids[2]),
1484+
notification.into_notification(ids[3]),
1485+
]);
1486+
1487+
widget.send_notifications(&mut ctx, &mut notifictions, &mut (), &env);
1488+
1489+
assert_eq!(ctx.notifications.len(), 4);
1490+
assert_eq!(ctx.notifications[0].source(), ids[0]);
1491+
assert_eq!(ctx.notifications[0].route(), widget.id());
1492+
assert_eq!(ctx.notifications[1].source(), ids[1]);
1493+
assert_eq!(ctx.notifications[1].route(), widget.id());
1494+
assert_eq!(ctx.notifications[2].source(), ids[2]);
1495+
assert_eq!(ctx.notifications[2].route(), widget.id());
1496+
assert_eq!(ctx.notifications[3].source(), ids[3]);
1497+
assert_eq!(ctx.notifications[3].route(), widget.id());
1498+
}
14411499
}

0 commit comments

Comments
 (0)