translated src/application.vala

This commit is contained in:
Erica Z 2024-10-29 14:02:29 +01:00
parent 5bfaeade17
commit 4193fb72c2
4 changed files with 79 additions and 24 deletions

View file

@ -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 { #[derive(Default)]
#[repr(C)] pub struct Application;
pub struct AudreyApplication {
pub parent_instance: adw::ffi::AdwApplication, #[glib::object_subclass]
impl ObjectSubclass for Application {
const NAME: &'static str = "AudreyApplication";
type Type = super::Application;
type ParentType = adw::Application;
} }
#[repr(C)] impl ObjectImpl for Application {}
pub struct AudreyApplicationClass {
pub parent_class: adw::ffi::AdwApplicationClass, 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" { use gtk::{gio, glib};
fn audrey_application_get_type() -> glib::ffi::GType;
fn audrey_application_new() -> *mut ffi::AudreyApplication;
}
glib::wrapper! { glib::wrapper! {
pub struct Application(Object<ffi::AudreyApplication, ffi::AudreyApplicationClass>) pub struct Application(ObjectSubclass<imp::Application>)
@extends adw::Application, gtk::Application, gio::Application, @extends adw::Application, gtk::Application, gio::Application,
@implements gio::ActionGroup, gio::ActionMap; @implements gio::ActionGroup, gio::ActionMap;
match fn {
type_ => || audrey_application_get_type(),
}
} }
impl Application { impl Application {
pub fn new() -> Self { pub fn new() -> Self {
unsafe { glib::Object::builder()
glib::translate::from_glib_full(audrey_application_new()) .property("application-id", "eu.callcc.audrey")
} .property("flags", gio::ApplicationFlags::default())
.build()
} }
} }

View file

@ -1,8 +1,10 @@
mod application; mod application;
use application::{Application}; pub use application::Application;
pub mod ui;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{glib, gio}; use gtk::{gio, glib};
extern crate libsecret; extern crate libsecret;
@ -11,8 +13,7 @@ extern crate libsecret;
#[link(name = "json-glib-1.0")] #[link(name = "json-glib-1.0")]
#[link(name = "mpv")] #[link(name = "mpv")]
#[link(name = "soup-3.0")] #[link(name = "soup-3.0")]
extern "C" { extern "C" {}
}
fn main() -> glib::ExitCode { fn main() -> glib::ExitCode {
gio::resources_register_include!("audrey.gresource").expect("could not register resources"); gio::resources_register_include!("audrey.gresource").expect("could not register resources");

2
src/ui.rs Normal file
View file

@ -0,0 +1,2 @@
mod window;
pub use window::Window;

39
src/ui/window.rs Normal file
View file

@ -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<ffi::UiWindow, ffi::UiWindowClass>)
@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<gtk::Application>) -> Self {
use glib::translate::*;
unsafe { from_glib_none(ffi::ui_window_new(app.as_ref().to_glib_none().0)) }
}
}