Skip to content

Commit 3766ae5

Browse files
hecrjjackpot51
authored andcommitted
Infinite List widget from upstream feature/list-widget-reloaded branch
1 parent d4585d5 commit 3766ae5

File tree

5 files changed

+910
-0
lines changed

5 files changed

+910
-0
lines changed

examples/list/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "list"
3+
version = "0.1.0"
4+
authors = ["Héctor Ramón Jiménez <[email protected]>"]
5+
edition = "2021"
6+
publish = false
7+
8+
[dependencies]
9+
iced.workspace = true
10+
iced.features = ["debug"]

examples/list/src/main.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use iced::widget::{
2+
button, center, column, container, horizontal_space, list, row, scrollable,
3+
text,
4+
};
5+
use iced::{Alignment, Element, Length, Theme};
6+
7+
pub fn main() -> iced::Result {
8+
iced::program("List - Iced", List::update, List::view)
9+
.theme(|_| Theme::TokyoNight)
10+
.run()
11+
}
12+
13+
struct List {
14+
content: list::Content<(usize, State)>,
15+
}
16+
17+
#[derive(Debug, Clone, Copy)]
18+
enum Message {
19+
Update(usize),
20+
Remove(usize),
21+
}
22+
23+
impl List {
24+
fn update(&mut self, message: Message) {
25+
match message {
26+
Message::Update(index) => {
27+
if let Some((_id, state)) = self.content.get_mut(index) {
28+
*state = State::Updated;
29+
}
30+
}
31+
Message::Remove(index) => {
32+
let _ = self.content.remove(index);
33+
}
34+
}
35+
}
36+
37+
fn view(&self) -> Element<Message> {
38+
center(
39+
scrollable(
40+
container(list(&self.content, |index, (id, state)| {
41+
row![
42+
match state {
43+
State::Idle =>
44+
Element::from(text(format!("I am item {id}!"))),
45+
State::Updated => center(
46+
column![
47+
text(format!("I am item {id}!")),
48+
text("... but different!")
49+
]
50+
.spacing(20)
51+
)
52+
.height(300)
53+
.into(),
54+
},
55+
horizontal_space(),
56+
button("Update").on_press_maybe(
57+
matches!(state, State::Idle)
58+
.then_some(Message::Update(index))
59+
),
60+
button("Remove")
61+
.on_press(Message::Remove(index))
62+
.style(button::danger)
63+
]
64+
.spacing(10)
65+
.padding(5)
66+
.align_items(Alignment::Center)
67+
.into()
68+
}))
69+
.padding(10),
70+
)
71+
.width(Length::Fill),
72+
)
73+
.padding(10)
74+
.into()
75+
}
76+
}
77+
78+
impl Default for List {
79+
fn default() -> Self {
80+
Self {
81+
content: list::Content::from_iter(
82+
(0..1_000).map(|id| (id, State::Idle)),
83+
),
84+
}
85+
}
86+
}
87+
88+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
89+
enum State {
90+
Idle,
91+
Updated,
92+
}

widget/src/helpers.rs

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::core::widget::operation::{self, Operation};
88
use crate::core::window;
99
use crate::core::{Element, Length, Pixels, Widget};
1010
use crate::keyed;
11+
use crate::list::{self, List};
1112
use crate::overlay;
1213
use crate::pane_grid::{self, PaneGrid};
1314
use crate::pick_list::{self, PickList};
@@ -814,6 +815,18 @@ where
814815
Scrollable::new(content)
815816
}
816817

818+
/// Creates a new [`List`] with the provided [`Content`] and
819+
/// closure to view an item of the [`List`].
820+
///
821+
/// [`List`]: crate::List
822+
/// [`Content`]: crate::list::Content
823+
pub fn list<'a, T, Message, Theme, Renderer>(
824+
content: &'a list::Content<T>,
825+
view_item: impl Fn(usize, &'a T) -> Element<'a, Message, Theme, Renderer> + 'a,
826+
) -> List<'a, T, Message, Theme, Renderer> {
827+
List::new(content, view_item)
828+
}
829+
817830
/// Creates a new [`Button`] with the provided content.
818831
///
819832
/// # Example

widget/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod checkbox;
2020
pub mod combo_box;
2121
pub mod container;
2222
pub mod keyed;
23+
pub mod list;
2324
pub mod overlay;
2425
pub mod pane_grid;
2526
pub mod pick_list;
@@ -56,6 +57,8 @@ pub use combo_box::ComboBox;
5657
#[doc(no_inline)]
5758
pub use container::Container;
5859
#[doc(no_inline)]
60+
pub use list::List;
61+
#[doc(no_inline)]
5962
pub use mouse_area::MouseArea;
6063
#[doc(no_inline)]
6164
pub use pane_grid::PaneGrid;

0 commit comments

Comments
 (0)