|
3 | 3 | // SPDX-License-Identifier: Apache-2.0
|
4 | 4 |
|
5 | 5 | use tao::{
|
6 |
| - dpi::LogicalSize, |
7 |
| - event::{Event, WindowEvent}, |
| 6 | + dpi::LogicalPixel, |
| 7 | + event::{ElementState, Event, KeyEvent, WindowEvent}, |
8 | 8 | event_loop::{ControlFlow, EventLoop},
|
9 |
| - window::WindowBuilder, |
| 9 | + keyboard::Key, |
| 10 | + window::{WindowBuilder, WindowSizeConstraints}, |
10 | 11 | };
|
11 | 12 |
|
12 | 13 | #[allow(clippy::single_match)]
|
13 | 14 | fn main() {
|
14 | 15 | env_logger::init();
|
15 | 16 | let event_loop = EventLoop::new();
|
16 | 17 |
|
| 18 | + let min_width = 400.0; |
| 19 | + let max_width = 800.0; |
| 20 | + let min_height = 200.0; |
| 21 | + let max_height = 400.0; |
| 22 | + let mut size_constraints = WindowSizeConstraints::default(); |
| 23 | + |
17 | 24 | let window = WindowBuilder::new().build(&event_loop).unwrap();
|
18 | 25 |
|
19 |
| - window.set_min_inner_size(Some(LogicalSize::new(400.0, 200.0))); |
20 |
| - window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0))); |
| 26 | + eprintln!("constraint keys:"); |
| 27 | + eprintln!(" (E) Toggle the min width"); |
| 28 | + eprintln!(" (F) Toggle the max width"); |
| 29 | + eprintln!(" (P) Toggle the min height"); |
| 30 | + eprintln!(" (V) Toggle the max height"); |
21 | 31 |
|
22 | 32 | event_loop.run(move |event, _, control_flow| {
|
23 | 33 | *control_flow = ControlFlow::Wait;
|
24 |
| - println!("{:?}", event); |
25 | 34 |
|
26 | 35 | match event {
|
27 | 36 | Event::WindowEvent {
|
28 | 37 | event: WindowEvent::CloseRequested,
|
29 | 38 | ..
|
30 | 39 | } => *control_flow = ControlFlow::Exit,
|
| 40 | + |
| 41 | + Event::WindowEvent { |
| 42 | + event: |
| 43 | + WindowEvent::KeyboardInput { |
| 44 | + event: |
| 45 | + KeyEvent { |
| 46 | + logical_key: Key::Character(key_str), |
| 47 | + state: ElementState::Released, |
| 48 | + .. |
| 49 | + }, |
| 50 | + .. |
| 51 | + }, |
| 52 | + .. |
| 53 | + } => match key_str { |
| 54 | + "e" => { |
| 55 | + size_constraints.min_width = |
| 56 | + (!size_constraints.min_width.is_some()).then_some(LogicalPixel::new(min_width).into()); |
| 57 | + window.set_inner_size_constraints(size_constraints); |
| 58 | + } |
| 59 | + "f" => { |
| 60 | + size_constraints.max_width = |
| 61 | + (!size_constraints.max_width.is_some()).then_some(LogicalPixel::new(max_width).into()); |
| 62 | + window.set_inner_size_constraints(size_constraints); |
| 63 | + } |
| 64 | + "p" => { |
| 65 | + size_constraints.min_height = (!size_constraints.min_height.is_some()) |
| 66 | + .then_some(LogicalPixel::new(min_height).into()); |
| 67 | + window.set_inner_size_constraints(size_constraints); |
| 68 | + } |
| 69 | + "v" => { |
| 70 | + size_constraints.max_height = (!size_constraints.max_height.is_some()) |
| 71 | + .then_some(LogicalPixel::new(max_height).into()); |
| 72 | + window.set_inner_size_constraints(size_constraints); |
| 73 | + } |
| 74 | + _ => {} |
| 75 | + }, |
31 | 76 | _ => (),
|
32 | 77 | }
|
33 | 78 | });
|
|
0 commit comments