From 72d40e77fc32b494c31de44a7f018eb5c1767407 Mon Sep 17 00:00:00 2001 From: "Ward Nakchbandi (Cosmic Fusion)" <83735213+CosmicFusion@users.noreply.github.com> Date: Tue, 30 Jan 2024 23:33:07 +0300 Subject: [PATCH] Initial commit --- Cargo.toml | 10 +++ LICENSE | 21 +++++ Makefile | 15 ++++ data/org.cosmicfusion.example.desktop | 10 +++ data/org.cosmicfusion.example.svg | 59 +++++++++++++ data/org.cosmicfusion.example.xml | 17 ++++ src/build_ui.rs | 119 ++++++++++++++++++++++++++ src/main.rs | 37 ++++++++ src/save_window_size.rs | 19 ++++ src/style.css | 4 + 10 files changed, 311 insertions(+) create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 data/org.cosmicfusion.example.desktop create mode 100644 data/org.cosmicfusion.example.svg create mode 100644 data/org.cosmicfusion.example.xml create mode 100644 src/build_ui.rs create mode 100644 src/main.rs create mode 100644 src/save_window_size.rs create mode 100644 src/style.css diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..103e33d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "gtk4-rs-adw-project-template" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +adw = { version = "0.5.3", package = "libadwaita", features = ["v1_4"] } +gtk = { version = "0.7.3", package = "gtk4", features = ["v4_12"] } diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..08b553f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Ward Nakchbandi (Cosmic Fusion) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..df90481 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +all: + true + +install: + mkdir -p $(DESTDIR)/usr/bin/ + cargo fetch + cargo build --release + cp -vf target/release/gtk4-rs-adw-project-template $(DESTDIR)/usr/bin/ + chmod 755 $(DESTDIR)/usr/bin/gtk4-rs-adw-project-template + mkdir -p $(DESTDIR)/usr/share/glib-2.0/schemas/ + cp data/org.cosmicfusion.example.xml $(DESTDIR)/usr/share/glib-2.0/schemas/ + mkdir -p $(DESTDIR)/usr/share/applications + cp -vf data/org.cosmicfusion.example.desktop $(DESTDIR)/usr/share/applications/ + mkdir -p $(DESTDIR)/usr/share/icons/hicolor/scalable/apps + cp -vf data/org.cosmicfusion.example.svg $(DESTDIR)/usr/share/icons/hicolor/scalable/apps/ diff --git a/data/org.cosmicfusion.example.desktop b/data/org.cosmicfusion.example.desktop new file mode 100644 index 0000000..767321a --- /dev/null +++ b/data/org.cosmicfusion.example.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Name=Libadwaita gtk4 example window in rust +Name[en_US]=Libadwaita gtk4 example window in rust +Comment=An Example Libadwaita and gtk4 application written in rust +Exec=gtk4-rs-adw-project-template +Icon=org.cosmicfusion.example +Terminal=false +Type=Application +Encoding=UTF-8 +StartupNotify=true diff --git a/data/org.cosmicfusion.example.svg b/data/org.cosmicfusion.example.svg new file mode 100644 index 0000000..f105031 --- /dev/null +++ b/data/org.cosmicfusion.example.svg @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/data/org.cosmicfusion.example.xml b/data/org.cosmicfusion.example.xml new file mode 100644 index 0000000..63add9c --- /dev/null +++ b/data/org.cosmicfusion.example.xml @@ -0,0 +1,17 @@ + + + + + 1200 + Default window width + + + 600 + Default window height + + + false + Default window maximized behaviour + + + diff --git a/src/build_ui.rs b/src/build_ui.rs new file mode 100644 index 0000000..46280ff --- /dev/null +++ b/src/build_ui.rs @@ -0,0 +1,119 @@ +// GTK crates +/// Use all gtk4 libraries (gtk4 -> gtk because cargo) +/// Use all libadwaita libraries (libadwaita -> adw because cargo) +use gtk::prelude::*; +use gtk::*; +use adw::prelude::*; +use adw::*; +use glib::*; +use gdk::Display; + +// application crates +use crate::save_window_size; + +pub fn build_ui(app: &adw::Application) { + + // setup glib + gtk::glib::set_prgname(Some("Cosmo libAdwaita in RS Template")); + glib::set_application_name("Cosmo libAdwaita in RS Template"); + let glib_settings = gio::Settings::new("com.github.adw-rs-test.cosmo"); + + // Create a label called "_warning_label" + let _warning_label = Label::builder() + // Label Text + .label("DO NOT LISTEN TO THE BUTTON!") + // Add Space to the top + .margin_top(12) + // Add Space to the buttom + .margin_bottom(12) + // Add Space to the left + .margin_start(12) + // Add Space to the right + .margin_end(12) + // build the button + .build(); + + // Create a button called "_click_me_button" + let _click_me_button = Button::builder() + // Button Label + .label("Click ME") + // Add Space to the top + .margin_top(12) + // Add Space to the buttom + .margin_bottom(12) + // Add Space to the left + .margin_start(12) + // Add Space to the right + .margin_end(12) + // build the button + .build(); + + // Create A box + let _main_box = gtk::Box::builder() + // that puts items vertically + .orientation(Orientation::Vertical) + .build(); + + // Add adwaita title box + let window_title_bar = gtk::HeaderBar::builder() + .show_title_buttons(true) + .build(); + + _main_box.append(&window_title_bar); + + // Add the "_warning_label" to "_main_box" + _main_box.append(&_warning_label); + // Add the "_click_me_button" to "_main_box" + _main_box.append(&_click_me_button); + + + + + // create the main Application window + let window = adw::ApplicationWindow::builder() + // The text on the titlebar + .title("Libadwaita gtk4 example window in rust") + // link it to the application "app" + .application(app) + // Add the box called "_main_box" to it + .content(&_main_box) + // Application icon + .icon_name("org.cosmicfusion.example") + // Get current size from glib + .default_width(glib_settings.int("window-width")) + .default_height(glib_settings.int("window-height")) + // Minimum Size/Default + .width_request(700) + .height_request(500) + // Hide window instead of destroy + .hide_on_close(true) + // Startup + .startup_id("org.cosmicfusion.example") + // build the window + .build(); + + // glib maximization + if glib_settings.boolean("is-maximized") == true { + window.maximize() + } + + // Connects the clicking of "_click_me_button" to the external function "print_why" and idk why but everyone tells me to be "move |_| " before the external function + /// and instead of () we put an aurgment for the target label with & before it so it's" + /// print_why() -> print_why(&_warning_label) + _click_me_button.connect_clicked(move |_| print_why(&_warning_label)); + + // Connect the hiding of window to the save_window_size function and window destruction + window.connect_hide(clone!(@weak window => move |_| save_window_size(&window, &glib_settings))); + window.connect_hide(clone!(@weak window => move |_| window.destroy())); + + // show the window + window.present() +} + + +// an external function to be called via "_click_me_button.connect_clicked" +fn print_why(label: &Label) { + // takes the aurgument from "_click_me_button.connect_clicked" which should be a label amd sets its text to "Why would you :(" + label.set_text("Why would you :("); + label.add_css_class("bigREDTEXT"); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..53fa9b0 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,37 @@ +// GTK crates +/// Use all gtk4 libraries (gtk4 -> gtk because cargo) +/// Use all libadwaita libraries (libadwaita -> adw because cargo) +use gtk::prelude::*; +use gtk::*; +use adw::prelude::*; +use adw::*; +use glib::*; +use gdk::Display; + +// application crates +mod build_ui; +use crate::build_ui::build_ui; +mod save_window_size; +use crate::save_window_size::save_window_size; + +/// main function +fn main() { + let application = adw::Application::new(Some("org.cosmicfusion.example"), Default::default()); + application.connect_startup(|app| { + // The CSS "magic" happens here. + let provider = CssProvider::new(); + provider.load_from_string(include_str!("style.css")); + // We give the CssProvided to the default screen so the CSS rules we added + // can be applied to our window. + gtk::style_context_add_provider_for_display( + &Display::default().expect("Could not connect to a display."), + &provider, + STYLE_PROVIDER_PRIORITY_APPLICATION, + ); + + app.connect_activate(build_ui); + }); + + application.run(); +} + diff --git a/src/save_window_size.rs b/src/save_window_size.rs new file mode 100644 index 0000000..92210f0 --- /dev/null +++ b/src/save_window_size.rs @@ -0,0 +1,19 @@ +// GTK crates +/// Use all gtk4 libraries (gtk4 -> gtk because cargo) +/// Use all libadwaita libraries (libadwaita -> adw because cargo) +use gtk::prelude::*; +use gtk::*; +use adw::prelude::*; +use adw::*; +use glib::*; +use gdk::Display; + +// Save current window size to glib +pub fn save_window_size(window: &adw::ApplicationWindow, glib_settings: &gio::Settings) { + + let size = window.default_size(); + + glib_settings.set_int("window-width", size.0); + glib_settings.set_int("window-height", size.1); + glib_settings.set_boolean("is-maximized", window.is_maximized()); +} diff --git a/src/style.css b/src/style.css new file mode 100644 index 0000000..4ca23dd --- /dev/null +++ b/src/style.css @@ -0,0 +1,4 @@ +.bigREDTEXT { + font-size: 14px; + color: red; +}