From 37ec16329ee8e3b2bfcc35920c9b2b463d561cdd Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 10 Jul 2021 18:07:16 -0700 Subject: [PATCH] fixup tests + wayland bits --- window/examples/async.rs | 15 ++---- window/examples/wgpu.rs | 83 ++++++++++++++++++++-------------- window/src/os/wayland/frame.rs | 30 ++++++------ 3 files changed, 68 insertions(+), 60 deletions(-) diff --git a/window/examples/async.rs b/window/examples/async.rs index b714f18c1..56699a137 100644 --- a/window/examples/async.rs +++ b/window/examples/async.rs @@ -8,7 +8,6 @@ struct MyWindow { allow_close: bool, cursor_pos: Point, dims: Dimensions, - win: Option, gl: Option>, } @@ -19,12 +18,7 @@ impl Drop for MyWindow { } impl MyWindow { - fn dispatch(&mut self, event: WindowEvent) { - let win = match self.win.as_ref() { - Some(win) => win, - None => return, - }; - + fn dispatch(&mut self, event: WindowEvent, win: &Window) { match dbg!(event) { WindowEvent::CloseRequested => { eprintln!("can I close?"); @@ -95,7 +89,6 @@ async fn spawn_window() -> Result<(), Box> { pixel_height: 600, dpi: 0, }, - win: None, gl: None, })); @@ -107,15 +100,13 @@ async fn spawn_window() -> Result<(), Box> { 600, None, fontconfig, - move |event| { + move |event, window| { let mut state = cb_state.borrow_mut(); - state.dispatch(event) + state.dispatch(event, window) }, ) .await?; - state.borrow_mut().win.replace(win.clone()); - eprintln!("before show"); win.show(); let gl = win.enable_opengl().await?; diff --git a/window/examples/wgpu.rs b/window/examples/wgpu.rs index fa485b71e..16f574146 100644 --- a/window/examples/wgpu.rs +++ b/window/examples/wgpu.rs @@ -3,6 +3,7 @@ use anyhow::Context; use promise::spawn::spawn; #[cfg(target_os = "macos")] use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; +use std::cell::RefCell; use std::rc::Rc; use wezterm_font::FontConfiguration; @@ -190,41 +191,14 @@ impl MyWindow { gpu.swap_chain = gpu.device.create_swap_chain(&gpu.surface, &gpu.sc_desc); } } -} - -async fn spawn_window() -> anyhow::Result<()> { - 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?; - - let mut state = MyWindow { - allow_close: false, - cursor_pos: Point::new(100, 200), - dims: Dimensions { - pixel_width: 800, - pixel_height: 600, - dpi: 0, - }, - gpu: None, - render_pipeline: None, - }; - - eprintln!("before show"); - win.show().await?; - state.enable_wgpu(&win).await?; - eprintln!("window is visible, do loop"); - - while let Ok(event) = events.recv().await { + fn dispatch(&mut self, event: WindowEvent, win: &Window) { match event { WindowEvent::CloseRequested => { eprintln!("can I close?"); - if state.allow_close { + if self.allow_close { win.close(); } else { - state.allow_close = true; + self.allow_close = true; } } WindowEvent::Destroyed => { @@ -235,7 +209,7 @@ async fn spawn_window() -> anyhow::Result<()> { dimensions, is_full_screen: _, } => { - state.resize(dimensions); + self.resize(dimensions); #[cfg(target_os = "macos")] if let RawWindowHandle::MacOS(h) = win.raw_window_handle() { use cocoa::base::{id, NO}; @@ -246,11 +220,11 @@ async fn spawn_window() -> anyhow::Result<()> { let () = msg_send![layer, setOpaque: NO]; } - state.paint()?; + self.paint()?; } } WindowEvent::MouseEvent(event) => { - state.cursor_pos = event.coords; + self.cursor_pos = event.coords; win.invalidate(); win.set_cursor(Some(MouseCursor::Arrow)); @@ -264,11 +238,52 @@ async fn spawn_window() -> anyhow::Result<()> { win.default_key_processing(key); } WindowEvent::NeedRepaint => { - state.paint()?; + self.paint().unwrap(); } WindowEvent::Notification(_) | WindowEvent::FocusChanged(_) => {} } } +} + +async fn spawn_window() -> anyhow::Result<()> { + 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, + }, + gpu: None, + render_pipeline: None, + })); + + let cb_state = Rc::clone(&state); + let win = Window::new_window( + "myclass", + "the title", + 800, + 600, + None, + fontconfig, + move |event, window| { + let mut state = cb_state.borrow_mut(); + state.dispatch(event, window) + }, + ) + .await?; + + eprintln!("before show"); + win.show(); + { + let mut state = state.borrow_mut(); + state.enable_wgpu(&win).await?; + } + eprintln!("window is visible, do loop"); Ok(()) } diff --git a/window/src/os/wayland/frame.rs b/window/src/os/wayland/frame.rs index 54c037c89..14ba73d5a 100644 --- a/window/src/os/wayland/frame.rs +++ b/window/src/os/wayland/frame.rs @@ -24,7 +24,8 @@ use wezterm_font::{FontConfiguration, FontMetrics, GlyphInfo, RasterizedGlyph}; fn color_to_paint(c: RgbColor) -> Paint<'static> { let mut paint = Paint::default(); - paint.set_color_rgba8(c.blue, c.green, c.red, 0xff); + let (red, green, blue) = c.to_tuple_rgb8(); + paint.set_color_rgba8(blue, green, red, 0xff); paint.anti_alias = true; paint } @@ -463,7 +464,8 @@ impl ConceptFrame { let title_color = match self.active { WindowState::Active => colors.active_titlebar_fg, WindowState::Inactive => colors.inactive_titlebar_fg, - }; + } + .to_tuple_rgba(); for info in infos { if let Ok(mut glyph) = font.rasterize_glyph(info.glyph_pos, info.font_idx) { @@ -481,10 +483,9 @@ impl ConceptFrame { } else { // Apply the preferred title color *p = ColorU8::from_rgba( - ((b as f32 / 255.) * (title_color.red as f32 / 255.) * 255.) as u8, - ((g as f32 / 255.) * (title_color.green as f32 / 255.) * 255.) - as u8, - ((r as f32 / 255.) * (title_color.blue as f32 / 255.) * 255.) as u8, + ((b as f32 / 255.) * (title_color.0 * 255.)) as u8, + ((g as f32 / 255.) * (title_color.1 * 255.)) as u8, + ((r as f32 / 255.) * (title_color.2 * 255.)) as u8, a, ) .premultiply(); @@ -1190,17 +1191,18 @@ fn request_for_location_on_rmb(pointer_data: &PointerUserData) -> Option RgbColor { #[inline] - fn gamma_mix(x: u8, y: u8) -> u8 { - let x = x as f32 / 255.0; - let y = y as f32 / 255.0; + fn gamma_mix(x: f32, y: f32) -> f32 { let z = ((x * x + y * y) / 2.0).sqrt(); - (z * 255.0) as u8 + z } - RgbColor::new( - gamma_mix(x.red, y.red), - gamma_mix(x.green, y.green), - gamma_mix(x.blue, y.blue), + let x = x.to_tuple_rgba(); + let y = y.to_tuple_rgba(); + + RgbColor::new_f32( + gamma_mix(x.0, y.0), + gamma_mix(x.1, y.1), + gamma_mix(x.2, y.2), ) }