Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

8.6 KB
Raw Blame
pub mod bounds;
pub mod platform;

use bounds::*;
pub use platform::{DisplayIdImpl, DisplayImpl, WindowIdImpl, WindowImpl};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::str::FromStr;

use crate::bounds::{LogicalPosition, LogicalSize};

#[derive(Clone, Copy)]
pub struct Display(DisplayImpl);

impl Display {
    pub fn list() -> Vec<Self> {
        DisplayImpl::list().into_iter().map(Self).collect()
    }

    pub fn primary() -> Self {
        Self(DisplayImpl::primary())
    }

    pub fn raw_handle(&self) -> &DisplayImpl {
        &self.0
    }

    pub fn id(&self) -> DisplayId {
        DisplayId(self.0.raw_id())
    }

    pub fn from_id(id: &DisplayId) -> Option<Self> {
        Self::list().into_iter().find(|d| &d.id() == id)
    }

    pub fn get_containing_cursor() -> Option<Self> {
        DisplayImpl::get_containing_cursor().map(Self)
    }

    pub fn name(&self) -> Option<String> {
        self.0.name()
    }

    pub fn physical_size(&self) -> Option<PhysicalSize> {
        self.0.physical_size()
    }

    pub fn logical_size(&self) -> Option<LogicalSize> {
        self.0.logical_size()
    }

    pub fn refresh_rate(&self) -> f64 {
        self.0.refresh_rate()
    }

    pub fn notch(&self) -> Option<NotchGeometry> {
        #[cfg(target_os = "macos")]
        {
            self.0.notch()
        }

        #[cfg(not(target_os = "macos"))]
        {
            None
        }
    }
}

/// Geometry of the camera housing ("notch") on a built-in MacBook display, as
/// fractions of that display's size. Fractions rather than points so the values
/// survive scaled display modes and the logical-to-physical step between
/// capture and playback.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NotchGeometry {
    /// Distance from the left edge of the display to the notch.
    pub x: f64,
    pub width: f64,
    pub height: f64,
}

#[derive(Serialize, Deserialize, Type, Clone, PartialEq, Debug)]
pub struct DisplayId(
    #[serde(with = "serde_display_id")]
    #[specta(type = String)]
    DisplayIdImpl,
);

impl std::fmt::Display for DisplayId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for DisplayId {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse::<DisplayIdImpl>().map(Self)
    }
}

mod serde_display_id {
    use serde::{Deserialize, Deserializer, Serializer};

    use crate::platform::DisplayIdImpl;

    pub fn serialize<S>(this: &DisplayIdImpl, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&this.to_string())
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<DisplayIdImpl, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        s.parse::<DisplayIdImpl>().map_err(serde::de::Error::custom)
    }
}

#[derive(Clone, Copy)]
pub struct Window(WindowImpl);

impl Window {
    pub fn list() -> Vec<Self> {
        WindowImpl::list().into_iter().map(Self).collect()
    }

    pub fn list_containing_cursor() -> Vec<Self> {
        WindowImpl::list_containing_cursor()
            .into_iter()
            .map(Self)
            .collect()
    }

    pub fn get_topmost_at_cursor() -> Option<Self> {
        WindowImpl::get_topmost_at_cursor().map(Self)
    }

    pub fn id(&self) -> WindowId {
        WindowId(self.0.id())
    }

    pub fn from_id(id: &WindowId) -> Option<Self> {
        Self::list().into_iter().find(|d| &d.id() == id)
    }

    /// Like [`Window::from_id`], but resolves the handle directly instead of
    /// searching `list()`. Prefer it when the window was already chosen and
    /// only needs to still exist: enumeration drops minimized windows, so
    /// `from_id` reports a minimized target as gone.
    #[cfg(any(windows, target_os = "macos"))]
    pub fn from_id_direct(id: &WindowId) -> Option<Self> {
        WindowImpl::from_id_direct(&id.0).map(Self)
    }

