Skip to content

Commit

Permalink
Delegate tao key events to tauri
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnfrishert committed Feb 21, 2025
1 parent 8e9339e commit 395671a
Show file tree
Hide file tree
Showing 12 changed files with 630 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/expose-keyboard-inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
tauri: 'minor:feat'
---

Expose window keyboard events from `tao`, as `WindowEvent::KeyboardInput`.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

567 changes: 567 additions & 0 deletions crates/tauri-runtime-wry/src/keyboard.rs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ use std::{
pub type WebviewId = u32;
type IpcHandler = dyn Fn(Request<String>) + 'static;

mod keyboard;
#[cfg(any(
windows,
target_os = "linux",
Expand Down Expand Up @@ -532,6 +533,11 @@ impl<'a> From<&TaoWindowEvent<'a>> for WindowEventWrapper {
},
#[cfg(any(target_os = "linux", target_os = "macos"))]
TaoWindowEvent::Focused(focused) => WindowEvent::Focused(*focused),
TaoWindowEvent::KeyboardInput { event, .. } => {
let event = keyboard::convert_key_event(event);

WindowEvent::KeyboardInput { event }
}
TaoWindowEvent::ThemeChanged(theme) => WindowEvent::ThemeChanged(map_theme(theme)),
_ => return Self(None),
};
Expand Down
1 change: 1 addition & 0 deletions crates/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ http = "1"
raw-window-handle = "0.6"
url = { version = "2" }
dpi = { version = "0.1", features = ["serde"] }
keyboard-types = "0.7.0"

[target."cfg(windows)".dependencies.windows]
version = "0.58"
Expand Down
28 changes: 28 additions & 0 deletions crates/tauri-runtime/src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use serde::{Deserialize, Serialize};

pub use keyboard_types::{Code, Key, KeyState, Location, Modifiers};

/// Keyboard events are issued for all pressed and released keys.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct KeyboardEvent {
/// Whether the key is pressed or released.
pub state: KeyState,
/// Logical key value.
pub key: Key,
/// Physical key position.
pub code: Code,
/// Location for keys with multiple instances on common keyboards.
pub location: Location,
/// True if the key is currently auto-repeated.
pub repeat: bool,
}

impl KeyboardEvent {
pub fn is_down(&self) -> bool {
self.state == KeyState::Down
}

pub fn is_up(&self) -> bool {
self.state == KeyState::Up
}
}
1 change: 1 addition & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use tauri_utils::Theme;
use url::Url;
use webview::{DetachedWebview, PendingWebview};

pub mod keyboard;
/// Types useful for interacting with a user's monitors.
pub mod monitor;
pub mod webview;
Expand Down
5 changes: 5 additions & 0 deletions crates/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! A layer between raw [`Runtime`] windows and Tauri.
use crate::{
keyboard::KeyboardEvent,
webview::{DetachedWebview, PendingWebview},
Icon, Runtime, UserEvent, WindowDispatch,
};
Expand Down Expand Up @@ -43,6 +44,10 @@ pub enum WindowEvent {
///
/// The parameter is true if the window has gained focus, and false if it has lost focus.
Focused(bool),

/// An event from the keyboard has been received.
KeyboardInput { event: KeyboardEvent },

/// The window's scale factor has changed.
///
/// The following user actions can cause DPI changes:
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
channel::ChannelDataIpcQueue, CallbackFn, CommandArg, CommandItem, Invoke, InvokeError,
InvokeHandler, InvokeResponseBody,
},
keyboard::KeyboardEvent,
manager::{webview::UriSchemeProtocol, AppManager, Asset},
plugin::{Plugin, PluginStore},
resources::ResourceTable,
Expand Down Expand Up @@ -115,6 +116,8 @@ pub enum WindowEvent {
///
/// The parameter is true if the window has gained focus, and false if it has lost focus.
Focused(bool),
/// An event from the keyboard has been received.
KeyboardInput(KeyboardEvent),
/// The window's scale factor has changed.
///
/// The following user actions can cause DPI changes:
Expand Down Expand Up @@ -151,6 +154,7 @@ impl From<RuntimeWindowEvent> for WindowEvent {
},
RuntimeWindowEvent::Destroyed => Self::Destroyed,
RuntimeWindowEvent::Focused(flag) => Self::Focused(flag),
RuntimeWindowEvent::KeyboardInput { event } => Self::KeyboardInput(event),
RuntimeWindowEvent::ScaleFactorChanged {
scale_factor,
new_inner_size,
Expand Down
7 changes: 7 additions & 0 deletions crates/tauri/src/keyboard/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

//! Types and functions related to keyboard input.
pub use tauri_runtime::keyboard::{Code, Key, KeyState, KeyboardEvent, Location, Modifiers};
1 change: 1 addition & 0 deletions crates/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub mod async_runtime;
mod error;
mod event;
pub mod ipc;
pub mod keyboard;
mod manager;
mod pattern;
pub mod plugin;
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/src/manager/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const WINDOW_MOVED_EVENT: EventName<&str> = EventName::from_str("tauri://move");
const WINDOW_CLOSE_REQUESTED_EVENT: EventName<&str> =
EventName::from_str("tauri://close-requested");
const WINDOW_DESTROYED_EVENT: EventName<&str> = EventName::from_str("tauri://destroyed");
const WINDOW_KEYBOARD_INPUT_EVENT: EventName<&str> = EventName::from_str("tauri://keyboard-input");
const WINDOW_FOCUS_EVENT: EventName<&str> = EventName::from_str("tauri://focus");
const WINDOW_BLUR_EVENT: EventName<&str> = EventName::from_str("tauri://blur");
const WINDOW_SCALE_FACTOR_CHANGED_EVENT: EventName<&str> =
Expand Down Expand Up @@ -180,6 +181,9 @@ fn on_window_event<R: Runtime>(window: &Window<R>, event: &WindowEvent) -> crate
},
&(),
)?,
WindowEvent::KeyboardInput(event) => {
window.emit_to_window(WINDOW_KEYBOARD_INPUT_EVENT, event)?;
}
WindowEvent::ScaleFactorChanged {
scale_factor,
new_inner_size,
Expand Down

0 comments on commit 395671a

Please sign in to comment.