Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor improve types and simplified usage #167

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ Enable this widget with the feature `number_input`.

*This widget does currently not support web*

### SelectionList

A selection space to show any options passed in.

<div align="center">

![SelectionList showcase](./images/showcase/selection_list.gif)
</div>

Enable this widget with the feature `selection_list`.

### Split

A split divides the available space to display two different elements.
Expand Down
4 changes: 2 additions & 2 deletions examples/selection_list/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Example {

#[derive(Debug, Clone)]
enum Message {
LanguageSelected((usize, String)),
LanguageSelected(usize, String),
AddAtSelection,
ManualSelection,
}
Expand All @@ -45,7 +45,7 @@ impl Sandbox for Example {

fn update(&mut self, message: Message) {
match message {
Message::LanguageSelected((index, language)) => {
Message::LanguageSelected(index, language) => {
self.selected_language = language;
self.selected_index = index;
self.manual_select = None;
Expand Down
Binary file added images/showcase/selection_list.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions src/native/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,14 @@ where
}

#[cfg(feature = "selection_list")]
use std::fmt::Display;
/// Shortcut helper to create a [`SelectionList`] Widget.
///
/// [`SelectionList`]: crate::SelectionList
#[must_use]
pub fn selection_list_with<'a, T, Message, Renderer>(
options: impl Into<Cow<'a, [T]>>,
on_selected: impl Fn((usize, T)) -> Message + 'static,
options: &'a [T],
on_selected: impl Fn(usize, T) -> Message + 'static,
text_size: f32,
padding: f32,
style: <Renderer::Theme as crate::style::selection_list::StyleSheet>::Style,
Expand All @@ -317,7 +318,7 @@ where
Renderer::Theme: crate::style::selection_list::StyleSheet
+ iced_widget::container::StyleSheet
+ iced_widget::scrollable::StyleSheet,
T: Clone + ToString + Eq + Hash,
T: Clone + Display + Eq + Hash,
[T]: ToOwned<Owned = Vec<T>>,
{
crate::SelectionList::new_with(
Expand All @@ -337,16 +338,16 @@ where
/// [`SelectionList`]: crate::SelectionList
#[must_use]
pub fn selection_list<'a, T, Message, Renderer>(
options: impl Into<Cow<'a, [T]>>,
on_selected: impl Fn((usize, T)) -> Message + 'static,
options: &'a [T],
on_selected: impl Fn(usize, T) -> Message + 'static,
) -> crate::SelectionList<'a, T, Message, Renderer>
where
Message: 'a + Clone,
Renderer: 'a + core::Renderer + core::text::Renderer<Font = core::Font>,
Renderer::Theme: crate::style::selection_list::StyleSheet
+ iced_widget::container::StyleSheet
+ iced_widget::scrollable::StyleSheet,
T: Clone + ToString + Eq + Hash,
T: Clone + Display + Eq + Hash,
[T]: ToOwned<Owned = Vec<T>>,
{
crate::SelectionList::new(options, on_selected)
Expand Down
23 changes: 9 additions & 14 deletions src/native/selection_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use iced_widget::{
};

pub use list::List;
use std::marker::PhantomData;
use std::{borrow::Cow, hash::Hash};
use std::hash::Hash;
use std::{fmt::Display, marker::PhantomData};

/// A widget for selecting a single value from a dynamic scrollable list of options.
#[allow(missing_debug_implementations)]
Expand All @@ -35,7 +35,7 @@ where
/// Container for Rendering List.
container: Container<'a, Message, Renderer>,
/// List of Elements to Render.
options: Cow<'a, [T]>,
options: &'a [T],
/// Label Font
font: Renderer::Font,
/// The Containers Width
Expand All @@ -56,20 +56,16 @@ where
Message: 'a + Clone,
Renderer: 'a + core::Renderer + core::text::Renderer<Font = core::Font>,
Renderer::Theme: StyleSheet + container::StyleSheet + scrollable::StyleSheet,
T: Clone + ToString + Eq + Hash,
T: Clone + Display + Eq + Hash,
[T]: ToOwned<Owned = Vec<T>>,
{
/// Creates a new [`SelectionList`] with the given list of `options`,
/// the current selected value, and the `message` to produce when an option is
/// selected. This will default the `style`, `text_size` and `padding`. use `new_with`
/// to set those.
pub fn new(
options: impl Into<Cow<'a, [T]>>,
on_selected: impl Fn((usize, T)) -> Message + 'static,
) -> Self {
let options = options.into();
pub fn new(options: &'a [T], on_selected: impl Fn(usize, T) -> Message + 'static) -> Self {
let container = Container::new(Scrollable::new(List {
options: options.clone(),
options,
font: Font::default(),
text_size: 12.0,
padding: 5.0,
Expand All @@ -96,17 +92,16 @@ where
/// the current selected value, the message to produce when an option is
/// selected, the `style`, `text_size`, `padding` and `font`.
pub fn new_with(
options: impl Into<Cow<'a, [T]>>,
on_selected: impl Fn((usize, T)) -> Message + 'static,
options: &'a [T],
on_selected: impl Fn(usize, T) -> Message + 'static,
text_size: f32,
padding: f32,
style: <Renderer::Theme as StyleSheet>::Style,
selected: Option<usize>,
font: Font,
) -> Self {
let options = options.into();
let container = Container::new(Scrollable::new(List {
options: options.clone(),
options,
font,
text_size,
padding,
Expand Down
14 changes: 7 additions & 7 deletions src/native/selection_list/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Build and show dropdown `ListMenus`.
use std::{
borrow::Cow,
collections::hash_map::DefaultHasher,
fmt::Display,
hash::{Hash, Hasher},
marker::PhantomData,
};
Expand All @@ -28,20 +28,20 @@ use iced_widget::{
#[allow(missing_debug_implementations)]
pub struct List<'a, T: 'a, Message, Renderer>
where
T: Clone + ToString + Eq + Hash,
T: Clone + Display + Eq + Hash,
[T]: ToOwned<Owned = Vec<T>>,
Renderer: core::Renderer + core::text::Renderer<Font = core::Font>,
Renderer::Theme: StyleSheet,
{
/// Options pointer to hold all rendered strings
pub options: Cow<'a, [T]>,
pub options: &'a [T],
/// Hovered Item Pointer
/// Label Font
pub font: Renderer::Font,
/// Style for Font colors and Box hover colors.
pub style: <Renderer::Theme as StyleSheet>::Style,
/// Function Pointer On Select to call on Mouse button press.
pub on_selected: Box<dyn Fn((usize, T)) -> Message>,
pub on_selected: Box<dyn Fn(usize, T) -> Message>,
/// The padding Width
pub padding: f32,
/// The Text Size
Expand All @@ -63,7 +63,7 @@ pub struct ListState {

impl<'a, T, Message, Renderer> Widget<Message, Renderer> for List<'a, T, Message, Renderer>
where
T: Clone + ToString + Eq + Hash,
T: Clone + Display + Eq + Hash,
Renderer: core::Renderer + core::text::Renderer<Font = core::Font>,
Renderer::Theme: StyleSheet,
{
Expand Down Expand Up @@ -164,7 +164,7 @@ where
.last_selected_index
.map_or(event::Status::Ignored, |last| {
if let Some(option) = self.options.get(last.0) {
shell.publish((self.on_selected)((last.0, option.clone())));
shell.publish((self.on_selected)(last.0, option.clone()));
event::Status::Captured
} else {
event::Status::Ignored
Expand Down Expand Up @@ -278,7 +278,7 @@ where
impl<'a, T, Message, Renderer> From<List<'a, T, Message, Renderer>>
for Element<'a, Message, Renderer>
where
T: Clone + ToString + Eq + Hash,
T: Clone + Display + Eq + Hash,
Message: 'a,
Renderer: 'a + core::Renderer + core::text::Renderer<Font = core::Font>,
Renderer::Theme: StyleSheet,
Expand Down