improv(widget): share object-select svg handle between widgets using it

This commit is contained in:
Michael Aaron Murphy 2024-02-22 21:05:21 +01:00 committed by Michael Murphy
parent 552077bd8e
commit bd353c6b54
5 changed files with 51 additions and 71 deletions

View file

@ -32,7 +32,6 @@ enum Variant<Message> {
Normal,
Image {
close_icon: svg::Handle,
selection_icon: svg::Handle,
on_remove: Option<Message>,
},
}
@ -117,14 +116,6 @@ where
let bytes: &'static [u8] = &[];
iced_core::svg::Handle::from_memory(bytes)
}),
selection_icon: crate::widget::icon::from_name("object-select-symbolic")
.size(16)
.icon()
.into_svg_handle()
.unwrap_or_else(|| {
let bytes: &'static [u8] = &[];
iced_core::svg::Handle::from_memory(bytes)
}),
},
}
}
@ -380,7 +371,6 @@ where
if let Variant::Image {
close_icon,
selection_icon,
on_remove,
} = &self.variant
{
@ -407,14 +397,14 @@ where
.into(),
..Default::default()
},
shadow: Default::default(),
shadow: Shadow::default(),
},
selection_background,
);
iced_core::svg::Renderer::draw(
renderer,
selection_icon.clone(),
crate::widget::common::object_select().clone(),
styling.icon_color,
Rectangle {
width: 16.0,
@ -432,7 +422,7 @@ where
renderer.fill_quad(
renderer::Quad {
bounds,
shadow: Default::default(),
shadow: Shadow::default(),
border: Border {
radius: c_rad.radius_m.into(),
..Default::default()
@ -731,7 +721,7 @@ where
color: styling.outline_color,
radius: styling.border_radius,
},
shadow: Default::default(),
shadow: Shadow::default(),
},
Color::TRANSPARENT,
);
@ -767,7 +757,7 @@ where
radius: styling.border_radius,
..Default::default()
},
shadow: Default::default(),
shadow: Shadow::default(),
},
background,
);
@ -785,7 +775,7 @@ where
color: styling.border_color,
radius: styling.border_radius,
},
shadow: Default::default(),
shadow: Shadow::default(),
},
Color::TRANSPARENT,
);

18
src/widget/common.rs Normal file
View file

@ -0,0 +1,18 @@
use crate::widget::svg;
use std::sync::OnceLock;
/// Static `svg::Handle` to the `object-select-symbolic` icon.
pub fn object_select() -> &'static svg::Handle {
static SELECTION_ICON: OnceLock<svg::Handle> = OnceLock::new();
SELECTION_ICON.get_or_init(|| {
crate::widget::icon::from_name("object-select-symbolic")
.size(16)
.icon()
.into_svg_handle()
.unwrap_or_else(|| {
let bytes: &'static [u8] = &[];
iced_core::svg::Handle::from_memory(bytes)
})
})
}

View file

@ -5,9 +5,7 @@
mod appearance;
pub use appearance::{Appearance, StyleSheet};
use std::ffi::OsStr;
use crate::widget::{icon, Container};
use crate::widget::Container;
use iced_core::event::{self, Event};
use iced_core::layout::{self, Layout};
use iced_core::text::{self, Text};
@ -149,8 +147,6 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
style,
} = menu;
let selected_icon = icon::from_name("object-select-symbolic").size(16).handle();
let mut container = Container::new(Scrollable::new(List {
options,
hovered_option,
@ -160,14 +156,6 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
text_size,
text_line_height,
padding,
selected_icon: match selected_icon.data {
icon::Data::Name(named) => named
.path()
.filter(|path| path.extension().is_some_and(|ext| ext == OsStr::new("svg")))
.map(iced_core::svg::Handle::from_path),
icon::Data::Svg(handle) => Some(handle),
icon::Data::Image(_) => None,
},
}));
container = container
@ -286,7 +274,6 @@ struct List<'a, S: AsRef<str>, Message> {
padding: Padding,
text_size: Option<f32>,
text_line_height: text::LineHeight,
selected_icon: Option<svg::Handle>,
}
impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
@ -460,19 +447,17 @@ impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
appearance.selected_background,
);
if let Some(handle) = self.selected_icon.clone() {
svg::Renderer::draw(
renderer,
handle,
Some(appearance.selected_text_color),
Rectangle {
x: item_x + item_width - 16.0 - 8.0,
y: bounds.y + (bounds.height / 2.0 - 8.0),
width: 16.0,
height: 16.0,
},
);
}
svg::Renderer::draw(
renderer,
crate::widget::common::object_select().clone(),
Some(appearance.selected_text_color),
Rectangle {
x: item_x + item_width - 16.0 - 8.0,
y: bounds.y + (bounds.height / 2.0 - 8.0),
width: 16.0,
height: 16.0,
},
);
(appearance.selected_text_color, crate::font::FONT_SEMIBOLD)
} else if *self.hovered_option == Some(i) {

View file

@ -1,9 +1,7 @@
use super::Model;
pub use crate::widget::dropdown::menu::{Appearance, StyleSheet};
use std::ffi::OsStr;
use crate::widget::{icon, Container};
use crate::widget::Container;
use iced_core::event::{self, Event};
use iced_core::layout::{self, Layout};
use iced_core::text::{self, Text};
@ -152,25 +150,15 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
style,
} = menu;
let selected_icon = icon::from_name("object-select-symbolic").size(16).handle();
let mut container = Container::new(Scrollable::new(InnerList {
options,
hovered_option,
selected_option,
on_selected,
on_option_hovered,
padding,
text_size,
text_line_height,
padding,
selected_icon: match selected_icon.data {
icon::Data::Name(named) => named
.path()
.filter(|path| path.extension().is_some_and(|ext| ext == OsStr::new("svg")))
.map(iced_core::svg::Handle::from_path),
icon::Data::Svg(handle) => Some(handle),
icon::Data::Image(_) => None,
},
}));
container = container
@ -291,7 +279,6 @@ struct InnerList<'a, S, Item, Message> {
padding: Padding,
text_size: Option<f32>,
text_line_height: text::LineHeight,
selected_icon: Option<svg::Handle>,
}
impl<'a, S, Item, Message> Widget<Message, crate::Theme, crate::Renderer>
@ -550,19 +537,17 @@ where
appearance.selected_background,
);
if let Some(handle) = self.selected_icon.clone() {
svg::Renderer::draw(
renderer,
handle,
Some(appearance.selected_text_color),
Rectangle {
x: item_x + item_width - 16.0 - 8.0,
y: bounds.y + (bounds.height / 2.0 - 8.0),
width: 16.0,
height: 16.0,
},
);
}
svg::Renderer::draw(
renderer,
crate::widget::common::object_select().clone(),
Some(appearance.selected_text_color),
Rectangle {
x: item_x + item_width - 16.0 - 8.0,
y: bounds.y + (bounds.height / 2.0 - 8.0),
width: 16.0,
height: 16.0,
},
);
(appearance.selected_text_color, crate::font::FONT_SEMIBOLD)
} else if self.hovered_option.as_ref() == Some(item) {

View file

@ -24,6 +24,8 @@ pub mod aspect_ratio;
pub mod button;
pub use button::{button, Button, IconButton, LinkButton, TextButton};
pub(crate) mod common;
pub mod card;
pub use card::*;