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

feat: split min/max size constraints #759

Merged
merged 17 commits into from
Jul 11, 2023
57 changes: 51 additions & 6 deletions examples/min_max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,76 @@
// SPDX-License-Identifier: Apache-2.0

use tao::{
dpi::LogicalSize,
event::{Event, WindowEvent},
dpi::LogicalPixel,
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
keyboard::Key,
window::{WindowBuilder, WindowSizeConstraints},
};

#[allow(clippy::single_match)]
fn main() {
env_logger::init();
let event_loop = EventLoop::new();

let min_width = 400.0;
let max_width = 800.0;
let min_height = 200.0;
let max_height = 400.0;
let mut size_constraints = WindowSizeConstraints::default();

let window = WindowBuilder::new().build(&event_loop).unwrap();

window.set_min_inner_size(Some(LogicalSize::new(400.0, 200.0)));
window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0)));
eprintln!("constraint keys:");
eprintln!(" (E) Toggle the min width");
eprintln!(" (F) Toggle the max width");
eprintln!(" (P) Toggle the min height");
eprintln!(" (V) Toggle the max height");

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
println!("{:?}", event);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,

Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Character(key_str),
state: ElementState::Released,
..
},
..
},
..
} => match key_str {
"e" => {
size_constraints.min_width =
(!size_constraints.min_width.is_some()).then_some(LogicalPixel::new(min_width).into());
window.set_inner_size_constraints(size_constraints);
}
"f" => {
size_constraints.max_width =
(!size_constraints.max_width.is_some()).then_some(LogicalPixel::new(max_width).into());
window.set_inner_size_constraints(size_constraints);
}
"p" => {
size_constraints.min_height = (!size_constraints.min_height.is_some())
.then_some(LogicalPixel::new(min_height).into());
window.set_inner_size_constraints(size_constraints);
}
"v" => {
size_constraints.max_height = (!size_constraints.max_height.is_some())
.then_some(LogicalPixel::new(max_height).into());
window.set_inner_size_constraints(size_constraints);
}
_ => {}
},
_ => (),
}
});
Expand Down
Loading