Initial commit

This commit is contained in:
Ward Nakchbandi (Cosmic Fusion) 2024-01-30 23:33:07 +03:00 committed by GitHub
commit 72d40e77fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 311 additions and 0 deletions

10
Cargo.toml Normal file
View File

@ -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"] }

21
LICENSE Normal file
View File

@ -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.

15
Makefile Normal file
View File

@ -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/

View File

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

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Generator: Adobe Illustrator 25.2.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 48 48" style="enable-background:new 0 0 48 48;" xml:space="preserve">
<g>
<polygon style="fill:#FFC400;" points="32.165,24.613 27.457,35.053 34.67,38.755 39.138,28.503 "/>
<polygon style="fill:#64DD17;" points="36.114,14.835 32.165,24.434 38.891,28.503 43.367,18.473 "/>
<polygon style="fill:#2979FF;" points="20.213,33.861 24.105,24 30.944,24.362 26.947,34.463 "/>
<polygon style="fill:#E53935;" points="29.326,10.775 24.506,22.985 31.16,24 36.114,12.657 "/>
<path style="fill:#212121;" d="M45.6,17.8c-2.604-3.471-6.137-5.272-9.508-6.178l-0.015-0.006l-0.001,0.002
c-2.49-0.668-4.883-0.844-6.751-0.842l-1.603,4.06c1.516-0.097,4.083-0.113,6.792,0.53l-3.302,7.924
c-2.442-0.689-4.717-0.832-6.523-0.772l-0.807,2.044c1.678-0.131,4.006-0.098,6.554,0.588l-3.381,8.114
c-1.864-0.466-3.995-0.714-6.377-0.585l-1.373,3.479c9.231-1.369,13.968,4.908,14.175,5.144l2.091,2.439l10.721-25.017L45.6,17.8z
M34.253,36.666c-1.255-0.938-3.021-2.016-5.271-2.822l3.359-8.063c1.733,0.682,3.512,1.698,5.21,3.19L34.253,36.666z
M38.373,27.053c-1.725-1.423-3.513-2.431-5.261-3.122l3.33-7.992c1.888,0.684,3.737,1.75,5.239,3.394L38.373,27.053z"/>
<polygon style="fill:#212121;" points="24.633,11.021 23.388,14.149 27.171,13.957 28.387,10.894 "/>
<polygon style="fill:#212121;" points="23.183,14.702 21.937,17.83 25.694,17.638 26.919,14.574 "/>
<polygon style="fill:#212121;" points="21.697,18.383 20.453,21.511 24.216,21.319 25.441,18.255 "/>
<polygon style="fill:#212121;" points="20.213,22.064 18.987,25.191 22.747,25 23.963,21.936 "/>
<polygon style="fill:#212121;" points="18.779,25.745 17.503,28.872 21.269,28.681 22.495,25.617 "/>
<polygon style="fill:#212121;" points="17.246,29.426 16.035,32.553 19.79,32.362 21.034,29.298 "/>
<polygon style="fill:#212121;" points="15.809,33.106 14.58,36.234 18.33,36.043 19.556,32.979 "/>
<polygon style="fill:#212121;" points="11.62,32.4 10.391,35.528 14.141,35.336 15.384,32.273 "/>
<polygon style="fill:#2979FF;" points="13.215,28.762 11.987,31.89 15.619,31.698 16.845,28.634 "/>
<polygon style="fill:#2979FF;" points="14.656,25.124 13.412,28.251 17.08,28.06 18.295,24.996 "/>
<polygon style="fill:#212121;" points="16.112,21.485 14.849,24.613 18.52,24.422 19.756,21.358 "/>
<polygon style="fill:#E53935;" points="17.565,17.847 16.331,20.975 19.99,20.783 21.197,17.719 "/>
<polygon style="fill:#E53935;" points="19.023,14.209 17.767,17.336 21.45,17.145 22.665,14.081 "/>
<polygon style="fill:#212121;" points="20.453,10.57 19.215,13.698 22.882,13.507 24.107,10.443 "/>
<polygon style="fill:#212121;" points="10.491,33.601 11.548,31 8.608,31 7.559,33.601 "/>
<polygon style="fill:#212121;" points="14.875,22.672 15.916,20.071 12.969,20.071 11.933,22.672 "/>
<polygon style="fill:#212121;" points="19.215,11.743 20.295,9.142 17.34,9.142 16.303,11.743 "/>
<polygon style="fill:#2979FF;" points="11.951,29.958 12.992,27.357 10.059,27.357 8.995,29.958 "/>
<polygon style="fill:#2979FF;" points="13.412,26.315 14.456,23.714 11.509,23.714 10.482,26.315 "/>
<polygon style="fill:#E53935;" points="16.331,19.029 17.371,16.428 14.429,16.428 13.411,19.029 "/>
<polygon style="fill:#E53935;" points="17.799,15.386 18.836,12.785 15.898,12.785 14.844,15.386 "/>
<polygon style="fill:#212121;" points="4.752,31.989 5.553,29.979 7.947,29.979 7.16,31.989 "/>
<polygon style="fill:#2979FF;" points="6.215,28.314 7.026,26.303 9.431,26.303 8.614,28.314 "/>
<polygon style="fill:#2979FF;" points="7.689,24.638 8.495,22.628 10.891,22.628 10.086,24.638 "/>
<polygon style="fill:#212121;" points="9.157,20.963 9.964,18.952 12.362,18.952 11.563,20.963 "/>
<polygon style="fill:#212121;" points="13.564,9.936 14.375,7.926 16.772,7.926 15.967,9.936 "/>
<polygon style="fill:#E53935;" points="10.635,17.287 11.437,15.277 13.834,15.277 13.029,17.287 "/>
<polygon style="fill:#E53935;" points="12.095,13.612 12.906,11.601 15.306,11.601 14.501,13.612 "/>
<polygon style="fill:#212121;" points="3.334,27.521 2.771,29.005 4.35,29.005 4.945,27.521 "/>
<polygon style="fill:#2979FF;" points="4.814,23.875 4.213,25.359 5.81,25.359 6.4,23.875 "/>
<polygon style="fill:#2979FF;" points="6.268,20.229 5.673,21.713 7.265,21.713 7.864,20.229 "/>
<polygon style="fill:#212121;" points="7.728,16.583 7.136,18.067 8.729,18.067 9.32,16.583 "/>
<polygon style="fill:#E53935;" points="9.184,12.937 8.596,14.421 10.185,14.421 10.784,12.937 "/>
<polygon style="fill:#E53935;" points="10.647,9.291 10.056,10.775 11.649,10.775 12.235,9.291 "/>
<polygon style="fill:#212121;" points="12.089,5.677 11.497,7.161 13.086,7.161 13.681,5.677 "/>
<polygon style="fill:#212121;" points="0.687,26.72 1.137,25.594 2.489,25.594 2.039,26.72 "/>
<polygon style="fill:#2979FF;" points="2.138,23.085 2.59,21.959 3.94,21.959 3.492,23.085 "/>
<polygon style="fill:#2979FF;" points="3.593,19.45 4.044,18.323 5.394,18.323 4.945,19.45 "/>
<polygon style="fill:#212121;" points="5.044,15.814 5.499,14.688 6.848,14.688 6.397,15.814 "/>
<polygon style="fill:#E53935;" points="6.497,12.179 6.954,11.052 8.301,11.052 7.851,12.179 "/>
<polygon style="fill:#E53935;" points="7.948,8.543 8.403,7.417 9.752,7.417 9.302,8.543 "/>
<polygon style="fill:#212121;" points="9.408,4.908 9.86,3.782 11.161,3.782 10.716,4.908 "/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<schemalist>
<schema id="org.cosmicfusion.example" path="/org/cosmicfusion/example/">
<key name="window-width" type="i">
<default>1200</default>
<summary>Default window width</summary>
</key>
<key name="window-height" type="i">
<default>600</default>
<summary>Default window height</summary>
</key>
<key name="is-maximized" type="b">
<default>false</default>
<summary>Default window maximized behaviour</summary>
</key>
</schema>
</schemalist>

119
src/build_ui.rs Normal file
View File

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

37
src/main.rs Normal file
View File

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

19
src/save_window_size.rs Normal file
View File

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

4
src/style.css Normal file
View File

@ -0,0 +1,4 @@
.bigREDTEXT {
font-size: 14px;
color: red;
}