Improve naming in iced_selector crate

This commit is contained in:
Héctor Ramón Jiménez 2025-09-17 22:56:58 +02:00
parent 59e2687146
commit 299eb54d6f
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
12 changed files with 235 additions and 219 deletions

View file

@ -8,9 +8,9 @@ static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
pub struct Id(Internal);
impl Id {
/// Creates a custom [`Id`].
pub fn new(id: impl Into<borrow::Cow<'static, str>>) -> Self {
Self(Internal::Custom(id.into()))
/// Creates a new [`Id`] from a static `str`.
pub const fn new(id: &'static str) -> Self {
Self(Internal::Custom(borrow::Cow::Borrowed(id)))
}
/// Creates a unique [`Id`].
@ -31,7 +31,7 @@ impl From<&'static str> for Id {
impl From<String> for Id {
fn from(value: String) -> Self {
Self::new(value)
Self(Internal::Custom(borrow::Cow::Owned(value)))
}
}

View file

@ -307,7 +307,7 @@ pub enum TaskMessage {
impl Task {
fn text_input_id(i: usize) -> widget::Id {
widget::Id::new(format!("task-{i}"))
widget::Id::from(format!("task-{i}"))
}
fn new(description: String) -> Self {

View file

@ -1,7 +1,7 @@
use iced::event::{self, Event};
use iced::mouse;
use iced::widget::{
column, container, row, scrollable, selector, space_x, space_y, text,
self, column, container, row, scrollable, selector, space_x, space_y, text,
};
use iced::window;
use iced::{
@ -28,8 +28,8 @@ enum Message {
MouseMoved(Point),
WindowResized,
Scrolled,
OuterFound(Option<selector::Match>),
InnerFound(Option<selector::Match>),
OuterFound(Option<Rectangle>),
InnerFound(Option<Rectangle>),
}
impl Example {
@ -41,18 +41,18 @@ impl Example {
Task::none()
}
Message::Scrolled | Message::WindowResized => Task::batch(vec![
selector::find_by_id(OUTER_CONTAINER).map(Message::OuterFound),
selector::find_by_id(INNER_CONTAINER).map(Message::InnerFound),
selector::visible_bounds(OUTER_CONTAINER)
.map(Message::OuterFound),
selector::visible_bounds(INNER_CONTAINER)
.map(Message::InnerFound),
]),
Message::OuterFound(outer) => {
self.outer_bounds =
outer.as_ref().and_then(selector::Bounded::visible_bounds);
self.outer_bounds = outer;
Task::none()
}
Message::InnerFound(inner) => {
self.inner_bounds =
inner.as_ref().and_then(selector::Bounded::visible_bounds);
self.inner_bounds = inner;
Task::none()
}
@ -157,5 +157,5 @@ impl Example {
}
}
const OUTER_CONTAINER: &str = "outer";
const INNER_CONTAINER: &str = "inner";
const OUTER_CONTAINER: widget::Id = widget::Id::new("outer");
const INNER_CONTAINER: widget::Id = widget::Id::new("inner");

View file

@ -1,7 +1,5 @@
//! Find and query widgets in your applications.
pub use iced_selector::Selector;
pub use iced_selector::target::{Bounded, Match, Target, Text};
pub use iced_selector::{Bounded, Candidate, Selector, Target, Text};
use crate::core::Rectangle;
@ -10,7 +8,7 @@ use crate::core::widget;
use crate::task;
/// Finds a widget by the given [`widget::Id`].
pub fn find_by_id(id: impl Into<widget::Id>) -> Task<Option<Match>> {
pub fn find_by_id(id: impl Into<widget::Id>) -> Task<Option<Target>> {
task::widget(id.into().find())
}

View file

@ -1,9 +1,10 @@
use crate::Selector;
use crate::core::widget::operation::{
Focusable, Outcome, Scrollable, TextInput,
};
use crate::core::widget::{Id, Operation};
use crate::core::{Rectangle, Vector};
use crate::{Selector, Target};
use crate::target::Candidate;
use std::any::Any;
@ -38,7 +39,7 @@ where
{
type Output = Option<S::Output>;
fn feed(&mut self, target: Target<'_>) {
fn feed(&mut self, target: Candidate<'_>) {
if let Some(output) = self.selector.select(target) {
self.output = Some(output);
}
@ -81,7 +82,7 @@ where
{
type Output = Vec<S::Output>;
fn feed(&mut self, target: Target<'_>) {
fn feed(&mut self, target: Candidate<'_>) {
if let Some(output) = self.selector.select(target) {
self.outputs.push(output);
}
@ -99,7 +100,7 @@ where
pub trait Strategy {
type Output;
fn feed(&mut self, target: Target<'_>);
fn feed(&mut self, target: Candidate<'_>);
fn is_done(&self) -> bool;
@ -152,7 +153,7 @@ where
return;
}
self.strategy.feed(Target::Container {
self.strategy.feed(Candidate::Container {
id,
bounds,
visible_bounds: self
@ -171,7 +172,7 @@ where
return;
}
self.strategy.feed(Target::Focusable {
self.strategy.feed(Candidate::Focusable {
id,
bounds,
visible_bounds: self
@ -196,7 +197,7 @@ where
let visible_bounds =
self.viewport.intersection(&(bounds + self.translation));
self.strategy.feed(Target::Scrollable {
self.strategy.feed(Candidate::Scrollable {
id,
bounds,
visible_bounds,
@ -219,7 +220,7 @@ where
return;
}
self.strategy.feed(Target::TextInput {
self.strategy.feed(Candidate::TextInput {
id,
bounds,
visible_bounds: self
@ -234,7 +235,7 @@ where
return;
}
self.strategy.feed(Target::Text {
self.strategy.feed(Candidate::Text {
id,
bounds,
visible_bounds: self
@ -254,7 +255,7 @@ where
return;
}
self.strategy.feed(Target::Custom {
self.strategy.feed(Candidate::Custom {
id,
bounds,
visible_bounds: self

View file

@ -1,12 +1,11 @@
#![allow(missing_docs)]
use iced_core as core;
pub mod target;
mod find;
mod target;
pub use find::{Find, FindAll};
pub use target::Target;
pub use target::{Bounded, Candidate, Target, Text};
use crate::core::Point;
use crate::core::widget;
@ -14,7 +13,7 @@ use crate::core::widget;
pub trait Selector {
type Output;
fn select(&mut self, target: Target<'_>) -> Option<Self::Output>;
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output>;
fn description(&self) -> String;
@ -36,9 +35,9 @@ pub trait Selector {
impl Selector for &str {
type Output = target::Text;
fn select(&mut self, target: Target<'_>) -> Option<Self::Output> {
match target {
Target::TextInput {
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output> {
match candidate {
Candidate::TextInput {
id,
bounds,
visible_bounds,
@ -48,7 +47,7 @@ impl Selector for &str {
bounds,
visible_bounds,
}),
Target::Text {
Candidate::Text {
id,
bounds,
visible_bounds,
@ -70,8 +69,8 @@ impl Selector for &str {
impl Selector for String {
type Output = target::Text;
fn select(&mut self, target: Target<'_>) -> Option<Self::Output> {
self.as_str().select(target)
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output> {
self.as_str().select(candidate)
}
fn description(&self) -> String {
@ -80,14 +79,14 @@ impl Selector for String {
}
impl Selector for widget::Id {
type Output = target::Match;
type Output = Target;
fn select(&mut self, target: Target<'_>) -> Option<Self::Output> {
if target.id() != Some(self) {
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output> {
if candidate.id() != Some(self) {
return None;
}
Some(target::Match::from_target(target))
Some(Target::from(candidate))
}
fn description(&self) -> String {
@ -96,13 +95,13 @@ impl Selector for widget::Id {
}
impl Selector for Point {
type Output = target::Match;
type Output = Target;
fn select(&mut self, target: Target<'_>) -> Option<Self::Output> {
target
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output> {
candidate
.visible_bounds()
.is_some_and(|visible_bounds| visible_bounds.contains(*self))
.then(|| target::Match::from_target(target))
.then(|| Target::from(candidate))
}
fn description(&self) -> String {
@ -112,12 +111,12 @@ impl Selector for Point {
impl<F, T> Selector for F
where
F: FnMut(Target<'_>) -> Option<T>,
F: FnMut(Candidate<'_>) -> Option<T>,
{
type Output = T;
fn select(&mut self, target: Target<'_>) -> Option<Self::Output> {
(self)(target)
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output> {
(self)(candidate)
}
fn description(&self) -> String {
@ -126,6 +125,6 @@ where
}
/// Creates a new [`Selector`] that matches widgets with the given [`widget::Id`].
pub fn id(id: impl Into<widget::Id>) -> impl Selector<Output = target::Match> {
pub fn id(id: impl Into<widget::Id>) -> impl Selector<Output = Target> {
id.into()
}

View file

@ -4,8 +4,152 @@ use crate::core::{Rectangle, Vector};
use std::any::Any;
#[derive(Debug, Clone, PartialEq)]
pub enum Target {
Container {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
},
Focusable {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
},
Scrollable {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
content_bounds: Rectangle,
translation: Vector,
},
TextInput {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
content: String,
},
Text {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
content: String,
},
Custom {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
},
}
impl Target {
pub fn bounds(&self) -> Rectangle {
match self {
Target::Container { bounds, .. }
| Target::Focusable { bounds, .. }
| Target::Scrollable { bounds, .. }
| Target::TextInput { bounds, .. }
| Target::Text { bounds, .. }
| Target::Custom { bounds, .. } => *bounds,
}
}
pub fn visible_bounds(&self) -> Option<Rectangle> {
match self {
Target::Container { visible_bounds, .. }
| Target::Focusable { visible_bounds, .. }
| Target::Scrollable { visible_bounds, .. }
| Target::TextInput { visible_bounds, .. }
| Target::Text { visible_bounds, .. }
| Target::Custom { visible_bounds, .. } => *visible_bounds,
}
}
}
impl From<Candidate<'_>> for Target {
fn from(candidate: Candidate<'_>) -> Self {
match candidate {
Candidate::Container {
id,
bounds,
visible_bounds,
} => Self::Container {
id: id.cloned(),
bounds,
visible_bounds,
},
Candidate::Focusable {
id,
bounds,
visible_bounds,
..
} => Self::Focusable {
id: id.cloned(),
bounds,
visible_bounds,
},
Candidate::Scrollable {
id,
bounds,
visible_bounds,
content_bounds,
translation,
..
} => Self::Scrollable {
id: id.cloned(),
bounds,
visible_bounds,
content_bounds,
translation,
},
Candidate::TextInput {
id,
bounds,
visible_bounds,
state,
} => Self::TextInput {
id: id.cloned(),
bounds,
visible_bounds,
content: state.text().to_owned(),
},
Candidate::Text {
id,
bounds,
visible_bounds,
content,
} => Self::Text {
id: id.cloned(),
bounds,
visible_bounds,
content: content.to_owned(),
},
Candidate::Custom {
id,
bounds,
visible_bounds,
..
} => Self::Custom {
id: id.cloned(),
bounds,
visible_bounds,
},
}
}
}
impl Bounded for Target {
fn bounds(&self) -> Rectangle {
self.bounds()
}
fn visible_bounds(&self) -> Option<Rectangle> {
self.visible_bounds()
}
}
#[derive(Clone)]
pub enum Target<'a> {
pub enum Candidate<'a> {
Container {
id: Option<&'a Id>,
bounds: Rectangle,
@ -45,171 +189,37 @@ pub enum Target<'a> {
},
}
impl<'a> Target<'a> {
impl<'a> Candidate<'a> {
pub fn id(&self) -> Option<&'a Id> {
match self {
Target::Container { id, .. }
| Target::Focusable { id, .. }
| Target::Scrollable { id, .. }
| Target::TextInput { id, .. }
| Target::Text { id, .. }
| Target::Custom { id, .. } => *id,
Candidate::Container { id, .. }
| Candidate::Focusable { id, .. }
| Candidate::Scrollable { id, .. }
| Candidate::TextInput { id, .. }
| Candidate::Text { id, .. }
| Candidate::Custom { id, .. } => *id,
}
}
pub fn bounds(&self) -> Rectangle {
match self {
Target::Container { bounds, .. }
| Target::Focusable { bounds, .. }
| Target::Scrollable { bounds, .. }
| Target::TextInput { bounds, .. }
| Target::Text { bounds, .. }
| Target::Custom { bounds, .. } => *bounds,
Candidate::Container { bounds, .. }
| Candidate::Focusable { bounds, .. }
| Candidate::Scrollable { bounds, .. }
| Candidate::TextInput { bounds, .. }
| Candidate::Text { bounds, .. }
| Candidate::Custom { bounds, .. } => *bounds,
}
}
pub fn visible_bounds(&self) -> Option<Rectangle> {
match self {
Target::Container { visible_bounds, .. }
| Target::Focusable { visible_bounds, .. }
| Target::Scrollable { visible_bounds, .. }
| Target::TextInput { visible_bounds, .. }
| Target::Text { visible_bounds, .. }
| Target::Custom { visible_bounds, .. } => *visible_bounds,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Match {
Container {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
},
Focusable {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
},
Scrollable {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
content_bounds: Rectangle,
translation: Vector,
},
TextInput {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
content: String,
},
Text {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
content: String,
},
Custom {
id: Option<Id>,
bounds: Rectangle,
visible_bounds: Option<Rectangle>,
},
}
impl Match {
pub fn from_target(target: Target<'_>) -> Self {
match target {
Target::Container {
id,
bounds,
visible_bounds,
} => Self::Container {
id: id.cloned(),
bounds,
visible_bounds,
},
Target::Focusable {
id,
bounds,
visible_bounds,
..
} => Self::Focusable {
id: id.cloned(),
bounds,
visible_bounds,
},
Target::Scrollable {
id,
bounds,
visible_bounds,
content_bounds,
translation,
..
} => Self::Scrollable {
id: id.cloned(),
bounds,
visible_bounds,
content_bounds,
translation,
},
Target::TextInput {
id,
bounds,
visible_bounds,
state,
} => Self::TextInput {
id: id.cloned(),
bounds,
visible_bounds,
content: state.text().to_owned(),
},
Target::Text {
id,
bounds,
visible_bounds,
content,
} => Self::Text {
id: id.cloned(),
bounds,
visible_bounds,
content: content.to_owned(),
},
Target::Custom {
id,
bounds,
visible_bounds,
..
} => Self::Custom {
id: id.cloned(),
bounds,
visible_bounds,
},
}
}
}
impl Bounded for Match {
fn bounds(&self) -> Rectangle {
match self {
Match::Container { bounds, .. }
| Match::Focusable { bounds, .. }
| Match::Scrollable { bounds, .. }
| Match::TextInput { bounds, .. }
| Match::Text { bounds, .. }
| Match::Custom { bounds, .. } => *bounds,
}
}
fn visible_bounds(&self) -> Option<Rectangle> {
match self {
Match::Container { visible_bounds, .. }
| Match::Focusable { visible_bounds, .. }
| Match::Scrollable { visible_bounds, .. }
| Match::TextInput { visible_bounds, .. }
| Match::Text { visible_bounds, .. }
| Match::Custom { visible_bounds, .. } => *visible_bounds,
Candidate::Container { visible_bounds, .. }
| Candidate::Focusable { visible_bounds, .. }
| Candidate::Scrollable { visible_bounds, .. }
| Candidate::TextInput { visible_bounds, .. }
| Candidate::Text { visible_bounds, .. }
| Candidate::Custom { visible_bounds, .. } => *visible_bounds,
}
}
}
@ -234,17 +244,27 @@ pub enum Text {
},
}
impl Bounded for Text {
fn bounds(&self) -> Rectangle {
impl Text {
pub fn bounds(&self) -> Rectangle {
match self {
Text::Raw { bounds, .. } | Text::Input { bounds, .. } => *bounds,
}
}
fn visible_bounds(&self) -> Option<Rectangle> {
pub fn visible_bounds(&self) -> Option<Rectangle> {
match self {
Text::Raw { visible_bounds, .. }
| Text::Input { visible_bounds, .. } => *visible_bounds,
}
}
}
impl Bounded for Text {
fn bounds(&self) -> Rectangle {
self.bounds()
}
fn visible_bounds(&self) -> Option<Rectangle> {
self.visible_bounds()
}
}

View file

@ -17,7 +17,6 @@ use crate::runtime::task;
use crate::runtime::user_interface;
use crate::runtime::window;
use crate::runtime::{Task, UserInterface};
use crate::selector;
use crate::{Instruction, Selector};
use std::fmt;
@ -25,8 +24,8 @@ use std::fmt;
/// A headless runtime that can run iced applications and execute
/// [instructions](crate::Instruction).
///
/// An [`Emulator`] runs its program as close as possible to the real thing.
/// It will run subscriptions and tasks in the [`Executor`](Program::Executor) of
/// An [`Emulator`] runs its program as faithfully as possible to the real thing.
/// It will run subscriptions and tasks with the [`Executor`](Program::Executor) of
/// the [`Program`].
///
/// If you want to run a simulation without side effects, use a [`Simulator`](crate::Simulator)
@ -289,7 +288,6 @@ impl<P: Program + 'static> Emulator<P> {
let Some(events) = interaction.events(|target| match target {
instruction::Target::Point(position) => Some(*position),
instruction::Target::Text(text) => {
use selector::target::Bounded;
use widget::Operation;
let mut operation = Selector::find(text.as_str());

View file

@ -354,7 +354,7 @@ impl fmt::Display for Target {
pub enum Keyboard {
/// A key was pressed.
Press(Key),
/// A key was release.
/// A key was released.
Release(Key),
/// A key was "typed" (press and released).
Type(Key),

View file

@ -112,7 +112,7 @@ use std::path::Path;
/// an [`Emulator`] of the given [`Program`](program::Program).
///
/// Remember that an [`Emulator`] executes the real thing! Side effects _will_
/// be performed. It is up to you to ensure your tests have reproducible environments
/// take place. It is up to you to ensure your tests have reproducible environments
/// by leveraging [`Preset`][program::Preset].
pub fn run(
program: impl program::Program + 'static,

View file

@ -12,7 +12,7 @@ use crate::core::{Element, Event, Font, Point, Settings, Size, SmolStr};
use crate::renderer;
use crate::runtime::UserInterface;
use crate::runtime::user_interface;
use crate::selector::target::Bounded;
use crate::selector::Bounded;
use crate::{Error, Selector};
use std::borrow::Cow;

View file

@ -11,7 +11,7 @@ use crate::core::{
};
use crate::test::Selector;
use crate::test::instruction::{Interaction, Mouse, Target};
use crate::test::selector::target;
use crate::test::selector;
pub fn recorder<'a, Message, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
@ -457,12 +457,12 @@ fn find_text(
let (content, visible_bounds) =
targets.into_iter().rev().find_map(|target| {
if let target::Match::Text {
if let selector::Target::Text {
content,
visible_bounds,
..
}
| target::Match::TextInput {
| selector::Target::TextInput {
content,
visible_bounds,
..