Add welcome page and improve generated code

This commit is contained in:
Elias Projahn 2023-09-13 14:58:31 +02:00
parent 379923e4ca
commit f61482c329
11 changed files with 175 additions and 92 deletions

View file

@ -1,6 +1,6 @@
use gtk::prelude::*;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
use gettextrs::gettext;
use gtk::{gio, glib, prelude::*};
use crate::config::VERSION;
use crate::MusicusWindow;
@ -28,13 +28,9 @@ mod imp {
}
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 {
@ -42,7 +38,6 @@ mod imp {
window.upcast()
};
// Ask the window manager/compositor to present the window
window.present();
}
}
@ -69,9 +64,11 @@ impl MusicusApplication {
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]);
}
@ -79,12 +76,14 @@ impl MusicusApplication {
let window = self.active_window().unwrap();
let about = adw::AboutWindow::builder()
.transient_for(&window)
.application_name("musicus")
.application_name(gettext("Musicus"))
.application_icon("de.johrpan.musicus")
.developer_name("Unknown")
.developer_name("Elias Projahn")
.version(VERSION)
.developers(vec!["Unknown"])
.copyright("© 2023 Unknown")
.website("https://code.johrpan.de/johrpan/musicus")
.developers(vec!["Elias Projahn <elias@johrpan.de>"])
.copyright("© 2023 Elias Projahn")
.license_type(gtk::License::Gpl30)
.build();
about.present();

View file

@ -1,35 +1,23 @@
mod application;
mod config;
mod welcome_page;
mod window;
use self::application::MusicusApplication;
use self::window::MusicusWindow;
use self::{application::MusicusApplication, window::MusicusWindow};
use config::{GETTEXT_PACKAGE, LOCALEDIR, PKGDATADIR};
use gettextrs::{bind_textdomain_codeset, bindtextdomain, textdomain};
use gtk::{gio, glib};
use gtk::prelude::*;
use gtk::{gio, glib, prelude::*};
fn main() -> glib::ExitCode {
// Set up gettext translations
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR).expect("Unable to bind the text domain");
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8")
.expect("Unable to set the text domain encoding");
textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain");
// Load resources
let resources = gio::Resource::load(PKGDATADIR.to_owned() + "/musicus.gresource")
.expect("Could not load resources");
gio::resources_register(&resources);
// Create a new GtkApplication. The application manages our main loop,
// application windows, integration with the window manager/compositor, and
// desktop features such as file opening and single-instance applications.
let app = MusicusApplication::new("de.johrpan.musicus", &gio::ApplicationFlags::empty());
// Run the application. This function will block until the application
// exits. Upon return, we have our exit code to return to the shell. (This
// is the code you see when you do `echo $?` after running a command in a
// terminal.
app.run()
MusicusApplication::new("de.johrpan.musicus", &gio::ApplicationFlags::empty()).run()
}

43
src/welcome_page.rs Normal file
View file

@ -0,0 +1,43 @@
use adw::subclass::{navigation_page::NavigationPageImpl, prelude::*};
use gtk::glib;
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(resource = "/de/johrpan/musicus/welcome_page.ui")]
pub struct MusicusWelcomePage {
#[template_child]
pub choose_library_button: TemplateChild<gtk::Button>,
}
#[glib::object_subclass]
impl ObjectSubclass for MusicusWelcomePage {
const NAME: &'static str = "MusicusWelcomePage";
type Type = super::MusicusWelcomePage;
type ParentType = adw::NavigationPage;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for MusicusWelcomePage {}
impl WidgetImpl for MusicusWelcomePage {}
impl NavigationPageImpl for MusicusWelcomePage {}
}
glib::wrapper! {
pub struct MusicusWelcomePage(ObjectSubclass<imp::MusicusWelcomePage>)
@extends gtk::Widget, adw::NavigationPage;
}
impl MusicusWelcomePage {
pub fn new() -> Self {
glib::Object::new()
}
}

View file

@ -1,3 +1,5 @@
use crate::welcome_page::MusicusWelcomePage;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
@ -7,11 +9,8 @@ mod imp {
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(resource = "/de/johrpan/musicus/window.ui")]
pub struct MusicusWindow {
// Template widgets
#[template_child]
pub header_bar: TemplateChild<gtk::HeaderBar>,
#[template_child]
pub label: TemplateChild<gtk::Label>,
pub navigation_view: TemplateChild<adw::NavigationView>,
}
#[glib::object_subclass]
@ -29,7 +28,14 @@ mod imp {
}
}
impl ObjectImpl for MusicusWindow {}
impl ObjectImpl for MusicusWindow {
fn constructed(&self) {
self.parent_constructed();
self.navigation_view.add(&MusicusWelcomePage::new());
}
}
impl WidgetImpl for MusicusWindow {}
impl WindowImpl for MusicusWindow {}
impl ApplicationWindowImpl for MusicusWindow {}
@ -38,7 +44,8 @@ mod imp {
glib::wrapper! {
pub struct MusicusWindow(ObjectSubclass<imp::MusicusWindow>)
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow, @implements gio::ActionGroup, gio::ActionMap;
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow,
@implements gio::ActionGroup, gio::ActionMap;
}
impl MusicusWindow {