Skip to content

Commit

Permalink
Merge pull request #2251 from iced-rs/borrow-pick-list-values
Browse files Browse the repository at this point in the history
Use `Borrow` for both `options` and `selected` in `PickList`
hecrj authored Feb 14, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 52e207b + 06973d3 commit 75b1964
Showing 6 changed files with 47 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 1 addition & 5 deletions examples/layout/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 1 addition & 5 deletions examples/qr_code/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 2 additions & 6 deletions examples/styling/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);

15 changes: 8 additions & 7 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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.
@@ -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
59 changes: 34 additions & 25 deletions widget/src/pick_list.rs
Original file line number Diff line number Diff line change
@@ -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};

@@ -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>,
@@ -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
@@ -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,
@@ -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
@@ -209,7 +215,7 @@ where
self.text_shaping,
self.font,
self.placeholder.as_deref(),
&self.options,
self.options.borrow(),
)
}

@@ -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>>(),
)
}
@@ -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>>(),
@@ -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
@@ -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)
}
}

0 comments on commit 75b1964

Please sign in to comment.