diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b4a92ba9e..76be1324d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ You can find its changes [documented below](#070---2021-01-01). - `scroll_to_view` and `scroll_area_to_view` methods on `UpdateCtx`, `LifecycleCtx` and `EventCtx` ([#1976] by [@xarvic]) - `Notification::route` ([#1978] by [@xarvic]) - Build on OpenBSD ([#1993] by [@klemensn]) +- `Event::WindowFocus(bool)` to notify widgets of window focus changes ([#2016] by [@ForLoveOfCats]) ### Changed @@ -804,6 +805,7 @@ Last release without a changelog :( [#1978]: https://github.com/linebender/druid/pull/1978 [#1993]: https://github.com/linebender/druid/pull/1993 [#1996]: https://github.com/linebender/druid/pull/1996 +[#2016]: https://github.com/linebender/druid/pull/2016 [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 diff --git a/druid/src/core.rs b/druid/src/core.rs index fe6c01eea9..43083c988d 100644 --- a/druid/src/core.rs +++ b/druid/src/core.rs @@ -725,6 +725,7 @@ impl> WidgetPod { } true } + Event::WindowFocus(_) => true, Event::WindowSize(_) => { self.state.needs_layout = true; ctx.is_root diff --git a/druid/src/event.rs b/druid/src/event.rs index c060e083af..b4c859b17c 100644 --- a/druid/src/event.rs +++ b/druid/src/event.rs @@ -74,6 +74,8 @@ pub enum Event { /// This event means the window *will* go away; it is safe to dispose of resources and /// do any other cleanup. WindowDisconnected, + /// Called when window focus changes. + WindowFocus(bool), /// Called on the root widget when the window size changes. /// /// Discussion: it's not obvious this should be propagated to user @@ -431,6 +433,7 @@ impl Event { Event::WindowConnected | Event::WindowCloseRequested | Event::WindowDisconnected + | Event::WindowFocus(_) | Event::WindowSize(_) | Event::Timer(_) | Event::AnimFrame(_) diff --git a/druid/src/win_handler.rs b/druid/src/win_handler.rs index ead7d6d7c0..39466afb17 100644 --- a/druid/src/win_handler.rs +++ b/druid/src/win_handler.rs @@ -977,6 +977,14 @@ impl WinHandler for DruidHandler { fn got_focus(&mut self) { self.app_state.window_got_focus(self.window_id); + + let event = Event::WindowFocus(true); + self.app_state.do_window_event(event, self.window_id); + } + + fn lost_focus(&mut self) { + let event = Event::WindowFocus(false); + self.app_state.do_window_event(event, self.window_id); } fn timer(&mut self, token: TimerToken) {