Skip to content

Commit

Permalink
feat: split min/max size constraints (#759)
Browse files Browse the repository at this point in the history
* feat: split min/max size constraints, closes #138

* windows impl

* linux impl

* fix linux impl

* cleanup linux impl

* macOS impl

* imports

* ios build

* macos build

* unsafe

* merge `set_min/max_width/height` into a single function

* fix macos build

* macos again

* use macros to generate DPI types

ref: rust-windowing/winit#2148

* fix windows impl

* fmt
  • Loading branch information
amrbashir committed Jul 11, 2023
1 parent bb3c53d commit ea14c6b
Show file tree
Hide file tree
Showing 13 changed files with 624 additions and 561 deletions.
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

0 comments on commit ea14c6b

Please sign in to comment.