2019-08-18 09:00:23 +03:00
|
|
|
use ::window::*;
|
2020-01-16 14:39:44 +03:00
|
|
|
use promise::spawn::spawn;
|
2021-05-04 18:38:43 +03:00
|
|
|
use std::rc::Rc;
|
2019-08-18 09:00:23 +03:00
|
|
|
|
|
|
|
struct MyWindow {
|
|
|
|
allow_close: bool,
|
2019-11-24 03:46:46 +03:00
|
|
|
cursor_pos: Point,
|
2021-05-04 18:38:43 +03:00
|
|
|
dims: Dimensions,
|
2019-08-18 09:00:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for MyWindow {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
eprintln!("MyWindow dropped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:38:43 +03:00
|
|
|
async fn spawn_window() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let (win, events) = Window::new_window("myclass", "the title", 800, 600, None).await?;
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-05-04 18:38:43 +03:00
|
|
|
let mut state = MyWindow {
|
|
|
|
allow_close: false,
|
|
|
|
cursor_pos: Point::new(100, 200),
|
|
|
|
dims: Dimensions {
|
|
|
|
pixel_width: 800,
|
|
|
|
pixel_height: 600,
|
|
|
|
dpi: 0,
|
|
|
|
},
|
|
|
|
};
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-05-04 18:38:43 +03:00
|
|
|
eprintln!("before show");
|
|
|
|
win.show().await?;
|
|
|
|
let gl = win.enable_opengl().await?;
|
|
|
|
eprintln!("window is visible, do loop");
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-05-04 18:38:43 +03:00
|
|
|
while let Ok(event) = events.recv().await {
|
|
|
|
match event {
|
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
eprintln!("can I close?");
|
|
|
|
if state.allow_close {
|
|
|
|
win.close();
|
|
|
|
} else {
|
|
|
|
state.allow_close = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WindowEvent::Destroyed => {
|
|
|
|
eprintln!("destroy was called!");
|
|
|
|
Connection::get().unwrap().terminate_message_loop();
|
|
|
|
}
|
|
|
|
WindowEvent::Resized {
|
|
|
|
dimensions,
|
|
|
|
is_full_screen,
|
|
|
|
} => {
|
|
|
|
eprintln!("resize {:?} is_full_screen={}", dimensions, is_full_screen);
|
|
|
|
state.dims = dimensions;
|
|
|
|
}
|
|
|
|
WindowEvent::MouseEvent(event) => {
|
|
|
|
state.cursor_pos = event.coords;
|
|
|
|
win.invalidate();
|
|
|
|
win.set_cursor(Some(MouseCursor::Arrow));
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-05-04 18:38:43 +03:00
|
|
|
if event.kind == MouseEventKind::Press(MousePress::Left) {
|
|
|
|
eprintln!("{:?}", event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WindowEvent::KeyEvent(key) => {
|
|
|
|
eprintln!("{:?}", key);
|
|
|
|
win.set_cursor(Some(MouseCursor::Text));
|
|
|
|
win.default_key_processing(key);
|
|
|
|
}
|
|
|
|
WindowEvent::NeedRepaint => {
|
|
|
|
if gl.is_context_lost() {
|
|
|
|
eprintln!("opengl context was lost; should reinit");
|
|
|
|
break;
|
|
|
|
}
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-05-04 18:38:43 +03:00
|
|
|
let mut frame = glium::Frame::new(
|
|
|
|
Rc::clone(&gl),
|
|
|
|
(
|
|
|
|
state.dims.pixel_width as u32,
|
|
|
|
state.dims.pixel_height as u32,
|
|
|
|
),
|
|
|
|
);
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-05-04 18:38:43 +03:00
|
|
|
use glium::Surface;
|
|
|
|
frame.clear_color_srgb(0.25, 0.125, 0.375, 1.0);
|
|
|
|
win.finish_frame(frame)?;
|
|
|
|
}
|
|
|
|
WindowEvent::FocusChanged(_) => {}
|
|
|
|
}
|
2019-08-18 09:00:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-12-15 08:43:05 +03:00
|
|
|
fn main() -> anyhow::Result<()> {
|
2019-08-18 09:00:23 +03:00
|
|
|
let conn = Connection::init()?;
|
2020-01-16 14:39:44 +03:00
|
|
|
spawn(async {
|
2019-08-18 09:00:23 +03:00
|
|
|
eprintln!("running this async block");
|
2021-05-04 18:38:43 +03:00
|
|
|
dbg!(spawn_window().await).ok();
|
2019-11-23 10:37:02 +03:00
|
|
|
eprintln!("end of async block");
|
2020-10-05 10:06:01 +03:00
|
|
|
})
|
|
|
|
.detach();
|
2019-08-18 09:00:23 +03:00
|
|
|
conn.run_message_loop()
|
|
|
|
}
|