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

refactor: keyboards events #82

Merged
merged 99 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
304d089
WIP windows keyboards
lemarier Jun 3, 2021
e728e06
wip macos keyboards events
lemarier Jun 3, 2021
a6db5e0
fix linux build
lemarier Jun 3, 2021
fb3e6e9
fix windows
lemarier Jun 3, 2021
2395b98
make clippy happy
lemarier Jun 3, 2021
f1a1d4e
WIP keyboards `gtk` Implementations
lemarier Jun 3, 2021
76e841b
wip linux
lemarier Jun 3, 2021
c557d5c
add new deps for linux
lemarier Jun 3, 2021
2ad4317
implement linux scancode
lemarier Jun 3, 2021
281460c
Linux modifiers
lemarier Jun 4, 2021
2f385ad
Merge remote-tracking branch 'origin/dev' into refactor/keyboard
lemarier Jun 4, 2021
500a212
finalize linux implementations
lemarier Jun 4, 2021
f1baab9
fmt
lemarier Jun 4, 2021
af95b1e
make clippy happy for macos
lemarier Jun 4, 2021
24bf235
Fix `iOS` and WIP `android`
lemarier Jun 4, 2021
7cf9608
fix doc
lemarier Jun 4, 2021
829f2f0
fix android `TryInto`
lemarier Jun 4, 2021
f5eab0f
make clippy happy and rename keycodes to keyboard
lemarier Jun 4, 2021
6dac864
wip hotkey implementation
lemarier Jun 4, 2021
8a5d3e9
WIP implement accelerators for windows
lemarier Jun 4, 2021
7c05cae
change menuid to `u16`
lemarier Jun 4, 2021
120c439
fix ios & android
lemarier Jun 4, 2021
86cded7
clean`custom_menu` example
lemarier Jun 4, 2021
43dd325
remove lifetime specifier and fix linux
lemarier Jun 4, 2021
7527603
fix all examples
lemarier Jun 4, 2021
943a184
fix lifetime specifiers
lemarier Jun 4, 2021
2f261e3
cleanup
lemarier Jun 4, 2021
ae230e5
fix windows
lemarier Jun 4, 2021
519f667
make clippy happyyy
lemarier Jun 4, 2021
7107323
fix `MenuItemAttributes` and fmt
lemarier Jun 4, 2021
0de31f7
windows cleanup
lemarier Jun 4, 2021
4440c66
fix `u32`
lemarier Jun 4, 2021
387a603
remove debug
lemarier Jun 4, 2021
d7a27f7
update doc
lemarier Jun 7, 2021
adae9eb
update `hotkey` example
lemarier Jun 7, 2021
8e7e1ce
add current changelog
lemarier Jun 7, 2021
80fec0d
fmt
lemarier Jun 7, 2021
b61c6ce
basic `global_accelerator` implementation for macOS
lemarier Jun 7, 2021
9f94db6
WIP `HotKeyManager`
lemarier Jun 8, 2021
c67311b
WIP add `GlobalHotKeyEvent`
lemarier Jun 8, 2021
4d58ced
wip windows
lemarier Jun 8, 2021
f9e5aaa
fix windows build
lemarier Jun 8, 2021
280dc84
add global events for windows
lemarier Jun 8, 2021
956ecff
wip `gtk` global accelerator
lemarier Jun 8, 2021
8ce0942
fix linux build
lemarier Jun 8, 2021
22729fc
wip(macos): refactor `Accelerator` and `ShortcutManager`
lemarier Jun 9, 2021
7f190fd
wip(windows): refactor `Accelerator` and `ShortcutManager`
lemarier Jun 9, 2021
8c2be94
Merge remote-tracking branch 'origin/dev' into refactor/keyboard
lemarier Jun 9, 2021
36dc4cf
make clippy happy
lemarier Jun 9, 2021
890fd32
wip(linux): refactor `Accelerator` and `ShortcutManager`
lemarier Jun 9, 2021
9861b5c
fix linux `Accelerator`
lemarier Jun 9, 2021
c58ff6d
review unregister
lemarier Jun 9, 2021
79a0660
fix windows
lemarier Jun 9, 2021
eb54bc2
wip linux global accelerator
lemarier Jun 9, 2021
43510b5
fmt
lemarier Jun 9, 2021
46c7e92
fix `iOS` and `android`
lemarier Jun 9, 2021
e6a0e32
fmt
lemarier Jun 9, 2021
597fc8a
remove accelerator limitation
lemarier Jun 9, 2021
07cb388
better example
lemarier Jun 9, 2021
2b737f9
add `from_str` to Accelerator to be compatible with current tauri use
lemarier Jun 10, 2021
5cab639
fix linux build
lemarier Jun 10, 2021
5fe20cd
implement `AcceleratorId` and use `KeyCode` instead of `Key` for acce…
lemarier Jun 10, 2021
369ab89
refactor linux accelerators
lemarier Jun 10, 2021
6105311
migrate windows to `KeyCode` and fix CI
lemarier Jun 10, 2021
d1065e3
make clippy happy
lemarier Jun 10, 2021
31e5de8
fix warning’s
lemarier Jun 10, 2021
32011f1
fix windows `reset_dead_keys`
lemarier Jun 10, 2021
4e5a704
fmt
lemarier Jun 10, 2021
327e4d3
make clippy happy
lemarier Jun 10, 2021
1c24c2a
expose `with_id` for `Accelerator`
lemarier Jun 11, 2021
3f94515
fix macOS default accelerator keys
lemarier Jun 11, 2021
53ec836
[ci skip] update readme
lemarier Jun 11, 2021
736fafd
WIP: use &str for `Key::Character`
lemarier Jun 16, 2021
ba8ce19
fix windows, linux & android
lemarier Jun 16, 2021
546e056
update changelog
lemarier Jun 16, 2021
59a6051
fix `text_with_all_modifiers`
lemarier Jun 16, 2021
6496ec1
add static lifetime
lemarier Jun 16, 2021
65c32ea
prevent windows to failed
lemarier Jun 16, 2021
2ac576c
add `GlobalShortcut` documentation
lemarier Jun 16, 2021
33caa99
[ci skip] update changelog
lemarier Jun 16, 2021
e83b2de
fix `X` char
lemarier Jun 16, 2021
45168fc
add accelerators tests
lemarier Jun 16, 2021
f62afea
remove `to_keycode` as its not needed anymore
lemarier Jun 16, 2021
50224da
move `to_scancode` and `from_scancode` builtin KeyCode
lemarier Jun 18, 2021
873a21f
fix mobiles and fmt
lemarier Jun 18, 2021
92c53bf
Add doc
lemarier Jun 18, 2021
188eae6
fix ci
lemarier Jun 18, 2021
bc3f6a1
fix windows
lemarier Jun 18, 2021
f2021ce
Merge remote-tracking branch 'origin/dev' into refactor/keyboard
lemarier Jun 21, 2021
d911339
remove `modifier_supplement` and merge it into `KeyEvent`
lemarier Jun 21, 2021
8e5683a
make clippy happy
lemarier Jun 21, 2021
d4ce4dc
fix windows typo in `text_with_all_modifiers`
lemarier Jun 21, 2021
fb014d1
remove unused imports in windows
lemarier Jun 21, 2021
6c8c4ee
Merge remote-tracking branch 'origin/dev' into refactor/keyboard
lemarier Jun 21, 2021
9cc3542
remove `nameof` and add `Debug` to `NativeKeyCode`
lemarier Jun 21, 2021
6c1a36b
fmt
lemarier Jun 21, 2021
6e07494
[ci skip] remove `modifier_supplement` from doc
lemarier Jun 21, 2021
c0a4ed5
[ci skip] fix ios and android clippy warning
lemarier Jun 21, 2021
330c91c
fix linux clippy warning
lemarier Jun 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changes/keyboard-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"tao": minor
---

