Remove Id for container, scrollable, and text_input
This commit is contained in:
parent
63142d34fc
commit
fbe60feb7e
9 changed files with 68 additions and 179 deletions
|
|
@ -29,6 +29,12 @@ impl From<&'static str> for Id {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<String> for Id {
|
||||
fn from(value: String) -> Self {
|
||||
Self::new(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
enum Internal {
|
||||
Unique(usize),
|
||||
|
|
|
|||
|
|
@ -4,11 +4,6 @@ use iced::widget::{
|
|||
};
|
||||
use iced::{Border, Center, Color, Element, Fill, Task, Theme};
|
||||
|
||||
use std::sync::LazyLock;
|
||||
|
||||
static SCROLLABLE_ID: LazyLock<scrollable::Id> =
|
||||
LazyLock::new(scrollable::Id::unique);
|
||||
|
||||
pub fn main() -> iced::Result {
|
||||
iced::application(
|
||||
ScrollableDemo::default,
|
||||
|
|
@ -65,19 +60,13 @@ impl ScrollableDemo {
|
|||
self.current_scroll_offset = scrollable::RelativeOffset::START;
|
||||
self.scrollable_direction = direction;
|
||||
|
||||
scrollable::snap_to(
|
||||
SCROLLABLE_ID.clone(),
|
||||
self.current_scroll_offset,
|
||||
)
|
||||
scrollable::snap_to(SCROLLABLE, self.current_scroll_offset)
|
||||
}
|
||||
Message::AlignmentChanged(alignment) => {
|
||||
self.current_scroll_offset = scrollable::RelativeOffset::START;
|
||||
self.anchor = alignment;
|
||||
|
||||
scrollable::snap_to(
|
||||
SCROLLABLE_ID.clone(),
|
||||
self.current_scroll_offset,
|
||||
)
|
||||
scrollable::snap_to(SCROLLABLE, self.current_scroll_offset)
|
||||
}
|
||||
Message::ScrollbarWidthChanged(width) => {
|
||||
self.scrollbar_width = width;
|
||||
|
|
@ -97,18 +86,12 @@ impl ScrollableDemo {
|
|||
Message::ScrollToBeginning => {
|
||||
self.current_scroll_offset = scrollable::RelativeOffset::START;
|
||||
|
||||
scrollable::snap_to(
|
||||
SCROLLABLE_ID.clone(),
|
||||
self.current_scroll_offset,
|
||||
)
|
||||
scrollable::snap_to(SCROLLABLE, self.current_scroll_offset)
|
||||
}
|
||||
Message::ScrollToEnd => {
|
||||
self.current_scroll_offset = scrollable::RelativeOffset::END;
|
||||
|
||||
scrollable::snap_to(
|
||||
SCROLLABLE_ID.clone(),
|
||||
self.current_scroll_offset,
|
||||
)
|
||||
scrollable::snap_to(SCROLLABLE, self.current_scroll_offset)
|
||||
}
|
||||
Message::Scrolled(viewport) => {
|
||||
self.current_scroll_offset = viewport.relative_offset();
|
||||
|
|
@ -226,7 +209,7 @@ impl ScrollableDemo {
|
|||
))
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.id(SCROLLABLE_ID.clone())
|
||||
.id(SCROLLABLE)
|
||||
.on_scroll(Message::Scrolled),
|
||||
Direction::Horizontal => scrollable(
|
||||
row![
|
||||
|
|
@ -252,7 +235,7 @@ impl ScrollableDemo {
|
|||
))
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.id(SCROLLABLE_ID.clone())
|
||||
.id(SCROLLABLE)
|
||||
.on_scroll(Message::Scrolled),
|
||||
Direction::Multi => scrollable(
|
||||
//horizontal content
|
||||
|
|
@ -299,7 +282,7 @@ impl ScrollableDemo {
|
|||
})
|
||||
.width(Fill)
|
||||
.height(Fill)
|
||||
.id(SCROLLABLE_ID.clone())
|
||||
.id(SCROLLABLE)
|
||||
.on_scroll(Message::Scrolled),
|
||||
});
|
||||
|
||||
|
|
@ -348,3 +331,5 @@ fn progress_bar_custom_style(theme: &Theme) -> progress_bar::Style {
|
|||
border: Border::default(),
|
||||
}
|
||||
}
|
||||
|
||||
const SCROLLABLE: &str = "scrollable";
|
||||
|
|
|
|||
|
|
@ -305,8 +305,8 @@ pub enum TaskMessage {
|
|||
}
|
||||
|
||||
impl Task {
|
||||
fn text_input_id(i: usize) -> text_input::Id {
|
||||
text_input::Id::new(format!("task-{i}"))
|
||||
fn text_input_id(i: usize) -> widget::Id {
|
||||
widget::Id::new(format!("task-{i}"))
|
||||
}
|
||||
|
||||
fn new(description: String) -> Self {
|
||||
|
|
|
|||
|
|
@ -41,9 +41,9 @@ impl Example {
|
|||
Task::none()
|
||||
}
|
||||
Message::Scrolled | Message::WindowResized => Task::batch(vec![
|
||||
container::visible_bounds(OUTER_CONTAINER.clone())
|
||||
container::visible_bounds(OUTER_CONTAINER)
|
||||
.map(Message::OuterBoundsFetched),
|
||||
container::visible_bounds(INNER_CONTAINER.clone())
|
||||
container::visible_bounds(INNER_CONTAINER)
|
||||
.map(Message::InnerBoundsFetched),
|
||||
]),
|
||||
Message::OuterBoundsFetched(outer_bounds) => {
|
||||
|
|
@ -113,7 +113,7 @@ impl Example {
|
|||
text("Scroll me!"),
|
||||
vertical_space().height(400),
|
||||
container(text("I am the outer container!"))
|
||||
.id(OUTER_CONTAINER.clone())
|
||||
.id(OUTER_CONTAINER)
|
||||
.padding(40)
|
||||
.style(container::rounded_box),
|
||||
vertical_space().height(400),
|
||||
|
|
@ -122,7 +122,7 @@ impl Example {
|
|||
text("Scroll me!"),
|
||||
vertical_space().height(400),
|
||||
container(text("I am the inner container!"))
|
||||
.id(INNER_CONTAINER.clone())
|
||||
.id(INNER_CONTAINER)
|
||||
.padding(40)
|
||||
.style(container::rounded_box),
|
||||
vertical_space().height(400),
|
||||
|
|
@ -157,9 +157,5 @@ impl Example {
|
|||
}
|
||||
}
|
||||
|
||||
use std::sync::LazyLock;
|
||||
|
||||
static OUTER_CONTAINER: LazyLock<container::Id> =
|
||||
LazyLock::new(|| container::Id::new("outer"));
|
||||
static INNER_CONTAINER: LazyLock<container::Id> =
|
||||
LazyLock::new(|| container::Id::new("inner"));
|
||||
const OUTER_CONTAINER: &str = "outer";
|
||||
const INNER_CONTAINER: &str = "inner";
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ use iced::widget::{
|
|||
};
|
||||
use iced::{Center, Element, Fill, Subscription, Task, color};
|
||||
|
||||
use std::sync::LazyLock;
|
||||
|
||||
pub fn main() -> iced::Result {
|
||||
iced::application(WebSocket::new, WebSocket::update, WebSocket::view)
|
||||
.subscription(WebSocket::subscription)
|
||||
|
|
@ -76,7 +74,7 @@ impl WebSocket {
|
|||
self.messages.push(message);
|
||||
|
||||
scrollable::snap_to(
|
||||
MESSAGE_LOG.clone(),
|
||||
MESSAGE_LOG,
|
||||
scrollable::RelativeOffset::END,
|
||||
)
|
||||
}
|
||||
|
|
@ -105,7 +103,7 @@ impl WebSocket {
|
|||
column(self.messages.iter().map(text).map(Element::from))
|
||||
.spacing(10),
|
||||
)
|
||||
.id(MESSAGE_LOG.clone())
|
||||
.id(MESSAGE_LOG)
|
||||
.height(Fill)
|
||||
.spacing(10)
|
||||
.into()
|
||||
|
|
@ -142,5 +140,4 @@ enum State {
|
|||
Connected(echo::Connection),
|
||||
}
|
||||
|
||||
static MESSAGE_LOG: LazyLock<scrollable::Id> =
|
||||
LazyLock::new(scrollable::Id::unique);
|
||||
const MESSAGE_LOG: &str = "message_log";
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ impl Selector for Id {
|
|||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
format!("id == \"{:?}\"", self)
|
||||
format!("id == {:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ pub struct Container<
|
|||
Theme: Catalog,
|
||||
Renderer: core::Renderer,
|
||||
{
|
||||
id: Option<Id>,
|
||||
id: Option<widget::Id>,
|
||||
padding: Padding,
|
||||
width: Length,
|
||||
height: Length,
|
||||
|
|
@ -108,7 +108,7 @@ where
|
|||
}
|
||||
|
||||
/// Sets the [`Id`] of the [`Container`].
|
||||
pub fn id(mut self, id: impl Into<Id>) -> Self {
|
||||
pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
|
||||
self.id = Some(id.into());
|
||||
self
|
||||
}
|
||||
|
|
@ -284,7 +284,7 @@ where
|
|||
renderer: &Renderer,
|
||||
operation: &mut dyn Operation,
|
||||
) {
|
||||
operation.container(self.id.as_ref().map(|id| &id.0), layout.bounds());
|
||||
operation.container(self.id.as_ref(), layout.bounds());
|
||||
operation.traverse(&mut |operation| {
|
||||
self.content.as_widget().operate(
|
||||
tree,
|
||||
|
|
@ -457,39 +457,9 @@ pub fn draw_background<Renderer>(
|
|||
}
|
||||
}
|
||||
|
||||
/// The identifier of a [`Container`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Id(widget::Id);
|
||||
|
||||
impl Id {
|
||||
/// Creates a custom [`Id`].
|
||||
pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self {
|
||||
Self(widget::Id::new(id))
|
||||
}
|
||||
|
||||
/// Creates a unique [`Id`].
|
||||
///
|
||||
/// This function produces a different [`Id`] every time it is called.
|
||||
pub fn unique() -> Self {
|
||||
Self(widget::Id::unique())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Id> for widget::Id {
|
||||
fn from(id: Id) -> Self {
|
||||
id.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for Id {
|
||||
fn from(value: &'static str) -> Self {
|
||||
Id::new(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that queries the visible screen bounds of the
|
||||
/// [`Container`] with the given [`Id`].
|
||||
pub fn visible_bounds(_id: impl Into<Id>) -> Task<Option<Rectangle>> {
|
||||
pub fn visible_bounds(_id: impl Into<widget::Id>) -> Task<Option<Rectangle>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ pub struct Scrollable<
|
|||
Theme: Catalog,
|
||||
Renderer: core::Renderer,
|
||||
{
|
||||
id: Option<Id>,
|
||||
id: Option<widget::Id>,
|
||||
width: Length,
|
||||
height: Length,
|
||||
direction: Direction,
|
||||
|
|
@ -150,7 +150,7 @@ where
|
|||
}
|
||||
|
||||
/// Sets the [`Id`] of the [`Scrollable`].
|
||||
pub fn id(mut self, id: impl Into<Id>) -> Self {
|
||||
pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
|
||||
self.id = Some(id.into());
|
||||
self
|
||||
}
|
||||
|
|
@ -542,7 +542,7 @@ where
|
|||
state.translation(self.direction, bounds, content_bounds);
|
||||
|
||||
operation.scrollable(
|
||||
self.id.as_ref().map(|id| &id.0),
|
||||
self.id.as_ref(),
|
||||
bounds,
|
||||
content_bounds,
|
||||
translation,
|
||||
|
|
@ -1262,59 +1262,38 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// The identifier of a [`Scrollable`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Id(widget::Id);
|
||||
|
||||
impl Id {
|
||||
/// Creates a custom [`Id`].
|
||||
pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self {
|
||||
Self(widget::Id::new(id))
|
||||
}
|
||||
|
||||
/// Creates a unique [`Id`].
|
||||
///
|
||||
/// This function produces a different [`Id`] every time it is called.
|
||||
pub fn unique() -> Self {
|
||||
Self(widget::Id::unique())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Id> for widget::Id {
|
||||
fn from(id: Id) -> Self {
|
||||
id.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for Id {
|
||||
fn from(id: &'static str) -> Self {
|
||||
Self::new(id)
|
||||
}
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that snaps the [`Scrollable`] with the given [`Id`]
|
||||
/// to the provided [`RelativeOffset`].
|
||||
pub fn snap_to<T>(id: impl Into<Id>, offset: RelativeOffset) -> Task<T> {
|
||||
pub fn snap_to<T>(
|
||||
id: impl Into<widget::Id>,
|
||||
offset: RelativeOffset,
|
||||
) -> Task<T> {
|
||||
task::effect(Action::widget(operation::scrollable::snap_to(
|
||||
id.into().0,
|
||||
id.into(),
|
||||
offset,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`Id`]
|
||||
/// to the provided [`AbsoluteOffset`].
|
||||
pub fn scroll_to<T>(id: impl Into<Id>, offset: AbsoluteOffset) -> Task<T> {
|
||||
pub fn scroll_to<T>(
|
||||
id: impl Into<widget::Id>,
|
||||
offset: AbsoluteOffset,
|
||||
) -> Task<T> {
|
||||
task::effect(Action::widget(operation::scrollable::scroll_to(
|
||||
id.into().0,
|
||||
id.into(),
|
||||
offset,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that scrolls the [`Scrollable`] with the given [`Id`]
|
||||
/// by the provided [`AbsoluteOffset`].
|
||||
pub fn scroll_by<T>(id: impl Into<Id>, offset: AbsoluteOffset) -> Task<T> {
|
||||
pub fn scroll_by<T>(
|
||||
id: impl Into<widget::Id>,
|
||||
offset: AbsoluteOffset,
|
||||
) -> Task<T> {
|
||||
task::effect(Action::widget(operation::scrollable::scroll_by(
|
||||
id.into().0,
|
||||
id.into(),
|
||||
offset,
|
||||
)))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ pub struct TextInput<
|
|||
Theme: Catalog,
|
||||
Renderer: text::Renderer,
|
||||
{
|
||||
id: Option<Id>,
|
||||
id: Option<widget::Id>,
|
||||
placeholder: String,
|
||||
value: Value,
|
||||
is_secure: bool,
|
||||
|
|
@ -157,7 +157,7 @@ where
|
|||
}
|
||||
|
||||
/// Sets the [`Id`] of the [`TextInput`].
|
||||
pub fn id(mut self, id: impl Into<Id>) -> Self {
|
||||
pub fn id(mut self, id: impl Into<widget::Id>) -> Self {
|
||||
self.id = Some(id.into());
|
||||
self
|
||||
}
|
||||
|
|
@ -688,17 +688,8 @@ where
|
|||
) {
|
||||
let state = tree.state.downcast_mut::<State<Renderer::Paragraph>>();
|
||||
|
||||
operation.focusable(
|
||||
self.id.as_ref().map(|id| &id.0),
|
||||
layout.bounds(),
|
||||
state,
|
||||
);
|
||||
|
||||
operation.text_input(
|
||||
self.id.as_ref().map(|id| &id.0),
|
||||
layout.bounds(),
|
||||
state,
|
||||
);
|
||||
operation.text_input(self.id.as_ref(), layout.bounds(), state);
|
||||
operation.focusable(self.id.as_ref(), layout.bounds(), state);
|
||||
}
|
||||
|
||||
fn update(
|
||||
|
|
@ -1454,82 +1445,47 @@ pub enum Side {
|
|||
Right,
|
||||
}
|
||||
|
||||
/// The identifier of a [`TextInput`].
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Id(widget::Id);
|
||||
|
||||
impl Id {
|
||||
/// Creates a custom [`Id`].
|
||||
pub fn new(id: impl Into<std::borrow::Cow<'static, str>>) -> Self {
|
||||
Self(widget::Id::new(id))
|
||||
}
|
||||
|
||||
/// Creates a unique [`Id`].
|
||||
///
|
||||
/// This function produces a different [`Id`] every time it is called.
|
||||
pub fn unique() -> Self {
|
||||
Self(widget::Id::unique())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Id> for widget::Id {
|
||||
fn from(id: Id) -> Self {
|
||||
id.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for Id {
|
||||
fn from(id: &'static str) -> Self {
|
||||
Self::new(id)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Id {
|
||||
fn from(id: String) -> Self {
|
||||
Self::new(id)
|
||||
}
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that returns whether the [`TextInput`] with the given [`Id`] is focused or not.
|
||||
pub fn is_focused(id: impl Into<Id>) -> Task<bool> {
|
||||
task::widget(operation::focusable::is_focused(id.into().into()))
|
||||
pub fn is_focused(id: impl Into<widget::Id>) -> Task<bool> {
|
||||
task::widget(operation::focusable::is_focused(id.into()))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that focuses the [`TextInput`] with the given [`Id`].
|
||||
pub fn focus<T>(id: impl Into<Id>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::focusable::focus(id.into().0)))
|
||||
pub fn focus<T>(id: impl Into<widget::Id>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::focusable::focus(id.into())))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the
|
||||
/// end.
|
||||
pub fn move_cursor_to_end<T>(id: impl Into<Id>) -> Task<T> {
|
||||
pub fn move_cursor_to_end<T>(id: impl Into<widget::Id>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::text_input::move_cursor_to_end(
|
||||
id.into().0,
|
||||
id.into(),
|
||||
)))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the
|
||||
/// front.
|
||||
pub fn move_cursor_to_front<T>(id: impl Into<Id>) -> Task<T> {
|
||||
pub fn move_cursor_to_front<T>(id: impl Into<widget::Id>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::text_input::move_cursor_to_front(
|
||||
id.into().0,
|
||||
id.into(),
|
||||
)))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the
|
||||
/// provided position.
|
||||
pub fn move_cursor_to<T>(id: impl Into<Id>, position: usize) -> Task<T> {
|
||||
pub fn move_cursor_to<T>(
|
||||
id: impl Into<widget::Id>,
|
||||
position: usize,
|
||||
) -> Task<T> {
|
||||
task::effect(Action::widget(operation::text_input::move_cursor_to(
|
||||
id.into().0,
|
||||
id.into(),
|
||||
position,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Produces a [`Task`] that selects all the content of the [`TextInput`] with the given [`Id`].
|
||||
pub fn select_all<T>(id: impl Into<Id>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::text_input::select_all(
|
||||
id.into().0,
|
||||
)))
|
||||
pub fn select_all<T>(id: impl Into<widget::Id>) -> Task<T> {
|
||||
task::effect(Action::widget(operation::text_input::select_all(id.into())))
|
||||
}
|
||||
|
||||
/// The state of a [`TextInput`].
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue