Skip to content

Commit

Permalink
chore(examples): update winit to 0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Jan 21, 2025
1 parent 53cf96f commit f3b9bd9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ features = [
x11-dl = "2.21"

[dev-dependencies]
winit = "0.29"
winit = "0.30"
tao = "0.30"
eframe = "0.27"
iced = "0.12.1"
Expand Down
59 changes: 47 additions & 12 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use global_hotkey::{
hotkey::{Code, HotKey, Modifiers},
GlobalHotKeyEvent, GlobalHotKeyManager, HotKeyState,
};
use winit::event_loop::{ControlFlow, EventLoopBuilder};
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event_loop::{ActiveEventLoop, EventLoop},
window::WindowId,
};

fn main() {
let event_loop = EventLoopBuilder::new().build().unwrap();

let hotkeys_manager = GlobalHotKeyManager::new().unwrap();

let hotkey = HotKey::new(Some(Modifiers::SHIFT), Code::KeyD);
Expand All @@ -21,19 +24,51 @@ fn main() {
hotkeys_manager.register(hotkey2).unwrap();
hotkeys_manager.register(hotkey3).unwrap();

let global_hotkey_channel = GlobalHotKeyEvent::receiver();
let event_loop = EventLoop::<AppEvent>::with_user_event().build().unwrap();
let proxy = event_loop.create_proxy();

GlobalHotKeyEvent::set_event_handler(Some(move |event| {
let _ = proxy.send_event(AppEvent::HotKey(event));
}));

let mut app = App {
hotkeys_manager,
hotkey2,
};

event_loop.run_app(&mut app).unwrap()
}

#[derive(Debug)]
enum AppEvent {
HotKey(GlobalHotKeyEvent),
}

struct App {
hotkeys_manager: GlobalHotKeyManager,
hotkey2: HotKey,
}

impl ApplicationHandler<AppEvent> for App {
fn resumed(&mut self, _event_loop: &ActiveEventLoop) {}

event_loop
.run(move |_event, event_loop| {
event_loop.set_control_flow(ControlFlow::Poll);
fn window_event(
&mut self,
_event_loop: &ActiveEventLoop,
_window_id: WindowId,
_event: WindowEvent,
) {
}

if let Ok(event) = global_hotkey_channel.try_recv() {
fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: AppEvent) {
match event {
AppEvent::HotKey(event) => {
println!("{event:?}");

if hotkey2.id() == event.id && event.state == HotKeyState::Released {
hotkeys_manager.unregister(hotkey2).unwrap();
if self.hotkey2.id() == event.id && event.state == HotKeyState::Released {
self.hotkeys_manager.unregister(self.hotkey2).unwrap();
}
}
})
.unwrap();
}
}
}

0 comments on commit f3b9bd9

Please sign in to comment.