From 7b3fd7d347deaa2ec637d53b384d998b20928eaa Mon Sep 17 00:00:00 2001 From: Euclid Ye Date: Mon, 19 May 2025 18:12:33 +0800 Subject: [PATCH] Implement PointerInputState::pointer_id Signed-off-by: Euclid Ye --- components/webdriver_server/actions.rs | 50 ++++++++++++++++++++------ components/webdriver_server/lib.rs | 22 +++++++----- 2 files changed, 53 insertions(+), 19 deletions(-) diff --git a/components/webdriver_server/actions.rs b/components/webdriver_server/actions.rs index cde418f920c..da83de7c1b8 100644 --- a/components/webdriver_server/actions.rs +++ b/components/webdriver_server/actions.rs @@ -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, + 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, + next_pointer_id_above_2: &Cell, + ) -> 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, + next_pointer_id_above_2: &Cell, + ) -> 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( + ¶meters.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( - ¶meters.pointer_type, - ))); + .or_insert(new_input_source); match action { PointerAction::Cancel => (), PointerAction::Down(action) => { diff --git a/components/webdriver_server/lib.rs b/components/webdriver_server/lib.rs index 0e3fa9058d6..211205e0c59 100644 --- a/components/webdriver_server/lib.rs +++ b/components/webdriver_server/lib.rs @@ -227,6 +227,8 @@ struct Handler { current_action_id: Cell>, resize_timeout: u32, + next_pointer_id_below_2: Cell, + next_pointer_id_above_2: Cell, } #[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)