feat(segmented-button): manually-definable icon colors
This commit is contained in:
parent
4fa61eeafd
commit
75e80857e2
5 changed files with 53 additions and 6 deletions
|
|
@ -97,3 +97,11 @@ pub type SecondaryMap<T> = slotmap::SecondaryMap<Entity, T>;
|
||||||
///
|
///
|
||||||
/// Sparse maps internally use a `HashMap`, for data that is sparsely associated.
|
/// Sparse maps internally use a `HashMap`, for data that is sparsely associated.
|
||||||
pub type SparseSecondaryMap<T> = slotmap::SparseSecondaryMap<Entity, T>;
|
pub type SparseSecondaryMap<T> = slotmap::SparseSecondaryMap<Entity, T>;
|
||||||
|
|
||||||
|
/// Defines the color of the icon for a segmented item.
|
||||||
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||||
|
enum IconColor {
|
||||||
|
#[default]
|
||||||
|
None,
|
||||||
|
Color(crate::iced::Color),
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright 2023 System76 <info@system76.com>
|
// Copyright 2023 System76 <info@system76.com>
|
||||||
// SPDX-License-Identifier: MPL-2.0
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
|
use iced::Color;
|
||||||
use slotmap::{SecondaryMap, SparseSecondaryMap};
|
use slotmap::{SecondaryMap, SparseSecondaryMap};
|
||||||
|
|
||||||
use super::{Entity, Model, Selectable};
|
use super::{Entity, Model, Selectable};
|
||||||
|
|
@ -108,6 +109,13 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Defines the color of an icon.
|
||||||
|
#[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)]
|
||||||
|
pub fn icon_color(mut self, icon: Option<Color>) -> Self {
|
||||||
|
self.model.0.icon_color_set(self.id, icon);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Define the position of the newly-inserted item.
|
/// Define the position of the newly-inserted item.
|
||||||
#[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)]
|
#[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)]
|
||||||
pub fn position(mut self, position: u16) -> Self {
|
pub fn position(mut self, position: u16) -> Self {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
use iced::Color;
|
||||||
use slotmap::{SecondaryMap, SparseSecondaryMap};
|
use slotmap::{SecondaryMap, SparseSecondaryMap};
|
||||||
|
|
||||||
use crate::widget::IconSource;
|
use crate::widget::IconSource;
|
||||||
|
|
@ -94,6 +95,13 @@ where
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Define the color for the icon.
|
||||||
|
#[allow(clippy::must_use_candidate, clippy::return_self_not_must_use)]
|
||||||
|
pub fn icon_color(self, icon: Option<Color>) -> Self {
|
||||||
|
self.model.icon_color_set(self.id, icon);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the ID of the item that was inserted.
|
/// Returns the ID of the item that was inserted.
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,14 @@ mod selection;
|
||||||
pub use self::selection::{MultiSelect, Selectable, SingleSelect};
|
pub use self::selection::{MultiSelect, Selectable, SingleSelect};
|
||||||
|
|
||||||
use crate::widget::IconSource;
|
use crate::widget::IconSource;
|
||||||
|
use iced::Color;
|
||||||
use slotmap::{SecondaryMap, SlotMap};
|
use slotmap::{SecondaryMap, SlotMap};
|
||||||
use std::any::{Any, TypeId};
|
use std::any::{Any, TypeId};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
|
||||||
|
use super::IconColor;
|
||||||
|
|
||||||
slotmap::new_key_type! {
|
slotmap::new_key_type! {
|
||||||
/// A unique ID for an item in the [`Model`].
|
/// A unique ID for an item in the [`Model`].
|
||||||
pub struct Entity;
|
pub struct Entity;
|
||||||
|
|
@ -244,6 +247,24 @@ where
|
||||||
self.icons.insert(id, icon.into())
|
self.icons.insert(id, icon.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the color of the icon. By default, the color matches the text.
|
||||||
|
pub fn icon_color_set(&mut self, id: Entity, color: Option<Color>) {
|
||||||
|
if self.contains_item(id) {
|
||||||
|
self.data_set(
|
||||||
|
id,
|
||||||
|
match color {
|
||||||
|
Some(color) => IconColor::Color(color),
|
||||||
|
None => IconColor::None,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Unsets the defined color of an icon.
|
||||||
|
pub fn icon_color_remove(&mut self, id: Entity) {
|
||||||
|
self.data_remove::<IconColor>(id);
|
||||||
|
}
|
||||||
|
|
||||||
/// Removes the icon from an item.
|
/// Removes the icon from an item.
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
use super::model::{Entity, Model, Selectable};
|
use super::model::{Entity, Model, Selectable};
|
||||||
use super::style::StyleSheet;
|
use super::style::StyleSheet;
|
||||||
|
use super::IconColor;
|
||||||
use crate::widget::{icon, IconSource};
|
use crate::widget::{icon, IconSource};
|
||||||
use derive_setters::Setters;
|
use derive_setters::Setters;
|
||||||
use iced::{
|
use iced::{
|
||||||
|
|
@ -466,6 +467,12 @@ where
|
||||||
status_appearance.middle
|
status_appearance.middle
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let icon_color = match self.model.data::<IconColor>(key).copied() {
|
||||||
|
Some(IconColor::None) => None,
|
||||||
|
Some(IconColor::Color(color)) => Some(color),
|
||||||
|
None => Some(status_appearance.text_color),
|
||||||
|
};
|
||||||
|
|
||||||
// Render the background of the button.
|
// Render the background of the button.
|
||||||
if status_appearance.background.is_some() {
|
if status_appearance.background.is_some() {
|
||||||
renderer.fill_quad(
|
renderer.fill_quad(
|
||||||
|
|
@ -529,12 +536,7 @@ where
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
icon::Handle::Svg(handle) => {
|
icon::Handle::Svg(handle) => {
|
||||||
iced_native::svg::Renderer::draw(
|
iced_native::svg::Renderer::draw(renderer, handle, icon_color, icon_bounds);
|
||||||
renderer,
|
|
||||||
handle,
|
|
||||||
Some(status_appearance.text_color),
|
|
||||||
icon_bounds,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue