From 96d68fc72cc74fd0858846b9c0df0424faa46adb Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Sat, 14 Dec 2024 10:47:52 +0000 Subject: Moves UI definition to XML --- Cargo.lock | 10 +++++ schist_gtk4_gui/Cargo.toml | 3 ++ schist_gtk4_gui/build.rs | 7 ++++ schist_gtk4_gui/resources/resources.gresource.xml | 6 +++ schist_gtk4_gui/resources/window.ui | 13 ++++++ schist_gtk4_gui/src/lib.rs | 11 ++++- schist_gtk4_gui/src/window/imp.rs | 50 +++++++++++++++++++++++ schist_gtk4_gui/src/window/mod.rs | 24 +++++++---- 8 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 schist_gtk4_gui/build.rs create mode 100644 schist_gtk4_gui/resources/resources.gresource.xml create mode 100644 schist_gtk4_gui/resources/window.ui create mode 100644 schist_gtk4_gui/src/window/imp.rs diff --git a/Cargo.lock b/Cargo.lock index 8646c54..f6d2d98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -746,6 +746,15 @@ dependencies = [ "smallvec", ] +[[package]] +name = "glib-build-tools" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7029c2651d9b5d5a3eea93ec8a1995665c6d3a69ce9bf6042ad9064d134736d8" +dependencies = [ + "gio", +] + [[package]] name = "glib-macros" version = "0.20.5" @@ -1739,6 +1748,7 @@ dependencies = [ name = "schist_gtk4_gui" version = "0.1.0" dependencies = [ + "glib-build-tools", "gtk4", ] diff --git a/schist_gtk4_gui/Cargo.toml b/schist_gtk4_gui/Cargo.toml index 79fae5d..a5884cb 100644 --- a/schist_gtk4_gui/Cargo.toml +++ b/schist_gtk4_gui/Cargo.toml @@ -5,3 +5,6 @@ edition = "2021" [dependencies] gtk = { version = "0.9.4", package = "gtk4", features = ["v4_8"] } + +[build-dependencies] +glib-build-tools = "0.20.0" diff --git a/schist_gtk4_gui/build.rs b/schist_gtk4_gui/build.rs new file mode 100644 index 0000000..6c2f68b --- /dev/null +++ b/schist_gtk4_gui/build.rs @@ -0,0 +1,7 @@ +fn main() { + glib_build_tools::compile_resources( + &["resources"], + "resources/resources.gresource.xml", + "resources.gresource", + ); +} diff --git a/schist_gtk4_gui/resources/resources.gresource.xml b/schist_gtk4_gui/resources/resources.gresource.xml new file mode 100644 index 0000000..a74cfc9 --- /dev/null +++ b/schist_gtk4_gui/resources/resources.gresource.xml @@ -0,0 +1,6 @@ + + + + window.ui + + diff --git a/schist_gtk4_gui/resources/window.ui b/schist_gtk4_gui/resources/window.ui new file mode 100644 index 0000000..6fced19 --- /dev/null +++ b/schist_gtk4_gui/resources/window.ui @@ -0,0 +1,13 @@ + + + + diff --git a/schist_gtk4_gui/src/lib.rs b/schist_gtk4_gui/src/lib.rs index 67a3946..4c3055c 100644 --- a/schist_gtk4_gui/src/lib.rs +++ b/schist_gtk4_gui/src/lib.rs @@ -1,12 +1,19 @@ mod window; -use gtk::{glib, prelude::*, Application}; -use window::build_window; +use gtk::{gio, glib, prelude::*, Application}; const APP_ID: &str = "net.joeac.schist"; pub fn run() -> glib::ExitCode { + gio::resources_register_include!("resources.gresource") + .expect("failed to register resources"); + let app = Application::builder().application_id(APP_ID).build(); app.connect_activate(build_window); app.run() } + +fn build_window(app: &Application) { + let window = window::Window::new(app); + window.present() +} diff --git a/schist_gtk4_gui/src/window/imp.rs b/schist_gtk4_gui/src/window/imp.rs new file mode 100644 index 0000000..82e14b0 --- /dev/null +++ b/schist_gtk4_gui/src/window/imp.rs @@ -0,0 +1,50 @@ +use gtk::{ + glib::{ + self, + subclass::{ + object::{ + ObjectImpl, + ObjectImplExt, + }, + types::ObjectSubclass, InitializingObject, + }, + }, + subclass::{application_window::ApplicationWindowImpl, widget::{ + CompositeTemplateClass, CompositeTemplateInitializingExt, WidgetImpl + }, window::WindowImpl}, +}; + +#[derive(gtk::CompositeTemplate, Default)] +#[template(resource = "/net/joeac/schist/window.ui")] +pub struct Window { +} + +#[glib::object_subclass] +impl ObjectSubclass for Window { + const NAME: &'static str = "SchistWindow"; + type Type = super::Window; + type ParentType = gtk::ApplicationWindow; + + fn class_init(klass: &mut Self::Class) { + klass.bind_template(); + } + + fn instance_init(obj: &InitializingObject) { + obj.init_template(); + } +} + +impl ObjectImpl for Window { + fn constructed(&self) { + self.parent_constructed(); + } +} + +impl WidgetImpl for Window { +} + +impl WindowImpl for Window { +} + +impl ApplicationWindowImpl for Window { +} diff --git a/schist_gtk4_gui/src/window/mod.rs b/schist_gtk4_gui/src/window/mod.rs index fde5f42..9482184 100644 --- a/schist_gtk4_gui/src/window/mod.rs +++ b/schist_gtk4_gui/src/window/mod.rs @@ -1,10 +1,18 @@ -use gtk::{prelude::*, Application, ApplicationWindow}; +mod imp; -pub fn build_window(app: &Application) { - ApplicationWindow::builder() - .application(app) - .title("Schist") - .fullscreened(true) - .build() - .present() +use gtk::{gio, glib::{self, object::IsA}}; + +gtk::glib::wrapper! { + pub struct Window(ObjectSubclass) + @extends gtk::ApplicationWindow, gtk::Window, gtk::Widget, + @implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, + gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager; +} + +impl Window { + pub fn new>(app: &A) -> Self { + glib::Object::builder() + .property("application", app) + .build() + } } -- cgit v1.2.3