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;
|
2021-06-27 08:57:48 +03:00
|
|
|
use wezterm_font::FontConfiguration;
|
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>> {
|
2021-06-27 08:57:48 +03:00
|
|
|
let fontconfig = Rc::new(FontConfiguration::new(
|
|
|
|
None,
|
|
|
|
::window::default_dpi() as usize,
|
|
|
|
)?);
|
|
|
|
let (win, events) =
|
|
|
|
Window::new_window("myclass", "the title", 800, 600, None, fontconfig).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 {
|
2021-05-04 19:24:39 +03:00
|
|
|
match dbg!(event) {
|
2021-05-04 18:38:43 +03:00
|
|
|
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)?;
|
|
|
|
}
|
2021-05-06 19:04:16 +03:00
|
|
|
WindowEvent::Notification(_) | WindowEvent::FocusChanged(_) => {}
|
2021-05-04 18:38:43 +03:00
|
|
|
}
|
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()
|
|
|
|
}
|