Synced project structure with krypt0nn/gtk-example-app

This commit is contained in:
Observer KRypt0n_ 2022-06-29 09:11:19 +02:00
parent 3f3ca086e2
commit 992f1ad02d
No known key found for this signature in database
GPG key ID: 844DA47BA25FE1E2
8 changed files with 78 additions and 68 deletions

2
.gitignore vendored
View file

@ -1,3 +1,3 @@
/target
/ui/.dist
/assets/ui/.dist
Cargo.lock

View file

Before

Width:  |  Height:  |  Size: 274 KiB

After

Width:  |  Height:  |  Size: 274 KiB

View file

@ -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;
}

View file

@ -74,7 +74,7 @@ Gtk.Box preferences {
Gtk.Box {
orientation: horizontal;
spacing: 8;
margin-top: 8;
margin-top: 16;
Gtk.Button {
label: "Revert patch";

View file

@ -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<T: ToString>(path: T) -> Result<String, String> {
// 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<T: ToString>(path: T) -> Result<String, String> {
}
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) => {

View file

@ -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: &gtk::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");

60
src/ui/main.rs Normal file
View file

@ -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: &gtk::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
}
}

View file

@ -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<T: IsA<gtk::glib::Object>>(builder: &gtk::Builder, name: &str) -> T {
builder.object::<T>(name).unwrap()