Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

fix(windows): stop OpenGL windows crashing on their first frame

RoversX26367e182bfc
diff --git a/window/src/egl.rs b/window/src/egl.rs
index 1511ae82..0467c200 100644
--- a/window/src/egl.rs
+++ b/window/src/egl.rs
@@ -55,6 +55,17 @@ impl std::fmt::Debug for EglWrapper {
#[cfg(windows)]
const DIRECT_COMPOSITION_ANGLE: i32 = 0x33A5;
+/// The size of `window`'s client area, in pixels.
+#[cfg(windows)]
+fn client_size(window: ffi::EGLNativeWindowType) -> (u32, u32) {
+ let mut rect: winapi::shared::windef::RECT = unsafe { std::mem::zeroed() };
+ unsafe { winapi::um::winuser::GetClientRect(window, &mut rect) };
+ (
+ (rect.right - rect.left).max(0) as u32,
+ (rect.bottom - rect.top).max(0) as u32,
+ )
+}
+
#[derive(Debug)]
pub struct GlConnection {
egl: EglWrapper,
@@ -91,6 +102,10 @@ pub struct GlState {
connection: Rc<GlConnection>,
surface: ffi::types::EGLSurface,
context: ffi::types::EGLContext,
+ /// The window a DirectComposition surface is drawn for, checked at
+ /// each swap: see `swap_buffers`.
+ #[cfg(windows)]
+ composition_window: Option<ffi::EGLNativeWindowType>,
}
impl Drop for GlState {
@@ -400,6 +415,26 @@ impl GlState {
&self.connection
}
+ fn surface_size(&self) -> (u32, u32) {
+ let mut width = 0;
+ let mut height = 0;
+ unsafe {
+ self.connection.QuerySurface(
+ self.connection.display,
+ self.surface,
+ ffi::WIDTH as i32,
+ &mut width,
+ );
+ self.connection.QuerySurface(
+ self.connection.display,
+ self.surface,
+ ffi::HEIGHT as i32,
+ &mut height,
+ );
+ }
+ (width as u32, height as u32)
+ }
+
fn with_egl_lib<T, F: FnMut(EglWrapper) -> anyhow::Result<T>>(
mut func: F,
) -> anyhow::Result<T> {
@@ -520,11 +555,13 @@ impl GlState {
connection: &Rc<GlConnection>,
window: ffi::EGLNativeWindowType,
) -> anyhow::Result<GlState> {
- Self::create_with_surface_attributes(
+ let mut state = Self::create_with_surface_attributes(
connection,
window,
&[DIRECT_COMPOSITION_ANGLE, ffi::TRUE as i32],
- )
+ )?;
+ state.composition_window = Some(window);
+ Ok(state)
}
fn connect_with(
@@ -692,6 +729,8 @@ impl GlState {
connection: Rc::clone(connection),
context,
surface,
+ #[cfg(windows)]
+ composition_window: None,
});
}
@@ -705,10 +744,27 @@ unsafe impl glium::backend::Backend for GlState {
}
fn swap_buffers(&self) -> Result<(), glium::SwapBuffersError> {
+ // ANGLE resizes the swap chain behind a DirectComposition window only
+ // as it presents: a frame drawn just after a resize went into the
+ // old size's buffer, so the window is asked to draw once more. Done
+ // here, with no window state touched: this runs inside its paint.
+ // A minimized window's client area is empty, and the swap chain keeps
+ // its size then: that is not a frame to redraw.
+ #[cfg(windows)]
+ let stale = self.composition_window.filter(|&window| {
+ let client = client_size(window);
+ client.0 > 0 && client.1 > 0 && self.surface_size() != client
+ });
let res = unsafe {
self.connection
.SwapBuffers(self.connection.display, self.surface)
};
+ #[cfg(windows)]
+ if let Some(window) = stale {
+ unsafe {
+ winapi::um::winuser::InvalidateRect(window, std::ptr::null(), 0);
+ }
+ }
if res != 1 {
Err(match unsafe { self.connection.GetError() } as u32 {
ffi::CONTEXT_LOST => glium::SwapBuffersError::ContextLost,
@@ -725,26 +781,7 @@ unsafe impl glium::backend::Backend for GlState {
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
- let mut width = 0;
- let mut height = 0;
-
- unsafe {
- self.connection.QuerySurface(
- self.connection.display,
- self.surface,
- ffi::WIDTH as i32,
- &mut width,
- );
- }
- unsafe {
- self.connection.QuerySurface(
- self.connection.display,
- self.surface,
- ffi::HEIGHT as i32,
- &mut height,
- );
- }
- (width as u32, height as u32)
+ self.surface_size()
}
fn is_current(&self) -> bool {
diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs
index 33d1f845..ebd516d1 100644
--- a/window/src/os/windows/window.rs
+++ b/window/src/os/windows/window.rs
@@ -1232,29 +1232,6 @@ impl HasWindowHandle for Window {
#[async_trait(?Send)]
impl WindowOps for Window {
- fn finish_frame(&self, frame: glium::Frame) -> anyhow::Result<()> {
- // ANGLE resizes the swap chain behind a DirectComposition window only
- // as it presents: a frame drawn just after a resize went into the
- // old size's buffer, so draw once more at the new one.
- let stale = rc_from_hwnd(self.0 .0).and_then(|inner| {
- let inner = inner.borrow();
- inner.composition.as_ref()?;
- let drawn = inner.gl_state.as_ref()?.get_framebuffer_dimensions();
- let mut rect: RECT = unsafe { std::mem::zeroed() };
- unsafe { GetClientRect(self.0 .0, &mut rect) };
- let client = (
- (rect.right - rect.left).max(0) as u32,
- (rect.bottom - rect.top).max(0) as u32,
- );
- Some(drawn != client)
- });
- frame.finish()?;
- if stale == Some(true) {
- self.invalidate();
- }
- Ok(())
- }
-
async fn enable_opengl(&self) -> anyhow::Result<Rc<glium::backend::Context>> {
let window = self.0;
promise::spawn::spawn(async move {