use std::cell::RefCell;
use gio::prelude::ApplicationExtManual;
use gio::{ApplicationExt, ApplicationFlags, Cancellable};
use gtk::{Application as GtkApplication, GtkApplicationExt};
use super::clipboard::Clipboard;
use super::util;
use crate::application::AppHandler;
thread_local!(
static GTK_APPLICATION: RefCell<Option<GtkApplication>> = RefCell::new(None);
);
pub struct Application;
impl Application {
pub fn new(_handler: Option<Box<dyn AppHandler>>) -> Application {
let application = GtkApplication::new(
Some("com.github.xi-editor.druid"),
ApplicationFlags::NON_UNIQUE,
)
.expect("Unable to create GTK application");
application.connect_activate(|_app| {
log::info!("gtk: Activated application");
});
application
.register(None as Option<&Cancellable>)
.expect("Could not register GTK application");
GTK_APPLICATION.with(move |x| *x.borrow_mut() = Some(application));
Application
}
pub fn run(&mut self) {
util::assert_main_thread();
GTK_APPLICATION.with(|x| {
x.borrow()
.as_ref()
.unwrap() .run(&[])
});
}
pub fn quit() {
util::assert_main_thread();
with_application(|app| {
match app.get_active_window() {
None => {
}
Some(_) => {
app.quit();
}
}
});
}
pub fn clipboard() -> Clipboard {
Clipboard
}
pub fn get_locale() -> String {
"en-US".into()
}
}
#[inline]
pub(crate) fn with_application<F, R>(f: F) -> R
where
F: std::ops::FnOnce(GtkApplication) -> R,
{
util::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)
})
}