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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
use gtk4 as gtk;
use async_channel::Sender;
use chrono::{Datelike, NaiveDate};
use gtk::glib;
use gtk::prelude::*;
use std::collections::HashMap;
use crate::{
gui::update, gui::update::Msg, gui::App, model::category::Category, model::event,
model::event::Event,
};
static DAYS: [&str; 7] = ["LUN", "MAR", "MER", "JEU", "VEN", "SAM", "DIM"];
static MONTHES: [&str; 12] = [
"Jan", "Fév", "Mar", "Avr", "Mai", "Juin", "Juil", "Aoû", "Sep", "Oct", "Nov", "Déc",
];
pub fn create(
tx: Sender<Msg>,
today: NaiveDate,
start_date: NaiveDate,
end_date: NaiveDate,
events: &[Event],
recurring_events: &[Event],
categories: &[Category],
default_color: &str,
) -> gtk::Grid {
let grid = gtk::Grid::builder().build();
for col in 0..7 {
grid.attach(&day_title(col), col, 0, 1, 1);
}
let repetitions = event::repetitions_between(recurring_events, start_date, end_date);
attach_days(tx.clone(), &grid, start_date, today, events, &repetitions, categories, default_color);
let event_controller_key = gtk::EventControllerKey::new();
event_controller_key.connect_key_released(glib::clone!(@strong tx => move |_, _, keycode, _| {
match keycode {
111 => update::send(tx.clone(), Msg::SelectPreviousWeek), // UP
116 => update::send(tx.clone(), Msg::SelectNextWeek), // DOWN
_ => ()
}
}));
grid.add_controller(&event_controller_key);
grid
}
fn attach_days(
tx: Sender<Msg>,
grid: >k::Grid,
start_date: NaiveDate,
today: NaiveDate,
events: &[Event],
repetitions: &HashMap<NaiveDate, Vec<Event>>,
categories: &[Category],
default_color: &str,
) {
let mut d = start_date;
for row in 1..5 {
for col in 0..7 {
grid.attach(
&day_entry(
tx.clone(),
d,
today,
events,
repetitions,
categories,
default_color,
),
col,
row,
1,
1,
);
// TODO: error handling
d = d.succ_opt().unwrap();
}
}
}
pub fn refresh_date(
app: &App,
date: NaiveDate,
repetitions: &HashMap<NaiveDate, Vec<Event>>,
categories: &[Category],
default_color: &str,
) {
let d = date.signed_duration_since(app.start_date).num_days();
let col = (d % 7) as i32;
let row = 1 + (d / 7) as i32;
app.calendar.attach(
&day_entry(
app.tx.clone(),
date,
app.today,
&app.events,
repetitions,
categories,
default_color,
),
col,
row,
1,
1,
)
}
fn day_title(col: i32) -> gtk::Box {
let vbox = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
vbox.add_css_class("g-Calendar__DayTitle");
let label = gtk::Label::builder().label(DAYS[col as usize]).build();
vbox.append(&label);
vbox
}
pub fn day_entry(
tx: Sender<Msg>,
date: NaiveDate,
today: NaiveDate,
events: &[Event],
repetitions: &HashMap<NaiveDate, Vec<Event>>,
categories: &[Category],
default_color: &str,
) -> gtk::ScrolledWindow {
let vbox = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
vbox.add_css_class("g-Calendar__Day");
let gesture = gtk::GestureClick::new();
gesture.connect_pressed(glib::clone!(@strong tx => move |_, n, _, _| {
if n == 2 {
update::send(tx.clone(), Msg::ShowAddForm { date });
}
}));
vbox.add_controller(&gesture);
if date == today {
vbox.add_css_class("g-Calendar__Day--Today");
}
vbox.append(&day_label(today, date));
let mut events = events
.iter()
.filter(|e| e.date == date)
.collect::<Vec<&Event>>();
let recurring_events = repetitions.get(&date).cloned().unwrap_or_default();
events.extend(recurring_events.iter());
events.sort_by_key(|e| e.start);
if !events.is_empty() {
vbox.append(&day_events(date, tx, events, categories, default_color));
}
gtk::ScrolledWindow::builder()
.hscrollbar_policy(gtk::PolicyType::Never)
.hexpand(true)
.vexpand(true)
.child(&vbox)
.build()
}
fn day_label(today: NaiveDate, date: NaiveDate) -> gtk::Label {
let label = gtk::Label::builder()
.label(&format!(
"{} {}",
date.day(),
if date == today || date.day() == 1 {
MONTHES[date.month0() as usize]
} else {
""
}
))
.halign(gtk::Align::Start)
.build();
label.add_css_class("g-Calendar__DayNumber");
if date.day() == 1 {
label.add_css_class("g-Calendar__DayNumber--FirstOfMonth")
}
label
}
fn day_events(
date: NaiveDate,
tx: Sender<Msg>,
events: Vec<&Event>,
categories: &[Category],
default_color: &str,
) -> gtk::Box {
let vbox = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
for event in events {
let hbox = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.hexpand(true)
.build();
let background_color = categories
.iter()
.find(|c| event.category == Some(c.id))
.map(|c| c.color.clone())
.unwrap_or_else(|| default_color.to_string());
let provider = gtk::CssProvider::new();
provider.load_from_data(
format!(
"
.event {{
background-color: {};
color: white;
border-radius: 4px;
padding: 4px;
margin: 4px;
}}
.event:hover {{
filter: brightness(120%);
}}
",
background_color
)
.as_bytes(),
);
hbox.style_context()
.add_provider(&provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION);
hbox.style_context().add_class("event");
let gesture = gtk::GestureClick::new();
gesture.connect_pressed(
glib::clone!(@strong event, @strong tx => move |gesture, n, _, _| {
gesture.set_state(gtk::EventSequenceState::Claimed);
if n == 2 {
if event.repetition.is_some() {
update::send(tx.clone(), Msg::ShowRepetitionDialog { date, event_id: event.id });
} else {
update::send(tx.clone(), Msg::ShowUpdateForm { event_id: event.id });
}
}
}),
);
hbox.add_controller(&gesture);
let event_txt = &event.pprint();
let label = gtk::Label::builder()
.label(event_txt)
.ellipsize(gtk::pango::EllipsizeMode::End)
.tooltip_text(event_txt)
.halign(gtk::Align::Start)
.build();
hbox.append(&label);
vbox.append(&hbox);
}
vbox
}
|