2019-08-18 09:00:23 +03:00
|
|
|
use ::window::*;
|
2020-01-16 14:39:44 +03:00
|
|
|
use promise::spawn::spawn;
|
2021-07-11 03:28:21 +03:00
|
|
|
use std::cell::RefCell;
|
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,
|
2021-07-11 03:28:21 +03:00
|
|
|
gl: Option<Rc<glium::backend::Context>>,
|
2019-08-18 09:00:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for MyWindow {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
eprintln!("MyWindow dropped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 03:28:21 +03:00
|
|
|
impl MyWindow {
|
2021-07-11 04:07:16 +03:00
|
|
|
fn dispatch(&mut self, event: WindowEvent, win: &Window) {
|
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?");
|
2021-07-11 03:28:21 +03:00
|
|
|
if self.allow_close {
|
2021-05-04 18:38:43 +03:00
|
|
|
win.close();
|
|
|
|
} else {
|
2021-07-11 03:28:21 +03:00
|
|
|
self.allow_close = true;
|
2021-05-04 18:38:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2021-07-11 03:28:21 +03:00
|
|
|
self.dims = dimensions;
|
2021-05-04 18:38:43 +03:00
|
|
|
}
|
|
|
|
WindowEvent::MouseEvent(event) => {
|
2021-07-11 03:28:21 +03:00
|
|
|
self.cursor_pos = event.coords;
|
|
|
|
// win.invalidate();
|
2021-05-04 18:38:43 +03:00
|
|
|
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 => {
|
2021-07-11 03:28:21 +03:00
|
|
|
if let Some(gl) = self.gl.as_mut() {
|
|
|
|
if gl.is_context_lost() {
|
|
|
|
eprintln!("opengl context was lost; should reinit");
|
|
|
|
return;
|
|
|
|
}
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-07-11 03:28:21 +03:00
|
|
|
let mut frame = glium::Frame::new(
|
|
|
|
Rc::clone(&gl),
|
|
|
|
(self.dims.pixel_width as u32, self.dims.pixel_height as u32),
|
|
|
|
);
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-07-11 03:28:21 +03:00
|
|
|
use glium::Surface;
|
|
|
|
frame.clear_color_srgb(0.25, 0.125, 0.375, 1.0);
|
|
|
|
win.finish_frame(frame).unwrap();
|
|
|
|
}
|
2021-05-04 18:38:43 +03:00
|
|
|
}
|
2021-07-18 09:57:59 +03:00
|
|
|
WindowEvent::AppearanceChanged(_)
|
|
|
|
| WindowEvent::Notification(_)
|
|
|
|
| WindowEvent::FocusChanged(_) => {}
|
2021-05-04 18:38:43 +03:00
|
|
|
}
|
2019-08-18 09:00:23 +03:00
|
|
|
}
|
2021-07-11 03:28:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn spawn_window() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let fontconfig = Rc::new(FontConfiguration::new(
|
|
|
|
None,
|
|
|
|
::window::default_dpi() as usize,
|
|
|
|
)?);
|
|
|
|
|
|
|
|
let state = Rc::new(RefCell::new(MyWindow {
|
|
|
|
allow_close: false,
|
|
|
|
cursor_pos: Point::new(100, 200),
|
|
|
|
dims: Dimensions {
|
|
|
|
pixel_width: 800,
|
|
|
|
pixel_height: 600,
|
|
|
|
dpi: 0,
|
|
|
|
},
|
|
|
|
gl: None,
|
|
|
|
}));
|
|
|
|
|
|
|
|
let cb_state = Rc::clone(&state);
|
|
|
|
let win = Window::new_window(
|
|
|
|
"myclass",
|
|
|
|
"the title",
|
|
|
|
800,
|
|
|
|
600,
|
|
|
|
None,
|
|
|
|
fontconfig,
|
2021-07-11 04:07:16 +03:00
|
|
|
move |event, window| {
|
2021-07-11 03:28:21 +03:00
|
|
|
let mut state = cb_state.borrow_mut();
|
2021-07-11 04:07:16 +03:00
|
|
|
state.dispatch(event, window)
|
2021-07-11 03:28:21 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
eprintln!("before show");
|
|
|
|
win.show();
|
|
|
|
let gl = win.enable_opengl().await?;
|
2019-08-18 09:00:23 +03:00
|
|
|
|
2021-07-11 03:28:21 +03:00
|
|
|
state.borrow_mut().gl.replace(gl);
|
|
|
|
win.invalidate();
|
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()
|
|
|
|
}
|