2019-08-09 03:44:57 +03:00
|
|
|
use ::window::*;
|
2019-08-08 05:19:04 +03:00
|
|
|
use failure::Fallible;
|
2019-08-08 07:18:30 +03:00
|
|
|
|
|
|
|
struct MyWindow {
|
|
|
|
allow_close: bool,
|
2019-08-11 21:23:23 +03:00
|
|
|
cursor_pos: (u16, u16),
|
2019-08-08 07:18:30 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 11:25:57 +03:00
|
|
|
impl Drop for MyWindow {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
eprintln!("MyWindow dropped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 07:18:30 +03:00
|
|
|
impl WindowCallbacks for MyWindow {
|
|
|
|
fn can_close(&mut self) -> bool {
|
|
|
|
eprintln!("can I close?");
|
|
|
|
if self.allow_close {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.allow_close = true;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 11:25:57 +03:00
|
|
|
|
|
|
|
fn destroy(&mut self) {
|
|
|
|
eprintln!("destroy was called!");
|
|
|
|
Connection::get().unwrap().terminate_message_loop();
|
|
|
|
}
|
2019-08-09 21:15:09 +03:00
|
|
|
|
|
|
|
fn paint(&mut self, context: &mut dyn PaintContext) {
|
2019-08-11 20:15:08 +03:00
|
|
|
// Pick a purple background color
|
2019-08-09 22:03:24 +03:00
|
|
|
context.clear(Color::rgb(0x40, 0x20, 0x60));
|
2019-08-11 20:15:08 +03:00
|
|
|
|
|
|
|
// This line doesn't need anti-aliasing
|
|
|
|
context.draw_line(0, 0, 100, 100, Color::rgb(0xff, 0xff, 0xff), Operator::Over);
|
|
|
|
|
|
|
|
// This shallower line should need some
|
|
|
|
context.draw_line(
|
|
|
|
100,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
120,
|
2019-08-11 21:23:23 +03:00
|
|
|
Color::rgb(0xff, 0x80, 0xff),
|
|
|
|
Operator::Over,
|
|
|
|
);
|
|
|
|
|
|
|
|
context.draw_line(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
self.cursor_pos.0 as isize,
|
|
|
|
self.cursor_pos.1 as isize,
|
|
|
|
Color::rgb(0xff, 0xff, 0x80),
|
2019-08-11 20:15:08 +03:00
|
|
|
Operator::Over,
|
|
|
|
);
|
2019-08-09 22:03:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resize(&mut self, dims: Dimensions) {
|
|
|
|
eprintln!("resize {:?}", dims);
|
2019-08-09 21:15:09 +03:00
|
|
|
}
|
2019-08-10 20:00:47 +03:00
|
|
|
|
2019-08-11 21:23:23 +03:00
|
|
|
fn key_event(&mut self, key: &KeyEvent, ctx: &mut WindowContext) -> bool {
|
2019-08-10 20:00:47 +03:00
|
|
|
eprintln!("{:?}", key);
|
2019-08-11 21:23:23 +03:00
|
|
|
ctx.set_cursor(Some(MouseCursor::Text));
|
2019-08-10 20:00:47 +03:00
|
|
|
false
|
|
|
|
}
|
2019-08-10 21:40:17 +03:00
|
|
|
|
2019-08-11 21:23:23 +03:00
|
|
|
fn mouse_event(&mut self, event: &MouseEvent, ctx: &mut WindowContext) {
|
2019-08-10 21:40:17 +03:00
|
|
|
eprintln!("{:?}", event);
|
2019-08-11 21:23:23 +03:00
|
|
|
self.cursor_pos = (event.x, event.y);
|
|
|
|
ctx.invalidate();
|
|
|
|
ctx.set_cursor(Some(MouseCursor::Arrow));
|
2019-08-10 21:40:17 +03:00
|
|
|
}
|
2019-08-08 07:18:30 +03:00
|
|
|
}
|
2019-08-08 05:19:04 +03:00
|
|
|
|
|
|
|
fn main() -> Fallible<()> {
|
2019-08-09 03:44:57 +03:00
|
|
|
let conn = Connection::init()?;
|
|
|
|
|
2019-08-08 07:18:30 +03:00
|
|
|
let win = Window::new_window(
|
|
|
|
"myclass",
|
|
|
|
"the title",
|
|
|
|
800,
|
|
|
|
600,
|
2019-08-11 21:23:23 +03:00
|
|
|
Box::new(MyWindow {
|
|
|
|
allow_close: false,
|
|
|
|
cursor_pos: (100, 200),
|
|
|
|
}),
|
2019-08-08 07:18:30 +03:00
|
|
|
)?;
|
2019-08-08 05:19:04 +03:00
|
|
|
|
|
|
|
win.show();
|
2019-08-09 11:25:57 +03:00
|
|
|
drop(win);
|
2019-08-08 05:19:04 +03:00
|
|
|
|
2019-08-09 03:44:57 +03:00
|
|
|
conn.run_message_loop()
|
2019-08-08 05:19:04 +03:00
|
|
|
}
|