**Breaking change**: New keyboard API, including `Accelerator` and `GlobalShortcut`.

`WindowEvent::ModifiersChanged` is emitted when a new keyboard modifier is pressed. This is your responsibility to keep a local state. When the modifier is released, `ModifiersState::empty()` is emitted.

`WindowEvent::KeyboardInput` as been refactored and is exposing the event `KeyEvent`.

All menus (`ContextMenu` and `MenuBar`), now includes `Accelerator` support on Windows, macOS and Linux.

New modules available: `keyboard`, `accelerator` and `platform::global_shortcut`.

_Please refer to the docs and examples for more details._
22 changes: 15 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,21 @@ core-graphics = "0.22"
dispatch = "0.2"
scopeguard = "1.1"

[target."cfg(target_os = \"macos\")".dependencies.core-video-sys]
version = "0.1"
default_features = false
features = [ "display_link" ]
[target."cfg(target_os = \"macos\")".dependencies.core-video-sys]
version = "0.1"
default_features = false
features = [ "display_link" ]

[target."cfg(target_os = \"macos\")".build-dependencies]
cc = "1"

[target."cfg(target_os = \"windows\")".dependencies]
parking_lot = "0.11"
unicode-segmentation = "1.7.1"

[target."cfg(target_os = \"windows\")".dependencies.winapi]
version = "0.3"
features = [
[target."cfg(target_os = \"windows\")".dependencies.winapi]
version = "0.3"
features = [
"combaseapi",
"commctrl",
"dwmapi",
Expand All @@ -91,6 +95,7 @@ parking_lot = "0.11"
"winerror",
"wingdi",
"winnt",
"winnls",
"winuser",
"impl-default"
]
Expand All @@ -99,8 +104,11 @@ parking_lot = "0.11"
cairo-rs = "0.9"
gio = "0.9"
glib = "0.10"
glib-sys = "0.10"
gtk = { version = "0.9", features = [ "v3_16" ] }
gdk = "0.13"
gdk-sys = "0.10"
gdk-pixbuf = { version = "0.9", features = [ "v2_36_8" ] }
sourceview = { version = "0.9", optional = true }
libappindicator = { version = "0.5", optional = true }
x11-dl = "2.18"
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ fn main() {
{
println!("cargo:rustc-cfg=use_colorsync_cgdisplaycreateuuidfromdisplayid");
}
// link carbon hotkey on macOS
#[cfg(target_os = "macos")]
{
if std::env::var("CARGO_CFG_TARGET_OS").map_or(false, |os| os == "macos") {
println!("cargo:rustc-link-lib=framework=Carbon");
cc::Build::new()
.file("src/platform_impl/macos/carbon_hotkey/carbon_hotkey_binding.c")
.compile("carbon_hotkey_binding.a");
}
}
}
55 changes: 55 additions & 0 deletions examples/accelerator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

fn main() {
use simple_logger::SimpleLogger;
use tao::{
accelerator::{Accelerator, RawMods},
dpi::LogicalSize,
event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, KeyCode, ModifiersState},
window::WindowBuilder,
};

SimpleLogger::new().init().unwrap();

// create a sample hotkey
let hotkey = Accelerator::new(RawMods::Shift, KeyCode::Digit1);
// create local modifier state
let mut modifiers = ModifiersState::default();

let event_loop = EventLoop::new();

let _window = WindowBuilder::new()
.with_inner_size(LogicalSize::new(400.0, 200.0))
.build(&event_loop)
.unwrap();

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

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::ModifiersChanged(new_state) => {
// update our local modifier state
modifiers = new_state;
}
// catch only pressed event
WindowEvent::KeyboardInput { event, .. } if event.state == ElementState::Pressed => {
if hotkey.matches(&modifiers, &event.physical_key) {
println!(
"KeyEvent: `Shift` + `1` | logical_key: {:?}",
&event.logical_key
);
// we can match manually without `Accelerator`
} else if event.key_without_modifiers() == Key::Character("1") && modifiers.is_empty() {
println!("KeyEvent: `1`");
}
}
_ => (),
}
}
});
}
30 changes: 16 additions & 14 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use std::{thread, time};

use simple_logger::SimpleLogger;
use tao::{
event::{Event, KeyboardInput, WindowEvent},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::WindowBuilder,
};

Expand Down Expand Up @@ -42,7 +43,7 @@ fn main() {
let mut close_requested = false;

event_loop.run(move |event, _, control_flow| {
use tao::event::{ElementState, StartCause, VirtualKeyCode};
use tao::event::StartCause;
println!("{:?}", event);
match event {
Event::NewEvents(start_cause) => {
Expand All @@ -56,36 +57,37 @@ fn main() {
close_requested = true;
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(virtual_code),
event:
KeyEvent {
logical_key,
state: ElementState::Pressed,
..
},
..
} => match virtual_code {
VirtualKeyCode::Key1 => {
} => {
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
if Key::Character("1") == logical_key {
mode = Mode::Wait;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::Key2 => {
if Key::Character("2") == logical_key {
mode = Mode::WaitUntil;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::Key3 => {
if Key::Character("3") == logical_key {
mode = Mode::Poll;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::R => {
if Key::Character("r") == logical_key {
request_redraw = !request_redraw;
println!("\nrequest_redraw: {}\n", request_redraw);
}
VirtualKeyCode::Escape => {
if Key::Escape == logical_key {
close_requested = true;
}
_ => (),
},
_ => (),
}
_ => {}
},
Event::MainEventsCleared => {
if request_redraw && !wait_cancelled && !close_requested {
Expand Down
10 changes: 4 additions & 6 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use simple_logger::SimpleLogger;
use tao::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{CursorIcon, WindowBuilder},
};
Expand All @@ -24,8 +24,8 @@ fn main() {
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
event:
KeyEvent {
state: ElementState::Pressed,
..
},
Expand All @@ -44,9 +44,7 @@ fn main() {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
}
} => *control_flow = ControlFlow::Exit,
_ => (),
}
});
Expand Down
21 changes: 13 additions & 8 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

use simple_logger::SimpleLogger;
use tao::{
event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent},
event::{DeviceEvent, ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, ModifiersState},
window::WindowBuilder,
};

Expand All @@ -27,19 +28,23 @@ fn main() {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
input:
KeyboardInput {
event:
KeyEvent {
logical_key: key,
state: ElementState::Released,
virtual_keycode: Some(key),
..
},
..
} => {
use tao::event::VirtualKeyCode::*;
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
match key {
Escape => *control_flow = ControlFlow::Exit,
G => window.set_cursor_grab(!modifiers.shift()).unwrap(),
H => window.set_cursor_visible(modifiers.shift()),
Key::Escape => *control_flow = ControlFlow::Exit,
Key::Character(ch) => match ch.to_lowercase().as_str() {
"g" => window.set_cursor_grab(!modifiers.shift_key()).unwrap(),
"h" => window.set_cursor_visible(modifiers.shift_key()),
_ => (),
},
_ => (),
}
}
Expand Down
15 changes: 9 additions & 6 deletions examples/custom_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use simple_logger::SimpleLogger;
#[cfg(target_os = "macos")]
use tao::platform::macos::{CustomMenuItemExtMacOS, NativeImage};
use tao::{
accelerator::{Accelerator, SysMods},
clipboard::Clipboard,
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::KeyCode,
menu::{MenuBar as Menu, MenuItem, MenuItemAttributes, MenuType},
window::WindowBuilder,
};
Expand All @@ -25,15 +27,15 @@ fn main() {
// create `first_menu`
let mut first_menu = Menu::new();

// create an empty menu to be used as submenu
let mut my_sub_menu = Menu::new();

// create second menu
let mut second_menu = Menu::new();
// create an empty menu to be used as submenu
let mut my_sub_menu = Menu::new();

// create custom item `Disable menu` children of `my_sub_menu`
let mut test_menu_item =
my_sub_menu.add_item(MenuItemAttributes::new("Disable menu").with_accelerators("<Primary>d"));
let mut test_menu_item = my_sub_menu.add_item(
MenuItemAttributes::new("Disable menu")
.with_accelerators(&Accelerator::new(SysMods::Cmd, KeyCode::KeyD)),
);

// add native `Copy` to `first_menu` menu
// in macOS native item are required to get keyboard shortcut
Expand All @@ -46,6 +48,7 @@ fn main() {

// add `my_sub_menu` children of `first_menu` with `Sub menu` title
first_menu.add_submenu("Sub menu", true, my_sub_menu);
first_menu.add_native_item(MenuItem::Quit);

// create custom item `Selected and disabled` children of `second_menu`
second_menu.add_item(
Expand Down
13 changes: 6 additions & 7 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

use simple_logger::SimpleLogger;
use tao::{
event::{
ElementState, Event, KeyboardInput, MouseButton, StartCause, VirtualKeyCode, WindowEvent,
},
event::{ElementState, Event, KeyEvent, MouseButton, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
window::{Window, WindowBuilder, WindowId},
};

Expand Down Expand Up @@ -47,14 +46,14 @@ fn main() {
name_windows(entered_id, switched, &window_1, &window_2)
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
event:
KeyEvent {
state: ElementState::Released,
virtual_keycode: Some(VirtualKeyCode::X),
logical_key,
..
},
..
} => {
} if logical_key == Key::Character("x") => {
switched = !switched;
name_windows(entered_id, switched, &window_1, &window_2);
println!("Switched!")
Expand Down
Loading