diff --git a/src/application.rs b/src/application.rs index 1895315..a2f1509 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,36 +1,49 @@ -use gtk::{glib, gio}; +mod imp { + use crate::ui; + use adw::prelude::*; + use adw::subclass::prelude::*; + use gtk::glib; -pub mod ffi { - #[repr(C)] - pub struct AudreyApplication { - pub parent_instance: adw::ffi::AdwApplication, + #[derive(Default)] + pub struct Application; + + #[glib::object_subclass] + impl ObjectSubclass for Application { + const NAME: &'static str = "AudreyApplication"; + type Type = super::Application; + type ParentType = adw::Application; } - #[repr(C)] - pub struct AudreyApplicationClass { - pub parent_class: adw::ffi::AdwApplicationClass, + impl ObjectImpl for Application {} + + impl ApplicationImpl for Application { + fn activate(&self) { + self.parent_activate(); + match self.obj().active_window() { + None => ui::Window::new(self.obj().as_ref()).present(), + Some(win) => win.present(), + } + } } + + impl GtkApplicationImpl for Application {} + + impl AdwApplicationImpl for Application {} } -extern "C" { - fn audrey_application_get_type() -> glib::ffi::GType; - fn audrey_application_new() -> *mut ffi::AudreyApplication; -} +use gtk::{gio, glib}; glib::wrapper! { - pub struct Application(Object) + pub struct Application(ObjectSubclass) @extends adw::Application, gtk::Application, gio::Application, @implements gio::ActionGroup, gio::ActionMap; - - match fn { - type_ => || audrey_application_get_type(), - } } impl Application { pub fn new() -> Self { - unsafe { - glib::translate::from_glib_full(audrey_application_new()) - } + glib::Object::builder() + .property("application-id", "eu.callcc.audrey") + .property("flags", gio::ApplicationFlags::default()) + .build() } } diff --git a/src/main.rs b/src/main.rs index 300c310..56f15c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,10 @@ mod application; -use application::{Application}; +pub use application::Application; + +pub mod ui; use gtk::prelude::*; -use gtk::{glib, gio}; +use gtk::{gio, glib}; extern crate libsecret; @@ -11,8 +13,7 @@ extern crate libsecret; #[link(name = "json-glib-1.0")] #[link(name = "mpv")] #[link(name = "soup-3.0")] -extern "C" { -} +extern "C" {} fn main() -> glib::ExitCode { gio::resources_register_include!("audrey.gresource").expect("could not register resources"); diff --git a/src/ui.rs b/src/ui.rs new file mode 100644 index 0000000..1a5b32a --- /dev/null +++ b/src/ui.rs @@ -0,0 +1,2 @@ +mod window; +pub use window::Window; diff --git a/src/ui/window.rs b/src/ui/window.rs new file mode 100644 index 0000000..12eb3d5 --- /dev/null +++ b/src/ui/window.rs @@ -0,0 +1,39 @@ +mod ffi { + use gtk::glib; + + #[repr(C)] + pub struct UiWindow { + parent_instance: adw::ffi::AdwApplicationWindow, + } + + #[repr(C)] + pub struct UiWindowClass { + parent_class: adw::ffi::AdwApplicationWindowClass, + } + + extern "C" { + pub fn ui_window_get_type() -> glib::ffi::GType; + pub fn ui_window_new(app: *mut gtk::ffi::GtkApplication) -> *mut UiWindow; + } +} + +use adw::prelude::*; +use gtk::{gio, glib}; + +glib::wrapper! { + pub struct Window(Object) + @extends adw::ApplicationWindow, gtk::ApplicationWindow, gtk::Window, gtk::Widget, + @implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager; + + match fn { + type_ => || ffi::ui_window_get_type(), + } +} + +impl Window { + pub fn new(app: &impl IsA) -> Self { + use glib::translate::*; + + unsafe { from_glib_none(ffi::ui_window_new(app.as_ref().to_glib_none().0)) } + } +}