From 13de707baa54c40600832e7ca19c47663f079a31 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Mon, 8 Aug 2022 21:31:46 +0200 Subject: [PATCH] Added icon loading from "icon" file, added `--run-game` argument --- assets/ui/main.blp | 2 +- src/main.rs | 49 +++++++++++++++++++++++++++++++++++++++++----- src/ui/main.rs | 10 +++++++--- src/ui/mod.rs | 8 ++++---- 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/assets/ui/main.blp b/assets/ui/main.blp index 6c54d76..79f3bdf 100644 --- a/assets/ui/main.blp +++ b/assets/ui/main.blp @@ -40,7 +40,7 @@ Adw.ApplicationWindow window { visible: false; Adw.PreferencesGroup { - Gtk.Image { + Gtk.Image icon { resource: "/org/app/assets/images/icon.png"; vexpand: true; diff --git a/src/main.rs b/src/main.rs index 9fb2ba6..656ddfd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ use libadwaita as adw; use gtk::{CssProvider, StyleContext, STYLE_PROVIDER_PRIORITY_APPLICATION}; use gtk::gdk::Display; +use gtk::glib; +use gtk::glib::clone; use std::path::Path; use std::fs; @@ -25,8 +27,8 @@ fn main() { .expect("Failed to register resources"); // Set application's title - gtk::glib::set_application_name("An Anime Game Launcher"); - gtk::glib::set_program_name(Some("An Anime Game Launcher")); + glib::set_application_name("An Anime Game Launcher"); + glib::set_program_name(Some("An Anime Game Launcher")); // Create app let application = gtk::Application::new( @@ -34,8 +36,27 @@ fn main() { Default::default() ); + application.add_main_option( + "run-game", + glib::Char::from(0), + glib::OptionFlags::empty(), + glib::OptionArg::None, + "Run the game", + None + ); + + let run_game = std::rc::Rc::new(std::cell::Cell::new(false)); + + application.connect_handle_local_options(clone!(@strong run_game => move |_, arg| { + if arg.contains("run-game") { + run_game.set(true); + } + + -1 + })); + // Init app window and show it - application.connect_activate(|app| { + application.connect_activate(move |app| { // Apply CSS styles to the application let provider = CssProvider::new(); @@ -65,10 +86,28 @@ fn main() { anime_game_core::consts::set_game_edition(config.launcher.edition.into()); - // Load and show main window + // Load main window let main = MainApp::new(app).expect("Failed to init MainApp"); - main.show(); + // Load initial launcher state + let awaiter = main.update_state(); + + if !run_game.get() { + main.show(); + } + + else { + awaiter.then(move |state| { + match state.as_ref().expect("Failed to load launcher state") { + lib::launcher::states::LauncherState::Launch => { + main.update(ui::main::Actions::PerformButtonEvent).unwrap(); + + std::process::exit(0); + }, + _ => main.show() + } + }); + } } }); diff --git a/src/ui/main.rs b/src/ui/main.rs index 1f0822d..96b422a 100644 --- a/src/ui/main.rs +++ b/src/ui/main.rs @@ -45,6 +45,7 @@ pub struct AppWidgets { pub status_page: adw::StatusPage, pub launcher_content: adw::PreferencesPage, + pub icon: gtk::Image, pub launch_game: gtk::Button, pub open_preferences: gtk::Button, @@ -71,6 +72,7 @@ impl AppWidgets { status_page: get_object(&builder, "status_page")?, launcher_content: get_object(&builder, "launcher_content")?, + icon: get_object(&builder, "icon")?, launch_game: get_object(&builder, "launch_game")?, open_preferences: get_object(&builder, "open_preferences")?, @@ -87,6 +89,11 @@ impl AppWidgets { if crate::APP_DEBUG { result.window.add_css_class("devel"); } + + // Load icon from "icon" file if it exists + if std::path::Path::new("icon").exists() { + result.icon.set_from_file(Some("icon")); + } // Set default About Dialog values if crate::APP_DEBUG { @@ -198,9 +205,6 @@ impl App { // Bind app to the window result.widgets.window.set_application(Some(app)); - // Load initial launcher state - result.update_state(); - Ok(result) } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 54630e3..d71bc54 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,10 +1,10 @@ use gtk4::{self as gtk, prelude::*}; use libadwaita as adw; -mod first_run; -mod main; -mod preferences; -mod traits; +pub mod first_run; +pub mod main; +pub mod preferences; +pub mod traits; pub mod components;