Rename HandleContent to Icon and simplify generics

This commit is contained in:
Héctor Ramón Jiménez 2023-02-14 06:56:59 +01:00
parent efaa80fb44
commit 5569e12149
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 21 additions and 55 deletions

View file

@ -20,49 +20,20 @@ use std::borrow::Cow;
pub use iced_style::pick_list::{Appearance, StyleSheet}; pub use iced_style::pick_list::{Appearance, StyleSheet};
/// The content of the [`Handle`]. /// The icon of a [`Handle`].
#[derive(Clone)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandleContent<Renderer> pub struct Icon<Font> {
where
Renderer: text::Renderer,
{
/// Font that will be used to display the `text`, /// Font that will be used to display the `text`,
pub font: Renderer::Font, pub font: Font,
/// Text that will be shown. /// Text that will be shown.
pub text: String, pub text: String,
/// Font size of the content. /// Font size of the content.
pub size: Option<u16>, pub size: Option<u16>,
} }
impl<Renderer> std::fmt::Debug for HandleContent<Renderer>
where
Renderer: text::Renderer,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HandleIcon")
.field("text", &self.text)
.field("size", &self.size)
.finish()
}
}
impl<Renderer> PartialEq for HandleContent<Renderer>
where
Renderer: text::Renderer,
{
fn eq(&self, other: &Self) -> bool {
self.text.eq(&other.text) && self.size.eq(&other.size)
}
}
impl<Renderer> Eq for HandleContent<Renderer> where Renderer: text::Renderer {}
/// The handle to the right side of the [`PickList`]. /// The handle to the right side of the [`PickList`].
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum Handle<Renderer> pub enum Handle<Font> {
where
Renderer: text::Renderer,
{
/// Displays an arrow icon (▼). /// Displays an arrow icon (▼).
/// ///
/// This is the default. /// This is the default.
@ -71,42 +42,36 @@ where
size: Option<u16>, size: Option<u16>,
}, },
/// A custom static handle. /// A custom static handle.
Static(HandleContent<Renderer>), Static(Icon<Font>),
/// A custom dynamic handle. /// A custom dynamic handle.
Dynamic { Dynamic {
/// The [`HandleContent`] used when [`PickList`] is closed. /// The [`Icon`] used when [`PickList`] is closed.
closed: HandleContent<Renderer>, closed: Icon<Font>,
/// The [`HandleContent`] used when [`PickList`] is open. /// The [`Icon`] used when [`PickList`] is open.
open: HandleContent<Renderer>, open: Icon<Font>,
}, },
/// No handle will be shown. /// No handle will be shown.
None, None,
} }
impl<Renderer> Default for Handle<Renderer> impl<Font> Default for Handle<Font> {
where
Renderer: text::Renderer,
{
fn default() -> Self { fn default() -> Self {
Self::Arrow { size: None } Self::Arrow { size: None }
} }
} }
impl<Renderer> Handle<Renderer> impl<Font: Clone> Handle<Font> {
where fn content<Renderer: text::Renderer<Font = Font>>(
Renderer: text::Renderer,
{
fn content(
&self, &self,
is_open: bool, is_open: bool,
) -> Option<(Renderer::Font, String, Option<u16>)> { ) -> Option<(Font, String, Option<u16>)> {
match self { match self {
Self::Arrow { size } => Some(( Self::Arrow { size } => Some((
Renderer::ICON_FONT, Renderer::ICON_FONT,
Renderer::ARROW_DOWN_ICON.to_string(), Renderer::ARROW_DOWN_ICON.to_string(),
*size, *size,
)), )),
Self::Static(HandleContent { font, text, size }) => { Self::Static(Icon { font, text, size }) => {
Some((font.clone(), text.clone(), *size)) Some((font.clone(), text.clone(), *size))
} }
Self::Dynamic { open, closed } => { Self::Dynamic { open, closed } => {
@ -141,7 +106,7 @@ where
padding: Padding, padding: Padding,
text_size: Option<u16>, text_size: Option<u16>,
font: Renderer::Font, font: Renderer::Font,
handle: Handle<Renderer>, handle: Handle<Renderer::Font>,
style: <Renderer::Theme as StyleSheet>::Style, style: <Renderer::Theme as StyleSheet>::Style,
} }
@ -212,7 +177,7 @@ where
} }
/// Sets the [`Handle`] of the [`PickList`]. /// Sets the [`Handle`] of the [`PickList`].
pub fn handle(mut self, handle: Handle<Renderer>) -> Self { pub fn handle(mut self, handle: Handle<Renderer::Font>) -> Self {
self.handle = handle; self.handle = handle;
self self
} }
@ -630,7 +595,7 @@ pub fn draw<'a, T, Renderer>(
font: &Renderer::Font, font: &Renderer::Font,
placeholder: Option<&str>, placeholder: Option<&str>,
selected: Option<&T>, selected: Option<&T>,
handle: &Handle<Renderer>, handle: &Handle<Renderer::Font>,
style: &<Renderer::Theme as StyleSheet>::Style, style: &<Renderer::Theme as StyleSheet>::Style,
state: impl FnOnce() -> &'a State<T>, state: impl FnOnce() -> &'a State<T>,
) where ) where
@ -659,7 +624,8 @@ pub fn draw<'a, T, Renderer>(
style.background, style.background,
); );
if let Some((font, text, size)) = handle.content(state.is_open) { if let Some((font, text, size)) = handle.content::<Renderer>(state.is_open)
{
let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); let size = f32::from(size.unwrap_or_else(|| renderer.default_size()));
renderer.fill_text(Text { renderer.fill_text(Text {

View file

@ -81,7 +81,7 @@ pub mod pane_grid {
pub mod pick_list { pub mod pick_list {
//! Display a dropdown list of selectable values. //! Display a dropdown list of selectable values.
pub use iced_native::widget::pick_list::{ pub use iced_native::widget::pick_list::{
Appearance, Handle, HandleContent, StyleSheet, Appearance, Handle, Icon, StyleSheet,
}; };
/// A widget allowing the selection of a single value from a list of options. /// A widget allowing the selection of a single value from a list of options.