Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
diff --git a/crates/gpui_windows/src/directx_atlas.rs b/crates/gpui_windows/src/directx_atlas.rs
index 6238699..d49ec18 100644
--- a/crates/gpui_windows/src/directx_atlas.rs
+++ b/crates/gpui_windows/src/directx_atlas.rs
@@ -108,6 +108,15 @@ impl PlatformAtlas for DirectXAtlas {
AtlasTextureKind::Polychrome => &mut lock.polychrome_textures,
AtlasTextureKind::Subpixel => &mut lock.subpixel_textures,
};
+ let cached_texture_index = if id.kind == AtlasTextureKind::Polychrome {
+ textures.textures.iter().position(|entry| {
+ entry
+ .as_ref()
+ .is_some_and(|entry| entry.live_atlas_keys == 0)
+ })
+ } else {
+ None
+ };
let Some(texture_slot) = textures.textures.get_mut(id.index as usize) else {
return;
@@ -116,7 +125,13 @@ impl PlatformAtlas for DirectXAtlas {
if let Some(mut texture) = texture_slot.take() {
texture.allocator.deallocate(tile.tile_id.into());
texture.decrement_ref_count();
- if texture.is_unreferenced() {
+ if texture.is_unreferenced() && id.kind == AtlasTextureKind::Polychrome {
+ *texture_slot = Some(texture);
+ if let Some(index) = cached_texture_index {
+ textures.textures[index] = None;
+ textures.free_list.push(index);
+ }
+ } else if texture.is_unreferenced() {
textures.free_list.push(texture.id.index as usize);
} else {
*texture_slot = Some(texture);
@@ -417,4 +432,36 @@ mod tests {
let tile_b = insert_tile(&atlas, &big_key_b, big);
assert_eq!(tile_b.texture_id, keeper_tile.texture_id);
}
+
+ #[test]
+ fn test_reuses_one_unreferenced_polychrome_texture() {
+ let Some(atlas) = create_atlas() else {
+ return;
+ };
+
+ let small = Size {
+ width: DevicePixels(64),
+ height: DevicePixels(64),
+ };
+ let large = Size {
+ width: DevicePixels(1280),
+ height: DevicePixels(720),
+ };
+ let small_key = make_image_key(10);
+ let first_key = make_image_key(11);
+ let second_key = make_image_key(12);
+
+ insert_tile(&atlas, &small_key, small);
+ atlas.remove(&small_key);
+ let first = insert_tile(&atlas, &first_key, large);
+ atlas.remove(&first_key);
+ let second = insert_tile(&atlas, &second_key, large);
+
+ assert_eq!(first.texture_id, second.texture_id);
+ let state = atlas.0.lock();
+ assert_eq!(
+ state.polychrome_textures.textures.iter().flatten().count(),
+ 1
+ );
+ }
}
diff --git a/crates/gpui_windows/src/directx_renderer.rs b/crates/gpui_windows/src/directx_renderer.rs
index 8a35068..35b504e 100644
--- a/crates/gpui_windows/src/directx_renderer.rs
+++ b/crates/gpui_windows/src/directx_renderer.rs
@@ -1983,3 +1983,41 @@ mod dxgi {
))
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::shader_resources::{RawShaderBytes, ShaderModule, ShaderTarget};
+ use gpui::{PolychromeSprite, Quad};
+ use windows::{Win32::Graphics::Direct3D::Fxc::D3DDisassemble, core::PCSTR};
+
+ #[test]
+ fn compiled_shader_strides_match_uploaded_primitives() {
+ for (module, stride) in [
+ (ShaderModule::Quad, size_of::<Quad>()),
+ (
+ ShaderModule::PolychromeSprite,
+ size_of::<PolychromeSprite>(),
+ ),
+ ] {
+ for target in [ShaderTarget::Vertex, ShaderTarget::Fragment] {
+ let shader = RawShaderBytes::new(module, target).unwrap();
+ let bytes = shader.as_bytes();
+ let assembly = unsafe {
+ D3DDisassemble(bytes.as_ptr().cast(), bytes.len(), 0, PCSTR::null()).unwrap()
+ };
+ let text = unsafe {
+ std::slice::from_raw_parts(
+ assembly.GetBufferPointer().cast::<u8>(),
+ assembly.GetBufferSize(),
+ )
+ };
+ let text = String::from_utf8_lossy(text);
+ let declaration = format!("dcl_resource_structured t1, {stride}");
+ assert!(
+ text.lines().any(|line| line.trim() == declaration),
+ "{module:?} {target:?} does not match Rust's {stride}-byte stride"
+ );
+ }
+ }
+ }
+}
diff --git a/crates/gpui_windows/src/display.rs b/crates/gpui_windows/src/display.rs
index bb75bf7..edaa793 100644
--- a/crates/gpui_windows/src/display.rs
+++ b/crates/gpui_windows/src/display.rs
@@ -122,6 +122,17 @@ impl WindowsDisplay {
.collect()
}
+ pub fn from_bounds(bounds: Bounds<Pixels>) -> Option<Self> {
+ available_monitors()
+ .into_iter()
+ .filter_map(|handle| WindowsDisplay::new(Self::display_id_for_monitor(handle)))
+ .find(|display| display.check_given_bounds(bounds))
+ }
+
+ pub fn scale_factor(&self) -> f32 {
+ self.scale_factor
+ }
+
pub fn physical_bounds(&self) -> Bounds<DevicePixels> {
self.physical_bounds
}
diff --git a/crates/gpui_windows/src/events.rs b/crates/gpui_windows/src/events.rs
index b293763..44f5505 100644
--- a/crates/gpui_windows/src/events.rs
+++ b/crates/gpui_windows/src/events.rs
@@ -129,25 +129,13 @@ impl WindowsWindowInner {
self.state.scale_factor.get(),
);
self.state.origin.set(origin);
- let size = self.state.logical_size.get();
- let center_x = origin.x.as_f32() + size.width.as_f32() / 2.;
- let center_y = origin.y.as_f32() + size.height.as_f32() / 2.;
- let monitor_bounds = self.state.display.get().bounds();
- if center_x < monitor_bounds.left().as_f32()
- || center_x > monitor_bounds.right().as_f32()
- || center_y < monitor_bounds.top().as_f32()
- || center_y > monitor_bounds.bottom().as_f32()
+ let monitor = unsafe { MonitorFromWindow(handle, MONITOR_DEFAULTTONULL) };
+ if !monitor.is_invalid()
+ && self.state.display.get().handle != monitor
+ && let Some(display) =
+ WindowsDisplay::new(WindowsDisplay::display_id_for_monitor(monitor))
{
- // center of the window may have moved to another monitor
- let monitor = unsafe { MonitorFromWindow(handle, MONITOR_DEFAULTTONULL) };
- // minimize the window can trigger this event too, in this case,
- // monitor is invalid, we do nothing.
- if !monitor.is_invalid() && self.state.display.get().handle != monitor {
- // we will get the same monitor if we only have one
- self.state.display.set(WindowsDisplay::new(
- WindowsDisplay::display_id_for_monitor(monitor),
- )?);
- }
+ self.state.display.set(display);
}
if let Some(mut callback) = self.state.callbacks.moved.take() {
callback();
@@ -714,6 +702,18 @@ impl WindowsWindowInner {
return None;
}
+ if self.custom_frame && !self.state.is_maximized() {
+ let dpi = unsafe { GetDpiForWindow(handle) };
+ let (frame, top) =
+ custom_frame_insets(get_frame_thicknessx(dpi), dpi, windows_build_number());
+ let params = unsafe { &mut *(lparam.0 as *mut NCCALCSIZE_PARAMS) };
+ params.rgrc[0].left += frame;
+ params.rgrc[0].top += top;
+ params.rgrc[0].right -= frame;
+ params.rgrc[0].bottom -= frame;
+ return Some(0);
+ }
+
unsafe {
let params = lparam.0 as *mut NCCALCSIZE_PARAMS;
let saved_top = (*params).rgrc[0].top;
@@ -844,20 +844,12 @@ impl WindowsWindowInner {
.context("unable to set maximized window position after dpi has changed")
.log_err();
}
-
- // SetWindowPos may not send WM_SIZE for maximized windows in some cases,
- // so we manually update the size to ensure proper rendering
- let device_size = size(DevicePixels(width), DevicePixels(height));
- self.handle_size_change(device_size, new_scale_factor, true);
}
} else {
// For non-maximized windows, use the suggested RECT from the system
let rect = unsafe { &*(lparam.0 as *const RECT) };
let width = rect.right - rect.left;
let height = rect.bottom - rect.top;
- // this will emit `WM_SIZE` and `WM_MOVE` right here
- // even before this function returns
- // the new size is handled in `WM_SIZE`
unsafe {
SetWindowPos(
handle,
@@ -873,6 +865,36 @@ impl WindowsWindowInner {
}
}
+ // SetWindowPos can omit a final WM_SIZE; its outer bounds also include native captions.
+ let monitor = unsafe { MonitorFromWindow(handle, MONITOR_DEFAULTTONULL) };
+ if !monitor.is_invalid()
+ && let Some(display) =
+ WindowsDisplay::new(WindowsDisplay::display_id_for_monitor(monitor))
+ {
+ self.state.display.set(display);
+ }
+ let mut client_origin = POINT::default();
+ if unsafe { ClientToScreen(handle, &mut client_origin) }.as_bool() {
+ self.state.origin.set(logical_point(
+ client_origin.x as f32,
+ client_origin.y as f32,
+ self.state.scale_factor.get(),
+ ));
+ }
+ let mut client_rect = RECT::default();
+ if !unsafe { IsIconic(handle) }.as_bool()
+ && unsafe { GetClientRect(handle, &mut client_rect) }
+ .context("unable to get client bounds after dpi has changed")
+ .log_err()
+ .is_some()
+ {
+ let device_size = size(
+ DevicePixels((client_rect.right - client_rect.left).max(1)),
+ DevicePixels((client_rect.bottom - client_rect.top).max(1)),
+ );
+ self.handle_size_change(device_size, self.state.scale_factor.get(), true);
+ }
+
Some(0)
}
@@ -1650,6 +1672,32 @@ pub(crate) fn current_capslock() -> Capslock {
Capslock { on }
}
+fn custom_frame_insets(frame: i32, dpi: u32, windows_build: u32) -> (i32, i32) {
+ let top = if windows_build >= 22000 {
+ (dpi as f32 / 96.).round() as i32
+ } else {
+ 0
+ };
+ (frame, top)
+}
+
+fn windows_build_number() -> u32 {
+ use windows::Win32::System::SystemInformation::OSVERSIONINFOW;
+
+ static BUILD: std::sync::OnceLock<u32> = std::sync::OnceLock::new();
+ *BUILD.get_or_init(|| {
+ let mut version = OSVERSIONINFOW {
+ dwOSVersionInfoSize: std::mem::size_of::<OSVERSIONINFOW>() as u32,
+ ..Default::default()
+ };
+ if unsafe { windows::Wdk::System::SystemServices::RtlGetVersion(&mut version) }.is_ok() {
+ version.dwBuildNumber
+ } else {
+ 0
+ }
+ })
+}
+
// there is some additional non-visible space when talking about window
// borders on Windows:
// - SM_CXSIZEFRAME: The resize handle.
@@ -1688,3 +1736,40 @@ fn notify_frame_changed(handle: HWND) {
.log_err();
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::custom_frame_insets;
+
+ #[test]
+ fn windows_10_has_no_top_inset_at_any_scale() {
+ for build in [19045, 21999] {
+ for (frame, dpi) in [(8, 96), (10, 120), (12, 144), (16, 192), (24, 288)] {
+ assert_eq!(custom_frame_insets(frame, dpi, build), (frame, 0));
+ }
+ }
+ }
+
+ #[test]
+ fn windows_11_preserves_the_first_client_row_at_each_scale() {
+ for build in [22000, 22621, 26100] {
+ for (frame, dpi, top) in [
+ (8, 96, 1),
+ (10, 120, 1),
+ (12, 144, 2),
+ (14, 168, 2),
+ (16, 192, 2),
+ (20, 240, 3),
+ (24, 288, 3),
+ ] {
+ assert_eq!(custom_frame_insets(frame, dpi, build), (frame, top));
+ }
+ }
+ }
+
+ #[test]
+ fn unknown_windows_build_does_not_restore_a_native_titlebar() {
+ assert_eq!(custom_frame_insets(8, 96, 0), (8, 0));
+ assert_eq!(custom_frame_insets(16, 192, 0), (16, 0));
+ }
+}
diff --git a/crates/gpui_windows/src/shaders.hlsl b/crates/gpui_windows/src/shaders.hlsl
index 483f039..78b1ada 100644
--- a/crates/gpui_windows/src/shaders.hlsl
+++ b/crates/gpui_windows/src/shaders.hlsl
@@ -36,6 +36,17 @@ struct Edges {
float left;
};
+struct EdgeFadeParams {
+ float top_y;
+ float bottom_y;
+ float band_top;
+ float band_bottom;
+ float left_x;
+ float right_x;
+ float band_left;
+ float band_right;
+};
+
struct Hsla {
float h;
float s;
@@ -93,6 +104,23 @@ struct TransformationMatrix {
static const float M_PI_F = 3.141592653f;
static const float3 GRAYSCALE_FACTORS = float3(0.2126f, 0.7152f, 0.0722f);
+float edge_fade_alpha(float2 position, EdgeFadeParams fade) {
+ float ramp = 1.0;
+ if (fade.band_top > 0.0) {
+ ramp = min(ramp, saturate((position.y - fade.top_y) / fade.band_top));
+ }
+ if (fade.band_bottom > 0.0) {
+ ramp = min(ramp, saturate((fade.bottom_y - position.y) / fade.band_bottom));
+ }
+ if (fade.band_left > 0.0) {
+ ramp = min(ramp, saturate((position.x - fade.left_x) / fade.band_left));
+ }
+ if (fade.band_right > 0.0) {
+ ramp = min(ramp, saturate((fade.right_x - position.x) / fade.band_right));
+ }
+ return ramp * ramp;
+}
+
float4 to_device_position_impl(float2 position) {
float2 device_position = position / global_viewport_size * float2(2.0, -2.0) + float2(-1.0, 1.0);
return float4(device_position, 0., 1.);
@@ -502,6 +530,7 @@ struct Quad {
Hsla border_color;
Corners corner_radii;
Edges border_widths;
+ EdgeFadeParams fade;
};
struct QuadVertexOutput {
@@ -554,6 +583,8 @@ float4 quad_fragment(QuadFragmentInput input): SV_Target {
Quad quad = quads[input.quad_id];
float4 background_color = gradient_color(quad.background, input.position.xy, quad.bounds,
input.background_solid, input.background_color0, input.background_color1);
+ float fade_alpha = edge_fade_alpha(input.position.xy, quad.fade);
+ background_color.a *= fade_alpha;
bool unrounded = quad.corner_radii.top_left == 0.0 &&
quad.corner_radii.top_right == 0.0 &&
@@ -661,6 +692,7 @@ float4 quad_fragment(QuadFragmentInput input): SV_Target {
float4 color = background_color;
if (border_sdf < antialias_threshold) {
float4 border_color = input.border_color;
+ border_color.a *= fade_alpha;
// Dashed border logic when border_style == 1
if (quad.border_style == 1) {
// Position along the perimeter in "dash space", where each dash
@@ -1209,6 +1241,7 @@ struct PolychromeSprite {
Bounds bounds;
Bounds content_mask;
Corners corner_radii;
+ EdgeFadeParams fade;
AtlasTile tile;
};
@@ -1253,6 +1286,6 @@ float4 polychrome_sprite_fragment(PolychromeSpriteFragmentInput input): SV_Targe
float3 grayscale = dot(color.rgb, GRAYSCALE_FACTORS);
color = float4(grayscale, sample.a);
}
- color.a *= sprite.opacity * saturate(0.5 - distance);
+ color.a *= sprite.opacity * saturate(0.5 - distance) * edge_fade_alpha(input.position.xy, sprite.fade);
return color;
}
diff --git a/crates/gpui_windows/src/window.rs b/crates/gpui_windows/src/window.rs
index 49a935b..8fdd318 100644
--- a/crates/gpui_windows/src/window.rs
+++ b/crates/gpui_windows/src/window.rs
@@ -93,6 +93,7 @@ pub(crate) struct WindowsWindowInner {
system_settings: WindowsSystemSettings,
pub(crate) handle: AnyWindowHandle,
pub(crate) hide_title_bar: bool,
+ pub(crate) custom_frame: bool,
pub(crate) is_movable: bool,
pub(crate) is_resizable: bool,
pub(crate) is_minimizable: bool,
@@ -213,7 +214,11 @@ impl WindowsWindowState {
};
(
calculate_client_rect(
- placement.rcNormalPosition,
+ translate_rect(
+ placement.rcNormalPosition,
+ workspace_offset(self.hwnd, self.display.get(), self.scale_factor.get()),
+ 1,
+ ),
&self.border_offset,
self.scale_factor.get(),
),
@@ -263,6 +268,7 @@ impl WindowsWindowInner {
state,
handle: context.handle,
hide_title_bar: context.hide_title_bar,
+ custom_frame: context.custom_frame,
is_movable: context.is_movable,
is_resizable: context.is_resizable,
is_minimizable: context.is_minimizable,
@@ -342,6 +348,9 @@ impl WindowsWindowInner {
let Some(open_status) = self.state.initial_placement.take() else {
return Ok(());
};
+ if unsafe { IsWindowVisible(self.hwnd) }.as_bool() {
+ return Ok(());
+ }
match open_status.state {
WindowOpenState::Maximized => unsafe {
SetWindowPlacement(self.hwnd, &open_status.placement)
@@ -386,6 +395,7 @@ struct WindowCreateContext {
inner: Option<Result<Rc<WindowsWindowInner>>>,
handle: AnyWindowHandle,
hide_title_bar: bool,
+ custom_frame: bool,
display: WindowsDisplay,
is_movable: bool,
is_resizable: bool,
@@ -405,6 +415,10 @@ struct WindowCreateContext {
parent_hwnd: Option<HWND>,
}
+fn custom_frame_enabled(normal_window: bool, transparent_titlebar: Option<bool>) -> bool {
+ normal_window && transparent_titlebar == Some(true)
+}
+
impl WindowsWindow {
pub(crate) fn new(
handle: AnyWindowHandle,
@@ -450,6 +464,13 @@ impl WindowsWindow {
.as_ref()
.map(|titlebar| titlebar.appears_transparent)
.unwrap_or(true);
+ let custom_frame = custom_frame_enabled(
+ params.kind == WindowKind::Normal,
+ params
+ .titlebar
+ .as_ref()
+ .map(|titlebar| titlebar.appears_transparent),
+ );
let window_name = HSTRING::from(
params
.titlebar
@@ -460,9 +481,14 @@ impl WindowsWindow {
);
let (mut dwexstyle, dwstyle) = if params.kind == WindowKind::PopUp {
- (WS_EX_TOOLWINDOW, WINDOW_STYLE(0x0))
+ (WS_EX_TOOLWINDOW, WS_POPUP)
} else {
let mut dwstyle = WS_SYSMENU;
+ if hide_title_bar && !custom_frame {
+ dwstyle |= WS_POPUP;
+ } else {
+ dwstyle |= WS_CAPTION;
+ }
if params.is_resizable {
dwstyle |= WS_THICKFRAME | WS_MAXIMIZEBOX;
@@ -480,6 +506,9 @@ impl WindowsWindow {
(dwexstyle, dwstyle)
};
+ if custom_frame {
+ dwexstyle |= WS_EX_WINDOWEDGE;
+ }
if !disable_direct_composition {
dwexstyle |= WS_EX_NOREDIRECTIONBITMAP;
}
@@ -490,13 +519,24 @@ impl WindowsWindow {
} else {
None
}
+ .or_else(|| WindowsDisplay::from_bounds(params.bounds))
.or_else(WindowsDisplay::primary_monitor)
.context("failed to find any monitor")?;
+ let initial_bounds = if display.check_given_bounds(params.bounds) {
+ params.bounds
+ } else {
+ display.default_bounds()
+ };
+ let initial_physical = initial_bounds.to_device_pixels(display.scale_factor());
+ let creation_origin = display.physical_bounds().origin;
+ let creation_width = initial_physical.size.width.0.max(1);
+ let creation_height = initial_physical.size.height.0.max(1);
let appearance = system_appearance().unwrap_or_default();
let mut context = WindowCreateContext {
inner: None,
handle,
hide_title_bar,
+ custom_frame,
display,
is_movable: params.is_movable,
is_resizable: params.is_resizable,
@@ -521,10 +561,10 @@ impl WindowsWindow {
WINDOW_CLASS_NAME,
&window_name,
dwstyle,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
+ creation_origin.x.0,
+ creation_origin.y.0,
+ creation_width,
+ creation_height,
parent_hwnd,
None,
Some(hinstance.into()),
@@ -548,10 +588,22 @@ impl WindowsWindow {
params.bounds,
this.state.scale_factor.get(),
&this.state.border_offset,
+ params.window_min_size,
)?;
if params.show {
unsafe { SetWindowPlacement(hwnd, &placement)? };
} else {
+ let client = calculate_client_rect(
+ translate_rect(
+ placement.rcNormalPosition,
+ workspace_offset(hwnd, display, this.state.scale_factor.get()),
+ 1,
+ ),
+ &this.state.border_offset,
+ this.state.scale_factor.get(),
+ );
+ this.state.origin.set(client.origin);
+ this.state.logical_size.set(client.size);
this.state.initial_placement.set(Some(WindowOpenStatus {
placement,
state: WindowOpenState::Windowed,
@@ -618,8 +670,36 @@ impl PlatformWindow for WindowsWindow {
fn resize(&mut self, size: Size<Pixels>) {
let hwnd = self.0.hwnd;
- let bounds = gpui::bounds(self.bounds().origin, size).to_device_pixels(self.scale_factor());
- let rect = calculate_window_rect(bounds, &self.state.border_offset);
+ let size = size.to_device_pixels(self.scale_factor());
+ let mut rect = RECT::default();
+ if unsafe { GetWindowRect(hwnd, &mut rect) }
+ .log_err()
+ .is_none()
+ {
+ return;
+ }
+ rect.right = rect.left + size.width.0 + self.state.border_offset.width_offset.get();
+ rect.bottom = rect.top + size.height.0 + self.state.border_offset.height_offset.get();
+ let movable = !is_tool_window(hwnd) && !self.state.is_fullscreen();
+ if movable {
+ rect = fit_window_rect(
+ rect,
+ self.state
+ .display
+ .get()
+ .visible_bounds()
+ .to_device_pixels(self.scale_factor()),
+ minimum_outer_size(
+ self.state.min_size,
+ self.scale_factor(),
+ &self.state.border_offset,
+ ),
+ );
+ }
+ let mut flags = SWP_NOACTIVATE | SWP_NOZORDER;
+ if !movable {
+ flags |= SWP_NOMOVE;
+ }
self.0
.executor
@@ -628,11 +708,11 @@ impl PlatformWindow for WindowsWindow {
SetWindowPos(
hwnd,
None,
- bounds.origin.x.0,
- bounds.origin.y.0,
+ rect.left,
+ rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
- SWP_NOMOVE,
+ flags,
)
.context("unable to set window content size")
.log_err();
@@ -641,6 +721,35 @@ impl PlatformWindow for WindowsWindow {
.detach();
}
+ fn start_window_move(&self) {
+ if !self.is_movable {
+ return;
+ }
+ let this = self.0.clone();
+ self.executor
+ .spawn(async move {
+ let mut cursor = POINT::default();
+ unsafe {
+ if GetAsyncKeyState(VK_LBUTTON.0 as i32) >= 0
+ || GetCursorPos(&mut cursor).log_err().is_none()
+ {
+ return;
+ }
+ ReleaseCapture().log_err();
+ let position = (cursor.x as u16 as u32) | ((cursor.y as u16 as u32) << 16);
+ PostMessageW(
+ Some(this.hwnd),
+ WM_SYSCOMMAND,
+ WPARAM((SC_MOVE | HTCAPTION) as usize),
+ LPARAM(position as isize),
+ )
+ .context("unable to start window move")
+ .log_err();
+ }
+ })
+ .detach();
+ }
+
fn scale_factor(&self) -> f32 {
self.state.scale_factor.get()
}
@@ -1508,6 +1617,7 @@ fn retrieve_window_placement(
initial_bounds: Bounds<Pixels>,
scale_factor: f32,
border_offset: &WindowBorderOffset,
+ minimum_size: Option<Size<Pixels>>,
) -> Result<WINDOWPLACEMENT> {
let mut placement = WINDOWPLACEMENT {
length: std::mem::size_of::<WINDOWPLACEMENT>() as u32,
@@ -1521,10 +1631,73 @@ fn retrieve_window_placement(
display.default_bounds()
};
let bounds = bounds.to_device_pixels(scale_factor);
- placement.rcNormalPosition = calculate_window_rect(bounds, border_offset);
+ let mut rect = calculate_window_rect(bounds, border_offset);
+ if !is_tool_window(hwnd) {
+ rect = fit_window_rect(
+ rect,
+ display.visible_bounds().to_device_pixels(scale_factor),
+ minimum_outer_size(minimum_size, scale_factor, border_offset),
+ );
+ }
+ placement.rcNormalPosition =
+ translate_rect(rect, workspace_offset(hwnd, display, scale_factor), -1);
Ok(placement)
}
+fn is_tool_window(hwnd: HWND) -> bool {
+ unsafe { get_window_long(hwnd, GWL_EXSTYLE) as u32 & WS_EX_TOOLWINDOW.0 != 0 }
+}
+
+fn workspace_offset(hwnd: HWND, display: WindowsDisplay, scale_factor: f32) -> Point<DevicePixels> {
+ if is_tool_window(hwnd) {
+ return point(DevicePixels(0), DevicePixels(0));
+ }
+ display
+ .visible_bounds()
+ .to_device_pixels(scale_factor)
+ .origin
+ - display.physical_bounds().origin
+}
+
+fn translate_rect(mut rect: RECT, offset: Point<DevicePixels>, direction: i32) -> RECT {
+ rect.left += offset.x.0 * direction;
+ rect.right += offset.x.0 * direction;
+ rect.top += offset.y.0 * direction;
+ rect.bottom += offset.y.0 * direction;
+ rect
+}
+
+fn minimum_outer_size(
+ minimum: Option<Size<Pixels>>,
+ scale_factor: f32,
+ border_offset: &WindowBorderOffset,
+) -> Size<DevicePixels> {
+ let minimum = minimum.unwrap_or_default().to_device_pixels(scale_factor);
+ size(
+ DevicePixels((minimum.width.0 + border_offset.width_offset.get()).max(1)),
+ DevicePixels((minimum.height.0 + border_offset.height_offset.get()).max(1)),
+ )
+}
+
+fn fit_window_rect(rect: RECT, work: Bounds<DevicePixels>, minimum: Size<DevicePixels>) -> RECT {
+ let width =
+ (rect.right - rect.left).clamp(minimum.width.0, work.size.width.0.max(minimum.width.0));
+ let height =
+ (rect.bottom - rect.top).clamp(minimum.height.0, work.size.height.0.max(minimum.height.0));
+ let left = rect
+ .left
+ .clamp(work.left().0, (work.right().0 - width).max(work.left().0));
+ let top = rect
+ .top
+ .clamp(work.top().0, (work.bottom().0 - height).max(work.top().0));
+ RECT {
+ left,
+ top,
+ right: left + width,
+ bottom: top + height,
+ }
+}
+
fn dwm_set_window_composition_attribute(hwnd: HWND, backdrop_type: u32) {
let mut version = unsafe { std::mem::zeroed() };
let status = unsafe { windows::Wdk::System::SystemServices::RtlGetVersion(&mut version) };
@@ -1609,9 +1782,89 @@ fn set_non_rude_hwnd(hwnd: HWND, non_rude: bool) {
#[cfg(test)]
mod tests {
- use super::ClickState;
- use gpui::{DevicePixels, MouseButton, point};
+ use super::{ClickState, custom_frame_enabled, fit_window_rect, translate_rect};
+ use gpui::{Bounds, DevicePixels, MouseButton, point, size};
use std::time::Duration;
+ use windows::Win32::Foundation::RECT;
+
+ #[test]
+ fn custom_frame_requires_a_normal_window_with_an_explicit_transparent_titlebar() {
+ assert!(custom_frame_enabled(true, Some(true)));
+ for (normal_window, transparent_titlebar) in [
+ (true, Some(false)),
+ (true, None),
+ (false, Some(true)),
+ (false, Some(false)),
+ (false, None),
+ ] {
+ assert!(!custom_frame_enabled(normal_window, transparent_titlebar));
+ }
+ }
+
+ #[test]
+ fn expanded_window_moves_above_bottom_taskbar() {
+ let work = Bounds {
+ origin: point(DevicePixels(0), DevicePixels(0)),
+ size: size(DevicePixels(1280), DevicePixels(760)),
+ };
+ let rect = fit_window_rect(
+ RECT {
+ left: 467,
+ top: 199,
+ right: 1083,
+ bottom: 867,
+ },
+ work,
+ size(DevicePixels(1), DevicePixels(1)),
+ );
+ assert_eq!(
+ (rect.left, rect.top, rect.right, rect.bottom),
+ (467, 92, 1083, 760)
+ );
+ }
+
+ #[test]
+ fn oversized_settings_shrink_but_editor_minimum_is_preserved() {
+ let work = Bounds {
+ origin: point(DevicePixels(-1280), DevicePixels(0)),
+ size: size(DevicePixels(1280), DevicePixels(760)),
+ };
+ let requested = RECT {
+ left: -1100,
+ top: -15,
+ right: -302,
+ bottom: 799,
+ };
+ let settings = fit_window_rect(requested, work, size(DevicePixels(796), DevicePixels(599)));
+ assert_eq!(
+ (settings.left, settings.top, settings.right, settings.bottom),
+ (-1100, 0, -302, 760)
+ );
+ let editor = fit_window_rect(requested, work, size(DevicePixels(1296), DevicePixels(839)));
+ assert_eq!(
+ (editor.left, editor.top, editor.right, editor.bottom),
+ (-1280, 0, 16, 839)
+ );
+ }
+
+ #[test]
+ fn top_and_left_taskbar_workspace_coordinates_round_trip() {
+ let screen = RECT {
+ left: -1800,
+ top: 80,
+ right: -900,
+ bottom: 700,
+ };
+ for offset in [
+ point(DevicePixels(0), DevicePixels(40)),
+ point(DevicePixels(60), DevicePixels(0)),
+ ] {
+ let workspace = translate_rect(screen, offset, -1);
+ assert_eq!(workspace.left, screen.left - offset.x.0);
+ assert_eq!(workspace.top, screen.top - offset.y.0);
+ assert_eq!(translate_rect(workspace, offset, 1), screen);
+ }
+ }
#[test]
fn test_double_click_interval() {