musicus/src/application.rs

93 lines
3 KiB
Rust
Raw Normal View History

2023-06-18 14:02:21 +02:00
use gtk::prelude::*;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
use crate::config::VERSION;
use crate::MusicusWindow;
mod imp {
use super::*;
#[derive(Debug, Default)]
pub struct MusicusApplication {}
#[glib::object_subclass]
impl ObjectSubclass for MusicusApplication {
const NAME: &'static str = "MusicusApplication";
type Type = super::MusicusApplication;
type ParentType = adw::Application;
}
impl ObjectImpl for MusicusApplication {
fn constructed(&self) {
self.parent_constructed();
let obj = self.obj();
obj.setup_gactions();
obj.set_accels_for_action("app.quit", &["<primary>q"]);
}
}
impl ApplicationImpl for MusicusApplication {
// We connect to the activate callback to create a window when the application
// has been launched. Additionally, this callback notifies us when the user
// tries to launch a "second instance" of the application. When they try
// to do that, we'll just present any existing window.
fn activate(&self) {
let application = self.obj();
// Get the current window or create one if necessary
let window = if let Some(window) = application.active_window() {
window
} else {
let window = MusicusWindow::new(&*application);
window.upcast()
};
// Ask the window manager/compositor to present the window
window.present();
}
}
impl GtkApplicationImpl for MusicusApplication {}
impl AdwApplicationImpl for MusicusApplication {}
}
glib::wrapper! {
pub struct MusicusApplication(ObjectSubclass<imp::MusicusApplication>)
@extends gio::Application, gtk::Application, adw::Application,
@implements gio::ActionGroup, gio::ActionMap;
}
impl MusicusApplication {
pub fn new(application_id: &str, flags: &gio::ApplicationFlags) -> Self {
glib::Object::builder()
.property("application-id", application_id)
.property("flags", flags)
.build()
}
fn setup_gactions(&self) {
let quit_action = gio::ActionEntry::builder("quit")
.activate(move |app: &Self, _, _| app.quit())
.build();
let about_action = gio::ActionEntry::builder("about")
.activate(move |app: &Self, _, _| app.show_about())
.build();
self.add_action_entries([quit_action, about_action]);
}
fn show_about(&self) {
let window = self.active_window().unwrap();
let about = adw::AboutWindow::builder()
.transient_for(&window)
.application_name("musicus")
.application_icon("de.johrpan.musicus")
.developer_name("Unknown")
.version(VERSION)
.developers(vec!["Unknown"])
.copyright("© 2023 Unknown")
.build();
about.present();
}
}