aboutsummaryrefslogtreecommitdiff
path: root/karlgui/src/helper.rs
blob: 4f8f4f49df93bfece1c65e90148c503857036e7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use chrono::NaiveDateTime;
use egui::{DragValue, Ui};
use karlcommon::Property;

pub fn from_timestamp(t: i64) -> NaiveDateTime {
    NaiveDateTime::from_timestamp(t, 0)
}

pub fn format_value(prop: Property, value: i64) -> String {
    match prop {
        Property::Year => format!("{value}"),
        Property::Monthofyear => format!("{}", month_to_str(value)),
        Property::Weekofmonth => format!("{value}"),
        Property::Dayofyear => format!("{value}"),
        Property::Dayofmonth => format!("{value}"),
        Property::Dayofweek => format!("{}", weekday_to_str(value)),
        Property::Hour => format!("{value}h"),
        Property::Minute => format!("{value}min"),
        Property::Second => format!("{value}s"),
        Property::Unix => format!("{value}s"),
    }
}

pub fn edit_value(ui: &mut Ui, prop: Property, value: &mut i64) {
    match prop {
        Property::Year => {
            ui.add(DragValue::new(value));
        }
        Property::Monthofyear => {
            egui::ComboBox::from_id_source(ui.id())
                .selected_text(format_value(prop, *value))
                .show_ui(ui, |ui| {
                    for v in 0..12 {
                        ui.selectable_value(value, v, format_value(prop, v));
                    }
                });
        }
        Property::Dayofweek => {
            egui::ComboBox::from_id_source(ui.id())
                .selected_text(format_value(prop, *value))
                .show_ui(ui, |ui| {
                    for v in 0..7 {
                        ui.selectable_value(value, v, format_value(prop, v));
                    }
                });
        }
        Property::Weekofmonth => {
            ui.add(DragValue::new(value).clamp_range(0..=5));
        }
        Property::Dayofyear => {
            ui.add(DragValue::new(value).clamp_range(0..=366));
        }
        Property::Dayofmonth => {
            ui.add(DragValue::new(value).clamp_range(0..=31));
        }
        Property::Hour => {
            ui.add(DragValue::new(value).clamp_range(0..=23).suffix("h"));
        }
        Property::Minute => {
            ui.add(DragValue::new(value).clamp_range(0..=59).suffix("m"));
        }
        Property::Second => {
            ui.add(DragValue::new(value).clamp_range(0..=59).suffix("s"));
        }
        Property::Unix => {
            ui.add(DragValue::new(value).suffix("s"));
        }
    }
}

pub fn weekday_to_str(value: i64) -> &'static str {
    match value {
        0 => "Monday",
        1 => "Thuesday",
        2 => "Wednesday",
        3 => "Thursday",
        4 => "Friday",
        5 => "Saturday",
        6 => "Sunday",
        _ => "(invalid)",
    }
}
pub fn month_to_str(value: i64) -> &'static str {
    match value {
        0 => "January",
        1 => "February",
        2 => "March",
        3 => "April",
        4 => "May",
        5 => "June",
        6 => "July",
        7 => "August",
        8 => "September",
        9 => "October",
        10 => "November",
        11 => "December",
        _ => "(invalid)",
    }
}