From eab21a669d696ae6118a786292273db499fad50a Mon Sep 17 00:00:00 2001 From: YuraIz <7516890@gmail.com> Date: Thu, 17 Nov 2022 23:23:21 +0300 Subject: [PATCH] integrated-title-bar: Improve windows support --- wezterm-gui/src/tabbar.rs | 1 - wezterm-gui/src/termwindow/render.rs | 6 +-- wezterm-input-types/src/lib.rs | 2 +- window/src/os/windows/window.rs | 75 +++++++++++++++------------- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/wezterm-gui/src/tabbar.rs b/wezterm-gui/src/tabbar.rs index 0ae22d0a1..d6782ec20 100644 --- a/wezterm-gui/src/tabbar.rs +++ b/wezterm-gui/src/tabbar.rs @@ -383,7 +383,6 @@ impl TabBarState { #[cfg(not(target_os = "macos"))] if use_integrated_title_buttons && config.integrated_title_button_alignment == IntegratedTitleButtonAlignment::Left - && !cfg!(widnows) { Self::integrated_title_buttons(mouse_x, &mut x, config, &mut items, &mut line, &colors); } diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index dc6eb7838..4ff3ca974 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -290,9 +290,6 @@ fn window_button_element( use window::IntegratedTitleButtonStyle as Style; use IntegratedTitleButton as Button; - #[cfg(windows)] - let style = Style::Windows; - let poly = match style { Style::Windows => { use window_buttons::windows::{CLOSE, HIDE, MAXIMIZE, RESTORE}; @@ -1109,7 +1106,6 @@ impl super::TermWindow { TabBarItem::WindowButton(_) => { if self.config.integrated_title_button_alignment == IntegratedTitleButtonAlignment::Left - && cfg!(not(windows)) { left_eles.push(item_to_elem(item)) } else { @@ -1202,7 +1198,7 @@ impl super::TermWindow { == window::WindowDecorations::INTEGRATED_BUTTONS && (self.config.integrated_title_button_alignment == IntegratedTitleButtonAlignment::Left - || (cfg!(target_os = "macos")) && cfg!(not(windows))); + || (cfg!(target_os = "macos"))); let left_padding = if window_buttons_at_left { if cfg!(target_os = "macos") { diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 6526f3d39..a820c1724 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -1465,7 +1465,7 @@ pub enum IntegratedTitleButtonAlignment { Left, } -#[derive(Debug, Default, FromDynamic, ToDynamic, Clone, Copy)] +#[derive(Debug, Default, FromDynamic, ToDynamic, PartialEq, Eq, Clone, Copy)] pub enum IntegratedTitleButtonStyle { #[default] Windows, diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 3f6189a8f..8be6a97e7 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -2,7 +2,8 @@ use super::*; use crate::connection::ConnectionOps; use crate::parameters::{self, Parameters}; use crate::{ - Appearance, Clipboard, DeadKeyStatus, Dimensions, Handled, KeyCode, KeyEvent, Modifiers, + Appearance, Clipboard, DeadKeyStatus, Dimensions, Handled, IntegratedTitleButton, + IntegratedTitleButtonAlignment, IntegratedTitleButtonStyle, KeyCode, KeyEvent, Modifiers, MouseButtons, MouseCursor, MouseEvent, MouseEventKind, MousePress, Point, RawKeyEvent, Rect, RequestedWindowGeometry, ResolvedGeometry, ScreenPoint, ULength, WindowDecorations, WindowEvent, WindowEventSender, WindowOps, WindowState, @@ -1037,8 +1038,11 @@ unsafe fn wm_nccalcsize(hwnd: HWND, _msg: UINT, wparam: WPARAM, lparam: LPARAM) let inner = rc_from_hwnd(hwnd)?; let inner = inner.borrow_mut(); - if !(wparam == 1 && (inner.config.window_decorations == WindowDecorations::RESIZE) - || (inner.config.window_decorations == WindowDecorations::INTEGRATED_BUTTONS)) + if !(wparam == 1 + && (matches!( + inner.config.window_decorations, + WindowDecorations::RESIZE | WindowDecorations::INTEGRATED_BUTTONS + ))) { return None; } @@ -1079,22 +1083,6 @@ unsafe fn wm_nccalcsize(hwnd: HWND, _msg: UINT, wparam: WPARAM, lparam: LPARAM) Some(0) } -unsafe fn maximize_button_coords( - hwnd: HWND, -) -> euclid::Point2D { - let mut client_rect = RECT::default(); - GetClientRect(hwnd, &mut client_rect); - - let scale = GetDpiForWindow(hwnd) as f64 / 96.0; - - let mut coords = mouse_coords(0); - - coords.x = client_rect.right as isize - (60.0 * scale) as isize; - coords.y = 5; - - coords -} - unsafe fn wm_nchittest(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option { let inner = rc_from_hwnd(hwnd)?; let inner = inner.borrow_mut(); @@ -1172,9 +1160,18 @@ unsafe fn wm_nchittest(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> } } - if inner.config.window_decorations == WindowDecorations::INTEGRATED_BUTTONS - && inner.config.use_fancy_tab_bar - { + // When you can predict maximize button position + let use_snap_layouts = !*IS_WIN10 + && inner.config.window_decorations == WindowDecorations::INTEGRATED_BUTTONS + && inner.config.integrated_title_button_style == IntegratedTitleButtonStyle::Windows + && inner.config.integrated_title_button_alignment == IntegratedTitleButtonAlignment::Right + && matches!( + &inner.config.integrated_title_buttons[..], + &[.., IntegratedTitleButton::Maximize, _] + ) + && inner.config.use_fancy_tab_bar; + + if use_snap_layouts { let scale = GetDpiForWindow(inner.hwnd.0) as f64 / 96.0; let button_height = (30.0 * scale) as isize; if cursor_point.y >= client_rect.top as isize @@ -1484,6 +1481,12 @@ fn mouse_coords(lparam: LPARAM) -> Point { Point::new(point.x as _, point.y as _) } +fn nc_mouse_coords(hwnd: HWND, lparam: LPARAM) -> Point { + let point = MAKEPOINTS(lparam as _); + let point = ScreenPoint::new(point.x as _, point.y as _); + screen_to_client(hwnd, point) +} + fn screen_to_client(hwnd: HWND, point: ScreenPoint) -> Point { let mut point = POINT { x: point.x.try_into().unwrap(), @@ -1561,7 +1564,7 @@ unsafe fn nc_mouse_button( hwnd: HWND, msg: UINT, wparam: WPARAM, - _lparam: LPARAM, + lparam: LPARAM, ) -> Option { let inner = rc_from_hwnd(hwnd)?; // To support dragging the window, capture when the left @@ -1580,7 +1583,7 @@ unsafe fn nc_mouse_button( } let (modifiers, mouse_buttons) = mods_and_buttons(0); - let coords = maximize_button_coords(hwnd); + let coords = nc_mouse_coords(hwnd, lparam); let event = MouseEvent { kind: match msg { @@ -1630,12 +1633,7 @@ unsafe fn mouse_move(hwnd: HWND, _msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Some(0) } -unsafe fn nc_mouse_move( - hwnd: HWND, - _msg: UINT, - wparam: WPARAM, - _lparam: LPARAM, -) -> Option { +unsafe fn nc_mouse_move(hwnd: HWND, _msg: UINT, wparam: WPARAM, lparam: LPARAM) -> Option { let inner = rc_from_hwnd(hwnd)?; let mut inner = inner.borrow_mut(); @@ -1657,7 +1655,7 @@ unsafe fn nc_mouse_move( } let (modifiers, mouse_buttons) = mods_and_buttons(0); - let coords = maximize_button_coords(hwnd); + let coords = nc_mouse_coords(hwnd, lparam); let event = MouseEvent { kind: MouseEventKind::Move, @@ -2701,11 +2699,20 @@ unsafe fn do_wnd_proc(hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM) -> ) { let inner = rc_from_hwnd(hwnd)?; let inner = inner.borrow(); - let use_shap_layouts = inner.config.window_decorations - == WindowDecorations::INTEGRATED_BUTTONS + // When you can predict maximize button position + let use_snap_layouts = !*IS_WIN10 + && inner.config.window_decorations == WindowDecorations::INTEGRATED_BUTTONS + && inner.config.integrated_title_button_style + == IntegratedTitleButtonStyle::Windows + && inner.config.integrated_title_button_alignment + == IntegratedTitleButtonAlignment::Right + && matches!( + &inner.config.integrated_title_buttons[..], + &[.., IntegratedTitleButton::Maximize, _] + ) && inner.config.use_fancy_tab_bar; std::mem::drop(inner); - if use_shap_layouts { + if use_snap_layouts { return match msg { WM_NCMOUSEMOVE => nc_mouse_move(hwnd, msg, wparam, lparam), WM_NCMOUSELEAVE => mouse_leave(hwnd, msg, wparam, lparam),