-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbutton.rs
155 lines (136 loc) · 5.17 KB
/
button.rs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use super::popup::DatePickerPopup;
use chrono::NaiveDate;
use egui::{Area, Button, Frame, InnerResponse, Key, Order, RichText, Ui, Widget};
#[derive(Default, Clone, serde::Deserialize, serde::Serialize)]
pub(crate) struct DatePickerButtonState {
pub picker_visible: bool,
}
/// Shows a date, and will open a date picker popup when clicked.
pub struct DatePickerButton<'a> {
selection: &'a mut NaiveDate,
id_source: Option<&'a str>,
combo_boxes: bool,
arrows: bool,
calendar: bool,
calendar_week: bool,
show_icon: bool,
}
impl<'a> DatePickerButton<'a> {
pub fn new(selection: &'a mut NaiveDate) -> Self {
Self {
selection,
id_source: None,
combo_boxes: true,
arrows: true,
calendar: true,
calendar_week: true,
show_icon: true,
}
}
/// Add id source.
/// Must be set if multiple date picker buttons are in the same Ui.
pub fn id_source(mut self, id_source: &'a str) -> Self {
self.id_source = Some(id_source);
self
}
/// Show combo boxes in date picker popup. (Default: true)
pub fn combo_boxes(mut self, combo_boxes: bool) -> Self {
self.combo_boxes = combo_boxes;
self
}
/// Show arrows in date picker popup. (Default: true)
pub fn arrows(mut self, arrows: bool) -> Self {
self.arrows = arrows;
self
}
/// Show calendar in date picker popup. (Default: true)
pub fn calendar(mut self, calendar: bool) -> Self {
self.calendar = calendar;
self
}
/// Show calendar week in date picker popup. (Default: true)
pub fn calendar_week(mut self, week: bool) -> Self {
self.calendar_week = week;
self
}
/// Show the calendar icon on the button. (Default: true)
pub fn show_icon(mut self, show_icon: bool) -> Self {
self.show_icon = show_icon;
self
}
}
impl<'a> Widget for DatePickerButton<'a> {
fn ui(self, ui: &mut Ui) -> egui::Response {
let id = ui.make_persistent_id(self.id_source);
let mut button_state = ui
.memory_mut(|mem| mem.data.get_persisted::<DatePickerButtonState>(id))
.unwrap_or_default();
let mut text = if self.show_icon {
RichText::new(format!("{} 📆", self.selection.format("%Y-%m-%d")))
} else {
RichText::new(format!("{}", self.selection.format("%Y-%m-%d")))
};
let visuals = ui.visuals().widgets.open;
if button_state.picker_visible {
text = text.color(visuals.text_color());
}
let mut button = Button::new(text);
if button_state.picker_visible {
button = button.fill(visuals.weak_bg_fill).stroke(visuals.bg_stroke);
}
let mut button_response = ui.add(button);
if button_response.clicked() {
button_state.picker_visible = true;
ui.memory_mut(|mem| mem.data.insert_persisted(id, button_state.clone()));
}
if button_state.picker_visible {
let width = 333.0;
let mut pos = button_response.rect.left_bottom();
let width_with_padding = width
+ ui.style().spacing.item_spacing.x
+ ui.style().spacing.window_margin.left
+ ui.style().spacing.window_margin.right;
if pos.x + width_with_padding > ui.clip_rect().right() {
pos.x = button_response.rect.right() - width_with_padding;
}
// Check to make sure the calendar never is displayed out of window
pos.x = pos.x.max(ui.style().spacing.window_margin.left);
//TODO(elwerene): Better positioning
let InnerResponse {
inner: saved,
response: area_response,
} = Area::new(ui.make_persistent_id(self.id_source))
.order(Order::Foreground)
.fixed_pos(pos)
.constrain_to(ui.ctx().screen_rect())
.show(ui.ctx(), |ui| {
let frame = Frame::popup(ui.style());
frame
.show(ui, |ui| {
ui.set_min_width(width);
ui.set_max_width(width);
DatePickerPopup {
selection: self.selection,
button_id: id,
combo_boxes: self.combo_boxes,
arrows: self.arrows,
calendar: self.calendar,
calendar_week: self.calendar_week,
}
.draw(ui)
})
.inner
});
if saved {
button_response.mark_changed();
}
if !button_response.clicked()
&& (ui.input(|i| i.key_pressed(Key::Escape)) || area_response.clicked_elsewhere())
{
button_state.picker_visible = false;
ui.memory_mut(|mem| mem.data.insert_persisted(id, button_state));
}
}
button_response
}
}