use iced_core::widget::operation::scrollable::AbsoluteOffset;
use iced_core::widget::operation::{Focusable, Outcome, Scrollable, TextInput};
use iced_core::widget::{Id, Operation};
use iced_core::{Rectangle, Vector};
#[derive(Debug)]
pub(crate) struct FocusOrdinal {
target: u32,
seen: u32,
found: bool,
}
impl FocusOrdinal {
pub(crate) fn new(target: u32) -> Self {
Self {
target,
seen: 0,
found: false,
}
}
}
impl Operation<bool> for FocusOrdinal {
fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<bool>)) {
operate(self);
}
fn focusable(&mut self, _id: Option<&Id>, _bounds: Rectangle, state: &mut dyn Focusable) {
if self.seen == self.target {
self.found = true;
if !state.is_focused() {
state.focus();
}
} else if state.is_focused() {
state.unfocus();
}
self.seen += 1;
}
fn finish(&self) -> Outcome<bool> {
Outcome::Some(self.found)
}
}
#[derive(Debug)]
pub(crate) struct SelectAllOrdinal {
target: u32,
seen: u32,
selected: bool,
}
impl SelectAllOrdinal {
pub(crate) fn new(target: u32) -> Self {
Self {
target,
seen: 0,
selected: false,
}
}
}
impl Operation<bool> for SelectAllOrdinal {
fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<bool>)) {
operate(self);
}
fn text_input(&mut self, _id: Option<&Id>, _bounds: Rectangle, state: &mut dyn TextInput) {
if self.seen == self.target {
state.select_all();
self.selected = true;
}
}
fn focusable(&mut self, _id: Option<&Id>, _bounds: Rectangle, _state: &mut dyn Focusable) {
self.seen += 1;
}
fn finish(&self) -> Outcome<bool> {
Outcome::Some(self.selected)
}
}
#[derive(Debug)]
pub(crate) struct ScrollOrdinal {
target: u32,
seen: u32,
offset: Option<Vector>,
translation: Option<Vector>,
}
impl ScrollOrdinal {
pub(crate) fn to(target: u32, offset: Vector) -> Self {
Self {
target,
seen: 0,
offset: Some(offset),
translation: None,
}
}
pub(crate) fn read(target: u32) -> Self {
Self {
target,
seen: 0,
offset: None,
translation: None,
}
}
}
impl Operation<Option<Vector>> for ScrollOrdinal {
fn traverse(&mut self, operate: &mut dyn FnMut(&mut dyn Operation<Option<Vector>>)) {
operate(self);
}
fn scrollable(
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
_content_bounds: Rectangle,
translation: Vector,
state: &mut dyn Scrollable,
) {
if self.seen == self.target {
self.translation = Some(translation);
if let Some(offset) = self.offset {
state.scroll_to(AbsoluteOffset {
x: Some(offset.x),
y: Some(offset.y),
});
}
}
self.seen += 1;
}
fn finish(&self) -> Outcome<Option<Vector>> {
Outcome::Some(self.translation)
}
}