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
|
use gtk4 as gtk;
use gtk::glib;
use gtk::prelude::*;
use crate::{
app::{update, update::Msg, App},
db,
model::{event, event::Event},
};
pub async fn show(app: &App, event: Event, is_new: bool) {
let dialog = gtk::Dialog::builder()
.transient_for(&*app.window)
.modal(true)
.title(if is_new { "Ajouter" } else { "Modifier" })
.css_classes(vec!["g-Form".to_string()])
.build();
let content_area = dialog.content_area();
let vbox = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
vbox.add_css_class("g-Form__Inputs");
content_area.append(&vbox);
let name = entry(&event.name);
vbox.append(&label("Événement"));
vbox.append(&name);
let date = entry(&event.date.format(event::DATE_FORMAT).to_string());
vbox.append(&label("Jour"));
vbox.append(&date);
let start = entry(
&event
.start
.map(event::pprint_time)
.unwrap_or("".to_string()),
);
vbox.append(&label("Début"));
vbox.append(&start);
let end = entry(&event.end.map(event::pprint_time).unwrap_or("".to_string()));
vbox.append(&label("Fin"));
vbox.append(&end);
let button = gtk::Button::builder()
.label(if is_new { "Créer" } else { "Modifier" })
.margin_bottom(10)
.build();
vbox.append(&button);
let conn = app.conn.clone();
let tx = app.tx.clone();
button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| {
match event::validate(event.id, date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) {
Some(new) => {
match if is_new { db::insert(&conn, &new) } else { db::update(&conn, &new) } {
Ok(_) => {
let msg = if is_new { Msg::AddEvent { new } } else { Msg::UpdateEvent { old: event.clone(), new } };
update::send(tx.clone(), msg);
dialog.close()
},
Err(_) => ()
}
},
None => ()
}
}));
if !is_new {
let button = gtk::Button::builder().label("Supprimer").build();
vbox.append(&button);
let conn = app.conn.clone();
let tx = app.tx.clone();
button.connect_clicked(glib::clone!(@weak dialog => move |_| {
match db::delete(&conn, &event.id) {
Ok(_) => {
update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() });
dialog.close()
},
Err(_) => ()
}
}));
}
dialog.run_future().await;
}
fn entry(text: &str) -> gtk::Entry {
gtk::Entry::builder().text(text).margin_bottom(10).build()
}
fn label(text: &str) -> gtk::Label {
gtk::Label::builder()
.label(text)
.halign(gtk::Align::Start)
.margin_bottom(5)
.build()
}
|