Browse Source

Wire up color

Getty Ritter 1 year ago
parent
commit
85b77233d2
3 changed files with 82 additions and 33 deletions
  1. 7 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 74 33
      src/main.rs

+ 7 - 0
Cargo.lock

@@ -388,6 +388,7 @@ dependencies = [
  "cairo-rs",
  "gio",
  "gtk",
+ "lazy_static",
  "pango",
  "pangocairo",
  "thiserror",
@@ -402,6 +403,12 @@ dependencies = [
  "either",
 ]
 
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
 [[package]]
 name = "libc"
 version = "0.2.74"

+ 1 - 0
Cargo.toml

@@ -11,6 +11,7 @@ pango = "*"
 pangocairo = "*"
 thiserror = "*"
 anyhow = "*"
+lazy_static = "*"
 
 [dependencies.cairo-rs]
 version = "*"

+ 74 - 33
src/main.rs

@@ -1,12 +1,16 @@
 use anyhow::{anyhow, bail, Result};
 use gio::prelude::{ApplicationExt, ApplicationExtManual};
-use gtk::{BoxExt, ButtonExt, ContainerExt, GtkWindowExt, WidgetExt};
+use gtk::{
+    BoxExt, ButtonExt, ContainerExt, EditableSignals, EntryExt, GtkWindowExt, LabelExt, WidgetExt,
+};
 use std::cell::RefCell;
 use std::rc::Rc;
 
+mod dmc;
+
 struct AppData {
     symbol: char,
-    color: (f64, f64, f64),
+    dmc: Option<dmc::DMC>,
 }
 
 impl AppData {
@@ -21,7 +25,8 @@ impl AppData {
     }
 
     fn render(&self, ctx: &cairo::Context) -> Result<()> {
-        ctx.set_source_rgb(self.color.0, self.color.1, self.color.2);
+        let color = self.color();
+        ctx.set_source_rgb(color.0, color.1, color.2);
         ctx.paint();
 
         ctx.set_source_rgb(0., 0., 0.);
@@ -38,6 +43,12 @@ impl AppData {
         pangocairo::functions::show_layout(&ctx, &layout);
         Ok(())
     }
+
+    fn color(&self) -> (f64, f64, f64) {
+        self.dmc
+            .map(|dmc| dmc.color)
+            .unwrap_or_else(|| (0.5, 0.5, 0.5))
+    }
 }
 
 #[derive(Clone)]
@@ -49,19 +60,23 @@ impl App {
     fn new() -> App {
         let app_data = AppData {
             symbol: 'X',
-            color: (0.5, 0.5, 0.5),
+            dmc: None,
         };
         let data = Rc::new(RefCell::new(app_data));
         App { data }
     }
 }
 
-fn mk_app(gtk_app: &gtk::Application) {
+fn mk_app() {
     let app = App::new();
-    let window = gtk::ApplicationWindow::new(gtk_app);
+    let window = gtk::Window::new(gtk::WindowType::Toplevel);
+    window.connect_delete_event(move |_, _| {
+        gtk::main_quit();
+        gtk::Inhibit(false)
+    });
 
     window.set_title("I Am Legend");
-    window.set_default_size(300, 100);
+    window.set_default_size(500, 100);
     window.set_resizable(false);
     let container = gtk::Box::new(gtk::Orientation::Vertical, 4);
     container.pack_start(&mk_icon_choice('A', app.clone()), false, true, 0);
@@ -71,24 +86,53 @@ fn mk_app(gtk_app: &gtk::Application) {
 
     let flow = gtk::Box::new(gtk::Orientation::Horizontal, 2);
     flow.pack_start(&container, false, true, 0);
-    let button = gtk::Button::with_label("Click me!");
-    flow.pack_start(&button, false, true, 0);
-
-    let canvas = gtk::DrawingArea::new();
-    let copy = app.clone();
-    canvas.connect_draw(move |_cv, ctx| {
-        if let Err(err) = copy.data.borrow_mut().render(ctx) {
-            eprintln!("Error in rendering: {}", err);
-        }
-        gtk::Inhibit(false)
-    });
-    flow.pack_start(&canvas, true, true, 0);
 
-    button.connect_clicked(move |_| {
-        if let Err(err) = app.data.borrow_mut().draw_to_file() {
-            eprintln!("Error in rendering: {}", err);
-        }
-    });
+    let container = gtk::Box::new(gtk::Orientation::Vertical, 2);
+    let label = gtk::Label::new(None);
+
+    {
+        let color = gtk::Entry::new();
+        let app = app.clone();
+        let window = window.clone();
+        let label = label.clone();
+        color.connect_changed(move |s| {
+            let str = s.get_text();
+            if let Some(color) = dmc::LOOKUP.get(str.as_str()) {
+                app.data.borrow_mut().dmc = Some(*color.clone());
+                label.set_text(color.name);
+            } else {
+                app.data.borrow_mut().dmc = None;
+                label.set_text("");
+            }
+            window.queue_draw_area(0, 0, 500, 100);
+        });
+        container.pack_start(&color, false, true, 0);
+    }
+
+    {
+        let button = gtk::Button::with_label("EXPORT IT");
+        let app = app.clone();
+        button.connect_clicked(move |_| {
+            if let Err(err) = app.data.borrow_mut().draw_to_file() {
+                eprintln!("Error in rendering: {}", err);
+            }
+        });
+        container.pack_start(&button, false, true, 0);
+    }
+
+    container.pack_start(&label, false, true, 0);
+    flow.pack_start(&container, false, true, 0);
+
+    {
+        let canvas = gtk::DrawingArea::new();
+        canvas.connect_draw(move |_cv, ctx| {
+            if let Err(err) = app.data.borrow_mut().render(ctx) {
+                eprintln!("Error in rendering: {}", err);
+            }
+            gtk::Inhibit(false)
+        });
+        flow.pack_start(&canvas, true, true, 0);
+    }
 
     window.add(&flow);
     window.show_all();
@@ -104,13 +148,10 @@ fn mk_icon_choice(choice: char, cell: App) -> gtk::Button {
 }
 
 fn main() {
-    let application =
-        gtk::Application::new(Some("com.github.gtk-rs.examples.basic"), Default::default())
-            .expect("failed to initialize GTK application");
-
-    application.connect_activate(|app| {
-        mk_app(app);
-    });
-
-    application.run(&[]);
+    if gtk::init().is_err() {
+        eprintln!("Failed to initialize GTK application");
+    } else {
+        mk_app();
+        gtk::main();
+    }
 }