From 0327df323d2b0690c1e35821b8241d4c826fdb38 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 27 Dec 2024 17:22:02 +0800 Subject: [PATCH] modal: Fix Model, Drawer overlay size on tiling mode. (#518) --- crates/story/examples/tiles.rs | 13 ++++++------- crates/story/src/main.rs | 11 ++++++----- crates/ui/src/drawer.rs | 16 +++++++++++----- crates/ui/src/modal.rs | 13 ++++++++----- crates/ui/src/window_border.rs | 31 +++++++++++++++++++++++++++---- 5 files changed, 58 insertions(+), 26 deletions(-) diff --git a/crates/story/examples/tiles.rs b/crates/story/examples/tiles.rs index d655b39e..db74a575 100644 --- a/crates/story/examples/tiles.rs +++ b/crates/story/examples/tiles.rs @@ -1,6 +1,5 @@ use anyhow::{Context, Result}; use gpui::*; -use prelude::FluentBuilder as _; use std::time::Duration; use story::{Assets, ButtonStory, IconStory, StoryContainer}; use ui::{ @@ -159,12 +158,12 @@ impl StoryTiles { } pub fn new_local(cx: &mut AppContext) -> Task>> { - let display_size = cx.primary_display().unwrap().bounds().size; - let window_size = size( - px(1600.0).min(display_size.width * 0.85), - px(1200.0).min(display_size.height * 0.85), - ); - + let mut window_size = size(px(1600.0), px(1200.0)); + if let Some(display) = cx.primary_display() { + let display_size = display.bounds().size; + window_size.width = window_size.width.min(display_size.width * 0.85); + window_size.height = window_size.height.min(display_size.height * 0.85); + } let window_bounds = Bounds::centered(None, window_size, cx); cx.spawn(|mut cx| async move { diff --git a/crates/story/src/main.rs b/crates/story/src/main.rs index 9c20f98d..56a8c0ab 100644 --- a/crates/story/src/main.rs +++ b/crates/story/src/main.rs @@ -332,11 +332,12 @@ impl StoryWorkspace { } pub fn new_local(cx: &mut AppContext) -> Task>> { - let display_size = cx.primary_display().unwrap().bounds().size; - let window_size = size( - px(1600.0).min(display_size.width * 0.85), - px(1200.0).min(display_size.height * 0.85), - ); + let mut window_size = size(px(1600.0), px(1200.0)); + if let Some(display) = cx.primary_display() { + let display_size = display.bounds().size; + window_size.width = window_size.width.min(display_size.width * 0.85); + window_size.height = window_size.height.min(display_size.height * 0.85); + } let window_bounds = Bounds::centered(None, window_size, cx); diff --git a/crates/ui/src/drawer.rs b/crates/ui/src/drawer.rs index df75a9f2..8d011bf6 100644 --- a/crates/ui/src/drawer.rs +++ b/crates/ui/src/drawer.rs @@ -15,9 +15,7 @@ use crate::{ scroll::ScrollbarAxis, theme::ActiveTheme, title_bar::TITLE_BAR_HEIGHT, - v_flex, - window_border::SHADOW_SIZE, - IconName, Placement, Sizable, StyledExt as _, + v_flex, IconName, Placement, Sizable, StyledExt as _, }; actions!(drawer, [Escape]); @@ -121,11 +119,19 @@ impl RenderOnce for Drawer { fn render(self, cx: &mut WindowContext) -> impl IntoElement { let placement = self.placement; let titlebar_height = self.margin_top; - let size = cx.viewport_size() - gpui::size(SHADOW_SIZE * 2, SHADOW_SIZE * 2); + let window_paddings = crate::window_border::window_paddings(cx); + let size = cx.viewport_size() + - gpui::size( + window_paddings.left + window_paddings.right, + window_paddings.top + window_paddings.bottom, + ); let on_close = self.on_close.clone(); anchored() - .position(point(px(0.) + SHADOW_SIZE, titlebar_height + SHADOW_SIZE)) + .position(point( + window_paddings.left, + window_paddings.top + titlebar_height - px(1.), + )) .snap_to_window() .child( div() diff --git a/crates/ui/src/modal.rs b/crates/ui/src/modal.rs index 106739bb..75e0abe3 100644 --- a/crates/ui/src/modal.rs +++ b/crates/ui/src/modal.rs @@ -11,9 +11,7 @@ use crate::{ animation::cubic_bezier, button::{Button, ButtonVariants as _}, theme::ActiveTheme as _, - v_flex, - window_border::SHADOW_SIZE, - ContextModal, IconName, Sizable as _, + v_flex, ContextModal, IconName, Sizable as _, }; actions!(modal, [Escape]); @@ -164,7 +162,12 @@ impl RenderOnce for Modal { fn render(self, cx: &mut WindowContext) -> impl gpui::IntoElement { let layer_ix = self.layer_ix; let on_close = self.on_close.clone(); - let view_size = cx.viewport_size() - gpui::size(SHADOW_SIZE * 2, SHADOW_SIZE * 2); + let window_paddings = crate::window_border::window_paddings(cx); + let view_size = cx.viewport_size() + - gpui::size( + window_paddings.left + window_paddings.right, + window_paddings.top + window_paddings.bottom, + ); let bounds = Bounds { origin: Point::default(), size: view_size, @@ -174,7 +177,7 @@ impl RenderOnce for Modal { let x = bounds.center().x - self.width / 2.; anchored() - .position(point(SHADOW_SIZE, SHADOW_SIZE)) + .position(point(window_paddings.left, window_paddings.top)) .snap_to_window() .child( div() diff --git a/crates/ui/src/window_border.rs b/crates/ui/src/window_border.rs index 31642d8c..34e604da 100644 --- a/crates/ui/src/window_border.rs +++ b/crates/ui/src/window_border.rs @@ -2,16 +2,16 @@ // https://github.com/zed-industries/zed/blob/a8afc63a91f6b75528540dcffe73dc8ce0c92ad8/crates/gpui/examples/window_shadow.rs use gpui::{ canvas, div, point, prelude::FluentBuilder as _, px, AnyElement, Bounds, CursorStyle, - Decorations, Hsla, InteractiveElement as _, IntoElement, MouseButton, ParentElement, Pixels, - Point, RenderOnce, ResizeEdge, Size, Styled as _, WindowContext, + Decorations, Edges, Hsla, InteractiveElement as _, IntoElement, MouseButton, ParentElement, + Pixels, Point, RenderOnce, ResizeEdge, Size, Styled as _, WindowContext, }; use crate::theme::ActiveTheme; #[cfg(not(target_os = "linux"))] -pub(crate) const SHADOW_SIZE: Pixels = Pixels(0.0); +const SHADOW_SIZE: Pixels = Pixels(0.0); #[cfg(target_os = "linux")] -pub(crate) const SHADOW_SIZE: Pixels = Pixels(12.0); +const SHADOW_SIZE: Pixels = Pixels(12.0); const BORDER_SIZE: Pixels = Pixels(1.0); pub(crate) const BORDER_RADIUS: Pixels = Pixels(0.0); @@ -34,6 +34,29 @@ impl WindowBorder { } } +/// Get the window paddings. +pub fn window_paddings(cx: &WindowContext) -> Edges { + match cx.window_decorations() { + Decorations::Server => Edges::all(px(0.0)), + Decorations::Client { tiling } => { + let mut paddings = Edges::all(SHADOW_SIZE); + if tiling.top { + paddings.top = px(0.0); + } + if tiling.bottom { + paddings.bottom = px(0.0); + } + if tiling.left { + paddings.left = px(0.0); + } + if tiling.right { + paddings.right = px(0.0); + } + paddings + } + } +} + impl ParentElement for WindowBorder { fn extend(&mut self, elements: impl IntoIterator) { self.children.extend(elements);