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 * 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/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::collections::HashSet; use std::collections::HashSet;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::{cmp, thread}; use std::{cmp, thread};
@ -33,29 +34,52 @@ pub(crate) enum InputSourceState {
} }
// https://w3c.github.io/webdriver/#dfn-pointer-input-source // 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)] #[allow(dead_code)]
pub(crate) struct PointerInputState { pub(crate) struct PointerInputState {
subtype: PointerType, subtype: PointerType,
pressed: HashSet<u64>, pressed: HashSet<u64>,
pointer_id: u32,
x: f64, x: f64,
y: f64, y: f64,
} }
impl PointerInputState { 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 { PointerInputState {
subtype: match subtype { subtype: *subtype,
PointerType::Mouse => PointerType::Mouse,
PointerType::Pen => PointerType::Pen,
PointerType::Touch => PointerType::Touch,
},
pressed: HashSet::new(), pressed: HashSet::new(),
pointer_id: PointerInputState::get_pointer_id(
subtype,
next_pointer_id_below_2,
next_pointer_id_above_2,
),
x: 0.0, x: 0.0,
y: 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 // https://w3c.github.io/webdriver/#dfn-computing-the-tick-duration
@ -244,14 +268,18 @@ impl Handler {
self.dispatch_general_action(source_id); self.dispatch_general_action(source_id);
}, },
PointerActionItem::Pointer(action) => { 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() self.session()
.unwrap() .unwrap()
.input_state_table .input_state_table
.borrow_mut() .borrow_mut()
.entry(source_id.to_string()) .entry(source_id.to_string())
.or_insert(InputSourceState::Pointer(PointerInputState::new( .or_insert(new_input_source);
&parameters.pointer_type,
)));
match action { match action {
PointerAction::Cancel => (), PointerAction::Cancel => (),
PointerAction::Down(action) => { PointerAction::Down(action) => {

View file

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