vizia_core/views/
chip.rs
1use crate::{icons::ICON_X, prelude::*};
2use std::sync::Arc;
3
4#[derive(Lens)]
6pub struct Chip {
7 on_close: Option<Arc<dyn Fn(&mut EventContext) + Send + Sync>>,
8}
9
10impl Chip {
11 pub fn new<T>(cx: &mut Context, text: impl Res<T> + Clone) -> Handle<Self>
22 where
23 T: ToStringLocalized,
24 {
25 Self { on_close: None }
26 .build(cx, move |cx| {
27 Label::new(cx, text).height(Stretch(1.0)).alignment(Alignment::Left);
28 Binding::new(cx, Chip::on_close.map(|on_close| on_close.is_some()), |cx, val| {
29 if val.get(cx) {
30 let on_close = Chip::on_close.get(cx).unwrap();
31 Button::new(cx, |cx| Svg::new(cx, ICON_X))
32 .class("close-icon")
33 .height(Pixels(16.0))
34 .width(Pixels(16.0))
35 .alignment(Alignment::Center)
36 .on_press(move |cx| (on_close)(cx));
37 }
38 });
39 })
40 .toggle_class("close", Chip::on_close.map(|on_close| on_close.is_some()))
41 .layout_type(LayoutType::Row)
42 }
43}
44
45impl View for Chip {
46 fn element(&self) -> Option<&'static str> {
47 Some("chip")
48 }
49}
50
51#[derive(Debug, Clone, Copy, Data, PartialEq, Eq)]
53pub enum ChipVariant {
54 Filled,
56 Outline,
58}
59
60impl_res_simple!(ChipVariant);
61
62impl Handle<'_, Chip> {
63 pub fn on_close(self, callback: impl 'static + Fn(&mut EventContext) + Send + Sync) -> Self {
66 self.modify(|chip: &mut Chip| {
67 chip.on_close = Some(Arc::new(callback));
68 })
69 }
70
71 pub fn variant<U: Into<ChipVariant>>(self, variant: impl Res<U>) -> Self {
84 self.bind(variant, |handle, variant| {
85 let variant = variant.get(&handle).into();
86
87 match variant {
88 ChipVariant::Filled => {
89 handle.toggle_class("outline", false);
90 }
91
92 ChipVariant::Outline => {
93 handle.toggle_class("outline", true);
94 }
95 }
96 })
97 }
98}