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
|
use gtk4 as gtk;
use async_channel::Sender;
use chrono::NaiveDate;
use gtk::glib;
use gtk::prelude::*;
use rusqlite::Connection;
use std::rc::Rc;
use crate::{app::update, app::update::Msg, db, model::event};
pub async fn dialog(
conn: Rc<Connection>,
tx: Sender<Msg>,
window: Rc<gtk::ApplicationWindow>,
date: NaiveDate,
) {
let dialog = gtk::Dialog::builder()
.transient_for(&*window)
.modal(true)
.title("Ajouter")
.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("");
vbox.append(&label("Événement"));
vbox.append(&name);
let date = entry(&date.format(event::DATE_FORMAT).to_string());
vbox.append(&label("Jour"));
vbox.append(&date);
let start = entry("");
vbox.append(&label("Début"));
vbox.append(&start);
let end = entry("");
vbox.append(&label("Fin"));
vbox.append(&end);
let button = gtk::Button::with_label("Créer");
vbox.append(&button);
button.connect_clicked(glib::clone!(@weak dialog => move |_| {
match event::validate(date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) {
Some(event) => {
match db::insert(&conn, &event) {
Ok(_) => {
update::send(tx.clone(), Msg::AddEvent { event: event });
dialog.close()
},
Err(_) => ()
}
},
None => ()
}
}));
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()
}
|