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