modal: Fix Model, Drawer overlay size on tiling mode. (#518)

This commit is contained in:
Jason Lee 2024-12-27 17:22:02 +08:00 committed by GitHub
parent 6851e53183
commit 0327df323d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 58 additions and 26 deletions

View file

@ -1,6 +1,5 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use gpui::*; use gpui::*;
use prelude::FluentBuilder as _;
use std::time::Duration; use std::time::Duration;
use story::{Assets, ButtonStory, IconStory, StoryContainer}; use story::{Assets, ButtonStory, IconStory, StoryContainer};
use ui::{ use ui::{
@ -159,12 +158,12 @@ impl StoryTiles {
} }
pub fn new_local(cx: &mut AppContext) -> Task<anyhow::Result<WindowHandle<Root>>> { pub fn new_local(cx: &mut AppContext) -> Task<anyhow::Result<WindowHandle<Root>>> {
let display_size = cx.primary_display().unwrap().bounds().size; let mut window_size = size(px(1600.0), px(1200.0));
let window_size = size( if let Some(display) = cx.primary_display() {
px(1600.0).min(display_size.width * 0.85), let display_size = display.bounds().size;
px(1200.0).min(display_size.height * 0.85), 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); let window_bounds = Bounds::centered(None, window_size, cx);
cx.spawn(|mut cx| async move { cx.spawn(|mut cx| async move {

View file

@ -332,11 +332,12 @@ impl StoryWorkspace {
} }
pub fn new_local(cx: &mut AppContext) -> Task<anyhow::Result<WindowHandle<Root>>> { pub fn new_local(cx: &mut AppContext) -> Task<anyhow::Result<WindowHandle<Root>>> {
let display_size = cx.primary_display().unwrap().bounds().size; let mut window_size = size(px(1600.0), px(1200.0));
let window_size = size( if let Some(display) = cx.primary_display() {
px(1600.0).min(display_size.width * 0.85), let display_size = display.bounds().size;
px(1200.0).min(display_size.height * 0.85), 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); let window_bounds = Bounds::centered(None, window_size, cx);

View file

@ -15,9 +15,7 @@ use crate::{
scroll::ScrollbarAxis, scroll::ScrollbarAxis,
theme::ActiveTheme, theme::ActiveTheme,
title_bar::TITLE_BAR_HEIGHT, title_bar::TITLE_BAR_HEIGHT,
v_flex, v_flex, IconName, Placement, Sizable, StyledExt as _,
window_border::SHADOW_SIZE,
IconName, Placement, Sizable, StyledExt as _,
}; };
actions!(drawer, [Escape]); actions!(drawer, [Escape]);
@ -121,11 +119,19 @@ impl RenderOnce for Drawer {
fn render(self, cx: &mut WindowContext) -> impl IntoElement { fn render(self, cx: &mut WindowContext) -> impl IntoElement {
let placement = self.placement; let placement = self.placement;
let titlebar_height = self.margin_top; 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(); let on_close = self.on_close.clone();
anchored() 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() .snap_to_window()
.child( .child(
div() div()

View file

@ -11,9 +11,7 @@ use crate::{
animation::cubic_bezier, animation::cubic_bezier,
button::{Button, ButtonVariants as _}, button::{Button, ButtonVariants as _},
theme::ActiveTheme as _, theme::ActiveTheme as _,
v_flex, v_flex, ContextModal, IconName, Sizable as _,
window_border::SHADOW_SIZE,
ContextModal, IconName, Sizable as _,
}; };
actions!(modal, [Escape]); actions!(modal, [Escape]);
@ -164,7 +162,12 @@ impl RenderOnce for Modal {
fn render(self, cx: &mut WindowContext) -> impl gpui::IntoElement { fn render(self, cx: &mut WindowContext) -> impl gpui::IntoElement {
let layer_ix = self.layer_ix; let layer_ix = self.layer_ix;
let on_close = self.on_close.clone(); 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 { let bounds = Bounds {
origin: Point::default(), origin: Point::default(),
size: view_size, size: view_size,
@ -174,7 +177,7 @@ impl RenderOnce for Modal {
let x = bounds.center().x - self.width / 2.; let x = bounds.center().x - self.width / 2.;
anchored() anchored()
.position(point(SHADOW_SIZE, SHADOW_SIZE)) .position(point(window_paddings.left, window_paddings.top))
.snap_to_window() .snap_to_window()
.child( .child(
div() div()

View file

@ -2,16 +2,16 @@
// https://github.com/zed-industries/zed/blob/a8afc63a91f6b75528540dcffe73dc8ce0c92ad8/crates/gpui/examples/window_shadow.rs // https://github.com/zed-industries/zed/blob/a8afc63a91f6b75528540dcffe73dc8ce0c92ad8/crates/gpui/examples/window_shadow.rs
use gpui::{ use gpui::{
canvas, div, point, prelude::FluentBuilder as _, px, AnyElement, Bounds, CursorStyle, canvas, div, point, prelude::FluentBuilder as _, px, AnyElement, Bounds, CursorStyle,
Decorations, Hsla, InteractiveElement as _, IntoElement, MouseButton, ParentElement, Pixels, Decorations, Edges, Hsla, InteractiveElement as _, IntoElement, MouseButton, ParentElement,
Point, RenderOnce, ResizeEdge, Size, Styled as _, WindowContext, Pixels, Point, RenderOnce, ResizeEdge, Size, Styled as _, WindowContext,
}; };
use crate::theme::ActiveTheme; use crate::theme::ActiveTheme;
#[cfg(not(target_os = "linux"))] #[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")] #[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); const BORDER_SIZE: Pixels = Pixels(1.0);
pub(crate) const BORDER_RADIUS: Pixels = Pixels(0.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<Pixels> {
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 { impl ParentElement for WindowBorder {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements); self.children.extend(elements);