1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-19 03:11:31 +03:00
wezterm/window/examples/basic.rs

116 lines
2.7 KiB
Rust
Raw Normal View History

2019-08-09 03:44:57 +03:00
use ::window::*;
use std::any::Any;
2019-08-08 07:18:30 +03:00
struct MyWindow {
allow_close: bool,
cursor_pos: Point,
2019-08-08 07:18:30 +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
}
}
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) {
// Pick a purple background color
2019-08-09 22:03:24 +03:00
context.clear(Color::rgb(0x40, 0x20, 0x60));
// This line doesn't need anti-aliasing
2019-09-21 09:53:36 +03:00
context.draw_line(
2019-09-23 18:01:27 +03:00
Point::new(0, 0),
Point::new(100, 100),
2019-09-21 09:53:36 +03:00
Color::rgb(0xff, 0xff, 0xff),
Operator::Over,
);
// This shallower line should need some
context.draw_line(
2019-09-23 18:01:27 +03:00
Point::new(100, 0),
Point::new(200, 120),
Color::rgb(0xff, 0x80, 0xff),
Operator::Over,
);
context.draw_line(
2019-09-23 18:01:27 +03:00
Point::new(0, 0),
self.cursor_pos,
Color::rgb(0xff, 0xff, 0x80),
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
fn key_event(&mut self, key: &KeyEvent, ctx: &dyn WindowOps) -> bool {
2019-08-10 20:00:47 +03:00
eprintln!("{:?}", key);
ctx.set_cursor(Some(MouseCursor::Text));
2019-08-10 20:00:47 +03:00
false
}
2019-08-10 21:40:17 +03:00
fn mouse_event(&mut self, event: &MouseEvent, ctx: &dyn WindowOps) {
2019-08-10 21:40:17 +03:00
eprintln!("{:?}", event);
self.cursor_pos = event.coords;
ctx.invalidate();
ctx.set_cursor(Some(MouseCursor::Arrow));
if event.kind == MouseEventKind::Press(MousePress::Left) {
// spawn_window().unwrap();
}
2019-08-10 21:40:17 +03:00
}
fn as_any(&mut self) -> &mut dyn Any {
self
}
2019-08-08 07:18:30 +03:00
}
2019-08-08 05:19:04 +03:00
2019-12-15 08:43:05 +03:00
fn spawn_window() -> anyhow::Result<()> {
2019-08-08 07:18:30 +03:00
let win = Window::new_window(
"myclass",
"the title",
800,
600,
Box::new(MyWindow {
allow_close: false,
cursor_pos: Point::new(100, 200),
}),
2019-08-08 07:18:30 +03:00
)?;
2019-08-08 05:19:04 +03:00
win.show();
2019-09-29 06:29:48 +03:00
win.apply(|myself, _win| {
if let Some(myself) = myself.downcast_ref::<MyWindow>() {
eprintln!(
"got myself; allow_close={}, cursor_pos:{:?}",
myself.allow_close, myself.cursor_pos
);
}
Ok(())
});
Ok(())
}
2019-08-08 05:19:04 +03:00
2019-12-15 08:43:05 +03:00
fn main() -> anyhow::Result<()> {
pretty_env_logger::init();
let conn = Connection::init()?;
spawn_window()?;
2019-08-09 03:44:57 +03:00
conn.run_message_loop()
2019-08-08 05:19:04 +03:00
}