use gdk;
use gtk::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
pub struct Picker {
pub canvas: gtk::DrawingArea,
pub mouse_loc: Rc<RefCell<Option<(i32, i32)>>>,
}
impl Picker {
pub fn new() -> Picker {
let canvas = gtk::DrawingArea::new();
let mouse_loc = Rc::new(RefCell::new(Some((2, 2))));
let reader_mouse = mouse_loc.clone();
let writer_mouse = mouse_loc.clone();
canvas.connect_draw(move |cv, ctx| {
let w = cv.allocated_width();
let h = cv.allocated_height();
ctx.set_source_rgb(1.0, 1.0, 1.0);
ctx.rectangle(0.0, 0.0, w as f64, h as f64);
ctx.fill();
reader_mouse.borrow().map(|(x, y)| {
ctx.set_source_rgb(0.9, 0.9, 0.9);
ctx.rectangle(
x as f64 * 32.0,
y as f64 * 32.0,
32.0,
32.0,
);
ctx.fill();
});
gtk::Inhibit(false)
});
canvas.connect_motion_notify_event(move |cv, motion| {
let (x, y) = motion.position();
*writer_mouse.borrow_mut() =
Some((x as i32 / 32, y as i32 / 32));
cv.queue_draw();
gtk::Inhibit(false)
});
canvas.add_events(gdk::EventMask::POINTER_MOTION_MASK);
Picker {
canvas,
mouse_loc,
}
}
}