From 992f1ad02de08fda9c0c7ba687890b181729fbc7 Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Wed, 29 Jun 2022 09:11:19 +0200 Subject: [PATCH] Synced project structure with `krypt0nn/gtk-example-app` --- .gitignore | 2 +- assets/{ => images}/icon.png | Bin {ui => assets/ui}/main.blp | 2 +- {ui => assets/ui}/preferences.blp | 2 +- build.rs | 14 +++---- src/main.rs | 62 ++---------------------------- src/ui/main.rs | 60 +++++++++++++++++++++++++++++ src/{lib => ui}/mod.rs | 4 ++ 8 files changed, 78 insertions(+), 68 deletions(-) rename assets/{ => images}/icon.png (100%) rename {ui => assets/ui}/main.blp (97%) rename {ui => assets/ui}/preferences.blp (99%) create mode 100644 src/ui/main.rs rename src/{lib => ui}/mod.rs (85%) diff --git a/.gitignore b/.gitignore index 798818e..ecf4752 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target -/ui/.dist +/assets/ui/.dist Cargo.lock diff --git a/assets/icon.png b/assets/images/icon.png similarity index 100% rename from assets/icon.png rename to assets/images/icon.png diff --git a/ui/main.blp b/assets/ui/main.blp similarity index 97% rename from ui/main.blp rename to assets/ui/main.blp index 4625362..5727e95 100644 --- a/ui/main.blp +++ b/assets/ui/main.blp @@ -29,7 +29,7 @@ Adw.ApplicationWindow window { Adw.PreferencesPage { Adw.PreferencesGroup { Gtk.Image { - file: "assets/icon.png"; + file: "assets/images/icon.png"; vexpand: true; margin-top: 48; } diff --git a/ui/preferences.blp b/assets/ui/preferences.blp similarity index 99% rename from ui/preferences.blp rename to assets/ui/preferences.blp index f976596..cdd7d97 100644 --- a/ui/preferences.blp +++ b/assets/ui/preferences.blp @@ -74,7 +74,7 @@ Gtk.Box preferences { Gtk.Box { orientation: horizontal; spacing: 8; - margin-top: 8; + margin-top: 16; Gtk.Button { label: "Revert patch"; diff --git a/build.rs b/build.rs index 0adcf8e..9b1a953 100644 --- a/build.rs +++ b/build.rs @@ -1,10 +1,10 @@ use std::process::{Command, Stdio}; -use std::fs::{self, read_dir, create_dir}; +use std::fs::{self, read_dir, create_dir_all}; use std::path::Path; fn compile_blueprint(path: T) -> Result { - // python blueprint-compiler/blueprint-compiler.py compile ui/main.blp - let output = Command::new("python") + // python3 blueprint-compiler/blueprint-compiler.py compile ui/main.blp + let output = Command::new("python3") .arg("blueprint-compiler/blueprint-compiler.py") .arg("compile") .arg(path.to_string()) @@ -27,9 +27,9 @@ fn compile_blueprint(path: T) -> Result { } fn main() { - if let Ok(entries) = read_dir("ui") { - if let Err(_) = read_dir("ui/.dist") { - create_dir("ui/.dist").expect("UI dist dir couldn't be created"); + if let Ok(entries) = read_dir("assets/ui") { + if let Err(_) = read_dir("assets/ui/.dist") { + create_dir_all("assets/ui/.dist").expect("UI dist dir couldn't be created"); } for entry in entries { @@ -39,7 +39,7 @@ fn main() { let entry_path = entry.path().to_str().unwrap().to_string(); let entry_filename = entry.file_name().to_str().unwrap().to_string(); - let entry_dist_path = format!("ui/.dist/{}.ui", &entry_filename[..entry_filename.len() - 4]); + let entry_dist_path = format!("assets/ui/.dist/{}.ui", &entry_filename[..entry_filename.len() - 4]); match compile_blueprint(&entry_path) { Ok(xml) => { diff --git a/src/main.rs b/src/main.rs index c5f70fc..4be13de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,63 +1,9 @@ use gtk4::{self as gtk, prelude::*}; -use libadwaita as adw; +use libadwaita::{self as adw, prelude::*}; -pub mod lib; +pub mod ui; -struct PreferencesPage { - pub preferences: gtk::Box, - pub preferences_go_back: gtk::Button -} - -impl PreferencesPage { - pub fn new() -> Self { - let builder = gtk::Builder::from_string(include_str!("../ui/.dist/preferences.ui")); - - let result = Self { - preferences: lib::get_object(&builder, "preferences"), - preferences_go_back: lib::get_object(&builder, "preferences_go_back") - }; - - result - } -} - -struct App { - pub window: adw::ApplicationWindow, - pub leaflet: adw::Leaflet, - pub launch_game: adw::SplitButton, - pub open_preferences: gtk::Button -} - -impl App { - pub fn new(app: >k::Application) -> Self { - // Create builder from UI file - let builder = gtk::Builder::from_string(include_str!("../ui/.dist/main.ui")); - - // Parse objects from builder - let result = Self { - window: lib::get_object(&builder, "window"), - leaflet: lib::get_object(&builder, "leaflet"), - launch_game: lib::get_object(&builder, "launch_game"), - open_preferences: lib::get_object(&builder, "open_preferences") - }; - - // Add preferences page to the leaflet - let page = PreferencesPage::new(); - let leaflet = result.leaflet.clone(); - - result.leaflet.append(&page.preferences).set_name(Some("preferences_page")); - - // Go back button for preferences page - page.preferences_go_back.connect_clicked(move |_| { - leaflet.navigate(adw::NavigationDirection::Back); - }); - - // Bind app to the window - result.window.set_application(Some(app)); - - result - } -} +use ui::MainApp; fn main() { gtk::init().expect("GTK initialization failed"); @@ -71,7 +17,7 @@ fn main() { // Init app window and show it application.connect_activate(|app| { - let app = App::new(app); + let app = MainApp::new(app); app.open_preferences.connect_clicked(move |_| { app.leaflet.set_visible_child_name("preferences_page"); diff --git a/src/ui/main.rs b/src/ui/main.rs new file mode 100644 index 0000000..4646644 --- /dev/null +++ b/src/ui/main.rs @@ -0,0 +1,60 @@ +use gtk4::{self as gtk, prelude::*}; +use libadwaita::{self as adw, prelude::*}; + +use super::get_object; + +pub struct PreferencesPage { + pub preferences: gtk::Box, + pub preferences_go_back: gtk::Button +} + +impl PreferencesPage { + pub fn new() -> Self { + let builder = gtk::Builder::from_string(include_str!("../../assets/ui/.dist/preferences.ui")); + + let result = Self { + preferences: get_object(&builder, "preferences"), + preferences_go_back: get_object(&builder, "preferences_go_back") + }; + + result + } +} + +pub struct App { + pub window: adw::ApplicationWindow, + pub leaflet: adw::Leaflet, + pub launch_game: adw::SplitButton, + pub open_preferences: gtk::Button +} + +impl App { + pub fn new(app: >k::Application) -> Self { + // Create builder from UI file + let builder = gtk::Builder::from_string(include_str!("../../assets/ui/.dist/main.ui")); + + // Parse objects from builder + let result = Self { + window: get_object(&builder, "window"), + leaflet: get_object(&builder, "leaflet"), + launch_game: get_object(&builder, "launch_game"), + open_preferences: get_object(&builder, "open_preferences") + }; + + // Add preferences page to the leaflet + let page = PreferencesPage::new(); + let leaflet = result.leaflet.clone(); + + result.leaflet.append(&page.preferences).set_name(Some("preferences_page")); + + // Go back button for preferences page + page.preferences_go_back.connect_clicked(move |_| { + leaflet.navigate(adw::NavigationDirection::Back); + }); + + // Bind app to the window + result.window.set_application(Some(app)); + + result + } +} diff --git a/src/lib/mod.rs b/src/ui/mod.rs similarity index 85% rename from src/lib/mod.rs rename to src/ui/mod.rs index 6576300..93d6279 100644 --- a/src/lib/mod.rs +++ b/src/ui/mod.rs @@ -1,5 +1,9 @@ use gtk4::{self as gtk, prelude::*}; +mod main; + +pub use main::App as MainApp; + /// This function loads object from builder or panics if it doesn't exist pub fn get_object>(builder: >k::Builder, name: &str) -> T { builder.object::(name).unwrap()