Implement PointerInputState::pointer_id

Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
This commit is contained in:
Euclid Ye 2025-05-19 18:12:33 +08:00
parent 4c1a09e17b
commit 7b3fd7d347
2 changed files with 53 additions and 19 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::collections::HashSet;
use std::time::{Duration, Instant};
use std::{cmp, thread};
@ -33,29 +34,52 @@ pub(crate) enum InputSourceState {
}
// https://w3c.github.io/webdriver/#dfn-pointer-input-source
// TODO: subtype is used for https://w3c.github.io/webdriver/#dfn-get-a-pointer-id
// Need to add pointer-id to the following struct
#[allow(dead_code)]
pub(crate) struct PointerInputState {
subtype: PointerType,
pressed: HashSet<u64>,
pointer_id: u32,
x: f64,
y: f64,
}
impl PointerInputState {
pub fn new(subtype: &PointerType) -> PointerInputState {
pub fn new(
subtype: &PointerType,
next_pointer_id_below_2: &Cell<u32>,
next_pointer_id_above_2: &Cell<u32>,
) -> PointerInputState {
PointerInputState {
subtype: match subtype {
PointerType::Mouse => PointerType::Mouse,
PointerType::Pen => PointerType::Pen,
PointerType::Touch => PointerType::Touch,
},
subtype: *subtype,
pressed: HashSet::new(),
pointer_id: PointerInputState::get_pointer_id(
subtype,
next_pointer_id_below_2,
next_pointer_id_above_2,
),
x: 0.0,
y: 0.0,
}
}
// https://w3c.github.io/webdriver/#dfn-get-a-pointer-id
fn get_pointer_id(
subtype: &PointerType,
next_pointer_id_below_2: &Cell<u32>,
next_pointer_id_above_2: &Cell<u32>,
) -> u32 {
if let PointerType::Mouse = *subtype {
let val = next_pointer_id_below_2.get();
if val < 2 {
next_pointer_id_below_2.set(val + 1);
return val;
}
}
let val = next_pointer_id_above_2.get();
next_pointer_id_above_2.set(val + 1);
val
}
}
// https://w3c.github.io/webdriver/#dfn-computing-the-tick-duration
@ -244,14 +268,18 @@ impl Handler {
self.dispatch_general_action(source_id);
},
PointerActionItem::Pointer(action) => {
let new_input_source =
InputSourceState::Pointer(PointerInputState::new(
&parameters.pointer_type,
&self.next_pointer_id_below_2,
&self.next_pointer_id_above_2,
));
self.session()
.unwrap()
.input_state_table
.borrow_mut()
.entry(source_id.to_string())
.or_insert(InputSourceState::Pointer(PointerInputState::new(
&parameters.pointer_type,
)));
.or_insert(new_input_source);
match action {
PointerAction::Cancel => (),
PointerAction::Down(action) => {

View file

@ -227,6 +227,8 @@ struct Handler {
current_action_id: Cell<Option<WebDriverMessageId>>,
resize_timeout: u32,
next_pointer_id_below_2: Cell<u32>,
next_pointer_id_above_2: Cell<u32>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
@ -459,6 +461,8 @@ impl Handler {
id_generator: WebDriverMessageIdGenerator::new(),
current_action_id: Cell::new(None),
resize_timeout: 500,
next_pointer_id_above_2: Cell::new(2),
next_pointer_id_below_2: Cell::new(0),
}
}
@ -1652,11 +1656,16 @@ impl Handler {
Some(element_id) => {
let id = Uuid::new_v4().to_string();
let new_input_source = InputSourceState::Pointer(PointerInputState::new(
&PointerType::Mouse,
&self.next_pointer_id_below_2,
&self.next_pointer_id_above_2,
));
// Step 8.1
self.session_mut()?.input_state_table.borrow_mut().insert(
id.clone(),
InputSourceState::Pointer(PointerInputState::new(&PointerType::Mouse)),
);
self.session()?
.input_state_table
.borrow_mut()
.insert(id.clone(), new_input_source);
// Step 8.7. Construct a pointer move action.
// Step 8.8. Set a property x to 0 on pointer move action.
@ -1705,10 +1714,7 @@ impl Handler {
let _ = self.dispatch_actions(&[action_sequence]);
// Step 8.17 Remove an input source with input state and input id.
self.session_mut()?
.input_state_table
.borrow_mut()
.remove(&id);
self.session()?.input_state_table.borrow_mut().remove(&id);
// Step 13
Ok(WebDriverResponse::Void)