rust_kanban/ui/widgets/
close_button.rs

1use crate::{
2    app::{state::Focus, App},
3    ui::{widgets::Widget, TextColorOptions},
4    util::lerp_between,
5};
6use ratatui::style::{Color, Style};
7use std::time::{Duration, Instant};
8
9#[derive(Debug)]
10pub struct CloseButtonWidget {
11    start_time: Instant,
12    fade_time: f32,
13    pub color: (u8, u8, u8),
14    offset: f32,
15}
16
17impl CloseButtonWidget {
18    pub fn new(style: Style) -> Self {
19        let color = style.fg.unwrap_or(Color::White);
20        let text_color = TextColorOptions::from(color).to_rgb();
21        Self {
22            start_time: Instant::now(),
23            fade_time: 1.0,
24            color: text_color,
25            offset: 0.8,
26        }
27    }
28}
29
30impl Widget for CloseButtonWidget {
31    fn update(app: &mut App) {
32        if app.state.focus == Focus::CloseButton {
33            let theme = app.current_theme.clone();
34            let disable_animations = app.config.disable_animations;
35            let widget = &mut app.widgets.close_button;
36            if disable_animations {
37                widget.color = TextColorOptions::from(theme.error_text_style.bg.unwrap()).to_rgb();
38                return;
39            }
40
41            let normal_color = TextColorOptions::from(theme.general_style.bg.unwrap()).to_rgb();
42            let hover_color = TextColorOptions::from(theme.error_text_style.bg.unwrap()).to_rgb();
43            let total_duration = Duration::from_millis((widget.fade_time * 1000.0) as u64);
44            let half_duration = Duration::from_millis((widget.fade_time * 500.0) as u64);
45
46            if widget.start_time.elapsed() > total_duration {
47                widget.start_time = Instant::now();
48            }
49
50            let mut t = (widget.start_time.elapsed().as_millis() as f32
51                / total_duration.as_millis() as f32)
52                + widget.offset; // offset to make it overall brighter
53
54            if widget.start_time.elapsed() < half_duration {
55                widget.color = lerp_between(normal_color, hover_color, t);
56            } else {
57                t = t - widget.fade_time - (widget.offset / 4.0); // offset to make it overall brighter
58                widget.color = lerp_between(hover_color, normal_color, t);
59            }
60        }
61    }
62}