From d217d683532845efacaffe071d68addccacc0585 Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 24 Jan 2021 18:06:29 +0100 Subject: Add nice rustgui example --- rustgui/.gitignore | 1 + rustgui/Cargo.lock | 277 ++++++++++++++++++++++++++++++++++++++++++++++++++++ rustgui/Cargo.toml | 10 ++ rustgui/src/main.rs | 132 +++++++++++++++++++++++++ 4 files changed, 420 insertions(+) create mode 100644 rustgui/.gitignore create mode 100644 rustgui/Cargo.lock create mode 100644 rustgui/Cargo.toml create mode 100644 rustgui/src/main.rs diff --git a/rustgui/.gitignore b/rustgui/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/rustgui/.gitignore @@ -0,0 +1 @@ +/target diff --git a/rustgui/Cargo.lock b/rustgui/Cargo.lock new file mode 100644 index 0000000..5385439 --- /dev/null +++ b/rustgui/Cargo.lock @@ -0,0 +1,277 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "atk-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c69658a4e18d5c9575f716e24559645d08a4044d6946c30c2e0025952c84d842" +dependencies = [ + "bitflags", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", +] + +[[package]] +name = "bitflags" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4efd02e230a02e18f92fc2735f44597385ed02ad8f831e7c1c1156ee5e1ab3a5" + +[[package]] +name = "c_vec" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6237ac5a4b1e81c213c24c6437964c61e646df910a914b4ab1487b46df20bd13" + +[[package]] +name = "cairo-rs" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d336f1b2ff46c17475a14360de7f456707008da475c54824887e52e453ab00" +dependencies = [ + "c_vec", + "cairo-sys-rs", + "glib", + "glib-sys", + "libc", + "winapi", +] + +[[package]] +name = "cairo-sys-rs" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e8a1e2a76ac09b959788c2c30a355d693ce6f7f7d7268f6d1dd5d8c3359c521" +dependencies = [ + "libc", + "pkg-config", + "winapi", +] + +[[package]] +name = "gdk" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30018ecbbb1e6f1d59c4024ec08675850744b799abc5420be0629ac9ba0abd2" +dependencies = [ + "bitflags", + "cairo-rs", + "cairo-sys-rs", + "gdk-pixbuf", + "gdk-sys", + "gio", + "glib", + "glib-sys", + "gobject-sys", + "libc", + "pango", +] + +[[package]] +name = "gdk-pixbuf" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf05dab73febcc6e90abaff8f24cfe1cf1bd2222cd648ddfe337bf3b994489f" +dependencies = [ + "gdk-pixbuf-sys", + "glib", + "glib-sys", + "gobject-sys", + "libc", +] + +[[package]] +name = "gdk-pixbuf-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eb441420653b33e5a29d13227ea34995383e65bf4f33b16492ec95e44a8996" +dependencies = [ + "bitflags", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", +] + +[[package]] +name = "gdk-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "679d86da2a4522a623e3ce4b67f2126a12e057a1f7269eee7028199f78b5a854" +dependencies = [ + "bitflags", + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", + "pkg-config", +] + +[[package]] +name = "gio" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7bc3126c94109e65871e4228b990d1ea2953259483d5b06eb96e8b36a7bf196" +dependencies = [ + "bitflags", + "gio-sys", + "glib", + "glib-sys", + "gobject-sys", + "libc", +] + +[[package]] +name = "gio-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "189969f8189604c371d42b613d928c9d17fcfbf6e175d6b0ce9475a950f76dc6" +dependencies = [ + "bitflags", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", +] + +[[package]] +name = "glib" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4da1d7f4bdc5c708d8ce4df1ac440dcb2f9d97d937c989032185a48aeef1d10" +dependencies = [ + "bitflags", + "glib-sys", + "gobject-sys", + "lazy_static", + "libc", +] + +[[package]] +name = "glib-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdd7d911c5dc610aabe37caae7d3b9d2cfe6d8f4c85ff4c062f3d6f490e75067" +dependencies = [ + "bitflags", + "libc", + "pkg-config", +] + +[[package]] +name = "gobject-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edc95561e538381576425264a4ddd08c65d5da218f10b2a47b4479dd147775da" +dependencies = [ + "bitflags", + "glib-sys", + "libc", + "pkg-config", +] + +[[package]] +name = "gtk" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce17f98e7dcdc9d06b3a5f7621d796a24937c04953481205b1be267c5a02697a" +dependencies = [ + "bitflags", + "cairo-rs", + "cairo-sys-rs", + "gdk", + "gdk-pixbuf", + "gdk-pixbuf-sys", + "gdk-sys", + "gio", + "gio-sys", + "glib", + "glib-sys", + "gobject-sys", + "gtk-sys", + "libc", + "pango", +] + +[[package]] +name = "gtk-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854b56ce6d6b05945f7735651482835c5ac1f8582142ce67306726259a3dafb0" +dependencies = [ + "atk-sys", + "bitflags", + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gdk-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", + "pkg-config", +] + +[[package]] +name = "lazy_static" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" + +[[package]] +name = "libc" +version = "0.2.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" + +[[package]] +name = "pango" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5afa4b4c5380315b12075e7767d9bdd62d53beeb6087d9287ef6990e57a6b643" +dependencies = [ + "bitflags", + "glib", + "glib-sys", + "gobject-sys", + "libc", + "pango-sys", +] + +[[package]] +name = "pango-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ec8d90306b5ff43f5836f4363267ea95be02b3df71d2b31ba8fbb1680bdee1" +dependencies = [ + "bitflags", + "glib-sys", + "gobject-sys", + "libc", + "pkg-config", +] + +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + +[[package]] +name = "rustgui" +version = "0.1.0" +dependencies = [ + "gtk", +] + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/rustgui/Cargo.toml b/rustgui/Cargo.toml new file mode 100644 index 0000000..0779cc6 --- /dev/null +++ b/rustgui/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rustgui" +version = "0.1.0" +authors = ["Julian T "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +gtk = { version = "0.2", features = ["v3_22"] } diff --git a/rustgui/src/main.rs b/rustgui/src/main.rs new file mode 100644 index 0000000..9a449eb --- /dev/null +++ b/rustgui/src/main.rs @@ -0,0 +1,132 @@ +extern crate gtk; +use gtk::*; +use std::process; +use std::sync::atomic::{ AtomicUsize, Ordering }; +use std::sync::Arc; + +// TODO listen for connect_delete_event + +pub struct App { + pub window: Window, + pub header: Header, + pub content: Content, +} + +pub struct Header { + pub container: HeaderBar, + pub reset: Button, +} + +pub struct Content { + pub container: Box, + pub add: Button, + pub counter: Label, +} + +pub struct Counter(AtomicUsize); + +impl App { + fn new(counter: &Counter) -> App { + let window = Window::new(WindowType::Toplevel); + let header = Header::new(); + let content = Content::new(counter); + + // Define windows stuff + window.set_titlebar(&header.container); + window.set_title("Hello world!"); + window.set_wmclass("test-app", "test app"); + Window::set_default_icon_name("iconname"); + + window.add(&content.container); + + window.connect_delete_event(move |_, _| { + main_quit(); + Inhibit(false) + }); + + App { window, header, content} + } +} + +impl Content { + fn new(counter: &Counter) -> Content { + let container = Box::new(Orientation::Horizontal, 0); + + let counter = Label::new(counter.get_count().to_string().as_str()); + let add = Button::new_with_label("Add"); + + add.get_style_context().map(|c| c.add_class("suggested-action")); + + container.pack_start(&counter, true, true, 0); + container.pack_start(&add, false, false, 0); + + Content { container, counter, add} + } +} + +impl Header { + fn new() -> Header { + let container = HeaderBar::new(); + let reset = Button::new_with_label("Reset"); + + container.set_title("Hello world!"); + container.set_show_close_button(true); + + reset.get_style_context().map(|c| c.add_class("destructive-action")); + container.pack_start(&reset); + + Header { container, reset } + } +} + +impl Counter { + fn new() -> Counter { + Counter(AtomicUsize::new(0)) + } + + fn get_count(&self) -> usize { self.0.load(Ordering::SeqCst) } + + fn reset(&self) { + self.0.store(0, Ordering::SeqCst); + } + + fn count(&self) -> usize { + let before = self.0.fetch_add(1, Ordering::SeqCst); + before + 1 + } +} + +fn main() { + println!("Hello, world!"); + + let c = Arc::new(Counter::new()); + + if gtk::init().is_err() { + eprintln!("failed to init GTK"); + process::exit(1); + } + + let app = App::new(&c); + + { + let counter = c.clone(); + let info = app.content.counter.clone(); + app.header.reset.clone().connect_clicked(move |_| { + counter.reset(); + info.set_label("0"); + }); + } + + { + let counter = c.clone(); + let info = app.content.counter.clone(); + app.content.add.clone().connect_clicked(move |_| { + let new = counter.count(); + info.set_label(new.to_string().as_str()); + }); + } + + app.window.show_all(); + + gtk::main(); +} -- cgit v1.2.3