gtk-rs 的教程和示例确实不完整且参差不齐,因此我试图拼凑如何在按钮回调中修改应用程序的状态以及某些子元素的状态。所以,简而言之,我有:
// ...
mod imp {
pub struct Window {
#[template_child]
pub headerbar: TemplateChild<gtk::HeaderBar>,
#[template_child]
pub open_button: TemplateChild<gtk::Button>,
// Internal state
pub state: Rc<RefCell<ScribeDownWindowState>>,
}
#[derive(Default)]
pub struct ScribeDownWindowState {
pub project_path: Option<String>,
}
}
在ObjectImpl
这个结构中,我有一个constructed
方法,它调用父构造的方法,然后调用setup_callbacks
父对象,它Window
实际上是 GTK 继承层次结构的一部分:
mod imp;
glib::wrapper! {
pub struct Window(ObjectSubclass<imp::Window>)
@extends gtk::ApplicationWindow, gtk::Window, gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap;
}
impl Window {
pub fn new<P: glib::IsA<gtk::Application>>(app: &P) -> Self {
glib::Object::new(&[("application", app)]).expect("Failed to create ScribeDownWindow")
}
fn setup_callbacks(&self) {
let state = self.imp().state;
let headerbar = Rc::new(&self.imp().headerbar);
self.imp().open_button
.connect_clicked(clone!(@strong state, @strong headerbar => move |_| {
let s = state.borrow_mut();
s.project_path = Some("fuck".to_string());
headerbar.set_subtitle(Some("fuck"));
}))
}
}
我需要同时访问结构的state
和headerbar
属性imp::Window
,并修改 的属性project_path
并state
调用set_subtitle
. headerbar
我已经尝试了各种变体,使用变量和Rc
s 的所有组合,RefCells
我似乎无法克服这个错误(或它的一些排列):
error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
--> src/scribedown_window/mod.rs:22:39
|
20 | fn setup_callbacks(&self) {
| ----- this data with an anonymous lifetime `'_`...
21 | let state = self.imp().state;
22 | let headerbar = Rc::new(&self.imp().headerbar);
| ---- ^^^
| |
| ...is captured here...
23 | self.imp().open_button.connect_clicked(
| --------------- ...and is required to live as long as `'static` here
必须有一种方法来完成我需要完成的工作,如果您无法在按钮单击回调中修改任何其他界面对象,您的 UI 将受到严重阻碍,但我不知道如何。