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

Notify widgets of window focus change #2016

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions druid/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
}
true
}
Event::WindowFocus(_) => true,
Event::WindowSize(_) => {
self.state.needs_layout = true;
ctx.is_root
Expand Down
3 changes: 3 additions & 0 deletions druid/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -431,6 +433,7 @@ impl Event {
Event::WindowConnected
| Event::WindowCloseRequested
| Event::WindowDisconnected
| Event::WindowFocus(_)
| Event::WindowSize(_)
| Event::Timer(_)
| Event::AnimFrame(_)
Expand Down
8 changes: 8 additions & 0 deletions druid/src/win_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,14 @@ impl<T: Data> WinHandler for DruidHandler<T> {

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) {
Expand Down