Skip to main content

gpui_platform/
display.rs

1//! Display abstractions shared by `gpui` and its platform backends.
2
3use crate::DEFAULT_WINDOW_SIZE;
4use anyhow::Result;
5use gpui_types::{Bounds, Pixels, point};
6use std::fmt::{self, Debug};
7use uuid::Uuid;
8
9/// An opaque identifier for a hardware display
10#[derive(PartialEq, Eq, Hash, Copy, Clone)]
11pub struct DisplayId(pub(crate) u64);
12
13impl DisplayId {
14    /// Create a new `DisplayId` from a raw platform display identifier.
15    pub fn new(id: u64) -> Self {
16        Self(id)
17    }
18}
19
20impl From<u64> for DisplayId {
21    fn from(id: u64) -> Self {
22        Self(id)
23    }
24}
25
26impl From<DisplayId> for u64 {
27    fn from(id: DisplayId) -> Self {
28        id.0
29    }
30}
31
32impl Debug for DisplayId {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        write!(f, "DisplayId({})", self.0)
35    }
36}
37
38/// A handle to a platform's display, e.g. a monitor or laptop screen.
39pub trait PlatformDisplay: Debug {
40    /// Get the ID for this display
41    fn id(&self) -> DisplayId;
42
43    /// Returns a stable identifier for this display that can be persisted and used
44    /// across system restarts.
45    fn uuid(&self) -> Result<Uuid>;
46
47    /// Get the bounds for this display
48    fn bounds(&self) -> Bounds<Pixels>;
49
50    /// Get the visible bounds for this display, excluding taskbar/dock areas.
51    /// This is the usable area where windows can be placed without being obscured.
52    /// Defaults to the full display bounds if not overridden.
53    fn visible_bounds(&self) -> Bounds<Pixels> {
54        self.bounds()
55    }
56
57    /// Get the default bounds for this display to place a window
58    fn default_bounds(&self) -> Bounds<Pixels> {
59        let bounds = self.bounds();
60        let center = bounds.center();
61        let clipped_window_size = DEFAULT_WINDOW_SIZE.min(&bounds.size);
62
63        let offset = clipped_window_size / 2.0;
64        let origin = point(center.x - offset.width, center.y - offset.height);
65        Bounds::new(origin, clipped_window_size)
66    }
67}