Skip to content

Commit

Permalink
feat(window): examples for minimizable and closable
Browse files Browse the repository at this point in the history
  • Loading branch information
caesar committed Sep 29, 2022
1 parent e75a415 commit ddb9f2b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
51 changes: 51 additions & 0 deletions examples/closable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2014-2021 The winit contributors
// Copyright 2021-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

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

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

// let mut closable = false;

let window = WindowBuilder::new()
.with_title("Hit space to toggle closability.")
.with_inner_size(LogicalSize::new(400.0, 200.0))
.with_closable(false)
.build(&event_loop)
.unwrap();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
event:
KeyEvent {
physical_key: KeyCode::Space,
state: ElementState::Released,
..
},
..
} => {
let closable = !window.is_closable();
println!("closable: {}", closable);
window.set_closable(closable);
}
_ => (),
},
_ => (),
};
});
}
34 changes: 20 additions & 14 deletions examples/minimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
extern crate tao;

use tao::{
event::{Event, WindowEvent},
event::{Event, WindowEvent, ElementState},
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::WindowBuilder,
Expand All @@ -17,7 +17,7 @@ fn main() {
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("A fantastic window!")
.with_title("Hit `m` to minimize, `space` to toggle minimizability")
.build(&event_loop)
.unwrap();

Expand All @@ -26,20 +26,26 @@ fn main() {

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

// Keyboard input event to handle minimize via a hotkey
Event::WindowEvent {
event: WindowEvent::KeyboardInput { event, .. },
event,
window_id,
..
} if window_id == window.id() && Key::Character("m") == event.logical_key => {
// Pressing the 'm' key will minimize the window
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
window.set_minimized(true);
} if window_id == window.id() => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,

// Keyboard input event to handle minimize via a hotkey
WindowEvent::KeyboardInput { event, .. } if event.state == ElementState::Pressed => match event.logical_key {
// Pressing the 'm' key will minimize the window
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
Key::Character("m") => window.set_minimized(true),
Key::Space => {
let minimizable = !window.is_minimizable();
println!("Minimizable: {}", minimizable);
window.set_minimizable(minimizable);
}
_ => (),
}
_ => (),
}
_ => (),
}
Expand Down

0 comments on commit ddb9f2b

Please sign in to comment.