Skip to content

Commit 7030d64

Browse files
committed
Create KeyItem struct for send_keys
Signed-off-by: PotatoCP <[email protected]>
1 parent 7634834 commit 7030d64

File tree

1 file changed

+75
-30
lines changed

1 file changed

+75
-30
lines changed

src/webdriver.rs

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -373,46 +373,29 @@ impl KeyInputState {
373373
is_composing: false,
374374
})
375375
}
376+
}
376377

377-
fn clear(&mut self, undo_actions: &mut HashSet<char>, result: &mut Vec<Event>) {
378-
let mut actions: Vec<_> = undo_actions.drain().collect();
379-
actions.sort_unstable();
380-
for action in actions {
381-
result.push(self.dispatch_keyup(action).unwrap().into());
382-
}
383-
assert!(undo_actions.is_empty());
384-
}
385-
386-
fn dispatch_typeable(&mut self, text: &mut String, result: &mut Vec<Event>) {
387-
for character in text.chars() {
388-
let shifted = self.modifiers.contains(Modifiers::SHIFT);
389-
if is_shifted_character(character) && !shifted {
390-
// dispatch left shift down
391-
result.push(self.dispatch_keydown('\u{E008}').into());
392-
}
393-
if !is_shifted_character(character) && shifted {
394-
// dispatch left shift up
395-
result.push(self.dispatch_keyup('\u{E008}').unwrap().into());
396-
}
397-
result.push(self.dispatch_keydown(character).into());
398-
result.push(self.dispatch_keyup(character).unwrap().into());
399-
}
400-
text.clear();
401-
}
378+
/// Give user the key state and char of the key event.
379+
/// User must then send the key event through `dispatch_actions`
380+
#[derive(Clone, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)]
381+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
382+
pub enum KeyItem {
383+
state: KeyState,
384+
raw_char: char,
402385
}
403386

404-
/// Either a [`KeyboardEvent`] or a [`CompositionEvent`].
387+
/// Either a [`KeyItem`] or a [`CompositionEvent`].
405388
///
406389
/// Returned by the [`send_keys`] function.
407390
#[derive(Clone, Eq, PartialEq, Hash, Debug, PartialOrd, Ord)]
408391
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
409392
pub enum Event {
410-
Keyboard(KeyboardEvent),
393+
Keyboard(KeyItem),
411394
Composition(CompositionEvent),
412395
}
413396

414-
impl From<KeyboardEvent> for Event {
415-
fn from(v: KeyboardEvent) -> Event {
397+
impl From<KeyItem> for Event {
398+
fn from(v: KeyItem) -> Event {
416399
Event::Keyboard(v)
417400
}
418401
}
@@ -423,6 +406,62 @@ impl From<CompositionEvent> for Event {
423406
}
424407
}
425408

409+
fn clear(undo_actions: &mut HashSet<char>, result: &mut Vec<Event>) {
410+
let mut actions: Vec<_> = undo_actions.drain().collect();
411+
actions.sort_unstable();
412+
for action in actions {
413+
result.push(
414+
KeyItem {
415+
state: KeyState::Up,
416+
raw_char: action,
417+
}
418+
.into()
419+
);
420+
}
421+
assert!(undo_actions.is_empty());
422+
}
423+
424+
fn dispatch_typeable(text: &mut String, result: &mut Vec<Event>) {
425+
for character in text.chars() {
426+
let shifted = self.modifiers.contains(Modifiers::SHIFT);
427+
if is_shifted_character(character) && !shifted {
428+
// dispatch left shift down
429+
result.push(
430+
KeyItem {
431+
state: KeyState::Down,
432+
raw_char: '\u{E008}',
433+
}
434+
.into()
435+
);
436+
}
437+
if !is_shifted_character(character) && shifted {
438+
// dispatch left shift up
439+
result.push(
440+
KeyItem {
441+
state: KeyState::Up,
442+
raw_char: '\u{E008}',
443+
}
444+
.into()
445+
);
446+
}
447+
result.push(
448+
KeyItem {
449+
state: KeyState::Down,
450+
raw_char: character,
451+
}
452+
.into()
453+
);
454+
result.push(
455+
KeyItem {
456+
state: KeyState::Up,
457+
raw_char: character,
458+
}
459+
.into()
460+
);
461+
}
462+
text.clear();
463+
}
464+
426465
/// Compute the events resulting from a WebDriver *Element Send Keys* command.
427466
///
428467
/// Spec: <https://w3c.github.io/webdriver/#element-send-keys>
@@ -472,7 +511,13 @@ pub fn send_keys(text: &str) -> Vec<Event> {
472511
s if is_modifier(s) => {
473512
state.dispatch_typeable(&mut typeable_text, &mut result);
474513
let raw_modifier = first_char(s);
475-
result.push(state.dispatch_keydown(raw_modifier).into());
514+
result.push(
515+
KeyItem {
516+
state: KeyState::Down,
517+
raw_char: raw_modifier,
518+
}
519+
.into()
520+
);
476521
undo_actions.insert(raw_modifier);
477522
}
478523
s if is_typeable(s) => typeable_text.push_str(s),

0 commit comments

Comments
 (0)