Bridge Servo keyboard text input
This commit is contained in:
@@ -8,8 +8,8 @@ use std::{
|
||||
|
||||
use ely_domain::{ProfileId, TabId, UrlText};
|
||||
use ely_servo_host::{
|
||||
MouseClickRequest, NavigationRequest, RenderedFrame, ScrollRequest, ServoHost, ServoHostError,
|
||||
ServoSurfaceSize, SoftwareServoHost, WebViewSnapshot, WebViewState,
|
||||
KeyboardTextRequest, MouseClickRequest, NavigationRequest, RenderedFrame, ScrollRequest,
|
||||
ServoHost, ServoHostError, ServoSurfaceSize, SoftwareServoHost, WebViewSnapshot, WebViewState,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
@@ -37,6 +37,7 @@ struct SnapshotArgs {
|
||||
scroll_x: i32,
|
||||
scroll_y: i32,
|
||||
click_point: Option<ClickPoint>,
|
||||
typed_text: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -118,6 +119,7 @@ fn parse_snapshot_args(
|
||||
let mut scroll_y = 0;
|
||||
let mut click_x = None;
|
||||
let mut click_y = None;
|
||||
let mut typed_text = None;
|
||||
|
||||
while let Some(name) = args.next() {
|
||||
match name.as_str() {
|
||||
@@ -151,6 +153,7 @@ fn parse_snapshot_args(
|
||||
next_argument(&mut args, "--click-y")?,
|
||||
)?)
|
||||
}
|
||||
"--type-text" => typed_text = Some(next_argument(&mut args, "--type-text")?),
|
||||
_ => return Err(SidecarError::UnknownArgument { value: name }),
|
||||
}
|
||||
}
|
||||
@@ -169,6 +172,7 @@ fn parse_snapshot_args(
|
||||
scroll_x,
|
||||
scroll_y,
|
||||
click_point,
|
||||
typed_text,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -225,12 +229,21 @@ fn run_snapshot(args: SnapshotArgs) -> Result<(), SidecarError> {
|
||||
apply_scroll_if_requested(&mut host, &webview_id, &args, snapshot)?;
|
||||
let (snapshot, click_changed_frame) =
|
||||
apply_click_if_requested(&mut host, &webview_id, &args, snapshot)?;
|
||||
let (snapshot, text_changed_frame) =
|
||||
apply_text_if_requested(&mut host, &webview_id, &args, snapshot)?;
|
||||
let frame = host.last_rendered_frame()?;
|
||||
std::fs::write(&args.rgba_out, frame.rgba_bytes())?;
|
||||
|
||||
serde_json::to_writer(
|
||||
std::io::stdout().lock(),
|
||||
&SnapshotReport::new(&args, &snapshot, &frame, scroll_changed_frame, click_changed_frame),
|
||||
&SnapshotReport::new(
|
||||
&args,
|
||||
&snapshot,
|
||||
&frame,
|
||||
scroll_changed_frame,
|
||||
click_changed_frame,
|
||||
text_changed_frame,
|
||||
),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -273,6 +286,24 @@ fn apply_click_if_requested(
|
||||
wait_for_changed_or_settled_frame(host, webview_id, previous_frame_hash)
|
||||
}
|
||||
|
||||
fn apply_text_if_requested(
|
||||
host: &mut SoftwareServoHost,
|
||||
webview_id: &ely_domain::WebViewId,
|
||||
args: &SnapshotArgs,
|
||||
snapshot: WebViewSnapshot,
|
||||
) -> Result<(WebViewSnapshot, bool), SidecarError> {
|
||||
let Some(typed_text) = args.typed_text.as_ref() else {
|
||||
return Ok((snapshot, false));
|
||||
};
|
||||
|
||||
let previous_frame_hash = host.last_rendered_frame()?.sample_hash();
|
||||
host.type_text(KeyboardTextRequest {
|
||||
webview_id: webview_id.clone(),
|
||||
text: typed_text.clone(),
|
||||
})?;
|
||||
wait_for_changed_or_settled_frame(host, webview_id, previous_frame_hash)
|
||||
}
|
||||
|
||||
fn wait_for_frame(
|
||||
host: &mut SoftwareServoHost,
|
||||
webview_id: &ely_domain::WebViewId,
|
||||
@@ -359,6 +390,8 @@ struct SnapshotReport {
|
||||
click_x: Option<u32>,
|
||||
click_y: Option<u32>,
|
||||
click_changed_frame: bool,
|
||||
typed_text_byte_count: usize,
|
||||
text_changed_frame: bool,
|
||||
}
|
||||
|
||||
impl SnapshotReport {
|
||||
@@ -368,6 +401,7 @@ impl SnapshotReport {
|
||||
frame: &RenderedFrame,
|
||||
scroll_changed_frame: bool,
|
||||
click_changed_frame: bool,
|
||||
text_changed_frame: bool,
|
||||
) -> Self {
|
||||
Self {
|
||||
requested_url: args.url.as_str().to_string(),
|
||||
@@ -388,6 +422,8 @@ impl SnapshotReport {
|
||||
click_x: args.click_point.map(|point| point.x),
|
||||
click_y: args.click_point.map(|point| point.y),
|
||||
click_changed_frame,
|
||||
typed_text_byte_count: args.typed_text.as_ref().map_or(0, String::len),
|
||||
text_changed_frame,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,6 +227,12 @@ pub struct MouseClickRequest {
|
||||
pub y: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct KeyboardTextRequest {
|
||||
pub webview_id: WebViewId,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct PermissionRequest {
|
||||
pub webview_id: WebViewId,
|
||||
@@ -255,6 +261,8 @@ pub trait ServoHost {
|
||||
|
||||
fn click(&mut self, request: MouseClickRequest) -> Result<(), ServoHostError>;
|
||||
|
||||
fn type_text(&mut self, request: KeyboardTextRequest) -> Result<(), ServoHostError>;
|
||||
|
||||
fn set_permission(
|
||||
&mut self,
|
||||
request: PermissionRequest,
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
use servo::Code;
|
||||
|
||||
pub(crate) fn keyboard_code_for_character(character: char) -> Code {
|
||||
match character {
|
||||
'a' | 'A' => Code::KeyA,
|
||||
'b' | 'B' => Code::KeyB,
|
||||
'c' | 'C' => Code::KeyC,
|
||||
'd' | 'D' => Code::KeyD,
|
||||
'e' | 'E' => Code::KeyE,
|
||||
'f' | 'F' => Code::KeyF,
|
||||
'g' | 'G' => Code::KeyG,
|
||||
'h' | 'H' => Code::KeyH,
|
||||
'i' | 'I' => Code::KeyI,
|
||||
'j' | 'J' => Code::KeyJ,
|
||||
'k' | 'K' => Code::KeyK,
|
||||
'l' | 'L' => Code::KeyL,
|
||||
'm' | 'M' => Code::KeyM,
|
||||
'n' | 'N' => Code::KeyN,
|
||||
'o' | 'O' => Code::KeyO,
|
||||
'p' | 'P' => Code::KeyP,
|
||||
'q' | 'Q' => Code::KeyQ,
|
||||
'r' | 'R' => Code::KeyR,
|
||||
's' | 'S' => Code::KeyS,
|
||||
't' | 'T' => Code::KeyT,
|
||||
'u' | 'U' => Code::KeyU,
|
||||
'v' | 'V' => Code::KeyV,
|
||||
'w' | 'W' => Code::KeyW,
|
||||
'x' | 'X' => Code::KeyX,
|
||||
'y' | 'Y' => Code::KeyY,
|
||||
'z' | 'Z' => Code::KeyZ,
|
||||
'0' => Code::Digit0,
|
||||
'1' => Code::Digit1,
|
||||
'2' => Code::Digit2,
|
||||
'3' => Code::Digit3,
|
||||
'4' => Code::Digit4,
|
||||
'5' => Code::Digit5,
|
||||
'6' => Code::Digit6,
|
||||
'7' => Code::Digit7,
|
||||
'8' => Code::Digit8,
|
||||
'9' => Code::Digit9,
|
||||
' ' => Code::Space,
|
||||
'-' | '_' => Code::Minus,
|
||||
'=' | '+' => Code::Equal,
|
||||
',' | '<' => Code::Comma,
|
||||
'.' | '>' => Code::Period,
|
||||
'/' | '?' => Code::Slash,
|
||||
';' | ':' => Code::Semicolon,
|
||||
'\'' | '"' => Code::Quote,
|
||||
'[' | '{' => Code::BracketLeft,
|
||||
']' | '}' => Code::BracketRight,
|
||||
'\\' | '|' => Code::Backslash,
|
||||
'`' | '~' => Code::Backquote,
|
||||
_ => Code::Unidentified,
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
mod error;
|
||||
mod host;
|
||||
#[cfg(feature = "servo-engine")]
|
||||
mod keyboard;
|
||||
#[cfg(feature = "servo-engine")]
|
||||
mod runtime;
|
||||
|
||||
pub use error::ServoHostError;
|
||||
pub use host::{
|
||||
MouseClickRequest, NavigationRequest, PermissionDecision, PermissionRequest, RenderedFrame,
|
||||
RenderedFrameSummary, ScrollRequest, ServoHost, WebViewSnapshot, WebViewState,
|
||||
KeyboardTextRequest, MouseClickRequest, NavigationRequest, PermissionDecision,
|
||||
PermissionRequest, RenderedFrame, RenderedFrameSummary, ScrollRequest, ServoHost,
|
||||
WebViewSnapshot, WebViewState,
|
||||
};
|
||||
#[cfg(feature = "servo-engine")]
|
||||
pub use runtime::{ServoSurfaceSize, SoftwareServoHost};
|
||||
|
||||
@@ -12,15 +12,16 @@ use dpi::PhysicalSize;
|
||||
use ely_domain::{ProfileId, TabId, WebViewId};
|
||||
use servo::{
|
||||
DeviceIntPoint, DeviceIntRect, DeviceIntSize, DevicePoint, DeviceVector2D, EventLoopWaker,
|
||||
InputEvent, LoadStatus, MouseButton, MouseButtonAction, MouseButtonEvent, MouseMoveEvent,
|
||||
RenderingContext, Scroll, Servo, ServoBuilder, WebView, WebViewBuilder, WebViewDelegate,
|
||||
WebViewPoint, WebViewVector,
|
||||
InputEvent, Key, KeyState, KeyboardEvent, LoadStatus, Location, Modifiers, MouseButton,
|
||||
MouseButtonAction, MouseButtonEvent, MouseMoveEvent, RenderingContext, Scroll, Servo,
|
||||
ServoBuilder, WebView, WebViewBuilder, WebViewDelegate, WebViewPoint, WebViewVector,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
MouseClickRequest, NavigationRequest, PermissionDecision, PermissionRequest, RenderedFrame,
|
||||
ScrollRequest, ServoHost, ServoHostError, WebViewSnapshot, WebViewState,
|
||||
KeyboardTextRequest, MouseClickRequest, NavigationRequest, PermissionDecision,
|
||||
PermissionRequest, RenderedFrame, ScrollRequest, ServoHost, ServoHostError, WebViewSnapshot,
|
||||
WebViewState, keyboard::keyboard_code_for_character,
|
||||
};
|
||||
|
||||
static SERVO_RUNTIME_STARTED: AtomicBool = AtomicBool::new(false);
|
||||
@@ -185,6 +186,41 @@ impl ServoHost for SoftwareServoHost {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn type_text(&mut self, request: KeyboardTextRequest) -> Result<(), ServoHostError> {
|
||||
let webview = self
|
||||
.webviews
|
||||
.get(&request.webview_id)
|
||||
.ok_or_else(|| ServoHostError::WebViewNotFound { id: request.webview_id.clone() })?;
|
||||
|
||||
for character in request.text.chars() {
|
||||
let key = Key::Character(character.to_string());
|
||||
let code = keyboard_code_for_character(character);
|
||||
webview.webview.notify_input_event(InputEvent::Keyboard(
|
||||
KeyboardEvent::new_without_event(
|
||||
KeyState::Down,
|
||||
key.clone(),
|
||||
code,
|
||||
Location::Standard,
|
||||
Modifiers::empty(),
|
||||
false,
|
||||
false,
|
||||
),
|
||||
));
|
||||
webview.webview.notify_input_event(InputEvent::Keyboard(
|
||||
KeyboardEvent::new_without_event(
|
||||
KeyState::Up,
|
||||
key,
|
||||
code,
|
||||
Location::Standard,
|
||||
Modifiers::empty(),
|
||||
false,
|
||||
false,
|
||||
),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_permission(
|
||||
&mut self,
|
||||
request: PermissionRequest,
|
||||
|
||||
Reference in New Issue
Block a user