Keyboard input, text input elements

This commit is contained in:
2026-03-21 01:17:07 -04:00
parent 423df4ae1f
commit 84077b718f
13 changed files with 1451 additions and 52 deletions

View File

@@ -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");
});
}
}