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
use gtk::prelude::{
BoxExt, ButtonExt, GtkWindowExt, ImageExt, LabelExt, OrientableExt, WidgetExt, WidgetExtManual,
};
use gtk::{self, gdk};
use nrelm::{ComponentParts, ComponentSender, RelmApp, SimpleComponent};
// In GTK3 the payload of a drag and drop operation is transported through
// the selection mechanism. We distinguish the dragged values by the
// `info` id of the `TargetEntry` the drop was performed with.
// The payload itself must be stored with `SelectionData::set` using the
// target type requested by the drop site (`SelectionData::set_text` only
// works for text targets such as `STRING` or `UTF8_STRING`).
struct AppModel {
status: String,
}
#[derive(Debug)]
enum AppIn {
NewStatus(String),
}
#[nrelm::component]
impl SimpleComponent for AppModel {
type Init = ();
type Input = AppIn;
type Output = ();
fn init(
_init: Self::Init,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = AppModel {
status: String::from("Drag the key or lock pick to a door"),
};
let widgets = view_output!();
ComponentParts { model, widgets }
}
view! {
gtk::Window {
set_title: "Drag and Drop Example",
set_size_request: (500, 200),
// outer box
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
// display status message
gtk::Label {
set_margin_all: 10,
#[watch]
set_label: &model.status,
},
gtk::Label {
set_margin_all: 10,
set_label: "The blue key will only unlock the blue door\nThe pick can open both",
},
// first horizontal box
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_halign: gtk::Align::Center,
set_spacing: 10,
// contains a image and label and serves as DropTarget
gtk::Button {
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_margin_all: 10,
gtk::Image {
set_from_icon_name: (Some("locked"), gtk::IconSize::Button),
},
gtk::Label {
set_label: "Blue Door",
}
},
// drops will be accepted on all widgets in Button
drag_dest_set: (
gtk::DestDefaults::ALL,
&[
gtk::TargetEntry::new(
"application/x-nrelm-blue-key",
gtk::TargetFlags::SAME_APP,
0,
),
gtk::TargetEntry::new(
"application/x-nrelm-lock-pick",
gtk::TargetFlags::SAME_APP,
1,
),
],
gdk::DragAction::MOVE,
),
connect_drag_data_received[sender] => move |_, context, _, _, data, info, time| {
let payload = String::from_utf8_lossy(&data.data()).to_string();
if info == 0 {
if payload == "blue-key" {
sender.input(AppIn::NewStatus(
String::from("Blue Door unlocked with Blue Key")
));
} else {
sender.input(AppIn::NewStatus(
String::from("Blue Door unlocked with Lock Pick")
));
}
} else if payload == "lock-pick" {
sender.input(AppIn::NewStatus(
String::from("Blue Door unlocked with Lock Pick")
));
} else {
sender.input(AppIn::NewStatus(
String::from("Blue Door rejects this item")
));
}
context.drop_finish(true, time);
}
},
gtk::Button {
set_label: "Blue Key",
// define the Button as a DragSource
drag_source_set: (
gdk::ModifierType::BUTTON1_MASK,
&[
gtk::TargetEntry::new(
"application/x-nrelm-blue-key",
gtk::TargetFlags::SAME_APP,
0,
),
],
gdk::DragAction::MOVE,
),
connect_drag_data_get => move |_, _, data, _, _| {
data.set(&data.target(), 8, b"blue-key");
},
}
},
// second horizontal box
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_halign: gtk::Align::Center,
set_spacing: 10,
// contains image and label and serves as a DropTarget
gtk::Button {
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_margin_all: 10,
gtk::Image {
set_from_icon_name: (Some("locked"), gtk::IconSize::Button),
},
gtk::Label {
set_label: "Red Door",
}
},
drag_dest_set: (
gtk::DestDefaults::ALL,
&[
gtk::TargetEntry::new(
"application/x-nrelm-lock-pick",
gtk::TargetFlags::SAME_APP,
1,
),
],
gdk::DragAction::MOVE,
),
connect_drag_data_received[sender] => move |_, context, _, _, data, _, time| {
// there is only the pick lock to unlock the door
let payload = String::from_utf8_lossy(&data.data()).to_string();
if payload == "lock-pick" {
sender.input(AppIn::NewStatus(
String::from("Red Door unlocked with Lock Pick")
));
} else {
sender.input(AppIn::NewStatus(
String::from("Red Door rejects this item")
));
}
context.drop_finish(true, time);
}
},
gtk::Button {
set_label: "Lock Pick",
drag_source_set: (
gdk::ModifierType::BUTTON1_MASK,
&[
gtk::TargetEntry::new(
"application/x-nrelm-lock-pick",
gtk::TargetFlags::SAME_APP,
1,
),
],
gdk::DragAction::MOVE,
),
connect_drag_data_get => move |_, _, data, _, _| {
data.set(&data.target(), 8, b"lock-pick");
},
}
}
}
}
}
fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {
match msg {
AppIn::NewStatus(status) => {
self.status = status;
}
}
}
}
fn main() {
let app = RelmApp::new("nrelm.example.drag-and-drop");
app.run::<AppModel>(());
}