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

Use Borrow for both options and selected in PickList #2251

Merged
merged 2 commits into from
Feb 14, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `on_open` and `on_close` handlers for `PickList`. [#2174](https://github.com/iced-rs/iced/pull/2174)
- Support for generic `Element` in `Tooltip`. [#2228](https://github.com/iced-rs/iced/pull/2228)
- Container and `gap` styling for `Scrollable`. [#2239](https://github.com/iced-rs/iced/pull/2239)
- Use `Borrow` for both `options` and `selected` in PickList. [#2251](https://github.com/iced-rs/iced/pull/2251)

### Changed
- Enable WebGPU backend in `wgpu` by default instead of WebGL. [#2068](https://github.com/iced-rs/iced/pull/2068)
Expand Down
6 changes: 1 addition & 5 deletions examples/layout/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ impl Application for Layout {
horizontal_space(Length::Fill),
checkbox("Explain", self.explain)
.on_toggle(Message::ExplainToggled),
pick_list(
Theme::ALL,
Some(self.theme.clone()),
Message::ThemeSelected
),
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeSelected),
]
.spacing(20)
.align_items(Alignment::Center);
Expand Down
6 changes: 1 addition & 5 deletions examples/qr_code/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ impl Sandbox for QRGenerator {

let choose_theme = row![
text("Theme:"),
pick_list(
Theme::ALL,
Some(self.theme.clone()),
Message::ThemeChanged,
)
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeChanged,)
]
.spacing(10)
.align_items(Alignment::Center);
Expand Down
8 changes: 2 additions & 6 deletions examples/styling/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ impl Sandbox for Styling {
fn view(&self) -> Element<Message> {
let choose_theme = column![
text("Theme:"),
pick_list(
Theme::ALL,
Some(self.theme.clone()),
Message::ThemeChanged
)
.width(Length::Fill),
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeChanged)
.width(Length::Fill),
]
.spacing(10);

Expand Down
15 changes: 8 additions & 7 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::toggler::{self, Toggler};
use crate::tooltip::{self, Tooltip};
use crate::{Column, MouseArea, Row, Space, Themer, VerticalSlider};

use std::borrow::Cow;
use std::borrow::Borrow;
use std::ops::RangeInclusive;

/// Creates a [`Column`] with the given children.
Expand Down Expand Up @@ -264,14 +264,15 @@ where
/// Creates a new [`PickList`].
///
/// [`PickList`]: crate::PickList
pub fn pick_list<'a, Message, Theme, Renderer, T>(
options: impl Into<Cow<'a, [T]>>,
selected: Option<T>,
pub fn pick_list<'a, T, L, V, Message, Theme, Renderer>(
options: L,
selected: Option<V>,
on_selected: impl Fn(T) -> Message + 'a,
) -> PickList<'a, T, Message, Theme, Renderer>
) -> PickList<'a, T, L, V, Message, Theme, Renderer>
where
T: ToString + PartialEq + 'static,
[T]: ToOwned<Owned = Vec<T>>,
T: ToString + PartialEq + Clone + 'a,
L: Borrow<[T]> + 'a,
V: Borrow<T> + 'a,
Message: Clone,
Renderer: core::text::Renderer,
Theme: pick_list::StyleSheet
Expand Down
59 changes: 34 additions & 25 deletions widget/src/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::core::{
use crate::overlay::menu::{self, Menu};
use crate::scrollable;

use std::borrow::Cow;
use std::borrow::Borrow;

pub use crate::style::pick_list::{Appearance, StyleSheet};

Expand All @@ -26,20 +26,24 @@ pub use crate::style::pick_list::{Appearance, StyleSheet};
pub struct PickList<
'a,
T,
L,
V,
Message,
Theme = crate::Theme,
Renderer = crate::Renderer,
> where
[T]: ToOwned<Owned = Vec<T>>,
T: ToString + PartialEq + Clone,
L: Borrow<[T]> + 'a,
V: Borrow<T> + 'a,
Theme: StyleSheet,
Renderer: text::Renderer,
{
on_select: Box<dyn Fn(T) -> Message + 'a>,
on_open: Option<Message>,
on_close: Option<Message>,
options: Cow<'a, [T]>,
options: L,
placeholder: Option<String>,
selected: Option<T>,
selected: Option<V>,
width: Length,
padding: Padding,
text_size: Option<Pixels>,
Expand All @@ -50,11 +54,12 @@ pub struct PickList<
style: Theme::Style,
}

impl<'a, T: 'a, Message, Theme, Renderer>
PickList<'a, T, Message, Theme, Renderer>
impl<'a, T, L, V, Message, Theme, Renderer>
PickList<'a, T, L, V, Message, Theme, Renderer>
where
T: ToString + PartialEq,
[T]: ToOwned<Owned = Vec<T>>,
T: ToString + PartialEq + Clone,
L: Borrow<[T]> + 'a,
V: Borrow<T> + 'a,
Message: Clone,
Theme: StyleSheet
+ scrollable::StyleSheet
Expand All @@ -69,15 +74,15 @@ where
/// Creates a new [`PickList`] with the given list of options, the current
/// selected value, and the message to produce when an option is selected.
pub fn new(
options: impl Into<Cow<'a, [T]>>,
selected: Option<T>,
options: L,
selected: Option<V>,
on_select: impl Fn(T) -> Message + 'a,
) -> Self {
Self {
on_select: Box::new(on_select),
on_open: None,
on_close: None,
options: options.into(),
options,
placeholder: None,
selected,
width: Length::Shrink,
Expand Down Expand Up @@ -164,11 +169,12 @@ where
}
}

impl<'a, T: 'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for PickList<'a, T, Message, Theme, Renderer>
impl<'a, T, L, V, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for PickList<'a, T, L, V, Message, Theme, Renderer>
where
T: Clone + ToString + PartialEq + 'static,
[T]: ToOwned<Owned = Vec<T>>,
T: Clone + ToString + PartialEq + 'a,
L: Borrow<[T]>,
V: Borrow<T>,
Message: Clone + 'a,
Theme: StyleSheet
+ scrollable::StyleSheet
Expand Down Expand Up @@ -209,7 +215,7 @@ where
self.text_shaping,
self.font,
self.placeholder.as_deref(),
&self.options,
self.options.borrow(),
)
}

Expand All @@ -232,8 +238,8 @@ where
self.on_select.as_ref(),
self.on_open.as_ref(),
self.on_close.as_ref(),
self.selected.as_ref(),
&self.options,
self.selected.as_ref().map(Borrow::borrow),
self.options.borrow(),
|| tree.state.downcast_mut::<State<Renderer::Paragraph>>(),
)
}
Expand Down Expand Up @@ -271,7 +277,7 @@ where
self.text_shaping,
font,
self.placeholder.as_deref(),
self.selected.as_ref(),
self.selected.as_ref().map(Borrow::borrow),
&self.handle,
&self.style,
|| tree.state.downcast_ref::<State<Renderer::Paragraph>>(),
Expand All @@ -296,19 +302,20 @@ where
self.text_size,
self.text_shaping,
self.font.unwrap_or_else(|| renderer.default_font()),
&self.options,
self.options.borrow(),
&self.on_select,
self.style.clone(),
)
}
}

impl<'a, T: 'a, Message, Theme, Renderer>
From<PickList<'a, T, Message, Theme, Renderer>>
impl<'a, T, L, V, Message, Theme, Renderer>
From<PickList<'a, T, L, V, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer>
where
T: Clone + ToString + PartialEq + 'static,
[T]: ToOwned<Owned = Vec<T>>,
T: Clone + ToString + PartialEq + 'a,
L: Borrow<[T]> + 'a,
V: Borrow<T> + 'a,
Message: Clone + 'a,
Theme: StyleSheet
+ scrollable::StyleSheet
Expand All @@ -318,7 +325,9 @@ where
<Theme as menu::StyleSheet>::Style: From<<Theme as StyleSheet>::Style>,
Renderer: text::Renderer + 'a,
{
fn from(pick_list: PickList<'a, T, Message, Theme, Renderer>) -> Self {
fn from(
pick_list: PickList<'a, T, L, V, Message, Theme, Renderer>,
) -> Self {
Self::new(pick_list)
}
}
Expand Down
Loading