summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Casper Rogild Storm <2248455+casperstorm@users.noreply.github.com>2022-11-29 11:36:00 +0100
committerLibravatar Casper Rogild Storm <2248455+casperstorm@users.noreply.github.com>2022-11-29 11:36:00 +0100
commit0974e9e86523174e7a07d992b48d681aa07d4a7e (patch)
tree8fd8784b89c0444029246183d1b9e208e51e97e5
parent8d67e21d48dcefbbb675cfad07849607ce0fe1b7 (diff)
downloadiced-0974e9e86523174e7a07d992b48d681aa07d4a7e.tar.gz
iced-0974e9e86523174e7a07d992b48d681aa07d4a7e.tar.bz2
iced-0974e9e86523174e7a07d992b48d681aa07d4a7e.zip
Added ability to customize accessory content.
-rw-r--r--native/src/widget/pick_list.rs98
-rw-r--r--src/widget.rs4
-rw-r--r--style/src/pick_list.rs2
-rw-r--r--style/src/theme.rs2
4 files changed, 87 insertions, 19 deletions
diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs
index a6459cd6..9fc92496 100644
--- a/native/src/widget/pick_list.rs
+++ b/native/src/widget/pick_list.rs
@@ -20,6 +20,60 @@ use std::borrow::Cow;
pub use iced_style::pick_list::{Appearance, StyleSheet};
+/// The content to the right side of the [`PickList`].
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub enum AccessoryContent<Renderer>
+where
+ Renderer: text::Renderer,
+{
+ /// Default accessory content.
+ Default {
+ /// Font size of the content.
+ size: Option<u16>,
+ },
+ /// Custom accessory content.
+ Custom {
+ /// Font which will be used in the accessory content.
+ font: Renderer::Font,
+ /// Content which will be shown.
+ content: String,
+ /// Font size of the content.
+ size: Option<u16>,
+ },
+ /// No accessory content will be shown.
+ None,
+}
+
+impl<Renderer> Default for AccessoryContent<Renderer>
+where
+ Renderer: text::Renderer,
+{
+ fn default() -> Self {
+ Self::Default { size: None }
+ }
+}
+
+impl<Renderer> AccessoryContent<Renderer>
+where
+ Renderer: text::Renderer,
+{
+ fn content(&self) -> Option<(Renderer::Font, String, Option<u16>)> {
+ match self {
+ AccessoryContent::Default { size } => Some((
+ Renderer::ICON_FONT,
+ Renderer::ARROW_DOWN_ICON.to_string(),
+ *size,
+ )),
+ AccessoryContent::Custom {
+ font,
+ content,
+ size,
+ } => Some((font.clone(), content.clone(), *size)),
+ AccessoryContent::None => None,
+ }
+ }
+}
+
/// A widget for selecting a single value from a list of options.
#[allow(missing_debug_implementations)]
pub struct PickList<'a, T, Message, Renderer>
@@ -36,6 +90,7 @@ where
padding: Padding,
text_size: Option<u16>,
font: Renderer::Font,
+ accessory_content: AccessoryContent<Renderer>,
style: <Renderer::Theme as StyleSheet>::Style,
}
@@ -67,9 +122,10 @@ where
placeholder: None,
selected,
width: Length::Shrink,
- text_size: None,
padding: Self::DEFAULT_PADDING,
+ text_size: None,
font: Default::default(),
+ accessory_content: Default::default(),
style: Default::default(),
}
}
@@ -104,6 +160,15 @@ where
self
}
+ /// Sets the [`AccessoryContent`] of the [`PickList`].
+ pub fn accessory_content(
+ mut self,
+ accessory_content: AccessoryContent<Renderer>,
+ ) -> Self {
+ self.accessory_content = accessory_content;
+ self
+ }
+
/// Sets the style of the [`PickList`].
pub fn style(
mut self,
@@ -541,19 +606,24 @@ pub fn draw<T, Renderer>(
style.background,
);
- renderer.fill_text(Text {
- content: &Renderer::ARROW_DOWN_ICON.to_string(),
- font: Renderer::ICON_FONT,
- size: bounds.height * style.icon_size,
- bounds: Rectangle {
- x: bounds.x + bounds.width - f32::from(padding.horizontal()),
- y: bounds.center_y(),
- ..bounds
- },
- color: style.text_color,
- horizontal_alignment: alignment::Horizontal::Right,
- vertical_alignment: alignment::Vertical::Center,
- });
+ if let Some((font, content, size)) = accessory_content.content() {
+ let size = f32::from(size.unwrap_or_else(|| renderer.default_size()));
+
+ renderer.fill_text(Text {
+ content: &content,
+ size,
+ font,
+ color: style.text_color,
+ bounds: Rectangle {
+ x: bounds.x + bounds.width - f32::from(padding.horizontal()),
+ y: bounds.center_y() - size / 2.0,
+ height: size,
+ ..bounds
+ },
+ horizontal_alignment: alignment::Horizontal::Right,
+ vertical_alignment: alignment::Vertical::Top,
+ });
+ }
let label = selected.map(ToString::to_string);
diff --git a/src/widget.rs b/src/widget.rs
index 7c67a599..505ae799 100644
--- a/src/widget.rs
+++ b/src/widget.rs
@@ -80,7 +80,9 @@ pub mod pane_grid {
pub mod pick_list {
//! Display a dropdown list of selectable values.
- pub use iced_native::widget::pick_list::{Appearance, StyleSheet};
+ pub use iced_native::widget::pick_list::{
+ AccessoryContent, Appearance, StyleSheet,
+ };
/// A widget allowing the selection of a single value from a list of options.
pub type PickList<'a, T, Message, Renderer = crate::Renderer> =
diff --git a/style/src/pick_list.rs b/style/src/pick_list.rs
index 8d93dff2..103cc026 100644
--- a/style/src/pick_list.rs
+++ b/style/src/pick_list.rs
@@ -16,8 +16,6 @@ pub struct Appearance {
pub border_width: f32,
/// The border color of the pick list.
pub border_color: Color,
- /// The size of the arrow icon of the pick list.
- pub icon_size: f32,
}
/// A set of rules that dictate the style of a container.
diff --git a/style/src/theme.rs b/style/src/theme.rs
index d7ebb827..3b38c2c1 100644
--- a/style/src/theme.rs
+++ b/style/src/theme.rs
@@ -537,7 +537,6 @@ impl pick_list::StyleSheet for Theme {
border_radius: 2.0,
border_width: 1.0,
border_color: palette.background.strong.color,
- icon_size: 0.7,
}
}
PickList::Custom(custom, _) => custom.active(self),
@@ -556,7 +555,6 @@ impl pick_list::StyleSheet for Theme {
border_radius: 2.0,
border_width: 1.0,
border_color: palette.primary.strong.color,
- icon_size: 0.7,
}
}
PickList::Custom(custom, _) => custom.active(self),