Implement is_focused selector and unify selector API
This commit is contained in:
parent
645643bfd6
commit
1ae3b5e96a
3 changed files with 48 additions and 24 deletions
|
|
@ -28,8 +28,8 @@ enum Message {
|
||||||
MouseMoved(Point),
|
MouseMoved(Point),
|
||||||
WindowResized,
|
WindowResized,
|
||||||
Scrolled,
|
Scrolled,
|
||||||
OuterFound(Option<Rectangle>),
|
OuterFound(Option<selector::Target>),
|
||||||
InnerFound(Option<Rectangle>),
|
InnerFound(Option<selector::Target>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Example {
|
impl Example {
|
||||||
|
|
@ -41,16 +41,18 @@ impl Example {
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
Message::Scrolled | Message::WindowResized => Task::batch(vec![
|
Message::Scrolled | Message::WindowResized => Task::batch(vec![
|
||||||
selector::delineate(OUTER_CONTAINER).map(Message::OuterFound),
|
selector::find(OUTER_CONTAINER).map(Message::OuterFound),
|
||||||
selector::delineate(INNER_CONTAINER).map(Message::InnerFound),
|
selector::find(INNER_CONTAINER).map(Message::InnerFound),
|
||||||
]),
|
]),
|
||||||
Message::OuterFound(outer) => {
|
Message::OuterFound(outer) => {
|
||||||
self.outer_bounds = outer;
|
self.outer_bounds =
|
||||||
|
outer.as_ref().and_then(selector::Target::visible_bounds);
|
||||||
|
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
Message::InnerFound(inner) => {
|
Message::InnerFound(inner) => {
|
||||||
self.inner_bounds = inner;
|
self.inner_bounds =
|
||||||
|
inner.as_ref().and_then(selector::Target::visible_bounds);
|
||||||
|
|
||||||
Task::none()
|
Task::none()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,25 @@
|
||||||
//! Find and query widgets in your applications.
|
//! Find and query widgets in your applications.
|
||||||
pub use iced_selector::{Bounded, Candidate, Selector, Target, Text};
|
pub use iced_selector::{
|
||||||
|
Bounded, Candidate, Selector, Target, Text, id, is_focused,
|
||||||
use crate::core::Rectangle;
|
};
|
||||||
|
|
||||||
use crate::Task;
|
use crate::Task;
|
||||||
use crate::core::widget;
|
|
||||||
use crate::task;
|
use crate::task;
|
||||||
|
|
||||||
/// Finds a widget by the given [`widget::Id`].
|
/// Finds a widget matching the given [`Selector`].
|
||||||
pub fn find_by_id(id: impl Into<widget::Id>) -> Task<Option<Target>> {
|
pub fn find<S>(selector: S) -> Task<Option<S::Output>>
|
||||||
task::widget(id.into().find())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Finds a widget that contains the given text.
|
|
||||||
pub fn find_by_text(text: impl Into<String>) -> Task<Option<Text>> {
|
|
||||||
task::widget(Selector::find(text.into()))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Finds the visible bounds of the first [`Selector`] target.
|
|
||||||
pub fn delineate<S>(selector: S) -> Task<Option<Rectangle>>
|
|
||||||
where
|
where
|
||||||
S: Selector + Send + 'static,
|
S: Selector + Send + 'static,
|
||||||
S::Output: Bounded + Clone + Send + 'static,
|
S::Output: Send + Clone + 'static,
|
||||||
{
|
{
|
||||||
task::widget(selector.find())
|
task::widget(selector.find())
|
||||||
.map(|target| target.as_ref().and_then(Bounded::visible_bounds))
|
}
|
||||||
|
|
||||||
|
/// Finds all widgets matching the given [`Selector`].
|
||||||
|
pub fn find_all<S>(selector: S) -> Task<Vec<S::Output>>
|
||||||
|
where
|
||||||
|
S: Selector + Send + 'static,
|
||||||
|
S::Output: Send + Clone + 'static,
|
||||||
|
{
|
||||||
|
task::widget(selector.find_all())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -146,3 +146,28 @@ where
|
||||||
pub fn id(id: impl Into<widget::Id>) -> impl Selector<Output = Target> {
|
pub fn id(id: impl Into<widget::Id>) -> impl Selector<Output = Target> {
|
||||||
id.into()
|
id.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a [`Selector`] that matches widgets that are currently focused.
|
||||||
|
pub fn is_focused() -> impl Selector<Output = Target> {
|
||||||
|
struct IsFocused;
|
||||||
|
|
||||||
|
impl Selector for IsFocused {
|
||||||
|
type Output = Target;
|
||||||
|
|
||||||
|
fn select(&mut self, candidate: Candidate<'_>) -> Option<Self::Output> {
|
||||||
|
if let Candidate::Focusable { state, .. } = candidate
|
||||||
|
&& state.is_focused()
|
||||||
|
{
|
||||||
|
Some(Target::from(candidate))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn description(&self) -> String {
|
||||||
|
"is focused".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
IsFocused
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue