Init with GTK Rust Template
This commit is contained in:
151
src/application.rs
Normal file
151
src/application.rs
Normal file
@ -0,0 +1,151 @@
|
||||
use gettextrs::gettext;
|
||||
use tracing::{debug, info};
|
||||
|
||||
use adw::subclass::prelude::*;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{gdk, gio, glib};
|
||||
|
||||
use crate::config::{APP_ID, PKGDATADIR, PROFILE, VERSION};
|
||||
use crate::window::ExampleApplicationWindow;
|
||||
|
||||
mod imp {
|
||||
use super::*;
|
||||
use glib::WeakRef;
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ExampleApplication {
|
||||
pub window: OnceCell<WeakRef<ExampleApplicationWindow>>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for ExampleApplication {
|
||||
const NAME: &'static str = "ExampleApplication";
|
||||
type Type = super::ExampleApplication;
|
||||
type ParentType = adw::Application;
|
||||
}
|
||||
|
||||
impl ObjectImpl for ExampleApplication {}
|
||||
|
||||
impl ApplicationImpl for ExampleApplication {
|
||||
fn activate(&self) {
|
||||
debug!("AdwApplication<ExampleApplication>::activate");
|
||||
self.parent_activate();
|
||||
let app = self.obj();
|
||||
|
||||
if let Some(window) = self.window.get() {
|
||||
let window = window.upgrade().unwrap();
|
||||
window.present();
|
||||
return;
|
||||
}
|
||||
|
||||
let window = ExampleApplicationWindow::new(&app);
|
||||
self.window
|
||||
.set(window.downgrade())
|
||||
.expect("Window already set.");
|
||||
|
||||
app.main_window().present();
|
||||
}
|
||||
|
||||
fn startup(&self) {
|
||||
debug!("AdwApplication<ExampleApplication>::startup");
|
||||
self.parent_startup();
|
||||
let app = self.obj();
|
||||
|
||||
// Set icons for shell
|
||||
gtk::Window::set_default_icon_name(APP_ID);
|
||||
|
||||
app.setup_css();
|
||||
app.setup_gactions();
|
||||
app.setup_accels();
|
||||
}
|
||||
}
|
||||
|
||||
impl GtkApplicationImpl for ExampleApplication {}
|
||||
impl AdwApplicationImpl for ExampleApplication {}
|
||||
}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct ExampleApplication(ObjectSubclass<imp::ExampleApplication>)
|
||||
@extends gio::Application, gtk::Application,
|
||||
@implements gio::ActionMap, gio::ActionGroup;
|
||||
}
|
||||
|
||||
impl ExampleApplication {
|
||||
fn main_window(&self) -> ExampleApplicationWindow {
|
||||
self.imp().window.get().unwrap().upgrade().unwrap()
|
||||
}
|
||||
|
||||
fn setup_gactions(&self) {
|
||||
// Quit
|
||||
let action_quit = gio::ActionEntry::builder("quit")
|
||||
.activate(move |app: &Self, _, _| {
|
||||
// This is needed to trigger the delete event and saving the window state
|
||||
app.main_window().close();
|
||||
app.quit();
|
||||
})
|
||||
.build();
|
||||
|
||||
// About
|
||||
let action_about = gio::ActionEntry::builder("about")
|
||||
.activate(|app: &Self, _, _| {
|
||||
app.show_about_dialog();
|
||||
})
|
||||
.build();
|
||||
self.add_action_entries([action_quit, action_about]);
|
||||
}
|
||||
|
||||
// Sets up keyboard shortcuts
|
||||
fn setup_accels(&self) {
|
||||
self.set_accels_for_action("app.quit", &["<Control>q"]);
|
||||
self.set_accels_for_action("window.close", &["<Control>w"]);
|
||||
}
|
||||
|
||||
fn setup_css(&self) {
|
||||
let provider = gtk::CssProvider::new();
|
||||
provider.load_from_resource("/com/ranfdev/Notify/style.css");
|
||||
if let Some(display) = gdk::Display::default() {
|
||||
gtk::StyleContext::add_provider_for_display(
|
||||
&display,
|
||||
&provider,
|
||||
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn show_about_dialog(&self) {
|
||||
let dialog = adw::AboutWindow::builder()
|
||||
.application_icon(APP_ID)
|
||||
.application_name("Notify")
|
||||
// Insert your license of choice here
|
||||
// .license_type(gtk::License::MitX11)
|
||||
// Insert your website here
|
||||
// .website("https://gitlab.gnome.org/bilelmoussaoui/notify/")
|
||||
.version(VERSION)
|
||||
.transient_for(&self.main_window())
|
||||
.translator_credits(gettext("translator-credits"))
|
||||
.modal(true)
|
||||
.developers(vec!["ranfdev"])
|
||||
.artists(vec!["ranfdev"])
|
||||
.build();
|
||||
|
||||
dialog.present();
|
||||
}
|
||||
|
||||
pub fn run(&self) -> glib::ExitCode {
|
||||
info!("Notify ({})", APP_ID);
|
||||
info!("Version: {} ({})", VERSION, PROFILE);
|
||||
info!("Datadir: {}", PKGDATADIR);
|
||||
|
||||
ApplicationExtManual::run(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ExampleApplication {
|
||||
fn default() -> Self {
|
||||
glib::Object::builder()
|
||||
.property("application-id", APP_ID)
|
||||
.property("resource-base-path", "/com/ranfdev/Notify/")
|
||||
.build()
|
||||
}
|
||||
}
|
||||
7
src/config.rs.in
Normal file
7
src/config.rs.in
Normal file
@ -0,0 +1,7 @@
|
||||
pub const APP_ID: &str = @APP_ID@;
|
||||
pub const GETTEXT_PACKAGE: &str = @GETTEXT_PACKAGE@;
|
||||
pub const LOCALEDIR: &str = @LOCALEDIR@;
|
||||
pub const PKGDATADIR: &str = @PKGDATADIR@;
|
||||
pub const PROFILE: &str = @PROFILE@;
|
||||
pub const RESOURCES_FILE: &str = concat!(@PKGDATADIR@, "/resources.gresource");
|
||||
pub const VERSION: &str = @VERSION@;
|
||||
28
src/main.rs
Normal file
28
src/main.rs
Normal file
@ -0,0 +1,28 @@
|
||||
mod application;
|
||||
#[rustfmt::skip]
|
||||
mod config;
|
||||
mod window;
|
||||
|
||||
use gettextrs::{gettext, LocaleCategory};
|
||||
use gtk::{gio, glib};
|
||||
|
||||
use self::application::ExampleApplication;
|
||||
use self::config::{GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE};
|
||||
|
||||
fn main() -> glib::ExitCode {
|
||||
// Initialize logger
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// Prepare i18n
|
||||
gettextrs::setlocale(LocaleCategory::LcAll, "");
|
||||
gettextrs::bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR).expect("Unable to bind the text domain");
|
||||
gettextrs::textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain");
|
||||
|
||||
glib::set_application_name(&gettext("Notify"));
|
||||
|
||||
let res = gio::Resource::load(RESOURCES_FILE).expect("Could not load gresource file");
|
||||
gio::resources_register(&res);
|
||||
|
||||
let app = ExampleApplication::default();
|
||||
app.run()
|
||||
}
|
||||
52
src/meson.build
Normal file
52
src/meson.build
Normal file
@ -0,0 +1,52 @@
|
||||
global_conf = configuration_data()
|
||||
global_conf.set_quoted('APP_ID', application_id)
|
||||
global_conf.set_quoted('PKGDATADIR', pkgdatadir)
|
||||
global_conf.set_quoted('PROFILE', profile)
|
||||
global_conf.set_quoted('VERSION', version + version_suffix)
|
||||
global_conf.set_quoted('GETTEXT_PACKAGE', gettext_package)
|
||||
global_conf.set_quoted('LOCALEDIR', localedir)
|
||||
config = configure_file(
|
||||
input: 'config.rs.in',
|
||||
output: 'config.rs',
|
||||
configuration: global_conf
|
||||
)
|
||||
# Copy the config.rs output to the source directory.
|
||||
run_command(
|
||||
'cp',
|
||||
meson.project_build_root() / 'src' / 'config.rs',
|
||||
meson.project_source_root() / 'src' / 'config.rs',
|
||||
check: true
|
||||
)
|
||||
|
||||
cargo_options = [ '--manifest-path', meson.project_source_root() / 'Cargo.toml' ]
|
||||
cargo_options += [ '--target-dir', meson.project_build_root() / 'src' ]
|
||||
|
||||
if get_option('profile') == 'default'
|
||||
cargo_options += [ '--release' ]
|
||||
rust_target = 'release'
|
||||
message('Building in release mode')
|
||||
else
|
||||
rust_target = 'debug'
|
||||
message('Building in debug mode')
|
||||
endif
|
||||
|
||||
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
|
||||
|
||||
cargo_build = custom_target(
|
||||
'cargo-build',
|
||||
build_by_default: true,
|
||||
build_always_stale: true,
|
||||
output: meson.project_name(),
|
||||
console: true,
|
||||
install: true,
|
||||
install_dir: bindir,
|
||||
depends: resources,
|
||||
command: [
|
||||
'env',
|
||||
cargo_env,
|
||||
cargo, 'build',
|
||||
cargo_options,
|
||||
'&&',
|
||||
'cp', 'src' / rust_target / meson.project_name(), '@OUTPUT@',
|
||||
]
|
||||
)
|
||||
118
src/window.rs
Normal file
118
src/window.rs
Normal file
@ -0,0 +1,118 @@
|
||||
use adw::subclass::prelude::*;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{gio, glib};
|
||||
|
||||
use crate::application::ExampleApplication;
|
||||
use crate::config::{APP_ID, PROFILE};
|
||||
|
||||
mod imp {
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, gtk::CompositeTemplate)]
|
||||
#[template(resource = "/com/ranfdev/Notify/ui/window.ui")]
|
||||
pub struct ExampleApplicationWindow {
|
||||
#[template_child]
|
||||
pub headerbar: TemplateChild<adw::HeaderBar>,
|
||||
pub settings: gio::Settings,
|
||||
}
|
||||
|
||||
impl Default for ExampleApplicationWindow {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
headerbar: TemplateChild::default(),
|
||||
settings: gio::Settings::new(APP_ID),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for ExampleApplicationWindow {
|
||||
const NAME: &'static str = "ExampleApplicationWindow";
|
||||
type Type = super::ExampleApplicationWindow;
|
||||
type ParentType = adw::ApplicationWindow;
|
||||
|
||||
fn class_init(klass: &mut Self::Class) {
|
||||
klass.bind_template();
|
||||
}
|
||||
|
||||
// You must call `Widget`'s `init_template()` within `instance_init()`.
|
||||
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
||||
obj.init_template();
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectImpl for ExampleApplicationWindow {
|
||||
fn constructed(&self) {
|
||||
self.parent_constructed();
|
||||
let obj = self.obj();
|
||||
|
||||
// Devel Profile
|
||||
if PROFILE == "Devel" {
|
||||
obj.add_css_class("devel");
|
||||
}
|
||||
|
||||
// Load latest window state
|
||||
obj.load_window_size();
|
||||
}
|
||||
|
||||
fn dispose(&self) {
|
||||
self.dispose_template();
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetImpl for ExampleApplicationWindow {}
|
||||
impl WindowImpl for ExampleApplicationWindow {
|
||||
// Save window state on delete event
|
||||
fn close_request(&self) -> gtk::Inhibit {
|
||||
if let Err(err) = self.obj().save_window_size() {
|
||||
tracing::warn!("Failed to save window state, {}", &err);
|
||||
}
|
||||
|
||||
// Pass close request on to the parent
|
||||
self.parent_close_request()
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationWindowImpl for ExampleApplicationWindow {}
|
||||
impl AdwApplicationWindowImpl for ExampleApplicationWindow {}
|
||||
}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct ExampleApplicationWindow(ObjectSubclass<imp::ExampleApplicationWindow>)
|
||||
@extends gtk::Widget, gtk::Window, adw::Window, adw::ApplicationWindow,
|
||||
@implements gio::ActionMap, gio::ActionGroup, gtk::Root;
|
||||
}
|
||||
|
||||
impl ExampleApplicationWindow {
|
||||
pub fn new(app: &ExampleApplication) -> Self {
|
||||
glib::Object::builder().property("application", app).build()
|
||||
}
|
||||
|
||||
fn save_window_size(&self) -> Result<(), glib::BoolError> {
|
||||
let imp = self.imp();
|
||||
|
||||
let (width, height) = self.default_size();
|
||||
|
||||
imp.settings.set_int("window-width", width)?;
|
||||
imp.settings.set_int("window-height", height)?;
|
||||
|
||||
imp.settings
|
||||
.set_boolean("is-maximized", self.is_maximized())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_window_size(&self) {
|
||||
let imp = self.imp();
|
||||
|
||||
let width = imp.settings.int("window-width");
|
||||
let height = imp.settings.int("window-height");
|
||||
let is_maximized = imp.settings.boolean("is-maximized");
|
||||
|
||||
self.set_default_size(width, height);
|
||||
|
||||
if is_maximized {
|
||||
self.maximize();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user