Currently, I manage GTK+ events with Rc
and RefCell
as shown in the following example:
extern crate gtk;
use std::cell::RefCell;
use std::rc::Rc;
use gtk::{Button, ButtonExt, ContainerExt, Inhibit, Label, WidgetExt, Window, WindowType};
use gtk::Orientation::Vertical;
struct Model {
count: i32,
}
fn main() {
gtk::init().unwrap();
let window = Window::new(WindowType::Toplevel);
let model = Rc::new(RefCell::new(Model { count: 0 }));
let vbox = gtk::Box::new(Vertical, 0);
window.add(&vbox);
let label = Label::new(Some("0"));
vbox.add(&label);
let button = Button::new_with_label("Increment");
vbox.add(&button);
window.show_all();
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
{
let model = model.clone();
button.connect_clicked(move |_| {
{
(*model.borrow_mut()).count += 1;
}
label.set_text(&format!("{}", (*model.borrow()).count));
});
}
gtk::main();
}
My main issue with this code is the boilerplate needed because of the RefCell
s.
Also I feel like it is bad practise and can lead to panics (this is not my main issue so don't propose to use a Mutex
because in some examples, that can lead to a deadlock).
So I thought I could handle events in a way similar to Elm: with one function receiving signals where the model could be updated. However, I am unable to implement this in Rust. Here is an attempt:
extern crate gtk;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::Rc;
use gtk::{Button, ButtonExt, ContainerExt, Inhibit, Label, WidgetExt, WindowType};
use gtk::Orientation::Vertical;
use Message::Increment;
enum Message {
Increment,
}
struct Window {
label: Label,
model: Model,
queue: Rc<RefCell<VecDeque<Message>>>,
view: gtk::Window,
}
impl Window {
fn new() -> Self {
let window = gtk::Window::new(WindowType::Toplevel);
let vbox = gtk::Box::new(Vertical, 0);
window.add(&vbox);
let label = Label::new(Some("0"));
vbox.add(&label);
let button = Button::new_with_label("Increment");
vbox.add(&button);
window.show_all();
window.connect_delete_event(|_, _| {
gtk::main_quit();
Inhibit(false)
});
let queue = Rc::new(RefCell::new(VecDeque::new()));
{
let queue = queue.clone();
button.connect_clicked(move |_| {
(*queue.borrow_mut()).push_back(Increment);
});
}
Window {
label: label,
queue: queue,
model: Model { count: 0 },
view: window,
}
}
// How to call this method when a message is received?
fn update(&mut self, message: Message) {
match message {
Increment => {
self.model.count += 1;
self.label.set_text(&format!("{}", self.model.count));
},
}
}
}
struct Model {
count: i32,
}
fn main() {
gtk::init().unwrap();
let window = Window::new();
gtk::main();
}
How can I call the update()
method when the message queue
is updated?
Is it a viable approach?
If not, do you know any alternatives that would provide a solution to this issue?
Perhaps some solution based on the future
crate could be used?
In this case, how do I manage both main loops (the gtk+ one and the tokio
one).
Or a solution using channels?
gtk::main()
runs on. I wouldn't know a good Rust-specific solution to the problem, but I can give a GTK+-specific one: use idle callbacks to send stuff from another thread to the GTK+ thread. – andlabsidle_add()
, but I don't know how I can call myupdate()
method from it because the object would need to be static (or in aRc<RefCell<_>>
which is what I'm trying to avoid). – antoyo