use std::cell::RefCell;
use gio::{ApplicationExt, ApplicationExtManual, ApplicationFlags, Cancellable};
use gtkrs::{Application, GtkApplicationExt};
use crate::util::assert_main_thread;
thread_local!(
static GTK_APPLICATION: RefCell<Option<Application>> = RefCell::new(None);
);
pub struct RunLoop {}
impl RunLoop {
pub fn new() -> RunLoop {
assert_main_thread();
let application = Application::new(
Some("com.github.xi-editor.druid"),
ApplicationFlags::FLAGS_NONE,
)
.expect("Unable to create GTK application");
application.connect_activate(|_app| {
eprintln!("Activated application");
});
application
.register(None as Option<&Cancellable>)
.expect("Could not register GTK application");
GTK_APPLICATION.with(move |x| *x.borrow_mut() = Some(application));
RunLoop {}
}
pub fn run(&mut self) {
assert_main_thread();
GTK_APPLICATION.with(|x| {
x.borrow()
.as_ref()
.unwrap() .run(&[])
});
}
}
pub fn request_quit() {
assert_main_thread();
with_application(|app| {
match app.get_active_window() {
None => {
}
Some(_) => {
gtk::main_quit();
}
}
});
}
#[inline]
pub(crate) fn with_application<F, R>(f: F) -> R
where
F: std::ops::FnOnce(Application) -> R,
{
assert_main_thread();
GTK_APPLICATION.with(move |app| {
let app = app
.borrow()
.clone()
.expect("Tried to manipulate the application before RunLoop::new was called");
f(app)
})
}
impl Default for RunLoop {
fn default() -> Self {
RunLoop::new()
}
}