    pub fn physical_size(&self) -> Option<PhysicalSize> {
        self.0.physical_size()
    }

    pub fn logical_size(&self) -> Option<LogicalSize> {
        self.0.logical_size()
    }

    pub fn owner_name(&self) -> Option<String> {
        self.0.owner_name()
    }

    pub fn app_icon(&self) -> Option<Vec<u8>> {
        self.0.app_icon()
    }

    pub fn raw_handle(&self) -> &WindowImpl {
        &self.0
    }

    pub fn display(&self) -> Option<Display> {
        self.0.display().map(Display)
    }

    pub fn name(&self) -> Option<String> {
        self.0.name()
    }

    pub fn display_relative_logical_bounds(&self) -> Option<LogicalBounds> {
        let display = self.display()?;

        #[cfg(target_os = "macos")]
        {
            let display_logical_bounds = display.raw_handle().logical_bounds()?;
            let window_logical_bounds = self.raw_handle().logical_bounds()?;

            Some(LogicalBounds::new(
                LogicalPosition::new(
                    window_logical_bounds.position().x() - display_logical_bounds.position().x(),
                    window_logical_bounds.position().y() - display_logical_bounds.position().y(),
                ),
                window_logical_bounds.size(),
            ))
        }

        #[cfg(windows)]
        {
            let display_physical_bounds = display.raw_handle().physical_bounds()?;
            let display_logical_size = display.logical_size()?;
            let window_physical_bounds: PhysicalBounds = self.raw_handle().physical_bounds()?;

            let scale = display_logical_size.width() / display_physical_bounds.size().width;

            let display_relative_physical_bounds = PhysicalBounds::new(
                PhysicalPosition::new(
                    window_physical_bounds.position().x - display_physical_bounds.position().x,
                    window_physical_bounds.position().y - display_physical_bounds.position().y,
                ),
                window_physical_bounds.size(),
            );

            Some(LogicalBounds::new(
                LogicalPosition::new(
                    display_relative_physical_bounds.position().x() * scale,
                    display_relative_physical_bounds.position().y() * scale,
                ),
                LogicalSize::new(
                    display_relative_physical_bounds.size().width() * scale,
                    display_relative_physical_bounds.size().height() * scale,
                ),
            ))
        }

        #[cfg(target_os = "linux")]
        {
            let display_bounds = display.raw_handle().logical_bounds()?;
            let window_bounds = self.raw_handle().logical_bounds()?;

            Some(LogicalBounds::new(
                LogicalPosition::new(
                    window_bounds.position().x() - display_bounds.position().x(),
                    window_bounds.position().y() - display_bounds.position().y(),
                ),
                window_bounds.size(),
            ))
        }
    }
}

#[derive(Serialize, Deserialize, Type, Clone, PartialEq, Debug)]
pub struct WindowId(
    #[serde(with = "serde_window_id")]
    #[specta(type = String)]
    WindowIdImpl,
);

impl std::fmt::Display for WindowId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl FromStr for WindowId {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        s.parse::<WindowIdImpl>().map(Self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn notch_geometry_is_within_the_display() {
        for display in Display::list() {
            let Some(notch) = display.notch() else {
                continue;
            };

            assert!(notch.width > 0.0 && notch.height > 0.0, "{notch:?}");
            assert!(notch.x > 0.0 && notch.x + notch.width < 1.0, "{notch:?}");
            assert!(notch.height < 0.5, "{notch:?}");

            println!("{:?}: {notch:?}", display.name());
        }
    }

    #[cfg(not(target_os = "macos"))]
    #[test]
    fn no_notch_off_macos() {
        assert!(Display::list().iter().all(|d| d.notch().is_none()));
    }
}

mod serde_window_id {
    use serde::{Deserialize, Deserializer, Serializer};

    use crate::WindowIdImpl;

    pub fn serialize<S>(this: &WindowIdImpl, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&this.to_string())
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<WindowIdImpl, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        s.parse::<WindowIdImpl>().map_err(serde::de::Error::custom)
    }
}