1use crate::theme::{self, spacing, surface, FontScale};
22use crate::widgets::scaled_text;
23use iced::widget::{container, row, stack, Space};
24use iced::{gradient, Background, Border, Color, Element, Length, Padding, Radians, Shadow, Vector};
25
26#[derive(Debug, Clone, Copy, PartialEq, Default)]
28pub enum DropLook {
29 #[default]
31 Idle,
32 Over { opening: f32 },
36}
37
38const OPENING_ALPHA: f32 = 0.16;
41
42pub fn drop_target_style(look: DropLook) -> container::Style {
44 let radius = crate::density::inner_radius();
45 let DropLook::Over { opening } = look else {
46 return container::Style {
47 border: Border { radius: radius.into(), ..Border::default() },
48 ..container::Style::default()
49 };
50 };
51 let opening = opening.clamp(0.0, 1.0);
52 let filled = Color { a: OPENING_ALPHA, ..theme::text() };
53 let background = if opening <= 0.0 {
54 Background::Color(surface::row())
55 } else {
56 Background::Gradient(
59 gradient::Linear::new(Radians(std::f32::consts::FRAC_PI_2))
60 .add_stop(0.0, filled)
61 .add_stop(opening, filled)
62 .add_stop(opening, surface::row())
63 .add_stop(1.0, surface::row())
64 .into(),
65 )
66 };
67 container::Style {
68 background: Some(background),
69 border: Border { color: Color { a: 0.85, ..theme::text() }, width: 1.5, radius: radius.into() },
70 ..container::Style::default()
71 }
72}
73
74#[derive(Debug, Clone, PartialEq)]
76pub struct DragCard {
77 pub label: String,
79 pub count: usize,
82 pub copying: bool,
85}
86
87pub fn drag_card<'a, Message: 'a>(card: &DragCard, lift: f32, scale: FontScale) -> Element<'a, Message> {
92 let lift = lift.clamp(0.0, 1.0);
93 let fade = move |c: Color| Color { a: c.a * lift, ..c };
94 let radius = crate::density::inner_radius();
95 let face = move |shadow: bool| {
96 move |_: &iced::Theme| container::Style {
97 background: Some(Background::Color(fade(surface::sidebar()))),
98 text_color: Some(fade(theme::text())),
99 border: Border { color: fade(surface::card_border()), width: 1.0, radius: radius.into() },
100 shadow: if shadow {
104 Shadow {
105 color: fade(Color { a: 0.55, ..surface::root() }),
106 offset: Vector::new(0.0, 2.0 + 6.0 * lift),
107 blur_radius: 4.0 + 14.0 * lift,
108 }
109 } else {
110 Shadow::default()
111 },
112 ..container::Style::default()
113 }
114 };
115 let label = scaled_text(card.label.clone(), 13.0, scale).wrapping(iced::widget::text::Wrapping::None);
116 let mut front = row![label].spacing(spacing::SM).align_y(iced::Alignment::Center);
117 if card.copying {
118 front = front.push(badge("+".to_string(), fade(theme::success()), scale));
119 }
120 if card.count > 1 {
121 front = front.push(badge(card.count.to_string(), fade(crate::color::to_iced(theme::active().accent)), scale));
122 }
123 let top = container(front).padding([spacing::XS + 2.0, spacing::SM + 4.0]).style(face(true));
124 if card.count <= 1 {
125 return container(top).padding(Padding { right: 8.0, bottom: 8.0, ..Padding::ZERO }).into();
126 }
127 let behind = |offset: f32| {
130 container(container(Space::new().width(Length::Fill).height(Length::Fill)).style(face(false)))
131 .padding(Padding { left: offset, top: offset, right: 8.0 - offset, bottom: 8.0 - offset })
132 .width(Length::Fill)
133 .height(Length::Fill)
134 };
135 stack![
136 behind(8.0),
137 behind(4.0),
138 container(top).padding(Padding { right: 8.0, bottom: 8.0, ..Padding::ZERO }),
139 ]
140 .into()
141}
142
143fn badge<'a, Message: 'a>(text: String, fill: Color, scale: FontScale) -> Element<'a, Message> {
145 container(scaled_text(text, 11.0, scale))
146 .padding([1.0, 6.0])
147 .style(move |_: &iced::Theme| container::Style {
148 background: Some(Background::Color(fill)),
149 text_color: Some(Color { a: fill.a, ..surface::root() }),
150 border: Border { radius: 999.0.into(), ..Border::default() },
151 ..container::Style::default()
152 })
153 .into()
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[test]
161 fn a_target_nothing_is_over_draws_nothing() {
162 let style = drop_target_style(DropLook::Idle);
163 assert!(style.background.is_none());
164 assert_eq!(style.border.width, 0.0);
165 }
166
167 #[test]
168 fn a_target_under_a_drag_has_an_outline_a_hover_does_not() {
169 let style = drop_target_style(DropLook::Over { opening: 0.0 });
170 assert!(style.border.width > 0.0);
171 }
172
173 #[test]
177 fn a_drop_target_never_uses_the_accent() {
178 let accent = crate::color::to_iced(theme::active().accent);
179 for opening in [0.0, 0.5, 1.0] {
180 let style = drop_target_style(DropLook::Over { opening });
181 assert_ne!(style.border.color, accent);
182 if let Some(Background::Color(c)) = style.background {
183 assert_ne!(c, accent);
184 }
185 }
186 }
187
188 #[test]
189 fn an_opening_target_fills_as_far_as_the_wait_has_gone() {
190 let Some(Background::Gradient(iced::Gradient::Linear(linear))) =
191 drop_target_style(DropLook::Over { opening: 0.4 }).background
192 else {
193 panic!("an opening target is filled part of the way")
194 };
195 let edges: Vec<f32> = linear.stops.iter().flatten().map(|s| s.offset).collect();
196 assert!(edges.contains(&0.4), "{edges:?}");
197 }
198}