Skip to content

Commit 67d3fd2

Browse files
authored
Move Event::RedrawRequested to WindowEvent (rust-windowing#3049)
1 parent a3cba83 commit 67d3fd2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+569
-559
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ And please only add new entries to the top of this list, right below the `# Unre
1111
- Fix window size sometimes being invalid when resizing on macOS.
1212
- On Web, `ControlFlow::Poll` and `ControlFlow::WaitUntil` are now using the Prioritized Task Scheduling API. `setTimeout()` with a trick to circumvent throttling to 4ms is used as a fallback.
1313
- On Web, never return a `MonitorHandle`.
14+
- **Breaking:** Move `Event::RedrawRequested` to `WindowEvent::RedrawRequested`.
1415

1516
# 0.29.1-beta
1617

examples/child_window.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ fn main() -> Result<(), impl std::error::Error> {
7272
} => {
7373
spawn_child_window(&parent_window, event_loop, &mut windows);
7474
}
75+
WindowEvent::RedrawRequested => {
76+
if let Some(window) = windows.get(&window_id) {
77+
fill::fill_window(window);
78+
}
79+
}
7580
_ => (),
7681
}
77-
} else if let Event::RedrawRequested(wid) = event {
78-
if let Some(window) = windows.get(&wid) {
79-
fill::fill_window(window);
80-
}
8182
}
8283
})
8384
}

examples/control_flow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ fn main() -> Result<(), impl std::error::Error> {
9393
}
9494
_ => (),
9595
},
96+
WindowEvent::RedrawRequested => {
97+
fill::fill_window(&window);
98+
}
9699
_ => (),
97100
},
98101
Event::AboutToWait => {
@@ -117,9 +120,6 @@ fn main() -> Result<(), impl std::error::Error> {
117120
control_flow.set_exit();
118121
}
119122
}
120-
Event::RedrawRequested(_window_id) => {
121-
fill::fill_window(&window);
122-
}
123123
_ => (),
124124
}
125125
})

examples/cursor.rs

+24-29
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,32 @@ fn main() -> Result<(), impl std::error::Error> {
2222
event_loop.run(move |event, _, control_flow| {
2323
control_flow.set_wait();
2424

25-
match event {
26-
Event::WindowEvent {
27-
event:
28-
WindowEvent::KeyboardInput {
29-
event:
30-
KeyEvent {
31-
state: ElementState::Pressed,
32-
..
33-
},
34-
..
35-
},
36-
..
37-
} => {
38-
println!("Setting cursor to \"{:?}\"", CURSORS[cursor_idx]);
39-
window.set_cursor_icon(CURSORS[cursor_idx]);
40-
if cursor_idx < CURSORS.len() - 1 {
41-
cursor_idx += 1;
42-
} else {
43-
cursor_idx = 0;
25+
if let Event::WindowEvent { event, .. } = event {
26+
match event {
27+
WindowEvent::KeyboardInput {
28+
event:
29+
KeyEvent {
30+
state: ElementState::Pressed,
31+
..
32+
},
33+
..
34+
} => {
35+
println!("Setting cursor to \"{:?}\"", CURSORS[cursor_idx]);
36+
window.set_cursor_icon(CURSORS[cursor_idx]);
37+
if cursor_idx < CURSORS.len() - 1 {
38+
cursor_idx += 1;
39+
} else {
40+
cursor_idx = 0;
41+
}
4442
}
43+
WindowEvent::RedrawRequested => {
44+
fill::fill_window(&window);
45+
}
46+
WindowEvent::CloseRequested => {
47+
control_flow.set_exit();
48+
}
49+
_ => (),
4550
}
46-
Event::WindowEvent {
47-
event: WindowEvent::CloseRequested,
48-
..
49-
} => {
50-
control_flow.set_exit();
51-
}
52-
Event::RedrawRequested(_) => {
53-
fill::fill_window(&window);
54-
}
55-
_ => (),
5651
}
5752
})
5853
}

examples/cursor_grab.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ fn main() -> Result<(), impl std::error::Error> {
6060
}
6161
}
6262
WindowEvent::ModifiersChanged(new) => modifiers = new.state(),
63+
WindowEvent::RedrawRequested => fill::fill_window(&window),
6364
_ => (),
6465
},
6566
Event::DeviceEvent { event, .. } => match event {
@@ -70,7 +71,6 @@ fn main() -> Result<(), impl std::error::Error> {
7071
},
7172
_ => (),
7273
},
73-
Event::RedrawRequested(_) => fill::fill_window(&window),
7474
_ => (),
7575
}
7676
})

examples/custom_events.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ fn main() -> Result<(), impl std::error::Error> {
4949
event: WindowEvent::CloseRequested,
5050
..
5151
} => control_flow.set_exit(),
52-
Event::RedrawRequested(_) => {
52+
Event::WindowEvent {
53+
event: WindowEvent::RedrawRequested,
54+
..
55+
} => {
5356
fill::fill_window(&window);
5457
}
5558
_ => (),

examples/drag_window.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ fn main() -> Result<(), impl std::error::Error> {
5959
name_windows(entered_id, switched, &window_1, &window_2);
6060
println!("Switched!")
6161
}
62+
WindowEvent::RedrawRequested => {
63+
if window_id == window_1.id() {
64+
fill::fill_window(&window_1);
65+
} else if window_id == window_2.id() {
66+
fill::fill_window(&window_2);
67+
}
68+
}
6269
_ => (),
6370
},
64-
Event::RedrawRequested(wid) => {
65-
if wid == window_1.id() {
66-
fill::fill_window(&window_1);
67-
} else if wid == window_2.id() {
68-
fill::fill_window(&window_2);
69-
}
70-
}
71+
7172
_ => (),
7273
})
7374
}

examples/fullscreen.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ fn main() -> Result<(), impl std::error::Error> {
5555
event_loop.run(move |event, elwt, control_flow| {
5656
control_flow.set_wait();
5757

58-
match event {
59-
Event::WindowEvent { event, .. } => match event {
58+
if let Event::WindowEvent { event, .. } = event {
59+
match event {
6060
WindowEvent::CloseRequested => control_flow.set_exit(),
6161
WindowEvent::KeyboardInput {
6262
event:
@@ -155,12 +155,11 @@ fn main() -> Result<(), impl std::error::Error> {
155155
},
156156
_ => (),
157157
},
158+
WindowEvent::RedrawRequested => {
159+
fill::fill_window(&window);
160+
}
158161
_ => (),
159-
},
160-
Event::RedrawRequested(_) => {
161-
fill::fill_window(&window);
162162
}
163-
_ => {}
164163
}
165164
})
166165
}

examples/handling_close.rs

+49-52
Original file line numberDiff line numberDiff line change
@@ -25,67 +25,64 @@ fn main() -> Result<(), impl std::error::Error> {
2525
event_loop.run(move |event, _, control_flow| {
2626
control_flow.set_wait();
2727

28-
match event {
29-
Event::WindowEvent { event, .. } => {
30-
match event {
31-
WindowEvent::CloseRequested => {
32-
// `CloseRequested` is sent when the close button on the window is pressed (or
33-
// through whatever other mechanisms the window manager provides for closing a
34-
// window). If you don't handle this event, the close button won't actually do
35-
// anything.
28+
if let Event::WindowEvent { event, .. } = event {
29+
match event {
30+
WindowEvent::CloseRequested => {
31+
// `CloseRequested` is sent when the close button on the window is pressed (or
32+
// through whatever other mechanisms the window manager provides for closing a
33+
// window). If you don't handle this event, the close button won't actually do
34+
// anything.
3635

37-
// A common thing to do here is prompt the user if they have unsaved work.
38-
// Creating a proper dialog box for that is far beyond the scope of this
39-
// example, so here we'll just respond to the Y and N keys.
40-
println!("Are you ready to bid your window farewell? [Y/N]");
41-
close_requested = true;
36+
// A common thing to do here is prompt the user if they have unsaved work.
37+
// Creating a proper dialog box for that is far beyond the scope of this
38+
// example, so here we'll just respond to the Y and N keys.
39+
println!("Are you ready to bid your window farewell? [Y/N]");
40+
close_requested = true;
4241

43-
// In applications where you can safely close the window without further
44-
// action from the user, this is generally where you'd handle cleanup before
45-
// closing the window. How to close the window is detailed in the handler for
46-
// the Y key.
47-
}
48-
WindowEvent::KeyboardInput {
49-
event:
50-
KeyEvent {
51-
logical_key: key,
52-
state: ElementState::Released,
53-
..
54-
},
55-
..
56-
} => {
57-
// WARNING: Consider using `key_without_modifers()` if available on your platform.
58-
// See the `key_binding` example
59-
match key.as_ref() {
60-
Key::Character("y") => {
61-
if close_requested {
62-
// This is where you'll want to do any cleanup you need.
63-
println!("Buh-bye!");
42+
// In applications where you can safely close the window without further
43+
// action from the user, this is generally where you'd handle cleanup before
44+
// closing the window. How to close the window is detailed in the handler for
45+
// the Y key.
46+
}
47+
WindowEvent::KeyboardInput {
48+
event:
49+
KeyEvent {
50+
logical_key: key,
51+
state: ElementState::Released,
52+
..
53+
},
54+
..
55+
} => {
56+
// WARNING: Consider using `key_without_modifers()` if available on your platform.
57+
// See the `key_binding` example
58+
match key.as_ref() {
59+
Key::Character("y") => {
60+
if close_requested {
61+
// This is where you'll want to do any cleanup you need.
62+
println!("Buh-bye!");
6463

65-
// For a single-window application like this, you'd normally just
66-
// break out of the event loop here. If you wanted to keep running the
67-
// event loop (i.e. if it's a multi-window application), you need to
68-
// drop the window. That closes it, and results in `Destroyed` being
69-
// sent.
70-
control_flow.set_exit();
71-
}
64+
// For a single-window application like this, you'd normally just
65+
// break out of the event loop here. If you wanted to keep running the
66+
// event loop (i.e. if it's a multi-window application), you need to
67+
// drop the window. That closes it, and results in `Destroyed` being
68+
// sent.
69+
control_flow.set_exit();
7270
}
73-
Key::Character("n") => {
74-
if close_requested {
75-
println!("Your window will continue to stay by your side.");
76-
close_requested = false;
77-
}
71+
}
72+
Key::Character("n") => {
73+
if close_requested {
74+
println!("Your window will continue to stay by your side.");
75+
close_requested = false;
7876
}
79-
_ => (),
8077
}
78+
_ => (),
8179
}
82-
_ => (),
8380
}
81+
WindowEvent::RedrawRequested => {
82+
fill::fill_window(&window);
83+
}
84+
_ => (),
8485
}
85-
Event::RedrawRequested(_) => {
86-
fill::fill_window(&window);
87-
}
88-
_ => (),
8986
}
9087
})
9188
}

0 commit comments

Comments
 (0)