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
use gtk::prelude::*;
use relm4::factory::FactoryVecDeque;
use relm4::prelude::*;
#[derive(Debug)]
struct Task {
name: String,
completed: bool,
}
#[derive(Debug)]
enum TaskInput {
Toggle(bool),
}
#[derive(Debug)]
enum TaskOutput {
Delete(DynamicIndex),
}
#[relm4::factory]
impl FactoryComponent for Task {
type Init = String;
type Input = TaskInput;
type Output = TaskOutput;
type CommandOutput = ();
type ParentInput = AppMsg;
type ParentWidget = gtk::ListBox;
view! {
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
gtk::CheckButton {
set_active: false,
set_margin_all: 12,
connect_toggled[sender] => move |checkbox| {
sender.input(TaskInput::Toggle(checkbox.is_active()));
}
},
#[name(label)]
gtk::Label {
set_label: &self.name,
set_hexpand: true,
set_halign: gtk::Align::Start,
set_margin_all: 12,
},
gtk::Button {
set_icon_name: "edit-delete",
set_margin_all: 12,
connect_clicked[sender, index] => move |_| {
sender.output(TaskOutput::Delete(index.clone()));
}
}
}
}
fn pre_view() {
let attrs = widgets.label.attributes().unwrap_or_default();
attrs.change(gtk::pango::AttrInt::new_strikethrough(self.completed));
widgets.label.set_attributes(Some(&attrs));
}
fn output_to_parent_input(output: Self::Output) -> Option<AppMsg> {
Some(match output {
TaskOutput::Delete(index) => AppMsg::DeleteEntry(index),
})
}
fn init_model(name: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> Self {
Self {
name,
completed: false,
}
}
}
#[derive(Debug)]
enum AppMsg {
DeleteEntry(DynamicIndex),
AddEntry(String),
}
struct App {
tasks: FactoryVecDeque<Task>,
}
#[relm4::component]
impl SimpleComponent for App {
type Init = ();
type Input = AppMsg;
type Output = ();
view! {
main_window = gtk::ApplicationWindow {
set_width_request: 360,
set_title: Some("To-Do"),
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_margin_all: 12,
set_spacing: 6,
gtk::Entry {
connect_activate[sender] => move |entry| {
let buffer = entry.buffer();
sender.input(AppMsg::AddEntry(buffer.text()));
buffer.delete_text(0, None);
}
},
gtk::ScrolledWindow {
set_hscrollbar_policy: gtk::PolicyType::Never,
set_min_content_height: 360,
set_vexpand: true,
#[local_ref]
task_list_box -> gtk::ListBox {}
}
}
}
}
fn update(&mut self, msg: AppMsg, _sender: ComponentSender<Self>) {
match msg {
AppMsg::DeleteEntry(index) => {
self.tasks.guard().remove(index.current_index());
}
AppMsg::AddEntry(name) => {
self.tasks.guard().push_back(name);
}
}
}
fn init(
_: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = App {
tasks: FactoryVecDeque::new(gtk::ListBox::default(), sender.input_sender()),
};
let task_list_box = model.tasks.widget();
let widgets = view_output!();
ComponentParts { model, widgets }
}
}
fn main() {
let app = RelmApp::new("relm4.example.to_do");
app.run::<App>(());
}