问题
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?
回答1:
I found a solution to this issue here.
Here is what I achieved with my example:
extern crate gtk;
use gtk::{Button, ButtonExt, ContainerExt, Inhibit, Label, WidgetExt, WindowType};
use gtk::Orientation::Vertical;
use Message::Increment;
macro_rules! connect {
($source:ident :: $event:ident, $target:ident :: $message:expr) => {
let target = &mut *$target as *mut _;
$source.$event(move |_| {
let target: &mut Window = unsafe { &mut *target };
target.update($message);
});
};
}
enum Message {
Increment,
}
struct Window {
label: Label,
model: Model,
view: gtk::Window,
}
impl Window {
fn new() -> Box<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 mut window = Box::new(Window {
label: label,
model: Model { count: 0 },
view: window,
});
connect!(button::connect_clicked, window::Increment);
window
}
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();
}
Do you think this use of unsafe
can cause segfault?
For instance, if the button
could outlive the window
, the window
could be used after being freed, no?
Is there a way to build a safe wrapper around this?
On an esthetical note, is there a way to achieve this:
connect!(button::clicked, window::Increment);
来源:https://stackoverflow.com/questions/40516510/alternative-way-to-handle-gtk-events-in-rust