WEFT_OS/crates/weft-app-shell/src/keyutils.rs

196 lines
6.5 KiB
Rust
Raw Permalink Normal View History

use servo::{Code, Key, KeyState, KeyboardEvent, Location, Modifiers};
use winit::event::ElementState;
use winit::keyboard::{Key as WinitKey, KeyLocation, ModifiersState, NamedKey, PhysicalKey};
pub fn keyboard_event_from_winit(
event: &winit::event::KeyEvent,
modifiers: ModifiersState,
) -> KeyboardEvent {
let state = match event.state {
ElementState::Pressed => KeyState::Down,
ElementState::Released => KeyState::Up,
};
let key = match &event.logical_key {
fix(shells): paint and present Servo frames and report readiness truthfully With servo-embed, weft-servo-shell and weft-app-shell did not compile against the selected Servo revision: the keyboard event shape, key and mouse action variants, frame readback, shutdown, URL type and window handle APIs had changed. The frame path never called WebView::paint; the software path rebuilt its softbuffer surface every frame and read pixels without painting, and the EGL path presented nothing. weft-app-shell printed READY right after creating its WebView, before any frame, and its feature-disabled build printed READY and parked. Servo also disables CSS Grid by default, which the system UI uses. Both hosts now build against the selected API. Each frame is painted with WebView::paint and presented: the software path reads the painted frame back, presents the context and copies it into a persistent softbuffer surface; the EGL path presents its window context. Nothing is presented before Servo's first repaint request. Shutdown drops the WebView and Servo, whose drop completes the shutdown, and exits the event loop. Both hosts enable layout_grid_enabled. weft-app-shell prints READY on the first frame presented after the document has loaded and Servo reports its rendering settled: on load completion it requests WebView::take_screenshot, which completes only once every frame has fired load, render-blocking resources, images and web fonts are done and the rendering is up to date. Feature-disabled builds of both hosts exit with an error instead of reporting readiness. weft-appd does not consume the app shell's READY yet. tests/frame/check_frame.py runs weft-compositor nested on Xvfb with the winit backend, starts a host showing tests/frame/reference.html and checks the presented pixels (four colour quadrants and text). For the app host the page must be on screen within two seconds of READY; --slow-style delays a render-blocking stylesheet that reveals the page, so premature readiness fails. Validation (Ubuntu 24.04, Mesa 25.2.8 llvmpipe, software path): - check_frame.py --host system, --host app and --host app --slow-style 3: 5 of 5 runs pass each - negative fixtures fail: page with red and green swapped; a fake host printing READY without rendering; a local build printing READY on its first presented frame (--slow-style 4, 3 of 3) - cargo clippy -D warnings with and without servo-embed; cargo fmt --check - unverified: EGL path, DRM, resize, output scale, idle wakeup, input
2026-10-09 01:02:26 +00:00
WinitKey::Named(NamedKey::Space) => Key::Character(" ".to_string()),
WinitKey::Named(n) => Key::Named(named_key(*n)),
fix(shells): paint and present Servo frames and report readiness truthfully With servo-embed, weft-servo-shell and weft-app-shell did not compile against the selected Servo revision: the keyboard event shape, key and mouse action variants, frame readback, shutdown, URL type and window handle APIs had changed. The frame path never called WebView::paint; the software path rebuilt its softbuffer surface every frame and read pixels without painting, and the EGL path presented nothing. weft-app-shell printed READY right after creating its WebView, before any frame, and its feature-disabled build printed READY and parked. Servo also disables CSS Grid by default, which the system UI uses. Both hosts now build against the selected API. Each frame is painted with WebView::paint and presented: the software path reads the painted frame back, presents the context and copies it into a persistent softbuffer surface; the EGL path presents its window context. Nothing is presented before Servo's first repaint request. Shutdown drops the WebView and Servo, whose drop completes the shutdown, and exits the event loop. Both hosts enable layout_grid_enabled. weft-app-shell prints READY on the first frame presented after the document has loaded and Servo reports its rendering settled: on load completion it requests WebView::take_screenshot, which completes only once every frame has fired load, render-blocking resources, images and web fonts are done and the rendering is up to date. Feature-disabled builds of both hosts exit with an error instead of reporting readiness. weft-appd does not consume the app shell's READY yet. tests/frame/check_frame.py runs weft-compositor nested on Xvfb with the winit backend, starts a host showing tests/frame/reference.html and checks the presented pixels (four colour quadrants and text). For the app host the page must be on screen within two seconds of READY; --slow-style delays a render-blocking stylesheet that reveals the page, so premature readiness fails. Validation (Ubuntu 24.04, Mesa 25.2.8 llvmpipe, software path): - check_frame.py --host system, --host app and --host app --slow-style 3: 5 of 5 runs pass each - negative fixtures fail: page with red and green swapped; a fake host printing READY without rendering; a local build printing READY on its first presented frame (--slow-style 4, 3 of 3) - cargo clippy -D warnings with and without servo-embed; cargo fmt --check - unverified: EGL path, DRM, resize, output scale, idle wakeup, input
2026-10-09 01:02:26 +00:00
WinitKey::Character(c) => Key::Character(c.to_string()),
WinitKey::Unidentified(_) => Key::Named(servo::NamedKey::Unidentified),
WinitKey::Dead(_) => Key::Named(servo::NamedKey::Dead),
};
let code = match event.physical_key {
PhysicalKey::Code(c) => winit_code_to_code(c),
PhysicalKey::Unidentified(_) => Code::Unidentified,
};
let location = match event.location {
KeyLocation::Standard => Location::Standard,
KeyLocation::Left => Location::Left,
KeyLocation::Right => Location::Right,
KeyLocation::Numpad => Location::Numpad,
};
let mut mods = Modifiers::empty();
if modifiers.shift_key() {
mods |= Modifiers::SHIFT;
}
if modifiers.control_key() {
mods |= Modifiers::CONTROL;
}
if modifiers.alt_key() {
mods |= Modifiers::ALT;
}
if modifiers.super_key() {
mods |= Modifiers::META;
}
fix(shells): paint and present Servo frames and report readiness truthfully With servo-embed, weft-servo-shell and weft-app-shell did not compile against the selected Servo revision: the keyboard event shape, key and mouse action variants, frame readback, shutdown, URL type and window handle APIs had changed. The frame path never called WebView::paint; the software path rebuilt its softbuffer surface every frame and read pixels without painting, and the EGL path presented nothing. weft-app-shell printed READY right after creating its WebView, before any frame, and its feature-disabled build printed READY and parked. Servo also disables CSS Grid by default, which the system UI uses. Both hosts now build against the selected API. Each frame is painted with WebView::paint and presented: the software path reads the painted frame back, presents the context and copies it into a persistent softbuffer surface; the EGL path presents its window context. Nothing is presented before Servo's first repaint request. Shutdown drops the WebView and Servo, whose drop completes the shutdown, and exits the event loop. Both hosts enable layout_grid_enabled. weft-app-shell prints READY on the first frame presented after the document has loaded and Servo reports its rendering settled: on load completion it requests WebView::take_screenshot, which completes only once every frame has fired load, render-blocking resources, images and web fonts are done and the rendering is up to date. Feature-disabled builds of both hosts exit with an error instead of reporting readiness. weft-appd does not consume the app shell's READY yet. tests/frame/check_frame.py runs weft-compositor nested on Xvfb with the winit backend, starts a host showing tests/frame/reference.html and checks the presented pixels (four colour quadrants and text). For the app host the page must be on screen within two seconds of READY; --slow-style delays a render-blocking stylesheet that reveals the page, so premature readiness fails. Validation (Ubuntu 24.04, Mesa 25.2.8 llvmpipe, software path): - check_frame.py --host system, --host app and --host app --slow-style 3: 5 of 5 runs pass each - negative fixtures fail: page with red and green swapped; a fake host printing READY without rendering; a local build printing READY on its first presented frame (--slow-style 4, 3 of 3) - cargo clippy -D warnings with and without servo-embed; cargo fmt --check - unverified: EGL path, DRM, resize, output scale, idle wakeup, input
2026-10-09 01:02:26 +00:00
KeyboardEvent::new_without_event(state, key, code, location, mods, event.repeat, false)
}
fn named_key(n: NamedKey) -> servo::NamedKey {
use servo::NamedKey as S;
match n {
NamedKey::Alt => S::Alt,
NamedKey::AltGraph => S::AltGraph,
NamedKey::CapsLock => S::CapsLock,
NamedKey::Control => S::Control,
NamedKey::Fn => S::Fn,
NamedKey::FnLock => S::FnLock,
// winit names the Super keys Super; the DOM names them Meta.
NamedKey::Meta | NamedKey::Super => S::Meta,
NamedKey::NumLock => S::NumLock,
NamedKey::ScrollLock => S::ScrollLock,
NamedKey::Shift => S::Shift,
NamedKey::Symbol => S::Symbol,
NamedKey::SymbolLock => S::SymbolLock,
NamedKey::Enter => S::Enter,
NamedKey::Tab => S::Tab,
NamedKey::ArrowDown => S::ArrowDown,
NamedKey::ArrowLeft => S::ArrowLeft,
NamedKey::ArrowRight => S::ArrowRight,
NamedKey::ArrowUp => S::ArrowUp,
NamedKey::End => S::End,
NamedKey::Home => S::Home,
NamedKey::PageDown => S::PageDown,
NamedKey::PageUp => S::PageUp,
NamedKey::Backspace => S::Backspace,
NamedKey::Clear => S::Clear,
NamedKey::Copy => S::Copy,
NamedKey::CrSel => S::CrSel,
NamedKey::Cut => S::Cut,
NamedKey::Delete => S::Delete,
NamedKey::EraseEof => S::EraseEof,
NamedKey::ExSel => S::ExSel,
NamedKey::Insert => S::Insert,
NamedKey::Paste => S::Paste,
NamedKey::Redo => S::Redo,
NamedKey::Undo => S::Undo,
NamedKey::Escape => S::Escape,
NamedKey::F1 => S::F1,
NamedKey::F2 => S::F2,
NamedKey::F3 => S::F3,
NamedKey::F4 => S::F4,
NamedKey::F5 => S::F5,
NamedKey::F6 => S::F6,
NamedKey::F7 => S::F7,
NamedKey::F8 => S::F8,
NamedKey::F9 => S::F9,
NamedKey::F10 => S::F10,
NamedKey::F11 => S::F11,
NamedKey::F12 => S::F12,
_ => S::Unidentified,
}
}
fn winit_code_to_code(c: winit::keyboard::KeyCode) -> Code {
use winit::keyboard::KeyCode as W;
match c {
W::Backquote => Code::Backquote,
W::Backslash => Code::Backslash,
W::BracketLeft => Code::BracketLeft,
W::BracketRight => Code::BracketRight,
W::Comma => Code::Comma,
W::Digit0 => Code::Digit0,
W::Digit1 => Code::Digit1,
W::Digit2 => Code::Digit2,
W::Digit3 => Code::Digit3,
W::Digit4 => Code::Digit4,
W::Digit5 => Code::Digit5,
W::Digit6 => Code::Digit6,
W::Digit7 => Code::Digit7,
W::Digit8 => Code::Digit8,
W::Digit9 => Code::Digit9,
W::Equal => Code::Equal,
W::KeyA => Code::KeyA,
W::KeyB => Code::KeyB,
W::KeyC => Code::KeyC,
W::KeyD => Code::KeyD,
W::KeyE => Code::KeyE,
W::KeyF => Code::KeyF,
W::KeyG => Code::KeyG,
W::KeyH => Code::KeyH,
W::KeyI => Code::KeyI,
W::KeyJ => Code::KeyJ,
W::KeyK => Code::KeyK,
W::KeyL => Code::KeyL,
W::KeyM => Code::KeyM,
W::KeyN => Code::KeyN,
W::KeyO => Code::KeyO,
W::KeyP => Code::KeyP,
W::KeyQ => Code::KeyQ,
W::KeyR => Code::KeyR,
W::KeyS => Code::KeyS,
W::KeyT => Code::KeyT,
W::KeyU => Code::KeyU,
W::KeyV => Code::KeyV,
W::KeyW => Code::KeyW,
W::KeyX => Code::KeyX,
W::KeyY => Code::KeyY,
W::KeyZ => Code::KeyZ,
W::Minus => Code::Minus,
W::Period => Code::Period,
W::Quote => Code::Quote,
W::Semicolon => Code::Semicolon,
W::Slash => Code::Slash,
W::Backspace => Code::Backspace,
W::CapsLock => Code::CapsLock,
W::Enter => Code::Enter,
W::Space => Code::Space,
W::Tab => Code::Tab,
W::Delete => Code::Delete,
W::End => Code::End,
W::Home => Code::Home,
W::Insert => Code::Insert,
W::PageDown => Code::PageDown,
W::PageUp => Code::PageUp,
W::ArrowDown => Code::ArrowDown,
W::ArrowLeft => Code::ArrowLeft,
W::ArrowRight => Code::ArrowRight,
W::ArrowUp => Code::ArrowUp,
W::NumLock => Code::NumLock,
W::Escape => Code::Escape,
W::F1 => Code::F1,
W::F2 => Code::F2,
W::F3 => Code::F3,
W::F4 => Code::F4,
W::F5 => Code::F5,
W::F6 => Code::F6,
W::F7 => Code::F7,
W::F8 => Code::F8,
W::F9 => Code::F9,
W::F10 => Code::F10,
W::F11 => Code::F11,
W::F12 => Code::F12,
W::ShiftLeft => Code::ShiftLeft,
W::ShiftRight => Code::ShiftRight,
W::ControlLeft => Code::ControlLeft,
W::ControlRight => Code::ControlRight,
W::AltLeft => Code::AltLeft,
W::AltRight => Code::AltRight,
W::SuperLeft => Code::MetaLeft,
W::SuperRight => Code::MetaRight,
_ => Code::Unidentified,
}
}