hammond-gtk: Split gtk::Application into its own module.

This commit is contained in:
Jordan Petridis 2017-12-28 16:59:05 +02:00
parent a9d1084e05
commit 5942e47f2a
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 116 additions and 63 deletions

112
hammond-gtk/src/app.rs Normal file
View File

@ -0,0 +1,112 @@
use gtk;
use glib;
use gio;
use gtk::prelude::*;
use gio::ApplicationExtManual;
use gio::ApplicationExt;
use hammond_data::utils::checkup;
use headerbar::Header;
use content::Content;
use utils;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub struct App {
app_instance: gtk::Application,
window: gtk::Window,
header: Rc<Header>,
content: Rc<Content>,
}
impl App {
pub fn new() -> App {
let application =
gtk::Application::new("org.gnome.Hammond", gio::ApplicationFlags::empty())
.expect("Initialization failed...");
// Weird magic I copy-pasted that sets the Application Name in the Shell.
glib::set_application_name("Hammond");
glib::set_prgname(Some("Hammond"));
// Create the main window
let window = gtk::Window::new(gtk::WindowType::Toplevel);
window.set_default_size(860, 640);
window.set_title("Hammond");
window.connect_delete_event(|w, _| {
w.destroy();
Inhibit(false)
});
// TODO: Refactor the initialization order.
// Create the headerbar
let header = Rc::new(Header::default());
// Create a content instance
let content = Content::new(header.clone());
// Initialize the headerbar
header.init(content.clone());
// Add the Headerbar to the window.
window.set_titlebar(&header.container);
// Add the content main stack to the window.
window.add(&content.get_stack());
App {
app_instance: application,
window,
header,
content,
}
}
pub fn run(&self) {
let window = self.window.clone();
let app = self.app_instance.clone();
self.app_instance.connect_startup(move |_| {
build_ui(&window, &app);
});
let content = self.content.clone();
// Update 30 seconds after the Application is initialized.
gtk::timeout_add_seconds(
30,
clone!(content => move || {
utils::refresh_feed(content.clone(), None);
glib::Continue(false)
}),
);
let content = self.content.clone();
// Auto-updater, runs every hour.
// TODO: expose the interval in which it run to a user setting.
// TODO: show notifications.
gtk::timeout_add_seconds(
3600,
clone!(content => move || {
utils::refresh_feed(content.clone(), None);
glib::Continue(true)
}),
);
// Run a database checkup once the application is initialized.
gtk::idle_add(move || {
let _ = checkup();
glib::Continue(false)
});
ApplicationExtManual::run(&self.app_instance, &[]);
}
}
fn build_ui(window: &gtk::Window, app: &gtk::Application) {
window.set_application(app);
window.show_all();
window.activate();
app.connect_activate(move |_| ());
}

View File

@ -24,11 +24,8 @@ extern crate send_cell;
// use rayon::prelude::*;
use log::LogLevel;
use hammond_data::utils::checkup;
use gtk::prelude::*;
use gio::ApplicationExt;
use std::rc::Rc;
// http://gtk-rs.org/tuto/closures
#[macro_export]
@ -53,70 +50,19 @@ mod views;
mod widgets;
mod headerbar;
mod content;
mod app;
mod utils;
mod static_resource;
fn build_ui(app: &gtk::Application) {
// Get the main window
let window = gtk::ApplicationWindow::new(app);
window.set_default_size(860, 640);
window.set_title("Hammond");
// Get the headerbar
let header = Rc::new(headerbar::Header::default());
let ct = content::Content::new(header.clone());
header.init(ct.clone());
window.set_titlebar(&header.container);
window.add(&ct.get_stack());
window.connect_delete_event(|w, _| {
w.destroy();
Inhibit(false)
});
// Update on startup
gtk::timeout_add_seconds(
30,
clone!(ct => move || {
utils::refresh_feed(ct.clone(), None);
glib::Continue(false)
}),
);
// Auto-updater, runs every hour.
// TODO: expose the interval in which it run to a user setting.
// TODO: show notifications.
gtk::timeout_add_seconds(
3600,
clone!(ct => move || {
utils::refresh_feed(ct.clone(), None);
glib::Continue(true)
}),
);
gtk::idle_add(move || {
let _ = checkup();
glib::Continue(false)
});
window.show_all();
window.activate();
app.connect_activate(move |_| ());
}
use app::App;
fn main() {
use gio::ApplicationExtManual;
// TODO: make the the logger a cli -vv option
loggerv::init_with_level(LogLevel::Info).unwrap();
gtk::init().expect("Error initializing gtk");
static_resource::init().expect("Something went wrong with the resource file initialization.");
let application = gtk::Application::new("org.gnome.Hammond", gio::ApplicationFlags::empty())
.expect("Initialization failed...");
glib::set_application_name("Hammond");
glib::set_prgname(Some("Hammond"));
// Add custom style
let provider = gtk::CssProvider::new();
gtk::CssProvider::load_from_resource(&provider, "/org/gnome/hammond/gtk/style.css");
@ -126,10 +72,5 @@ fn main() {
600,
);
application.connect_startup(move |app| {
build_ui(app);
});
// application.run(&[]);
ApplicationExtManual::run(&application, &[]);
App::new().run();
}