use std::num::NonZeroU16;
use iced_core::mouse;
use icedwright_check::ClickCount;
use icedwright_locator::TextMatch;
use icedwright_types::{Logical, ModifierSet, Point, VirtualDuration};
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub struct Click {
pub(crate) button: mouse::Button,
pub(crate) count: ClickCount,
pub(crate) position: Option<Logical<Point>>,
pub(crate) modifiers: ModifierSet,
pub(crate) force: bool,
pub(crate) trial: bool,
pub(crate) no_wait_after: bool,
pub(crate) timeout: Option<VirtualDuration>,
pub(crate) steps: NonZeroU16,
}
impl Default for Click {
fn default() -> Self {
Self {
button: mouse::Button::Left,
count: ClickCount::Single,
position: None,
modifiers: ModifierSet::NONE,
force: false,
trial: false,
no_wait_after: false,
timeout: None,
steps: NonZeroU16::MIN,
}
}
}
impl Click {
pub fn new() -> Self {
Self::default()
}
pub fn button(mut self, button: mouse::Button) -> Self {
self.button = button;
self
}
pub fn count(mut self, count: ClickCount) -> Self {
self.count = count;
self
}
pub fn position(mut self, at: impl Into<Logical<Point>>) -> Self {
self.position = Some(at.into());
self
}
pub fn modifiers(mut self, modifiers: ModifierSet) -> Self {
self.modifiers = modifiers;
self
}
pub fn force(mut self) -> Self {
self.force = true;
self
}
pub fn trial(mut self) -> Self {
self.trial = true;
self
}
pub fn no_wait_after(mut self) -> Self {
self.no_wait_after = true;
self
}
pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
self.timeout = Some(timeout.into());
self
}
pub fn steps(mut self, steps: NonZeroU16) -> Self {
self.steps = steps;
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub struct Hover {
pub(crate) position: Option<Logical<Point>>,
pub(crate) force: bool,
pub(crate) trial: bool,
pub(crate) timeout: Option<VirtualDuration>,
pub(crate) steps: NonZeroU16,
}
impl Default for Hover {
fn default() -> Self {
Self {
position: None,
force: false,
trial: false,
timeout: None,
steps: NonZeroU16::MIN,
}
}
}
impl Hover {
pub fn new() -> Self {
Self::default()
}
pub fn position(mut self, at: impl Into<Logical<Point>>) -> Self {
self.position = Some(at.into());
self
}
pub fn force(mut self) -> Self {
self.force = true;
self
}
pub fn trial(mut self) -> Self {
self.trial = true;
self
}
pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
self.timeout = Some(timeout.into());
self
}
pub fn steps(mut self, steps: NonZeroU16) -> Self {
self.steps = steps;
self
}
}
const DRAG_STEPS: NonZeroU16 = match NonZeroU16::new(5) {
Some(steps) => steps,
None => NonZeroU16::MIN,
};
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub struct Drag {
pub(crate) source: Option<Logical<Point>>,
pub(crate) target: Option<Logical<Point>>,
pub(crate) steps: NonZeroU16,
pub(crate) modifiers: ModifierSet,
pub(crate) force: bool,
pub(crate) timeout: Option<VirtualDuration>,
}
impl Default for Drag {
fn default() -> Self {
Self {
source: None,
target: None,
steps: DRAG_STEPS,
modifiers: ModifierSet::NONE,
force: false,
timeout: None,
}
}
}
impl Drag {
pub fn new() -> Self {
Self::default()
}
pub fn source_position(mut self, at: impl Into<Logical<Point>>) -> Self {
self.source = Some(at.into());
self
}
pub fn target_position(mut self, at: impl Into<Logical<Point>>) -> Self {
self.target = Some(at.into());
self
}
pub fn steps(mut self, steps: NonZeroU16) -> Self {
self.steps = steps;
self
}
pub fn modifiers(mut self, modifiers: ModifierSet) -> Self {
self.modifiers = modifiers;
self
}
pub fn force(mut self) -> Self {
self.force = true;
self
}
pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
self.timeout = Some(timeout.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Default)]
#[must_use]
pub struct Type {
pub(crate) delay: VirtualDuration,
pub(crate) layout: Option<icedwright_check::KeyboardLayout>,
pub(crate) timeout: Option<VirtualDuration>,
}
impl Type {
pub fn new() -> Self {
Self::default()
}
pub fn delay(mut self, delay: impl Into<VirtualDuration>) -> Self {
self.delay = delay.into();
self
}
pub fn layout(mut self, layout: icedwright_check::KeyboardLayout) -> Self {
self.layout = Some(layout);
self
}
pub fn timeout(mut self, timeout: impl Into<VirtualDuration>) -> Self {
self.timeout = Some(timeout.into());
self
}
}
#[derive(Debug, Clone, PartialEq)]
#[must_use]
pub enum OptionMatch {
Name(TextMatch),
Index(usize),
Value(String),
}
impl OptionMatch {
pub fn index(index: usize) -> Self {
Self::Index(index)
}
pub fn value(value: impl Into<String>) -> Self {
Self::Value(value.into())
}
#[must_use]
pub(crate) fn picks(
&self,
index: usize,
name: Option<&str>,
text: &str,
value: Option<&str>,
) -> bool {
match self {
OptionMatch::Name(wanted) => name.map_or_else(
|| wanted.matches(text),
|n| wanted.matches(n) || wanted.matches(text),
),
OptionMatch::Index(wanted) => *wanted == index,
OptionMatch::Value(wanted) => value == Some(wanted.as_str()),
}
}
}
impl From<&str> for OptionMatch {
fn from(name: &str) -> Self {
Self::Name(name.into())
}
}
impl From<String> for OptionMatch {
fn from(name: String) -> Self {
Self::Name(name.into())
}
}
impl From<TextMatch> for OptionMatch {
fn from(name: TextMatch) -> Self {
Self::Name(name)
}
}
impl std::fmt::Display for OptionMatch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OptionMatch::Name(name) => write!(f, "the option named {name}"),
OptionMatch::Index(index) => write!(f, "option {index}"),
OptionMatch::Value(value) => write!(f, "the option with value {value:?}"),
}
}
}