improv(widget): share object-select svg handle between widgets using it
This commit is contained in:
parent
552077bd8e
commit
bd353c6b54
5 changed files with 51 additions and 71 deletions
|
|
@ -32,7 +32,6 @@ enum Variant<Message> {
|
||||||
Normal,
|
Normal,
|
||||||
Image {
|
Image {
|
||||||
close_icon: svg::Handle,
|
close_icon: svg::Handle,
|
||||||
selection_icon: svg::Handle,
|
|
||||||
on_remove: Option<Message>,
|
on_remove: Option<Message>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -117,14 +116,6 @@ where
|
||||||
let bytes: &'static [u8] = &[];
|
let bytes: &'static [u8] = &[];
|
||||||
iced_core::svg::Handle::from_memory(bytes)
|
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 {
|
if let Variant::Image {
|
||||||
close_icon,
|
close_icon,
|
||||||
selection_icon,
|
|
||||||
on_remove,
|
on_remove,
|
||||||
} = &self.variant
|
} = &self.variant
|
||||||
{
|
{
|
||||||
|
|
@ -407,14 +397,14 @@ where
|
||||||
.into(),
|
.into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
shadow: Default::default(),
|
shadow: Shadow::default(),
|
||||||
},
|
},
|
||||||
selection_background,
|
selection_background,
|
||||||
);
|
);
|
||||||
|
|
||||||
iced_core::svg::Renderer::draw(
|
iced_core::svg::Renderer::draw(
|
||||||
renderer,
|
renderer,
|
||||||
selection_icon.clone(),
|
crate::widget::common::object_select().clone(),
|
||||||
styling.icon_color,
|
styling.icon_color,
|
||||||
Rectangle {
|
Rectangle {
|
||||||
width: 16.0,
|
width: 16.0,
|
||||||
|
|
@ -432,7 +422,7 @@ where
|
||||||
renderer.fill_quad(
|
renderer.fill_quad(
|
||||||
renderer::Quad {
|
renderer::Quad {
|
||||||
bounds,
|
bounds,
|
||||||
shadow: Default::default(),
|
shadow: Shadow::default(),
|
||||||
border: Border {
|
border: Border {
|
||||||
radius: c_rad.radius_m.into(),
|
radius: c_rad.radius_m.into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
|
@ -731,7 +721,7 @@ where
|
||||||
color: styling.outline_color,
|
color: styling.outline_color,
|
||||||
radius: styling.border_radius,
|
radius: styling.border_radius,
|
||||||
},
|
},
|
||||||
shadow: Default::default(),
|
shadow: Shadow::default(),
|
||||||
},
|
},
|
||||||
Color::TRANSPARENT,
|
Color::TRANSPARENT,
|
||||||
);
|
);
|
||||||
|
|
@ -767,7 +757,7 @@ where
|
||||||
radius: styling.border_radius,
|
radius: styling.border_radius,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
shadow: Default::default(),
|
shadow: Shadow::default(),
|
||||||
},
|
},
|
||||||
background,
|
background,
|
||||||
);
|
);
|
||||||
|
|
@ -785,7 +775,7 @@ where
|
||||||
color: styling.border_color,
|
color: styling.border_color,
|
||||||
radius: styling.border_radius,
|
radius: styling.border_radius,
|
||||||
},
|
},
|
||||||
shadow: Default::default(),
|
shadow: Shadow::default(),
|
||||||
},
|
},
|
||||||
Color::TRANSPARENT,
|
Color::TRANSPARENT,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
18
src/widget/common.rs
Normal file
18
src/widget/common.rs
Normal 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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -5,9 +5,7 @@
|
||||||
mod appearance;
|
mod appearance;
|
||||||
pub use appearance::{Appearance, StyleSheet};
|
pub use appearance::{Appearance, StyleSheet};
|
||||||
|
|
||||||
use std::ffi::OsStr;
|
use crate::widget::Container;
|
||||||
|
|
||||||
use crate::widget::{icon, Container};
|
|
||||||
use iced_core::event::{self, Event};
|
use iced_core::event::{self, Event};
|
||||||
use iced_core::layout::{self, Layout};
|
use iced_core::layout::{self, Layout};
|
||||||
use iced_core::text::{self, Text};
|
use iced_core::text::{self, Text};
|
||||||
|
|
@ -149,8 +147,6 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
|
||||||
style,
|
style,
|
||||||
} = menu;
|
} = menu;
|
||||||
|
|
||||||
let selected_icon = icon::from_name("object-select-symbolic").size(16).handle();
|
|
||||||
|
|
||||||
let mut container = Container::new(Scrollable::new(List {
|
let mut container = Container::new(Scrollable::new(List {
|
||||||
options,
|
options,
|
||||||
hovered_option,
|
hovered_option,
|
||||||
|
|
@ -160,14 +156,6 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
|
||||||
text_size,
|
text_size,
|
||||||
text_line_height,
|
text_line_height,
|
||||||
padding,
|
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
|
container = container
|
||||||
|
|
@ -286,7 +274,6 @@ struct List<'a, S: AsRef<str>, Message> {
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
text_size: Option<f32>,
|
text_size: Option<f32>,
|
||||||
text_line_height: text::LineHeight,
|
text_line_height: text::LineHeight,
|
||||||
selected_icon: Option<svg::Handle>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S: AsRef<str>, Message> Widget<Message, crate::Theme, crate::Renderer>
|
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,
|
appearance.selected_background,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(handle) = self.selected_icon.clone() {
|
svg::Renderer::draw(
|
||||||
svg::Renderer::draw(
|
renderer,
|
||||||
renderer,
|
crate::widget::common::object_select().clone(),
|
||||||
handle,
|
Some(appearance.selected_text_color),
|
||||||
Some(appearance.selected_text_color),
|
Rectangle {
|
||||||
Rectangle {
|
x: item_x + item_width - 16.0 - 8.0,
|
||||||
x: item_x + item_width - 16.0 - 8.0,
|
y: bounds.y + (bounds.height / 2.0 - 8.0),
|
||||||
y: bounds.y + (bounds.height / 2.0 - 8.0),
|
width: 16.0,
|
||||||
width: 16.0,
|
height: 16.0,
|
||||||
height: 16.0,
|
},
|
||||||
},
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
(appearance.selected_text_color, crate::font::FONT_SEMIBOLD)
|
(appearance.selected_text_color, crate::font::FONT_SEMIBOLD)
|
||||||
} else if *self.hovered_option == Some(i) {
|
} else if *self.hovered_option == Some(i) {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
use super::Model;
|
use super::Model;
|
||||||
pub use crate::widget::dropdown::menu::{Appearance, StyleSheet};
|
pub use crate::widget::dropdown::menu::{Appearance, StyleSheet};
|
||||||
|
|
||||||
use std::ffi::OsStr;
|
use crate::widget::Container;
|
||||||
|
|
||||||
use crate::widget::{icon, Container};
|
|
||||||
use iced_core::event::{self, Event};
|
use iced_core::event::{self, Event};
|
||||||
use iced_core::layout::{self, Layout};
|
use iced_core::layout::{self, Layout};
|
||||||
use iced_core::text::{self, Text};
|
use iced_core::text::{self, Text};
|
||||||
|
|
@ -152,25 +150,15 @@ impl<'a, Message: 'a> Overlay<'a, Message> {
|
||||||
style,
|
style,
|
||||||
} = menu;
|
} = menu;
|
||||||
|
|
||||||
let selected_icon = icon::from_name("object-select-symbolic").size(16).handle();
|
|
||||||
|
|
||||||
let mut container = Container::new(Scrollable::new(InnerList {
|
let mut container = Container::new(Scrollable::new(InnerList {
|
||||||
options,
|
options,
|
||||||
hovered_option,
|
hovered_option,
|
||||||
selected_option,
|
selected_option,
|
||||||
on_selected,
|
on_selected,
|
||||||
on_option_hovered,
|
on_option_hovered,
|
||||||
|
padding,
|
||||||
text_size,
|
text_size,
|
||||||
text_line_height,
|
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
|
container = container
|
||||||
|
|
@ -291,7 +279,6 @@ struct InnerList<'a, S, Item, Message> {
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
text_size: Option<f32>,
|
text_size: Option<f32>,
|
||||||
text_line_height: text::LineHeight,
|
text_line_height: text::LineHeight,
|
||||||
selected_icon: Option<svg::Handle>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S, Item, Message> Widget<Message, crate::Theme, crate::Renderer>
|
impl<'a, S, Item, Message> Widget<Message, crate::Theme, crate::Renderer>
|
||||||
|
|
@ -550,19 +537,17 @@ where
|
||||||
appearance.selected_background,
|
appearance.selected_background,
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(handle) = self.selected_icon.clone() {
|
svg::Renderer::draw(
|
||||||
svg::Renderer::draw(
|
renderer,
|
||||||
renderer,
|
crate::widget::common::object_select().clone(),
|
||||||
handle,
|
Some(appearance.selected_text_color),
|
||||||
Some(appearance.selected_text_color),
|
Rectangle {
|
||||||
Rectangle {
|
x: item_x + item_width - 16.0 - 8.0,
|
||||||
x: item_x + item_width - 16.0 - 8.0,
|
y: bounds.y + (bounds.height / 2.0 - 8.0),
|
||||||
y: bounds.y + (bounds.height / 2.0 - 8.0),
|
width: 16.0,
|
||||||
width: 16.0,
|
height: 16.0,
|
||||||
height: 16.0,
|
},
|
||||||
},
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
(appearance.selected_text_color, crate::font::FONT_SEMIBOLD)
|
(appearance.selected_text_color, crate::font::FONT_SEMIBOLD)
|
||||||
} else if self.hovered_option.as_ref() == Some(item) {
|
} else if self.hovered_option.as_ref() == Some(item) {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,8 @@ pub mod aspect_ratio;
|
||||||
pub mod button;
|
pub mod button;
|
||||||
pub use button::{button, Button, IconButton, LinkButton, TextButton};
|
pub use button::{button, Button, IconButton, LinkButton, TextButton};
|
||||||
|
|
||||||
|
pub(crate) mod common;
|
||||||
|
|
||||||
pub mod card;
|
pub mod card;
|
||||||
pub use card::*;
|
pub use card::*;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue