1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-20 03:41:36 +03:00
wezterm/window/examples/basic.rs

195 lines
4.7 KiB
Rust
Raw Normal View History

2019-08-09 03:44:57 +03:00
use ::window::*;
use glium::{program, uniform};
use std::any::Any;
2019-08-08 07:18:30 +03:00
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
color: [f32; 3],
}
glium::implement_vertex!(Vertex, position, color);
2019-08-08 07:18:30 +03:00
struct MyWindow {
allow_close: bool,
cursor_pos: Point,
gl: Option<GlState>,
}
struct GlState {
vb: glium::VertexBuffer<Vertex>,
index_buffer: glium::IndexBuffer<u16>,
program: glium::Program,
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 created(
&mut self,
_window: &Window,
context: std::rc::Rc<glium::backend::Context>,
) -> anyhow::Result<()> {
let vb = glium::VertexBuffer::new(
&context,
&[
Vertex {
position: [-0.5, -0.5],
color: [0.0, 1.0, 0.0],
},
Vertex {
position: [0.0, 0.5],
color: [0.0, 0.0, 1.0],
},
Vertex {
position: [0.5, -0.5],
color: [1.0, 0.0, 0.0],
},
],
)?;
let index_buffer = glium::IndexBuffer::new(
&context,
glium::index::PrimitiveType::TrianglesList,
&[0u16, 1, 2],
)?;
let program = glium::program!(&context,
140 => {
vertex: "
#version 140
uniform mat4 matrix;
in vec2 position;
in vec3 color;
out vec3 vColor;
void main() {
gl_Position = vec4(position, 0.0, 1.0) * matrix;
vColor = color;
}
",
fragment: "
#version 140
in vec3 vColor;
out vec4 f_color;
void main() {
f_color = vec4(vColor, 1.0);
}
"
},
)?;
self.gl.replace(GlState {
vb,
index_buffer,
program,
});
Ok(())
}
fn paint(&mut self, frame: &mut glium::Frame) {
// Window contents are gray in opengl mode
use glium::Surface;
frame.clear_color_srgb(0.25, 0.125, 0.375, 1.0);
let uniforms = uniform! {
matrix: [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0f32]
]
};
// drawing a frame
let gl = self.gl.as_ref().unwrap();
frame
.draw(
&gl.vb,
&gl.index_buffer,
&gl.program,
&uniforms,
&Default::default(),
)
.unwrap();
}
add `window-resized` event This is to support <https://github.com/wez/wezterm/issues/291>. The window resized event happens asynchronously wrt. processing a window resize, triggering at the end of the normal window resize handling. This commit introduces the notion of whether we are in full screen mode or not in the underlying event callback, which is useful to gate the desired feature, which is: when in full screen mode, increase the padding for the window to center its content. While poking around at this, I noticed that we weren't passing the per-window config down to the code that computes the quad locations for the window. This commit also changes the font size increase/decrease behavior so that in full screen mode it doesn't try to resize the window. ```lua local wezterm = require 'wezterm'; wezterm.on("window-resized", function(window, pane) local window_dims = window:get_dimensions(); local pane_dims = pane:get_dimensions(); local overrides = window:get_config_overrides() or {} if not window_dims.is_full_screen then if not overrides.window_padding then -- not changing anything return; end overrides.window_padding = nil; else -- Use only the middle 33% local third = math.floor(window_dims.pixel_width / 3) local new_padding = { left = third, right = third, top = 0, bottom = 0 }; if overrides.window_padding and new_padding.left == overrides.window_padding.left then -- padding is same, avoid triggering further changes return end overrides.window_padding = new_padding end window:set_config_overrides(overrides) end); return { } ```
2021-03-07 22:54:15 +03:00
fn resize(&mut self, dims: Dimensions, is_full_screen: bool) {
eprintln!("resize {:?} is_full_screen={}", dims, is_full_screen);
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),
gl: None,
}),
None,
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
}