Keyboard input, text input elements
This commit is contained in:
@@ -79,6 +79,34 @@ fn log_platform_event(event: &PlatformEvent) {
|
||||
"pointer event received"
|
||||
);
|
||||
}
|
||||
PlatformEvent::Keyboard { window_id, event } => {
|
||||
tracing::debug!(
|
||||
event = "keyboard_event",
|
||||
window_id = window_id.raw(),
|
||||
keycode = event.keycode,
|
||||
?event.kind,
|
||||
?event.key,
|
||||
?event.modifiers,
|
||||
text = event.text.as_deref().unwrap_or(""),
|
||||
"keyboard event received"
|
||||
);
|
||||
}
|
||||
PlatformEvent::Wake { window_id, token } => {
|
||||
tracing::debug!(
|
||||
event = "wake_event",
|
||||
window_id = window_id.raw(),
|
||||
token,
|
||||
"internal wake event received"
|
||||
);
|
||||
}
|
||||
PlatformEvent::PrimarySelectionText { window_id, text } => {
|
||||
tracing::debug!(
|
||||
event = "primary_selection_text",
|
||||
window_id = window_id.raw(),
|
||||
text,
|
||||
"primary selection text received"
|
||||
);
|
||||
}
|
||||
PlatformEvent::CloseRequested { window_id } => {
|
||||
tracing::info!(
|
||||
event = "close_requested",
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::scene::Point;
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum PointerButton {
|
||||
Primary,
|
||||
Middle,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
@@ -164,6 +165,7 @@ mod tests {
|
||||
element_id: None,
|
||||
rect: Rect::new(0.0, 0.0, 200.0, 120.0),
|
||||
pointer_events: false,
|
||||
focusable: false,
|
||||
cursor: CursorIcon::Default,
|
||||
prepared_text: None,
|
||||
children: vec![
|
||||
@@ -172,6 +174,7 @@ mod tests {
|
||||
element_id: Some(ElementId::new(1)),
|
||||
rect: Rect::new(0.0, 0.0, 120.0, 120.0),
|
||||
pointer_events: true,
|
||||
focusable: false,
|
||||
cursor: CursorIcon::Default,
|
||||
prepared_text: None,
|
||||
children: Vec::new(),
|
||||
@@ -181,6 +184,7 @@ mod tests {
|
||||
element_id: Some(ElementId::new(2)),
|
||||
rect: Rect::new(80.0, 0.0, 120.0, 120.0),
|
||||
pointer_events: true,
|
||||
focusable: false,
|
||||
cursor: CursorIcon::Default,
|
||||
prepared_text: None,
|
||||
children: Vec::new(),
|
||||
@@ -197,6 +201,7 @@ mod tests {
|
||||
element_id: None,
|
||||
rect: Rect::new(0.0, 0.0, 200.0, 120.0),
|
||||
pointer_events: false,
|
||||
focusable: false,
|
||||
cursor: CursorIcon::Default,
|
||||
prepared_text: None,
|
||||
children: vec![LayoutNode {
|
||||
@@ -204,6 +209,7 @@ mod tests {
|
||||
element_id: Some(ElementId::new(1)),
|
||||
rect: Rect::new(0.0, 0.0, 160.0, 120.0),
|
||||
pointer_events: true,
|
||||
focusable: false,
|
||||
cursor: CursorIcon::Default,
|
||||
prepared_text: None,
|
||||
children: vec![LayoutNode {
|
||||
@@ -211,6 +217,7 @@ mod tests {
|
||||
element_id: Some(ElementId::new(2)),
|
||||
rect: Rect::new(16.0, 16.0, 80.0, 40.0),
|
||||
pointer_events: true,
|
||||
focusable: false,
|
||||
cursor: CursorIcon::Default,
|
||||
prepared_text: None,
|
||||
children: Vec::new(),
|
||||
|
||||
57
lib/ui/src/keyboard.rs
Normal file
57
lib/ui/src/keyboard.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub struct KeyboardModifiers {
|
||||
pub shift: bool,
|
||||
pub control: bool,
|
||||
pub alt: bool,
|
||||
pub super_key: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum KeyboardKey {
|
||||
Character(String),
|
||||
Enter,
|
||||
Tab,
|
||||
Escape,
|
||||
Backspace,
|
||||
Delete,
|
||||
ArrowLeft,
|
||||
ArrowRight,
|
||||
ArrowUp,
|
||||
ArrowDown,
|
||||
Home,
|
||||
End,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum KeyboardEventKind {
|
||||
Pressed,
|
||||
Released,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct KeyboardEvent {
|
||||
pub keycode: u32,
|
||||
pub kind: KeyboardEventKind,
|
||||
pub key: KeyboardKey,
|
||||
pub modifiers: KeyboardModifiers,
|
||||
pub text: Option<String>,
|
||||
}
|
||||
|
||||
impl KeyboardEvent {
|
||||
pub fn new(
|
||||
keycode: u32,
|
||||
kind: KeyboardEventKind,
|
||||
key: KeyboardKey,
|
||||
modifiers: KeyboardModifiers,
|
||||
text: Option<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
keycode,
|
||||
kind,
|
||||
key,
|
||||
modifiers,
|
||||
text,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ pub struct HitTarget {
|
||||
pub path: LayoutPath,
|
||||
pub element_id: Option<ElementId>,
|
||||
pub rect: Rect,
|
||||
pub focusable: bool,
|
||||
pub cursor: CursorIcon,
|
||||
}
|
||||
|
||||
@@ -57,6 +58,7 @@ pub struct LayoutNode {
|
||||
pub element_id: Option<ElementId>,
|
||||
pub rect: Rect,
|
||||
pub pointer_events: bool,
|
||||
pub focusable: bool,
|
||||
pub cursor: CursorIcon,
|
||||
pub prepared_text: Option<PreparedText>,
|
||||
pub children: Vec<LayoutNode>,
|
||||
@@ -184,6 +186,7 @@ fn layout_element(
|
||||
element_id: element.id,
|
||||
rect,
|
||||
pointer_events: element.style.pointer_events,
|
||||
focusable: element.style.focusable,
|
||||
cursor,
|
||||
prepared_text: None,
|
||||
children: Vec::new(),
|
||||
@@ -330,6 +333,7 @@ fn hit_path_node(node: &LayoutNode, point: crate::scene::Point) -> Option<Vec<Hi
|
||||
path: node.path.clone(),
|
||||
element_id: node.element_id,
|
||||
rect: node.rect,
|
||||
focusable: node.focusable,
|
||||
cursor: node.cursor,
|
||||
});
|
||||
}
|
||||
@@ -342,6 +346,7 @@ fn hit_path_node(node: &LayoutNode, point: crate::scene::Point) -> Option<Vec<Hi
|
||||
path: node.path.clone(),
|
||||
element_id: node.element_id,
|
||||
rect: node.rect,
|
||||
focusable: node.focusable,
|
||||
cursor: node.cursor,
|
||||
}]);
|
||||
}
|
||||
@@ -373,6 +378,7 @@ fn text_hit_test_node(node: &LayoutNode, point: crate::scene::Point) -> Option<T
|
||||
path: node.path.clone(),
|
||||
element_id: node.element_id,
|
||||
rect: node.rect,
|
||||
focusable: node.focusable,
|
||||
cursor: node.cursor,
|
||||
},
|
||||
byte_offset: prepared_text.byte_offset_for_position(point),
|
||||
|
||||
@@ -11,6 +11,7 @@ pub(crate) mod trace_targets {
|
||||
}
|
||||
|
||||
mod interaction;
|
||||
mod keyboard;
|
||||
mod layout;
|
||||
mod platform;
|
||||
mod runtime;
|
||||
@@ -23,6 +24,7 @@ pub use interaction::{
|
||||
PointerButton, PointerEvent, PointerEventKind, PointerRouter, RoutedPointerEvent,
|
||||
RoutedPointerEventKind,
|
||||
};
|
||||
pub use keyboard::{KeyboardEvent, KeyboardEventKind, KeyboardKey, KeyboardModifiers};
|
||||
pub use layout::{
|
||||
HitTarget, InteractionTree, LayoutNode, LayoutPath, LayoutSnapshot, TextHitTarget,
|
||||
layout_snapshot, layout_snapshot_with_text_system,
|
||||
|
||||
@@ -12,6 +12,7 @@ use ruin_runtime::{WorkerHandle, queue_future, queue_microtask, spawn_worker};
|
||||
use tracing::{debug, info};
|
||||
|
||||
use crate::interaction::PointerEvent;
|
||||
use crate::keyboard::KeyboardEvent;
|
||||
use crate::scene::{SceneSnapshot, UiSize};
|
||||
use crate::trace_targets;
|
||||
use crate::tree::CursorIcon;
|
||||
@@ -64,6 +65,18 @@ pub enum PlatformEvent {
|
||||
window_id: WindowId,
|
||||
event: PointerEvent,
|
||||
},
|
||||
Keyboard {
|
||||
window_id: WindowId,
|
||||
event: KeyboardEvent,
|
||||
},
|
||||
PrimarySelectionText {
|
||||
window_id: WindowId,
|
||||
text: String,
|
||||
},
|
||||
Wake {
|
||||
window_id: WindowId,
|
||||
token: u64,
|
||||
},
|
||||
CloseRequested {
|
||||
window_id: WindowId,
|
||||
},
|
||||
@@ -98,6 +111,9 @@ pub enum PlatformRequest {
|
||||
window_id: WindowId,
|
||||
text: String,
|
||||
},
|
||||
RequestPrimarySelectionText {
|
||||
window_id: WindowId,
|
||||
},
|
||||
SetCursorIcon {
|
||||
window_id: WindowId,
|
||||
cursor: CursorIcon,
|
||||
@@ -109,6 +125,14 @@ pub enum PlatformRequest {
|
||||
window_id: WindowId,
|
||||
event: PointerEvent,
|
||||
},
|
||||
EmitKeyboardEvent {
|
||||
window_id: WindowId,
|
||||
event: KeyboardEvent,
|
||||
},
|
||||
EmitWake {
|
||||
window_id: WindowId,
|
||||
token: u64,
|
||||
},
|
||||
Shutdown,
|
||||
}
|
||||
|
||||
@@ -223,6 +247,13 @@ impl PlatformProxy {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn request_primary_selection_text(
|
||||
&self,
|
||||
window_id: WindowId,
|
||||
) -> Result<(), PlatformClosed> {
|
||||
self.send(PlatformRequest::RequestPrimarySelectionText { window_id })
|
||||
}
|
||||
|
||||
pub fn set_cursor_icon(
|
||||
&self,
|
||||
window_id: WindowId,
|
||||
@@ -243,6 +274,18 @@ impl PlatformProxy {
|
||||
self.send(PlatformRequest::EmitPointerEvent { window_id, event })
|
||||
}
|
||||
|
||||
pub fn emit_keyboard_event(
|
||||
&self,
|
||||
window_id: WindowId,
|
||||
event: KeyboardEvent,
|
||||
) -> Result<(), PlatformClosed> {
|
||||
self.send(PlatformRequest::EmitKeyboardEvent { window_id, event })
|
||||
}
|
||||
|
||||
pub fn emit_wake(&self, window_id: WindowId, token: u64) -> Result<(), PlatformClosed> {
|
||||
self.send(PlatformRequest::EmitWake { window_id, token })
|
||||
}
|
||||
|
||||
pub fn shutdown(&self) -> Result<(), PlatformClosed> {
|
||||
self.send(PlatformRequest::Shutdown)
|
||||
}
|
||||
@@ -286,6 +329,7 @@ pub fn start_headless() -> PlatformRuntime {
|
||||
handle_replace_scene(&state, window_id, scene);
|
||||
}
|
||||
PlatformRequest::SetPrimarySelectionText { .. } => {}
|
||||
PlatformRequest::RequestPrimarySelectionText { .. } => {}
|
||||
PlatformRequest::SetCursorIcon { .. } => {}
|
||||
PlatformRequest::EmitCloseRequested { window_id } => {
|
||||
let sender = state.borrow().events.clone();
|
||||
@@ -294,6 +338,14 @@ pub fn start_headless() -> PlatformRuntime {
|
||||
PlatformRequest::EmitPointerEvent { window_id, event } => {
|
||||
handle_emit_pointer_event(&state, window_id, event);
|
||||
}
|
||||
PlatformRequest::EmitKeyboardEvent { window_id, event } => {
|
||||
let sender = state.borrow().events.clone();
|
||||
let _ = sender.send(PlatformEvent::Keyboard { window_id, event });
|
||||
}
|
||||
PlatformRequest::EmitWake { window_id, token } => {
|
||||
let sender = state.borrow().events.clone();
|
||||
let _ = sender.send(PlatformEvent::Wake { window_id, token });
|
||||
}
|
||||
PlatformRequest::Shutdown => {
|
||||
debug!(
|
||||
target: trace_targets::PLATFORM,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use ruin_reactivity::{EffectHandle, effect};
|
||||
|
||||
use crate::keyboard::KeyboardEvent;
|
||||
use crate::platform::{PlatformClosed, PlatformEvent, PlatformProxy, PlatformRuntime};
|
||||
use crate::scene::SceneSnapshot;
|
||||
use crate::tree::CursorIcon;
|
||||
@@ -116,6 +117,11 @@ impl WindowController {
|
||||
self.proxy.set_primary_selection_text(self.id, text)
|
||||
}
|
||||
|
||||
/// Requests the current plain-text primary selection contents from the platform.
|
||||
pub fn request_primary_selection_text(&self) -> Result<(), PlatformClosed> {
|
||||
self.proxy.request_primary_selection_text(self.id)
|
||||
}
|
||||
|
||||
/// Updates the platform cursor icon for this window.
|
||||
pub fn set_cursor_icon(&self, cursor: CursorIcon) -> Result<(), PlatformClosed> {
|
||||
self.proxy.set_cursor_icon(self.id, cursor)
|
||||
@@ -134,6 +140,16 @@ impl WindowController {
|
||||
self.proxy.emit_pointer_event(self.id, event)
|
||||
}
|
||||
|
||||
/// Delivers a keyboard event for this window through the platform event stream.
|
||||
pub fn emit_keyboard_event(&self, event: KeyboardEvent) -> Result<(), PlatformClosed> {
|
||||
self.proxy.emit_keyboard_event(self.id, event)
|
||||
}
|
||||
|
||||
/// Delivers an internal wake event for this window through the platform event stream.
|
||||
pub fn emit_wake(&self, token: u64) -> Result<(), PlatformClosed> {
|
||||
self.proxy.emit_wake(self.id, token)
|
||||
}
|
||||
|
||||
/// Attaches a reactive effect that rebuilds and replaces the window scene whenever dependent UI
|
||||
/// state changes.
|
||||
pub fn attach_scene_effect(
|
||||
@@ -156,7 +172,10 @@ mod tests {
|
||||
use crate::platform::PlatformEvent;
|
||||
use crate::scene::{Color, Point, PreparedText, Rect, SceneSnapshot, UiSize};
|
||||
use crate::window::{WindowSpec, WindowUpdate};
|
||||
use crate::{PointerEvent, PointerEventKind};
|
||||
use crate::{
|
||||
KeyboardEvent, KeyboardEventKind, KeyboardKey, KeyboardModifiers, PointerEvent,
|
||||
PointerEventKind,
|
||||
};
|
||||
use ruin_runtime::{current_thread_handle, queue_future, run};
|
||||
use std::future::Future;
|
||||
|
||||
@@ -310,4 +329,52 @@ mod tests {
|
||||
ui.shutdown().expect("shutdown should queue");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn window_controller_emits_keyboard_events_through_runtime() {
|
||||
run_async_test(async move {
|
||||
let mut ui = UiRuntime::headless();
|
||||
let window = ui
|
||||
.create_window(WindowSpec::new("keyboard-events").visible(true))
|
||||
.expect("window should be created");
|
||||
|
||||
let _ = ui
|
||||
.wait_for_event_matching(|event| {
|
||||
matches!(event, PlatformEvent::Opened { window_id } if *window_id == window.id())
|
||||
})
|
||||
.await
|
||||
.expect("window should open before keyboard delivery");
|
||||
|
||||
let keyboard_event = KeyboardEvent::new(
|
||||
30,
|
||||
KeyboardEventKind::Pressed,
|
||||
KeyboardKey::Character("a".to_owned()),
|
||||
KeyboardModifiers::default(),
|
||||
Some("a".to_owned()),
|
||||
);
|
||||
window
|
||||
.emit_keyboard_event(keyboard_event.clone())
|
||||
.expect("keyboard event should queue");
|
||||
|
||||
let event = ui
|
||||
.wait_for_event_matching(|event| {
|
||||
matches!(
|
||||
event,
|
||||
PlatformEvent::Keyboard { window_id, .. } if *window_id == window.id()
|
||||
)
|
||||
})
|
||||
.await
|
||||
.expect("keyboard event should be delivered");
|
||||
|
||||
assert_eq!(
|
||||
event,
|
||||
PlatformEvent::Keyboard {
|
||||
window_id: window.id(),
|
||||
event: keyboard_event,
|
||||
}
|
||||
);
|
||||
|
||||
ui.shutdown().expect("shutdown should queue");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,6 +228,18 @@ impl PreparedText {
|
||||
rects
|
||||
}
|
||||
|
||||
pub fn caret_rect(&self, offset: usize, width: f32) -> Option<Rect> {
|
||||
let width = width.max(0.0);
|
||||
let line = self.line_for_offset(offset)?;
|
||||
let x = self.caret_x_for_line_offset(line, offset);
|
||||
Some(Rect::new(
|
||||
x,
|
||||
line.rect.origin.y,
|
||||
width,
|
||||
line.rect.size.height,
|
||||
))
|
||||
}
|
||||
|
||||
pub fn apply_selected_text_color(&mut self, start: usize, end: usize) {
|
||||
let Some(selected_color) = self.selection_style.text_color else {
|
||||
return;
|
||||
@@ -260,6 +272,18 @@ impl PreparedText {
|
||||
Some(last)
|
||||
}
|
||||
|
||||
fn line_for_offset(&self, offset: usize) -> Option<&PreparedTextLine> {
|
||||
let offset = offset.min(self.text.len());
|
||||
let mut lines = self.lines.iter();
|
||||
let first = lines.next()?;
|
||||
for line in std::iter::once(first).chain(lines) {
|
||||
if offset <= line.text_end {
|
||||
return Some(line);
|
||||
}
|
||||
}
|
||||
Some(self.lines.last().unwrap_or(first))
|
||||
}
|
||||
|
||||
fn byte_offset_for_line_position(&self, line: &PreparedTextLine, x: f32) -> usize {
|
||||
if line.glyph_start == line.glyph_end {
|
||||
return line.text_start;
|
||||
@@ -285,6 +309,33 @@ impl PreparedText {
|
||||
|
||||
line.text_end
|
||||
}
|
||||
|
||||
fn caret_x_for_line_offset(&self, line: &PreparedTextLine, offset: usize) -> f32 {
|
||||
if line.glyph_start == line.glyph_end {
|
||||
return line.rect.origin.x;
|
||||
}
|
||||
|
||||
let offset = offset.min(self.text.len());
|
||||
let line_glyphs = &self.glyphs[line.glyph_start..line.glyph_end];
|
||||
let first_glyph = &line_glyphs[0];
|
||||
if offset <= first_glyph.text_start {
|
||||
return first_glyph.position.x;
|
||||
}
|
||||
|
||||
for glyph in line_glyphs {
|
||||
if offset <= glyph.text_start {
|
||||
return glyph.position.x;
|
||||
}
|
||||
if offset <= glyph.text_end {
|
||||
return glyph.position.x + glyph.advance.max(0.0);
|
||||
}
|
||||
}
|
||||
|
||||
line_glyphs
|
||||
.last()
|
||||
.map(|glyph| glyph.position.x + glyph.advance.max(0.0))
|
||||
.unwrap_or(line.rect.origin.x)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -409,4 +460,28 @@ mod tests {
|
||||
assert_eq!(text.glyphs[1].color, Color::rgb(0x11, 0x12, 0x1A));
|
||||
assert_eq!(text.glyphs[2].color, Color::rgb(0x11, 0x12, 0x1A));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prepared_text_caret_rect_tracks_cluster_boundaries() {
|
||||
let text = PreparedText::monospace(
|
||||
"abcd",
|
||||
Point::new(10.0, 20.0),
|
||||
16.0,
|
||||
8.0,
|
||||
Color::rgb(0xFF, 0xFF, 0xFF),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
text.caret_rect(0, 2.0),
|
||||
Some(Rect::new(10.0, 20.0, 2.0, 16.0))
|
||||
);
|
||||
assert_eq!(
|
||||
text.caret_rect(2, 2.0),
|
||||
Some(Rect::new(26.0, 20.0, 2.0, 16.0))
|
||||
);
|
||||
assert_eq!(
|
||||
text.caret_rect(4, 2.0),
|
||||
Some(Rect::new(42.0, 20.0, 2.0, 16.0))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ pub struct Style {
|
||||
pub padding: Edges,
|
||||
pub background: Option<Color>,
|
||||
pub pointer_events: bool,
|
||||
pub focusable: bool,
|
||||
pub cursor: Option<CursorIcon>,
|
||||
}
|
||||
|
||||
@@ -86,6 +87,7 @@ impl Default for Style {
|
||||
padding: Edges::ZERO,
|
||||
background: None,
|
||||
pointer_events: true,
|
||||
focusable: false,
|
||||
cursor: None,
|
||||
}
|
||||
}
|
||||
@@ -198,6 +200,11 @@ impl Element {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn focusable(mut self, focusable: bool) -> Self {
|
||||
self.style.focusable = focusable;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cursor(mut self, cursor: CursorIcon) -> Self {
|
||||
self.style.cursor = Some(cursor);
|
||||
self
|
||||
|
||||
@@ -13,3 +13,4 @@ tracing = "0.1"
|
||||
wayland-backend = { version = "0.3", features = ["client_system"] }
|
||||
wayland-client = "0.31"
|
||||
wayland-protocols = { version = "0.32", features = ["client", "staging", "unstable"] }
|
||||
xkbcommon = "0.8"
|
||||
|
||||
@@ -4,12 +4,13 @@ use std::error::Error;
|
||||
use std::ffi::c_void;
|
||||
use std::fs::File;
|
||||
use std::io::ErrorKind;
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
use std::num::NonZeroU32;
|
||||
use std::os::fd::{AsFd, AsRawFd};
|
||||
use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
|
||||
use std::ptr::NonNull;
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use raw_window_handle::{
|
||||
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle,
|
||||
@@ -18,15 +19,17 @@ use raw_window_handle::{
|
||||
use ruin_runtime::channel::mpsc;
|
||||
use ruin_runtime::{WorkerHandle, queue_future, queue_task, spawn_worker};
|
||||
use ruin_ui::{
|
||||
CursorIcon, PlatformEndpoint, PlatformEvent, PlatformRequest, PlatformRuntime, Point,
|
||||
PointerButton, PointerEvent, PointerEventKind, SceneSnapshot, UiRuntime, UiSize,
|
||||
WindowConfigured, WindowId, WindowLifecycle, WindowSpec, WindowUpdate,
|
||||
CursorIcon, KeyboardEvent, KeyboardEventKind, KeyboardKey, KeyboardModifiers, PlatformEndpoint,
|
||||
PlatformEvent, PlatformRequest, PlatformRuntime, Point, PointerButton, PointerEvent,
|
||||
PointerEventKind, SceneSnapshot, UiRuntime, UiSize, WindowConfigured, WindowId,
|
||||
WindowLifecycle, WindowSpec, WindowUpdate,
|
||||
};
|
||||
use ruin_ui_renderer_wgpu::{RenderError, WgpuSceneRenderer};
|
||||
use tracing::Level;
|
||||
use tracing::{debug, trace};
|
||||
use wayland_client::globals::{GlobalListContents, registry_queue_init};
|
||||
use wayland_client::protocol::{
|
||||
wl_callback, wl_compositor, wl_pointer, wl_registry, wl_seat, wl_surface,
|
||||
wl_callback, wl_compositor, wl_keyboard, wl_pointer, wl_registry, wl_seat, wl_surface,
|
||||
};
|
||||
use wayland_client::{
|
||||
Connection, Dispatch, Proxy, QueueHandle, WEnum, delegate_noop, event_created_child,
|
||||
@@ -39,6 +42,7 @@ use wayland_protocols::wp::primary_selection::zv1::client::{
|
||||
zwp_primary_selection_offer_v1, zwp_primary_selection_source_v1,
|
||||
};
|
||||
use wayland_protocols::xdg::shell::client::{xdg_surface, xdg_toplevel, xdg_wm_base};
|
||||
use xkbcommon::xkb;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WaylandSurfaceTarget {
|
||||
@@ -114,6 +118,7 @@ struct WindowWorkerState {
|
||||
enum WindowWorkerCommand {
|
||||
ReplaceScene(SceneSnapshot),
|
||||
SetPrimarySelectionText(String),
|
||||
RequestPrimarySelectionText,
|
||||
SetCursorIcon(CursorIcon),
|
||||
ApplySpec(WindowSpec),
|
||||
Shutdown,
|
||||
@@ -139,6 +144,11 @@ struct State {
|
||||
primary_selection_device: Option<zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1>,
|
||||
qh: QueueHandle<State>,
|
||||
pointer: Option<wl_pointer::WlPointer>,
|
||||
keyboard: Option<wl_keyboard::WlKeyboard>,
|
||||
keyboard_focused: bool,
|
||||
xkb_context: xkb::Context,
|
||||
xkb_keymap: Option<xkb::Keymap>,
|
||||
xkb_state: Option<xkb::State>,
|
||||
current_size: (u32, u32),
|
||||
configured: bool,
|
||||
pending_size: Option<(u32, u32)>,
|
||||
@@ -146,17 +156,54 @@ struct State {
|
||||
frame_callback: Option<wl_callback::WlCallback>,
|
||||
pointer_position: Option<Point>,
|
||||
pending_pointer_events: Vec<PointerEvent>,
|
||||
pending_keyboard_events: Vec<KeyboardEvent>,
|
||||
keyboard_modifiers: KeyboardModifiers,
|
||||
keyboard_repeat_rate: i32,
|
||||
keyboard_repeat_delay: Duration,
|
||||
keyboard_repeat: Option<KeyboardRepeatState>,
|
||||
primary_selection_source: Option<zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1>,
|
||||
primary_selection_text: Option<String>,
|
||||
primary_selection_offer: Option<zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1>,
|
||||
primary_selection_offer_mime_types: Vec<String>,
|
||||
last_selection_serial: Option<u32>,
|
||||
last_pointer_enter_serial: Option<u32>,
|
||||
cursor_icon: CursorIcon,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct KeyboardRepeatState {
|
||||
keycode: u32,
|
||||
next_at: Instant,
|
||||
interval: Duration,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn request_redraw(&mut self) {
|
||||
self.needs_redraw = true;
|
||||
}
|
||||
|
||||
fn queue_ready_keyboard_repeats(&mut self) {
|
||||
let Some(repeat) = self.keyboard_repeat.as_mut() else {
|
||||
return;
|
||||
};
|
||||
let Some(xkb_state) = self.xkb_state.as_ref() else {
|
||||
return;
|
||||
};
|
||||
let now = Instant::now();
|
||||
while now >= repeat.next_at {
|
||||
let keycode = xkb::Keycode::new(repeat.keycode + 8);
|
||||
let text = keyboard_text_for_xkb(xkb_state, keycode);
|
||||
let key = keyboard_key_from_xkb(xkb_state.key_get_one_sym(keycode), text.as_deref());
|
||||
self.pending_keyboard_events.push(KeyboardEvent::new(
|
||||
repeat.keycode,
|
||||
KeyboardEventKind::Pressed,
|
||||
key,
|
||||
self.keyboard_modifiers,
|
||||
text,
|
||||
));
|
||||
repeat.next_at += repeat.interval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn wayland_cursor_shape(icon: CursorIcon) -> wp_cursor_shape_device_v1::Shape {
|
||||
@@ -178,6 +225,48 @@ fn apply_cursor_icon(state: &mut State) {
|
||||
let _ = state._connection.flush();
|
||||
}
|
||||
|
||||
fn keyboard_modifiers_from_xkb(state: &xkb::State) -> KeyboardModifiers {
|
||||
KeyboardModifiers {
|
||||
shift: state.mod_name_is_active(xkb::MOD_NAME_SHIFT, xkb::STATE_MODS_EFFECTIVE),
|
||||
control: state.mod_name_is_active(xkb::MOD_NAME_CTRL, xkb::STATE_MODS_EFFECTIVE),
|
||||
alt: state.mod_name_is_active(xkb::MOD_NAME_ALT, xkb::STATE_MODS_EFFECTIVE),
|
||||
super_key: state.mod_name_is_active(xkb::MOD_NAME_LOGO, xkb::STATE_MODS_EFFECTIVE),
|
||||
}
|
||||
}
|
||||
|
||||
fn keyboard_text_for_xkb(state: &xkb::State, keycode: xkb::Keycode) -> Option<String> {
|
||||
let text = state.key_get_utf8(keycode);
|
||||
if text.is_empty() || text.chars().any(char::is_control) {
|
||||
return None;
|
||||
}
|
||||
Some(text)
|
||||
}
|
||||
|
||||
fn keyboard_key_from_xkb(keysym: xkb::Keysym, text: Option<&str>) -> KeyboardKey {
|
||||
match xkb::keysym_get_name(keysym).as_str() {
|
||||
"BackSpace" => KeyboardKey::Backspace,
|
||||
"Delete" => KeyboardKey::Delete,
|
||||
"Return" => KeyboardKey::Enter,
|
||||
"Tab" => KeyboardKey::Tab,
|
||||
"Escape" => KeyboardKey::Escape,
|
||||
"Left" => KeyboardKey::ArrowLeft,
|
||||
"Right" => KeyboardKey::ArrowRight,
|
||||
"Up" => KeyboardKey::ArrowUp,
|
||||
"Down" => KeyboardKey::ArrowDown,
|
||||
"Home" => KeyboardKey::Home,
|
||||
"End" => KeyboardKey::End,
|
||||
_ => text
|
||||
.filter(|text| !text.is_empty())
|
||||
.map(str::to_owned)
|
||||
.or_else(|| {
|
||||
let utf8 = xkb::keysym_to_utf8(keysym);
|
||||
(!utf8.is_empty() && !utf8.chars().any(char::is_control)).then_some(utf8)
|
||||
})
|
||||
.map(KeyboardKey::Character)
|
||||
.unwrap_or(KeyboardKey::Unknown),
|
||||
}
|
||||
}
|
||||
|
||||
impl WaylandWindow {
|
||||
pub fn open(spec: WindowSpec) -> Result<Self, Box<dyn Error>> {
|
||||
let connection = Connection::connect_to_env()?;
|
||||
@@ -241,6 +330,11 @@ impl WaylandWindow {
|
||||
primary_selection_device,
|
||||
qh,
|
||||
pointer: None,
|
||||
keyboard: None,
|
||||
keyboard_focused: false,
|
||||
xkb_context: xkb::Context::new(xkb::CONTEXT_NO_FLAGS),
|
||||
xkb_keymap: None,
|
||||
xkb_state: None,
|
||||
current_size: (initial_width, initial_height),
|
||||
configured: false,
|
||||
pending_size: None,
|
||||
@@ -248,8 +342,15 @@ impl WaylandWindow {
|
||||
frame_callback: None,
|
||||
pointer_position: None,
|
||||
pending_pointer_events: Vec::new(),
|
||||
pending_keyboard_events: Vec::new(),
|
||||
keyboard_modifiers: KeyboardModifiers::default(),
|
||||
keyboard_repeat_rate: 25,
|
||||
keyboard_repeat_delay: Duration::from_millis(500),
|
||||
keyboard_repeat: None,
|
||||
primary_selection_source: None,
|
||||
primary_selection_text: None,
|
||||
primary_selection_offer: None,
|
||||
primary_selection_offer_mime_types: Vec::new(),
|
||||
last_selection_serial: None,
|
||||
last_pointer_enter_serial: None,
|
||||
cursor_icon: CursorIcon::Default,
|
||||
@@ -430,6 +531,43 @@ impl WaylandWindow {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read_primary_selection_text(&mut self) -> Result<Option<String>, Box<dyn Error>> {
|
||||
let preferred_mime = self
|
||||
.state
|
||||
.primary_selection_offer_mime_types
|
||||
.iter()
|
||||
.find(|mime| mime.as_str() == "text/plain;charset=utf-8")
|
||||
.or_else(|| {
|
||||
self.state
|
||||
.primary_selection_offer_mime_types
|
||||
.iter()
|
||||
.find(|mime| mime.as_str() == "text/plain")
|
||||
})
|
||||
.cloned();
|
||||
let Some(mime_type) = preferred_mime else {
|
||||
return Ok(self.state.primary_selection_text.clone());
|
||||
};
|
||||
let Some(offer) = self.state.primary_selection_offer.as_ref() else {
|
||||
return Ok(self.state.primary_selection_text.clone());
|
||||
};
|
||||
|
||||
let mut pipe_fds = [0; 2];
|
||||
let pipe_result = unsafe { libc::pipe2(pipe_fds.as_mut_ptr(), libc::O_CLOEXEC) };
|
||||
if pipe_result != 0 {
|
||||
return Err(Box::new(std::io::Error::last_os_error()));
|
||||
}
|
||||
|
||||
let read_fd = unsafe { OwnedFd::from_raw_fd(pipe_fds[0]) };
|
||||
let write_fd = unsafe { OwnedFd::from_raw_fd(pipe_fds[1]) };
|
||||
offer.receive(mime_type, write_fd.as_fd());
|
||||
self.state._connection.flush()?;
|
||||
drop(write_fd);
|
||||
let mut file = File::from(read_fd);
|
||||
let mut text = String::new();
|
||||
file.read_to_string(&mut text)?;
|
||||
Ok((!text.is_empty()).then_some(text))
|
||||
}
|
||||
|
||||
pub fn set_cursor_icon(&mut self, cursor: CursorIcon) -> Result<(), Box<dyn Error>> {
|
||||
self.state.cursor_icon = cursor;
|
||||
apply_cursor_icon(&mut self.state);
|
||||
@@ -476,6 +614,11 @@ impl WaylandWindow {
|
||||
std::mem::take(&mut self.state.pending_pointer_events)
|
||||
}
|
||||
|
||||
pub fn drain_keyboard_events(&mut self) -> Vec<KeyboardEvent> {
|
||||
self.state.queue_ready_keyboard_repeats();
|
||||
std::mem::take(&mut self.state.pending_keyboard_events)
|
||||
}
|
||||
|
||||
pub fn prepare_frame(&mut self) -> Option<FrameRequest> {
|
||||
if !self.state.configured {
|
||||
return None;
|
||||
@@ -537,6 +680,9 @@ fn run_wayland_platform(mut endpoint: PlatformEndpoint) {
|
||||
PlatformRequest::SetPrimarySelectionText { window_id, text } => {
|
||||
handle_set_primary_selection_text(&state, window_id, text);
|
||||
}
|
||||
PlatformRequest::RequestPrimarySelectionText { window_id } => {
|
||||
handle_request_primary_selection_text(&state, window_id);
|
||||
}
|
||||
PlatformRequest::SetCursorIcon { window_id, cursor } => {
|
||||
handle_set_cursor_icon(&state, window_id, cursor);
|
||||
}
|
||||
@@ -546,6 +692,12 @@ fn run_wayland_platform(mut endpoint: PlatformEndpoint) {
|
||||
PlatformRequest::EmitPointerEvent { window_id, event } => {
|
||||
emit_wayland_event(&state, PlatformEvent::Pointer { window_id, event });
|
||||
}
|
||||
PlatformRequest::EmitKeyboardEvent { window_id, event } => {
|
||||
emit_wayland_event(&state, PlatformEvent::Keyboard { window_id, event });
|
||||
}
|
||||
PlatformRequest::EmitWake { window_id, token } => {
|
||||
emit_wayland_event(&state, PlatformEvent::Wake { window_id, token });
|
||||
}
|
||||
PlatformRequest::Shutdown => {
|
||||
shutdown_wayland_backend(&state);
|
||||
break;
|
||||
@@ -702,6 +854,21 @@ fn handle_set_primary_selection_text(
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_request_primary_selection_text(
|
||||
state: &Rc<RefCell<WaylandBackendState>>,
|
||||
window_id: WindowId,
|
||||
) {
|
||||
if let Some(command_tx) = state
|
||||
.borrow()
|
||||
.windows
|
||||
.get(&window_id)
|
||||
.and_then(|record| record.worker.as_ref())
|
||||
.map(|worker| worker.command_tx.clone())
|
||||
{
|
||||
let _ = command_tx.send(WindowWorkerCommand::RequestPrimarySelectionText);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_set_cursor_icon(
|
||||
state: &Rc<RefCell<WaylandBackendState>>,
|
||||
window_id: WindowId,
|
||||
@@ -818,6 +985,28 @@ fn spawn_window_worker(
|
||||
);
|
||||
}
|
||||
}
|
||||
WindowWorkerCommand::RequestPrimarySelectionText => {
|
||||
let mut state_ref = state.borrow_mut();
|
||||
match state_ref.window.read_primary_selection_text() {
|
||||
Ok(Some(text)) => {
|
||||
let _ = state_ref.event_tx.send(
|
||||
PlatformEvent::PrimarySelectionText {
|
||||
window_id: state_ref.window_id,
|
||||
text,
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(error) => {
|
||||
debug!(
|
||||
target: "ruin_ui_platform_wayland::clipboard",
|
||||
window_id = state_ref.window_id.raw(),
|
||||
error = %error,
|
||||
"failed to read primary selection text"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
WindowWorkerCommand::SetCursorIcon(cursor) => {
|
||||
let mut state_ref = state.borrow_mut();
|
||||
if let Err(error) = state_ref.window.set_cursor_icon(cursor) {
|
||||
@@ -877,6 +1066,21 @@ fn pump_window_worker(state: Rc<RefCell<WindowWorkerState>>) {
|
||||
event,
|
||||
});
|
||||
}
|
||||
for event in state_ref.window.drain_keyboard_events() {
|
||||
tracing::trace!(
|
||||
target: "ruin_ui_platform_wayland::event_bridge",
|
||||
window_id = state_ref.window_id.raw(),
|
||||
keycode = event.keycode,
|
||||
?event.kind,
|
||||
?event.key,
|
||||
text = event.text.as_deref().unwrap_or(""),
|
||||
"forwarding keyboard event to UI runtime"
|
||||
);
|
||||
let _ = state_ref.event_tx.send(PlatformEvent::Keyboard {
|
||||
window_id: state_ref.window_id,
|
||||
event,
|
||||
});
|
||||
}
|
||||
|
||||
if !state_ref.window.is_running() {
|
||||
emit_window_closed(&mut state_ref, true);
|
||||
@@ -1086,8 +1290,6 @@ delegate_noop!(
|
||||
State: ignore
|
||||
zwp_primary_selection_device_manager_v1::ZwpPrimarySelectionDeviceManagerV1
|
||||
);
|
||||
delegate_noop!(State: ignore zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1);
|
||||
|
||||
impl Dispatch<wl_seat::WlSeat, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
@@ -1116,6 +1318,17 @@ impl Dispatch<wl_seat::WlSeat, ()> for State {
|
||||
state.pointer_position = None;
|
||||
state.last_pointer_enter_serial = None;
|
||||
}
|
||||
if capabilities.contains(wl_seat::Capability::Keyboard) {
|
||||
if state.keyboard.is_none() {
|
||||
state.keyboard = Some(seat.get_keyboard(qh, ()));
|
||||
}
|
||||
} else {
|
||||
state.keyboard = None;
|
||||
state.xkb_keymap = None;
|
||||
state.xkb_state = None;
|
||||
state.keyboard_modifiers = KeyboardModifiers::default();
|
||||
state.keyboard_repeat = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1178,17 +1391,21 @@ impl Dispatch<wl_pointer::WlPointer, ()> for State {
|
||||
let Some(position) = state.pointer_position else {
|
||||
return;
|
||||
};
|
||||
if button != 0x110 {
|
||||
return;
|
||||
let button = match button {
|
||||
0x110 => PointerButton::Primary,
|
||||
0x112 => PointerButton::Middle,
|
||||
_ => return,
|
||||
};
|
||||
if button == PointerButton::Primary {
|
||||
state.last_selection_serial = Some(serial);
|
||||
}
|
||||
state.last_selection_serial = Some(serial);
|
||||
let kind = match button_state {
|
||||
WEnum::Value(wl_pointer::ButtonState::Pressed) => PointerEventKind::Down {
|
||||
button: PointerButton::Primary,
|
||||
},
|
||||
WEnum::Value(wl_pointer::ButtonState::Released) => PointerEventKind::Up {
|
||||
button: PointerButton::Primary,
|
||||
},
|
||||
WEnum::Value(wl_pointer::ButtonState::Pressed) => {
|
||||
PointerEventKind::Down { button }
|
||||
}
|
||||
WEnum::Value(wl_pointer::ButtonState::Released) => {
|
||||
PointerEventKind::Up { button }
|
||||
}
|
||||
WEnum::Value(_) | WEnum::Unknown(_) => return,
|
||||
};
|
||||
state
|
||||
@@ -1200,15 +1417,200 @@ impl Dispatch<wl_pointer::WlPointer, ()> for State {
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1, ()> for State {
|
||||
impl Dispatch<wl_keyboard::WlKeyboard, ()> for State {
|
||||
fn event(
|
||||
_state: &mut Self,
|
||||
_data_device: &zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1,
|
||||
_event: zwp_primary_selection_device_v1::Event,
|
||||
state: &mut Self,
|
||||
_keyboard: &wl_keyboard::WlKeyboard,
|
||||
event: wl_keyboard::Event,
|
||||
_data: &(),
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
) {
|
||||
tracing::event!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
Level::INFO,
|
||||
?event,
|
||||
"received keyboard event"
|
||||
);
|
||||
match event {
|
||||
wl_keyboard::Event::Keymap { format, fd, size } => {
|
||||
let WEnum::Value(wl_keyboard::KeymapFormat::XkbV1) = format else {
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
?format,
|
||||
"ignored unsupported keymap format"
|
||||
);
|
||||
return;
|
||||
};
|
||||
let Ok(size) = usize::try_from(size) else {
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
size,
|
||||
"ignored keymap with invalid size"
|
||||
);
|
||||
return;
|
||||
};
|
||||
let keymap = match unsafe {
|
||||
xkb::Keymap::new_from_fd(
|
||||
&state.xkb_context,
|
||||
fd,
|
||||
size,
|
||||
xkb::KEYMAP_FORMAT_TEXT_V1,
|
||||
xkb::KEYMAP_COMPILE_NO_FLAGS,
|
||||
)
|
||||
} {
|
||||
Ok(Some(keymap)) => keymap,
|
||||
Ok(None) => {
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
"failed to compile XKB keymap"
|
||||
);
|
||||
return;
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
size,
|
||||
error = %error,
|
||||
"failed to map compositor keymap fd"
|
||||
);
|
||||
return;
|
||||
}
|
||||
};
|
||||
state.xkb_state = Some(xkb::State::new(&keymap));
|
||||
state.xkb_keymap = Some(keymap);
|
||||
state.keyboard_modifiers = KeyboardModifiers::default();
|
||||
state.keyboard_repeat = None;
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
"installed XKB keymap"
|
||||
);
|
||||
}
|
||||
wl_keyboard::Event::Enter { .. } => {
|
||||
state.keyboard_focused = true;
|
||||
state.keyboard_repeat = None;
|
||||
}
|
||||
wl_keyboard::Event::Leave { .. } => {
|
||||
state.keyboard_focused = false;
|
||||
state.keyboard_modifiers = KeyboardModifiers::default();
|
||||
state.keyboard_repeat = None;
|
||||
}
|
||||
wl_keyboard::Event::RepeatInfo { rate, delay } => {
|
||||
state.keyboard_repeat_rate = rate;
|
||||
state.keyboard_repeat_delay = Duration::from_millis(delay.max(0) as u64);
|
||||
if rate <= 0 {
|
||||
state.keyboard_repeat = None;
|
||||
}
|
||||
}
|
||||
wl_keyboard::Event::Modifiers {
|
||||
mods_depressed,
|
||||
mods_latched,
|
||||
mods_locked,
|
||||
group,
|
||||
..
|
||||
} => {
|
||||
let Some(xkb_state) = state.xkb_state.as_mut() else {
|
||||
return;
|
||||
};
|
||||
xkb_state.update_mask(mods_depressed, mods_latched, mods_locked, 0, 0, group);
|
||||
state.keyboard_modifiers = keyboard_modifiers_from_xkb(xkb_state);
|
||||
}
|
||||
wl_keyboard::Event::Key {
|
||||
key,
|
||||
state: key_state,
|
||||
..
|
||||
} => {
|
||||
if !state.keyboard_focused {
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
key,
|
||||
?key_state,
|
||||
"dropping key because keyboard focus is not active"
|
||||
);
|
||||
return;
|
||||
}
|
||||
let Some(xkb_state) = state.xkb_state.as_mut() else {
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
key,
|
||||
?key_state,
|
||||
"dropping key because XKB state is not initialized"
|
||||
);
|
||||
return;
|
||||
};
|
||||
let (kind, direction) = match key_state {
|
||||
WEnum::Value(wl_keyboard::KeyState::Pressed) => {
|
||||
(KeyboardEventKind::Pressed, xkb::KeyDirection::Down)
|
||||
}
|
||||
WEnum::Value(wl_keyboard::KeyState::Released) => {
|
||||
(KeyboardEventKind::Released, xkb::KeyDirection::Up)
|
||||
}
|
||||
WEnum::Value(_) | WEnum::Unknown(_) => return,
|
||||
};
|
||||
let keycode = xkb::Keycode::new(key + 8);
|
||||
xkb_state.update_key(keycode, direction);
|
||||
state.keyboard_modifiers = keyboard_modifiers_from_xkb(xkb_state);
|
||||
let text = matches!(kind, KeyboardEventKind::Pressed)
|
||||
.then(|| keyboard_text_for_xkb(xkb_state, keycode))
|
||||
.flatten();
|
||||
let logical_key =
|
||||
keyboard_key_from_xkb(xkb_state.key_get_one_sym(keycode), text.as_deref());
|
||||
state.pending_keyboard_events.push(KeyboardEvent::new(
|
||||
key,
|
||||
kind,
|
||||
logical_key.clone(),
|
||||
state.keyboard_modifiers,
|
||||
text,
|
||||
));
|
||||
tracing::info!(
|
||||
target: "ruin_ui_platform_wayland::keyboard",
|
||||
keycode = key,
|
||||
?kind,
|
||||
?logical_key,
|
||||
modifiers = ?state.keyboard_modifiers,
|
||||
queued = state.pending_keyboard_events.len(),
|
||||
"queued translated keyboard event"
|
||||
);
|
||||
if kind == KeyboardEventKind::Pressed
|
||||
&& state.keyboard_repeat_rate > 0
|
||||
&& state
|
||||
.xkb_keymap
|
||||
.as_ref()
|
||||
.is_some_and(|keymap| keymap.key_repeats(keycode))
|
||||
{
|
||||
state.keyboard_repeat = Some(KeyboardRepeatState {
|
||||
keycode: key,
|
||||
next_at: Instant::now() + state.keyboard_repeat_delay,
|
||||
interval: Duration::from_secs_f64(
|
||||
1.0 / f64::from(state.keyboard_repeat_rate),
|
||||
),
|
||||
});
|
||||
} else if state
|
||||
.keyboard_repeat
|
||||
.as_ref()
|
||||
.is_some_and(|repeat| repeat.keycode == key)
|
||||
{
|
||||
state.keyboard_repeat = None;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
_data_device: &zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1,
|
||||
event: zwp_primary_selection_device_v1::Event,
|
||||
_data: &(),
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
) {
|
||||
if let zwp_primary_selection_device_v1::Event::Selection { id } = event {
|
||||
state.primary_selection_offer = id;
|
||||
state.primary_selection_offer_mime_types.clear();
|
||||
}
|
||||
}
|
||||
|
||||
event_created_child!(State, zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1, [
|
||||
@@ -1217,6 +1619,23 @@ impl Dispatch<zwp_primary_selection_device_v1::ZwpPrimarySelectionDeviceV1, ()>
|
||||
]);
|
||||
}
|
||||
|
||||
impl Dispatch<zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
offer: &zwp_primary_selection_offer_v1::ZwpPrimarySelectionOfferV1,
|
||||
event: zwp_primary_selection_offer_v1::Event,
|
||||
_data: &(),
|
||||
_conn: &Connection,
|
||||
_qh: &QueueHandle<Self>,
|
||||
) {
|
||||
if let zwp_primary_selection_offer_v1::Event::Offer { mime_type } = event
|
||||
&& state.primary_selection_offer.as_ref() == Some(offer)
|
||||
{
|
||||
state.primary_selection_offer_mime_types.push(mime_type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<zwp_primary_selection_source_v1::ZwpPrimarySelectionSourceV1, ()> for State {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
|
||||
Reference in New Issue
Block a user