platform.rs

   1mod app_menu;
   2mod keyboard;
   3mod keystroke;
   4
   5#[cfg(all(target_os = "linux", feature = "wayland"))]
   6#[expect(missing_docs)]
   7pub mod layer_shell;
   8
   9#[cfg(any(test, feature = "test-support"))]
  10mod test;
  11
  12#[cfg(all(target_os = "macos", any(test, feature = "test-support")))]
  13mod visual_test;
  14
  15#[cfg(all(
  16    feature = "screen-capture",
  17    any(target_os = "windows", target_os = "linux", target_os = "freebsd",)
  18))]
  19pub mod scap_screen_capture;
  20
  21#[cfg(all(
  22    any(target_os = "windows", target_os = "linux"),
  23    feature = "screen-capture"
  24))]
  25pub(crate) type PlatformScreenCaptureFrame = scap::frame::Frame;
  26#[cfg(not(feature = "screen-capture"))]
  27pub(crate) type PlatformScreenCaptureFrame = ();
  28#[cfg(all(target_os = "macos", feature = "screen-capture"))]
  29pub(crate) type PlatformScreenCaptureFrame = core_video::image_buffer::CVImageBuffer;
  30
  31use crate::{
  32    Action, AnyWindowHandle, App, AsyncWindowContext, BackgroundExecutor, Bounds,
  33    DEFAULT_WINDOW_SIZE, DevicePixels, DispatchEventResult, Font, FontId, FontMetrics, FontRun,
  34    ForegroundExecutor, GlyphId, GpuSpecs, ImageSource, Keymap, LineLayout, Pixels, PlatformInput,
  35    Point, Priority, RenderGlyphParams, RenderImage, RenderImageParams, RenderSvgParams, Scene,
  36    ShapedGlyph, ShapedRun, SharedString, Size, SvgRenderer, SystemWindowTab, Task,
  37    ThreadTaskTimings, Window, WindowControlArea, hash, point, px, size,
  38};
  39use anyhow::Result;
  40#[cfg(any(target_os = "linux", target_os = "freebsd"))]
  41use anyhow::bail;
  42use async_task::Runnable;
  43use futures::channel::oneshot;
  44#[cfg(any(test, feature = "test-support"))]
  45use image::RgbaImage;
  46use image::codecs::gif::GifDecoder;
  47use image::{AnimationDecoder as _, Frame};
  48use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
  49use scheduler::Instant;
  50pub use scheduler::RunnableMeta;
  51use schemars::JsonSchema;
  52use seahash::SeaHasher;
  53use serde::{Deserialize, Serialize};
  54use smallvec::SmallVec;
  55use std::borrow::Cow;
  56use std::hash::{Hash, Hasher};
  57use std::io::Cursor;
  58use std::ops;
  59use std::time::Duration;
  60use std::{
  61    fmt::{self, Debug},
  62    ops::Range,
  63    path::{Path, PathBuf},
  64    rc::Rc,
  65    sync::Arc,
  66};
  67use strum::EnumIter;
  68use uuid::Uuid;
  69
  70pub use app_menu::*;
  71pub use keyboard::*;
  72pub use keystroke::*;
  73
  74#[cfg(any(test, feature = "test-support"))]
  75pub(crate) use test::*;
  76
  77#[cfg(any(test, feature = "test-support"))]
  78pub use test::{TestDispatcher, TestScreenCaptureSource, TestScreenCaptureStream};
  79
  80#[cfg(all(target_os = "macos", any(test, feature = "test-support")))]
  81pub use visual_test::VisualTestPlatform;
  82
  83// TODO(jk): return an enum instead of a string
  84/// Return which compositor we're guessing we'll use.
  85/// Does not attempt to connect to the given compositor.
  86#[cfg(any(target_os = "linux", target_os = "freebsd"))]
  87#[inline]
  88pub fn guess_compositor() -> &'static str {
  89    if std::env::var_os("ZED_HEADLESS").is_some() {
  90        return "Headless";
  91    }
  92
  93    #[cfg(feature = "wayland")]
  94    let wayland_display = std::env::var_os("WAYLAND_DISPLAY");
  95    #[cfg(not(feature = "wayland"))]
  96    let wayland_display: Option<std::ffi::OsString> = None;
  97
  98    #[cfg(feature = "x11")]
  99    let x11_display = std::env::var_os("DISPLAY");
 100    #[cfg(not(feature = "x11"))]
 101    let x11_display: Option<std::ffi::OsString> = None;
 102
 103    let use_wayland = wayland_display.is_some_and(|display| !display.is_empty());
 104    let use_x11 = x11_display.is_some_and(|display| !display.is_empty());
 105
 106    if use_wayland {
 107        "Wayland"
 108    } else if use_x11 {
 109        "X11"
 110    } else {
 111        "Headless"
 112    }
 113}
 114
 115#[expect(missing_docs)]
 116pub trait Platform: 'static {
 117    fn background_executor(&self) -> BackgroundExecutor;
 118    fn foreground_executor(&self) -> ForegroundExecutor;
 119    fn text_system(&self) -> Arc<dyn PlatformTextSystem>;
 120
 121    fn run(&self, on_finish_launching: Box<dyn 'static + FnOnce()>);
 122    fn quit(&self);
 123    fn restart(&self, binary_path: Option<PathBuf>);
 124    fn activate(&self, ignoring_other_apps: bool);
 125    fn hide(&self);
 126    fn hide_other_apps(&self);
 127    fn unhide_other_apps(&self);
 128
 129    fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>>;
 130    fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>>;
 131    fn active_window(&self) -> Option<AnyWindowHandle>;
 132    fn window_stack(&self) -> Option<Vec<AnyWindowHandle>> {
 133        None
 134    }
 135
 136    fn is_screen_capture_supported(&self) -> bool {
 137        false
 138    }
 139
 140    fn screen_capture_sources(
 141        &self,
 142    ) -> oneshot::Receiver<anyhow::Result<Vec<Rc<dyn ScreenCaptureSource>>>> {
 143        let (sources_tx, sources_rx) = oneshot::channel();
 144        sources_tx
 145            .send(Err(anyhow::anyhow!(
 146                "gpui was compiled without the screen-capture feature"
 147            )))
 148            .ok();
 149        sources_rx
 150    }
 151
 152    fn open_window(
 153        &self,
 154        handle: AnyWindowHandle,
 155        options: WindowParams,
 156    ) -> anyhow::Result<Box<dyn PlatformWindow>>;
 157
 158    /// Returns the appearance of the application's windows.
 159    fn window_appearance(&self) -> WindowAppearance;
 160
 161    /// Returns the window button layout configuration when supported.
 162    fn button_layout(&self) -> Option<WindowButtonLayout> {
 163        None
 164    }
 165
 166    fn open_url(&self, url: &str);
 167    fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
 168    fn register_url_scheme(&self, url: &str) -> Task<Result<()>>;
 169
 170    fn prompt_for_paths(
 171        &self,
 172        options: PathPromptOptions,
 173    ) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>>;
 174    fn prompt_for_new_path(
 175        &self,
 176        directory: &Path,
 177        suggested_name: Option<&str>,
 178    ) -> oneshot::Receiver<Result<Option<PathBuf>>>;
 179    fn can_select_mixed_files_and_dirs(&self) -> bool;
 180    fn reveal_path(&self, path: &Path);
 181    fn open_with_system(&self, path: &Path);
 182
 183    fn on_quit(&self, callback: Box<dyn FnMut()>);
 184    fn on_reopen(&self, callback: Box<dyn FnMut()>);
 185
 186    fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap);
 187    fn get_menus(&self) -> Option<Vec<OwnedMenu>> {
 188        None
 189    }
 190
 191    fn set_dock_menu(&self, menu: Vec<MenuItem>, keymap: &Keymap);
 192    fn perform_dock_menu_action(&self, _action: usize) {}
 193    fn add_recent_document(&self, _path: &Path) {}
 194    fn update_jump_list(
 195        &self,
 196        _menus: Vec<MenuItem>,
 197        _entries: Vec<SmallVec<[PathBuf; 2]>>,
 198    ) -> Task<Vec<SmallVec<[PathBuf; 2]>>> {
 199        Task::ready(Vec::new())
 200    }
 201    fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>);
 202    fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>);
 203    fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);
 204
 205    fn thermal_state(&self) -> ThermalState;
 206    fn on_thermal_state_change(&self, callback: Box<dyn FnMut()>);
 207
 208    fn compositor_name(&self) -> &'static str {
 209        ""
 210    }
 211    fn app_path(&self) -> Result<PathBuf>;
 212    fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
 213
 214    fn set_cursor_style(&self, style: CursorStyle);
 215    fn should_auto_hide_scrollbars(&self) -> bool;
 216
 217    fn read_from_clipboard(&self) -> Option<ClipboardItem>;
 218    fn write_to_clipboard(&self, item: ClipboardItem);
 219
 220    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
 221    fn read_from_primary(&self) -> Option<ClipboardItem>;
 222    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
 223    fn write_to_primary(&self, item: ClipboardItem);
 224
 225    #[cfg(target_os = "macos")]
 226    fn read_from_find_pasteboard(&self) -> Option<ClipboardItem>;
 227    #[cfg(target_os = "macos")]
 228    fn write_to_find_pasteboard(&self, item: ClipboardItem);
 229
 230    fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task<Result<()>>;
 231    fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>>;
 232    fn delete_credentials(&self, url: &str) -> Task<Result<()>>;
 233
 234    fn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout>;
 235    fn keyboard_mapper(&self) -> Rc<dyn PlatformKeyboardMapper>;
 236    fn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>);
 237}
 238
 239/// A handle to a platform's display, e.g. a monitor or laptop screen.
 240pub trait PlatformDisplay: Debug {
 241    /// Get the ID for this display
 242    fn id(&self) -> DisplayId;
 243
 244    /// Returns a stable identifier for this display that can be persisted and used
 245    /// across system restarts.
 246    fn uuid(&self) -> Result<Uuid>;
 247
 248    /// Get the bounds for this display
 249    fn bounds(&self) -> Bounds<Pixels>;
 250
 251    /// Get the visible bounds for this display, excluding taskbar/dock areas.
 252    /// This is the usable area where windows can be placed without being obscured.
 253    /// Defaults to the full display bounds if not overridden.
 254    fn visible_bounds(&self) -> Bounds<Pixels> {
 255        self.bounds()
 256    }
 257
 258    /// Get the default bounds for this display to place a window
 259    fn default_bounds(&self) -> Bounds<Pixels> {
 260        let bounds = self.bounds();
 261        let center = bounds.center();
 262        let clipped_window_size = DEFAULT_WINDOW_SIZE.min(&bounds.size);
 263
 264        let offset = clipped_window_size / 2.0;
 265        let origin = point(center.x - offset.width, center.y - offset.height);
 266        Bounds::new(origin, clipped_window_size)
 267    }
 268}
 269
 270/// Thermal state of the system
 271#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 272pub enum ThermalState {
 273    /// System has no thermal constraints
 274    Nominal,
 275    /// System is slightly constrained, reduce discretionary work
 276    Fair,
 277    /// System is moderately constrained, reduce CPU/GPU intensive work
 278    Serious,
 279    /// System is critically constrained, minimize all resource usage
 280    Critical,
 281}
 282
 283/// Metadata for a given [ScreenCaptureSource]
 284#[derive(Clone)]
 285pub struct SourceMetadata {
 286    /// Opaque identifier of this screen.
 287    pub id: u64,
 288    /// Human-readable label for this source.
 289    pub label: Option<SharedString>,
 290    /// Whether this source is the main display.
 291    pub is_main: Option<bool>,
 292    /// Video resolution of this source.
 293    pub resolution: Size<DevicePixels>,
 294}
 295
 296/// A source of on-screen video content that can be captured.
 297pub trait ScreenCaptureSource {
 298    /// Returns metadata for this source.
 299    fn metadata(&self) -> Result<SourceMetadata>;
 300
 301    /// Start capture video from this source, invoking the given callback
 302    /// with each frame.
 303    fn stream(
 304        &self,
 305        foreground_executor: &ForegroundExecutor,
 306        frame_callback: Box<dyn Fn(ScreenCaptureFrame) + Send>,
 307    ) -> oneshot::Receiver<Result<Box<dyn ScreenCaptureStream>>>;
 308}
 309
 310/// A video stream captured from a screen.
 311pub trait ScreenCaptureStream {
 312    /// Returns metadata for this source.
 313    fn metadata(&self) -> Result<SourceMetadata>;
 314}
 315
 316/// A frame of video captured from a screen.
 317pub struct ScreenCaptureFrame(pub PlatformScreenCaptureFrame);
 318
 319/// An opaque identifier for a hardware display
 320#[derive(PartialEq, Eq, Hash, Copy, Clone)]
 321pub struct DisplayId(pub(crate) u32);
 322
 323impl DisplayId {
 324    /// Create a new `DisplayId` from a raw platform display identifier.
 325    pub fn new(id: u32) -> Self {
 326        Self(id)
 327    }
 328}
 329
 330impl From<u32> for DisplayId {
 331    fn from(id: u32) -> Self {
 332        Self(id)
 333    }
 334}
 335
 336impl From<DisplayId> for u32 {
 337    fn from(id: DisplayId) -> Self {
 338        id.0
 339    }
 340}
 341
 342impl Debug for DisplayId {
 343    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 344        write!(f, "DisplayId({})", self.0)
 345    }
 346}
 347
 348/// Which part of the window to resize
 349#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 350pub enum ResizeEdge {
 351    /// The top edge
 352    Top,
 353    /// The top right corner
 354    TopRight,
 355    /// The right edge
 356    Right,
 357    /// The bottom right corner
 358    BottomRight,
 359    /// The bottom edge
 360    Bottom,
 361    /// The bottom left corner
 362    BottomLeft,
 363    /// The left edge
 364    Left,
 365    /// The top left corner
 366    TopLeft,
 367}
 368
 369/// A type to describe the appearance of a window
 370#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
 371pub enum WindowDecorations {
 372    #[default]
 373    /// Server side decorations
 374    Server,
 375    /// Client side decorations
 376    Client,
 377}
 378
 379/// A type to describe how this window is currently configured
 380#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
 381pub enum Decorations {
 382    /// The window is configured to use server side decorations
 383    #[default]
 384    Server,
 385    /// The window is configured to use client side decorations
 386    Client {
 387        /// The edge tiling state
 388        tiling: Tiling,
 389    },
 390}
 391
 392/// What window controls this platform supports
 393#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
 394pub struct WindowControls {
 395    /// Whether this platform supports fullscreen
 396    pub fullscreen: bool,
 397    /// Whether this platform supports maximize
 398    pub maximize: bool,
 399    /// Whether this platform supports minimize
 400    pub minimize: bool,
 401    /// Whether this platform supports a window menu
 402    pub window_menu: bool,
 403}
 404
 405impl Default for WindowControls {
 406    fn default() -> Self {
 407        // Assume that we can do anything, unless told otherwise
 408        Self {
 409            fullscreen: true,
 410            maximize: true,
 411            minimize: true,
 412            window_menu: true,
 413        }
 414    }
 415}
 416
 417/// A window control button type used in [`WindowButtonLayout`].
 418#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 419pub enum WindowButton {
 420    /// The minimize button
 421    Minimize,
 422    /// The maximize button
 423    Maximize,
 424    /// The close button
 425    Close,
 426}
 427
 428impl WindowButton {
 429    /// Returns a stable element ID for rendering this button.
 430    pub fn id(&self) -> &'static str {
 431        match self {
 432            WindowButton::Minimize => "minimize",
 433            WindowButton::Maximize => "maximize",
 434            WindowButton::Close => "close",
 435        }
 436    }
 437
 438    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
 439    fn index(&self) -> usize {
 440        match self {
 441            WindowButton::Minimize => 0,
 442            WindowButton::Maximize => 1,
 443            WindowButton::Close => 2,
 444        }
 445    }
 446}
 447
 448/// Maximum number of [`WindowButton`]s per side in the titlebar.
 449pub const MAX_BUTTONS_PER_SIDE: usize = 3;
 450
 451/// Describes which [`WindowButton`]s appear on each side of the titlebar.
 452///
 453/// On Linux, this is read from the desktop environment's configuration
 454/// (e.g. GNOME's `gtk-decoration-layout` gsetting) via [`WindowButtonLayout::parse`].
 455#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 456pub struct WindowButtonLayout {
 457    /// Buttons on the left side of the titlebar.
 458    pub left: [Option<WindowButton>; MAX_BUTTONS_PER_SIDE],
 459    /// Buttons on the right side of the titlebar.
 460    pub right: [Option<WindowButton>; MAX_BUTTONS_PER_SIDE],
 461}
 462
 463#[cfg(any(target_os = "linux", target_os = "freebsd"))]
 464impl WindowButtonLayout {
 465    /// Returns Zed's built-in fallback button layout for Linux titlebars.
 466    pub fn linux_default() -> Self {
 467        Self {
 468            left: [None; MAX_BUTTONS_PER_SIDE],
 469            right: [
 470                Some(WindowButton::Minimize),
 471                Some(WindowButton::Maximize),
 472                Some(WindowButton::Close),
 473            ],
 474        }
 475    }
 476
 477    /// Parses a GNOME-style `button-layout` string (e.g. `"close,minimize:maximize"`).
 478    pub fn parse(layout_string: &str) -> Result<Self> {
 479        fn parse_side(
 480            s: &str,
 481            seen_buttons: &mut [bool; MAX_BUTTONS_PER_SIDE],
 482            unrecognized: &mut Vec<String>,
 483        ) -> [Option<WindowButton>; MAX_BUTTONS_PER_SIDE] {
 484            let mut result = [None; MAX_BUTTONS_PER_SIDE];
 485            let mut i = 0;
 486            for name in s.split(',') {
 487                let trimmed = name.trim();
 488                if trimmed.is_empty() {
 489                    continue;
 490                }
 491                let button = match trimmed {
 492                    "minimize" => Some(WindowButton::Minimize),
 493                    "maximize" => Some(WindowButton::Maximize),
 494                    "close" => Some(WindowButton::Close),
 495                    other => {
 496                        unrecognized.push(other.to_string());
 497                        None
 498                    }
 499                };
 500                if let Some(button) = button {
 501                    if seen_buttons[button.index()] {
 502                        continue;
 503                    }
 504                    if let Some(slot) = result.get_mut(i) {
 505                        *slot = Some(button);
 506                        seen_buttons[button.index()] = true;
 507                        i += 1;
 508                    }
 509                }
 510            }
 511            result
 512        }
 513
 514        let (left_str, right_str) = layout_string.split_once(':').unwrap_or(("", layout_string));
 515        let mut unrecognized = Vec::new();
 516        let mut seen_buttons = [false; MAX_BUTTONS_PER_SIDE];
 517        let layout = Self {
 518            left: parse_side(left_str, &mut seen_buttons, &mut unrecognized),
 519            right: parse_side(right_str, &mut seen_buttons, &mut unrecognized),
 520        };
 521
 522        if !unrecognized.is_empty()
 523            && layout.left.iter().all(Option::is_none)
 524            && layout.right.iter().all(Option::is_none)
 525        {
 526            bail!(
 527                "button layout string {:?} contains no valid buttons (unrecognized: {})",
 528                layout_string,
 529                unrecognized.join(", ")
 530            );
 531        }
 532
 533        Ok(layout)
 534    }
 535
 536    /// Formats the layout back into a GNOME-style `button-layout` string.
 537    #[cfg(test)]
 538    pub fn format(&self) -> String {
 539        fn format_side(buttons: &[Option<WindowButton>; MAX_BUTTONS_PER_SIDE]) -> String {
 540            buttons
 541                .iter()
 542                .flatten()
 543                .map(|button| match button {
 544                    WindowButton::Minimize => "minimize",
 545                    WindowButton::Maximize => "maximize",
 546                    WindowButton::Close => "close",
 547                })
 548                .collect::<Vec<_>>()
 549                .join(",")
 550        }
 551
 552        format!("{}:{}", format_side(&self.left), format_side(&self.right))
 553    }
 554}
 555
 556/// A type to describe which sides of the window are currently tiled in some way
 557#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
 558pub struct Tiling {
 559    /// Whether the top edge is tiled
 560    pub top: bool,
 561    /// Whether the left edge is tiled
 562    pub left: bool,
 563    /// Whether the right edge is tiled
 564    pub right: bool,
 565    /// Whether the bottom edge is tiled
 566    pub bottom: bool,
 567}
 568
 569impl Tiling {
 570    /// Initializes a [`Tiling`] type with all sides tiled
 571    pub fn tiled() -> Self {
 572        Self {
 573            top: true,
 574            left: true,
 575            right: true,
 576            bottom: true,
 577        }
 578    }
 579
 580    /// Whether any edge is tiled
 581    pub fn is_tiled(&self) -> bool {
 582        self.top || self.left || self.right || self.bottom
 583    }
 584}
 585
 586#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
 587#[expect(missing_docs)]
 588pub struct RequestFrameOptions {
 589    /// Whether a presentation is required.
 590    pub require_presentation: bool,
 591    /// Force refresh of all rendering states when true.
 592    pub force_render: bool,
 593}
 594
 595#[expect(missing_docs)]
 596pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle {
 597    fn bounds(&self) -> Bounds<Pixels>;
 598    fn is_maximized(&self) -> bool;
 599    fn window_bounds(&self) -> WindowBounds;
 600    fn content_size(&self) -> Size<Pixels>;
 601    fn resize(&mut self, size: Size<Pixels>);
 602    fn scale_factor(&self) -> f32;
 603    fn appearance(&self) -> WindowAppearance;
 604    fn display(&self) -> Option<Rc<dyn PlatformDisplay>>;
 605    fn mouse_position(&self) -> Point<Pixels>;
 606    fn modifiers(&self) -> Modifiers;
 607    fn capslock(&self) -> Capslock;
 608    fn set_input_handler(&mut self, input_handler: PlatformInputHandler);
 609    fn take_input_handler(&mut self) -> Option<PlatformInputHandler>;
 610    fn prompt(
 611        &self,
 612        level: PromptLevel,
 613        msg: &str,
 614        detail: Option<&str>,
 615        answers: &[PromptButton],
 616    ) -> Option<oneshot::Receiver<usize>>;
 617    fn activate(&self);
 618    fn is_active(&self) -> bool;
 619    fn is_hovered(&self) -> bool;
 620    fn background_appearance(&self) -> WindowBackgroundAppearance;
 621    fn set_title(&mut self, title: &str);
 622    fn set_background_appearance(&self, background_appearance: WindowBackgroundAppearance);
 623    fn minimize(&self);
 624    fn zoom(&self);
 625    fn toggle_fullscreen(&self);
 626    fn is_fullscreen(&self) -> bool;
 627    fn on_request_frame(&self, callback: Box<dyn FnMut(RequestFrameOptions)>);
 628    fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> DispatchEventResult>);
 629    fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>);
 630    fn on_hover_status_change(&self, callback: Box<dyn FnMut(bool)>);
 631    fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>);
 632    fn on_moved(&self, callback: Box<dyn FnMut()>);
 633    fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>);
 634    fn on_hit_test_window_control(&self, callback: Box<dyn FnMut() -> Option<WindowControlArea>>);
 635    fn on_close(&self, callback: Box<dyn FnOnce()>);
 636    fn on_appearance_changed(&self, callback: Box<dyn FnMut()>);
 637    fn on_button_layout_changed(&self, _callback: Box<dyn FnMut()>) {}
 638    fn draw(&self, scene: &Scene);
 639    fn completed_frame(&self) {}
 640    fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas>;
 641    fn is_subpixel_rendering_supported(&self) -> bool;
 642
 643    // macOS specific methods
 644    fn get_title(&self) -> String {
 645        String::new()
 646    }
 647    fn tabbed_windows(&self) -> Option<Vec<SystemWindowTab>> {
 648        None
 649    }
 650    fn tab_bar_visible(&self) -> bool {
 651        false
 652    }
 653    fn set_edited(&mut self, _edited: bool) {}
 654    fn show_character_palette(&self) {}
 655    fn titlebar_double_click(&self) {}
 656    fn on_move_tab_to_new_window(&self, _callback: Box<dyn FnMut()>) {}
 657    fn on_merge_all_windows(&self, _callback: Box<dyn FnMut()>) {}
 658    fn on_select_previous_tab(&self, _callback: Box<dyn FnMut()>) {}
 659    fn on_select_next_tab(&self, _callback: Box<dyn FnMut()>) {}
 660    fn on_toggle_tab_bar(&self, _callback: Box<dyn FnMut()>) {}
 661    fn merge_all_windows(&self) {}
 662    fn move_tab_to_new_window(&self) {}
 663    fn toggle_window_tab_overview(&self) {}
 664    fn set_tabbing_identifier(&self, _identifier: Option<String>) {}
 665
 666    #[cfg(target_os = "windows")]
 667    fn get_raw_handle(&self) -> windows::Win32::Foundation::HWND;
 668
 669    // Linux specific methods
 670    fn inner_window_bounds(&self) -> WindowBounds {
 671        self.window_bounds()
 672    }
 673    fn request_decorations(&self, _decorations: WindowDecorations) {}
 674    fn show_window_menu(&self, _position: Point<Pixels>) {}
 675    fn start_window_move(&self) {}
 676    fn start_window_resize(&self, _edge: ResizeEdge) {}
 677    fn window_decorations(&self) -> Decorations {
 678        Decorations::Server
 679    }
 680    fn set_app_id(&mut self, _app_id: &str) {}
 681    fn map_window(&mut self) -> anyhow::Result<()> {
 682        Ok(())
 683    }
 684    fn window_controls(&self) -> WindowControls {
 685        WindowControls::default()
 686    }
 687    fn set_client_inset(&self, _inset: Pixels) {}
 688    fn gpu_specs(&self) -> Option<GpuSpecs>;
 689
 690    fn update_ime_position(&self, _bounds: Bounds<Pixels>);
 691
 692    #[cfg(any(test, feature = "test-support"))]
 693    fn as_test(&mut self) -> Option<&mut TestWindow> {
 694        None
 695    }
 696
 697    /// Renders the given scene to a texture and returns the pixel data as an RGBA image.
 698    /// This does not present the frame to screen - useful for visual testing where we want
 699    /// to capture what would be rendered without displaying it or requiring the window to be visible.
 700    #[cfg(any(test, feature = "test-support"))]
 701    fn render_to_image(&self, _scene: &Scene) -> Result<RgbaImage> {
 702        anyhow::bail!("render_to_image not implemented for this platform")
 703    }
 704}
 705
 706/// A renderer for headless windows that can produce real rendered output.
 707#[cfg(any(test, feature = "test-support"))]
 708pub trait PlatformHeadlessRenderer {
 709    /// Render a scene and return the result as an RGBA image.
 710    fn render_scene_to_image(
 711        &mut self,
 712        scene: &Scene,
 713        size: Size<DevicePixels>,
 714    ) -> Result<RgbaImage>;
 715
 716    /// Returns the sprite atlas used by this renderer.
 717    fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas>;
 718}
 719
 720/// Type alias for runnables with metadata.
 721/// Previously an enum with a single variant, now simplified to a direct type alias.
 722#[doc(hidden)]
 723pub type RunnableVariant = Runnable<RunnableMeta>;
 724
 725#[doc(hidden)]
 726pub type TimerResolutionGuard = gpui_util::Deferred<Box<dyn FnOnce() + Send>>;
 727
 728/// This type is public so that our test macro can generate and use it, but it should not
 729/// be considered part of our public API.
 730#[doc(hidden)]
 731pub trait PlatformDispatcher: Send + Sync {
 732    fn get_all_timings(&self) -> Vec<ThreadTaskTimings>;
 733    fn get_current_thread_timings(&self) -> ThreadTaskTimings;
 734    fn is_main_thread(&self) -> bool;
 735    fn dispatch(&self, runnable: RunnableVariant, priority: Priority);
 736    fn dispatch_on_main_thread(&self, runnable: RunnableVariant, priority: Priority);
 737    fn dispatch_after(&self, duration: Duration, runnable: RunnableVariant);
 738
 739    fn spawn_realtime(&self, f: Box<dyn FnOnce() + Send>);
 740
 741    fn now(&self) -> Instant {
 742        Instant::now()
 743    }
 744
 745    fn increase_timer_resolution(&self) -> TimerResolutionGuard {
 746        gpui_util::defer(Box::new(|| {}))
 747    }
 748
 749    #[cfg(any(test, feature = "test-support"))]
 750    fn as_test(&self) -> Option<&TestDispatcher> {
 751        None
 752    }
 753}
 754
 755#[expect(missing_docs)]
 756pub trait PlatformTextSystem: Send + Sync {
 757    fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()>;
 758    /// Get all available font names.
 759    fn all_font_names(&self) -> Vec<String>;
 760    /// Get the font ID for a font descriptor.
 761    fn font_id(&self, descriptor: &Font) -> Result<FontId>;
 762    /// Get metrics for a font.
 763    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
 764    /// Get typographic bounds for a glyph.
 765    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>>;
 766    /// Get the advance width for a glyph.
 767    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>>;
 768    /// Get the glyph ID for a character.
 769    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
 770    /// Get raster bounds for a glyph.
 771    fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>>;
 772    /// Rasterize a glyph.
 773    fn rasterize_glyph(
 774        &self,
 775        params: &RenderGlyphParams,
 776        raster_bounds: Bounds<DevicePixels>,
 777    ) -> Result<(Size<DevicePixels>, Vec<u8>)>;
 778    /// Layout a line of text with the given font runs.
 779    fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout;
 780    /// Returns the recommended text rendering mode for the given font and size.
 781    fn recommended_rendering_mode(&self, _font_id: FontId, _font_size: Pixels)
 782    -> TextRenderingMode;
 783}
 784
 785#[expect(missing_docs)]
 786pub struct NoopTextSystem;
 787
 788#[expect(missing_docs)]
 789impl NoopTextSystem {
 790    #[allow(dead_code)]
 791    pub fn new() -> Self {
 792        Self
 793    }
 794}
 795
 796impl PlatformTextSystem for NoopTextSystem {
 797    fn add_fonts(&self, _fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
 798        Ok(())
 799    }
 800
 801    fn all_font_names(&self) -> Vec<String> {
 802        Vec::new()
 803    }
 804
 805    fn font_id(&self, _descriptor: &Font) -> Result<FontId> {
 806        Ok(FontId(1))
 807    }
 808
 809    fn font_metrics(&self, _font_id: FontId) -> FontMetrics {
 810        FontMetrics {
 811            units_per_em: 1000,
 812            ascent: 1025.0,
 813            descent: -275.0,
 814            line_gap: 0.0,
 815            underline_position: -95.0,
 816            underline_thickness: 60.0,
 817            cap_height: 698.0,
 818            x_height: 516.0,
 819            bounding_box: Bounds {
 820                origin: Point {
 821                    x: -260.0,
 822                    y: -245.0,
 823                },
 824                size: Size {
 825                    width: 1501.0,
 826                    height: 1364.0,
 827                },
 828            },
 829        }
 830    }
 831
 832    fn typographic_bounds(&self, _font_id: FontId, _glyph_id: GlyphId) -> Result<Bounds<f32>> {
 833        Ok(Bounds {
 834            origin: Point { x: 54.0, y: 0.0 },
 835            size: size(392.0, 528.0),
 836        })
 837    }
 838
 839    fn advance(&self, _font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
 840        Ok(size(600.0 * glyph_id.0 as f32, 0.0))
 841    }
 842
 843    fn glyph_for_char(&self, _font_id: FontId, ch: char) -> Option<GlyphId> {
 844        Some(GlyphId(ch.len_utf16() as u32))
 845    }
 846
 847    fn glyph_raster_bounds(&self, _params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
 848        Ok(Default::default())
 849    }
 850
 851    fn rasterize_glyph(
 852        &self,
 853        _params: &RenderGlyphParams,
 854        raster_bounds: Bounds<DevicePixels>,
 855    ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
 856        Ok((raster_bounds.size, Vec::new()))
 857    }
 858
 859    fn layout_line(&self, text: &str, font_size: Pixels, _runs: &[FontRun]) -> LineLayout {
 860        let mut position = px(0.);
 861        let metrics = self.font_metrics(FontId(0));
 862        let em_width = font_size
 863            * self
 864                .advance(FontId(0), self.glyph_for_char(FontId(0), 'm').unwrap())
 865                .unwrap()
 866                .width
 867            / metrics.units_per_em as f32;
 868        let mut glyphs = Vec::new();
 869        for (ix, c) in text.char_indices() {
 870            if let Some(glyph) = self.glyph_for_char(FontId(0), c) {
 871                glyphs.push(ShapedGlyph {
 872                    id: glyph,
 873                    position: point(position, px(0.)),
 874                    index: ix,
 875                    is_emoji: glyph.0 == 2,
 876                });
 877                if glyph.0 == 2 {
 878                    position += em_width * 2.0;
 879                } else {
 880                    position += em_width;
 881                }
 882            } else {
 883                position += em_width
 884            }
 885        }
 886        let mut runs = Vec::default();
 887        if !glyphs.is_empty() {
 888            runs.push(ShapedRun {
 889                font_id: FontId(0),
 890                glyphs,
 891            });
 892        } else {
 893            position = px(0.);
 894        }
 895
 896        LineLayout {
 897            font_size,
 898            width: position,
 899            ascent: font_size * (metrics.ascent / metrics.units_per_em as f32),
 900            descent: font_size * (metrics.descent / metrics.units_per_em as f32),
 901            runs,
 902            len: text.len(),
 903        }
 904    }
 905
 906    fn recommended_rendering_mode(
 907        &self,
 908        _font_id: FontId,
 909        _font_size: Pixels,
 910    ) -> TextRenderingMode {
 911        TextRenderingMode::Grayscale
 912    }
 913}
 914
 915// Adapted from https://github.com/microsoft/terminal/blob/1283c0f5b99a2961673249fa77c6b986efb5086c/src/renderer/atlas/dwrite.cpp
 916// Copyright (c) Microsoft Corporation.
 917// Licensed under the MIT license.
 918/// Compute gamma correction ratios for subpixel text rendering.
 919#[allow(dead_code)]
 920pub fn get_gamma_correction_ratios(gamma: f32) -> [f32; 4] {
 921    const GAMMA_INCORRECT_TARGET_RATIOS: [[f32; 4]; 13] = [
 922        [0.0000 / 4.0, 0.0000 / 4.0, 0.0000 / 4.0, 0.0000 / 4.0], // gamma = 1.0
 923        [0.0166 / 4.0, -0.0807 / 4.0, 0.2227 / 4.0, -0.0751 / 4.0], // gamma = 1.1
 924        [0.0350 / 4.0, -0.1760 / 4.0, 0.4325 / 4.0, -0.1370 / 4.0], // gamma = 1.2
 925        [0.0543 / 4.0, -0.2821 / 4.0, 0.6302 / 4.0, -0.1876 / 4.0], // gamma = 1.3
 926        [0.0739 / 4.0, -0.3963 / 4.0, 0.8167 / 4.0, -0.2287 / 4.0], // gamma = 1.4
 927        [0.0933 / 4.0, -0.5161 / 4.0, 0.9926 / 4.0, -0.2616 / 4.0], // gamma = 1.5
 928        [0.1121 / 4.0, -0.6395 / 4.0, 1.1588 / 4.0, -0.2877 / 4.0], // gamma = 1.6
 929        [0.1300 / 4.0, -0.7649 / 4.0, 1.3159 / 4.0, -0.3080 / 4.0], // gamma = 1.7
 930        [0.1469 / 4.0, -0.8911 / 4.0, 1.4644 / 4.0, -0.3234 / 4.0], // gamma = 1.8
 931        [0.1627 / 4.0, -1.0170 / 4.0, 1.6051 / 4.0, -0.3347 / 4.0], // gamma = 1.9
 932        [0.1773 / 4.0, -1.1420 / 4.0, 1.7385 / 4.0, -0.3426 / 4.0], // gamma = 2.0
 933        [0.1908 / 4.0, -1.2652 / 4.0, 1.8650 / 4.0, -0.3476 / 4.0], // gamma = 2.1
 934        [0.2031 / 4.0, -1.3864 / 4.0, 1.9851 / 4.0, -0.3501 / 4.0], // gamma = 2.2
 935    ];
 936
 937    const NORM13: f32 = ((0x10000 as f64) / (255.0 * 255.0) * 4.0) as f32;
 938    const NORM24: f32 = ((0x100 as f64) / (255.0) * 4.0) as f32;
 939
 940    let index = ((gamma * 10.0).round() as usize).clamp(10, 22) - 10;
 941    let ratios = GAMMA_INCORRECT_TARGET_RATIOS[index];
 942
 943    [
 944        ratios[0] * NORM13,
 945        ratios[1] * NORM24,
 946        ratios[2] * NORM13,
 947        ratios[3] * NORM24,
 948    ]
 949}
 950
 951#[derive(PartialEq, Eq, Hash, Clone)]
 952#[expect(missing_docs)]
 953pub enum AtlasKey {
 954    Glyph(RenderGlyphParams),
 955    Svg(RenderSvgParams),
 956    Image(RenderImageParams),
 957}
 958
 959impl AtlasKey {
 960    #[cfg_attr(
 961        all(
 962            any(target_os = "linux", target_os = "freebsd"),
 963            not(any(feature = "x11", feature = "wayland"))
 964        ),
 965        allow(dead_code)
 966    )]
 967    /// Returns the texture kind for this atlas key.
 968    pub fn texture_kind(&self) -> AtlasTextureKind {
 969        match self {
 970            AtlasKey::Glyph(params) => {
 971                if params.is_emoji {
 972                    AtlasTextureKind::Polychrome
 973                } else if params.subpixel_rendering {
 974                    AtlasTextureKind::Subpixel
 975                } else {
 976                    AtlasTextureKind::Monochrome
 977                }
 978            }
 979            AtlasKey::Svg(_) => AtlasTextureKind::Monochrome,
 980            AtlasKey::Image(_) => AtlasTextureKind::Polychrome,
 981        }
 982    }
 983}
 984
 985impl From<RenderGlyphParams> for AtlasKey {
 986    fn from(params: RenderGlyphParams) -> Self {
 987        Self::Glyph(params)
 988    }
 989}
 990
 991impl From<RenderSvgParams> for AtlasKey {
 992    fn from(params: RenderSvgParams) -> Self {
 993        Self::Svg(params)
 994    }
 995}
 996
 997impl From<RenderImageParams> for AtlasKey {
 998    fn from(params: RenderImageParams) -> Self {
 999        Self::Image(params)
1000    }
1001}
1002
1003#[expect(missing_docs)]
1004pub trait PlatformAtlas {
1005    fn get_or_insert_with<'a>(
1006        &self,
1007        key: &AtlasKey,
1008        build: &mut dyn FnMut() -> Result<Option<(Size<DevicePixels>, Cow<'a, [u8]>)>>,
1009    ) -> Result<Option<AtlasTile>>;
1010    fn remove(&self, key: &AtlasKey);
1011}
1012
1013#[doc(hidden)]
1014pub struct AtlasTextureList<T> {
1015    pub textures: Vec<Option<T>>,
1016    pub free_list: Vec<usize>,
1017}
1018
1019impl<T> Default for AtlasTextureList<T> {
1020    fn default() -> Self {
1021        Self {
1022            textures: Vec::default(),
1023            free_list: Vec::default(),
1024        }
1025    }
1026}
1027
1028impl<T> ops::Index<usize> for AtlasTextureList<T> {
1029    type Output = Option<T>;
1030
1031    fn index(&self, index: usize) -> &Self::Output {
1032        &self.textures[index]
1033    }
1034}
1035
1036impl<T> AtlasTextureList<T> {
1037    #[allow(unused)]
1038    pub fn drain(&mut self) -> std::vec::Drain<'_, Option<T>> {
1039        self.free_list.clear();
1040        self.textures.drain(..)
1041    }
1042
1043    #[allow(dead_code)]
1044    pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
1045        self.textures.iter_mut().flatten()
1046    }
1047}
1048
1049#[derive(Clone, Debug, PartialEq, Eq)]
1050#[repr(C)]
1051#[expect(missing_docs)]
1052pub struct AtlasTile {
1053    /// The texture this tile belongs to.
1054    pub texture_id: AtlasTextureId,
1055    /// The unique ID of this tile within its texture.
1056    pub tile_id: TileId,
1057    /// Padding around the tile content in pixels.
1058    pub padding: u32,
1059    /// The bounds of this tile within the texture.
1060    pub bounds: Bounds<DevicePixels>,
1061}
1062
1063#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1064#[repr(C)]
1065#[expect(missing_docs)]
1066pub struct AtlasTextureId {
1067    // We use u32 instead of usize for Metal Shader Language compatibility
1068    /// The index of this texture in the atlas.
1069    pub index: u32,
1070    /// The kind of content stored in this texture.
1071    pub kind: AtlasTextureKind,
1072}
1073
1074#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1075#[repr(C)]
1076#[cfg_attr(
1077    all(
1078        any(target_os = "linux", target_os = "freebsd"),
1079        not(any(feature = "x11", feature = "wayland"))
1080    ),
1081    allow(dead_code)
1082)]
1083#[expect(missing_docs)]
1084pub enum AtlasTextureKind {
1085    Monochrome = 0,
1086    Polychrome = 1,
1087    Subpixel = 2,
1088}
1089
1090#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
1091#[repr(C)]
1092#[expect(missing_docs)]
1093pub struct TileId(pub u32);
1094
1095impl From<etagere::AllocId> for TileId {
1096    fn from(id: etagere::AllocId) -> Self {
1097        Self(id.serialize())
1098    }
1099}
1100
1101impl From<TileId> for etagere::AllocId {
1102    fn from(id: TileId) -> Self {
1103        Self::deserialize(id.0)
1104    }
1105}
1106
1107#[expect(missing_docs)]
1108pub struct PlatformInputHandler {
1109    cx: AsyncWindowContext,
1110    handler: Box<dyn InputHandler>,
1111}
1112
1113#[expect(missing_docs)]
1114#[cfg_attr(
1115    all(
1116        any(target_os = "linux", target_os = "freebsd"),
1117        not(any(feature = "x11", feature = "wayland"))
1118    ),
1119    allow(dead_code)
1120)]
1121impl PlatformInputHandler {
1122    pub fn new(cx: AsyncWindowContext, handler: Box<dyn InputHandler>) -> Self {
1123        Self { cx, handler }
1124    }
1125
1126    pub fn selected_text_range(&mut self, ignore_disabled_input: bool) -> Option<UTF16Selection> {
1127        self.cx
1128            .update(|window, cx| {
1129                self.handler
1130                    .selected_text_range(ignore_disabled_input, window, cx)
1131            })
1132            .ok()
1133            .flatten()
1134    }
1135
1136    #[cfg_attr(target_os = "windows", allow(dead_code))]
1137    pub fn marked_text_range(&mut self) -> Option<Range<usize>> {
1138        self.cx
1139            .update(|window, cx| self.handler.marked_text_range(window, cx))
1140            .ok()
1141            .flatten()
1142    }
1143
1144    #[cfg_attr(
1145        any(target_os = "linux", target_os = "freebsd", target_os = "windows"),
1146        allow(dead_code)
1147    )]
1148    pub fn text_for_range(
1149        &mut self,
1150        range_utf16: Range<usize>,
1151        adjusted: &mut Option<Range<usize>>,
1152    ) -> Option<String> {
1153        self.cx
1154            .update(|window, cx| {
1155                self.handler
1156                    .text_for_range(range_utf16, adjusted, window, cx)
1157            })
1158            .ok()
1159            .flatten()
1160    }
1161
1162    pub fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, text: &str) {
1163        self.cx
1164            .update(|window, cx| {
1165                self.handler
1166                    .replace_text_in_range(replacement_range, text, window, cx);
1167            })
1168            .ok();
1169    }
1170
1171    pub fn replace_and_mark_text_in_range(
1172        &mut self,
1173        range_utf16: Option<Range<usize>>,
1174        new_text: &str,
1175        new_selected_range: Option<Range<usize>>,
1176    ) {
1177        self.cx
1178            .update(|window, cx| {
1179                self.handler.replace_and_mark_text_in_range(
1180                    range_utf16,
1181                    new_text,
1182                    new_selected_range,
1183                    window,
1184                    cx,
1185                )
1186            })
1187            .ok();
1188    }
1189
1190    #[cfg_attr(target_os = "windows", allow(dead_code))]
1191    pub fn unmark_text(&mut self) {
1192        self.cx
1193            .update(|window, cx| self.handler.unmark_text(window, cx))
1194            .ok();
1195    }
1196
1197    pub fn bounds_for_range(&mut self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>> {
1198        self.cx
1199            .update(|window, cx| self.handler.bounds_for_range(range_utf16, window, cx))
1200            .ok()
1201            .flatten()
1202    }
1203
1204    #[allow(dead_code)]
1205    pub fn apple_press_and_hold_enabled(&mut self) -> bool {
1206        self.handler.apple_press_and_hold_enabled()
1207    }
1208
1209    pub fn dispatch_input(&mut self, input: &str, window: &mut Window, cx: &mut App) {
1210        self.handler.replace_text_in_range(None, input, window, cx);
1211    }
1212
1213    pub fn selected_bounds(&mut self, window: &mut Window, cx: &mut App) -> Option<Bounds<Pixels>> {
1214        let selection = self.handler.selected_text_range(true, window, cx)?;
1215        self.handler.bounds_for_range(
1216            if selection.reversed {
1217                selection.range.start..selection.range.start
1218            } else {
1219                selection.range.end..selection.range.end
1220            },
1221            window,
1222            cx,
1223        )
1224    }
1225
1226    #[allow(unused)]
1227    pub fn character_index_for_point(&mut self, point: Point<Pixels>) -> Option<usize> {
1228        self.cx
1229            .update(|window, cx| self.handler.character_index_for_point(point, window, cx))
1230            .ok()
1231            .flatten()
1232    }
1233
1234    #[allow(dead_code)]
1235    pub fn accepts_text_input(&mut self, window: &mut Window, cx: &mut App) -> bool {
1236        self.handler.accepts_text_input(window, cx)
1237    }
1238
1239    #[allow(dead_code)]
1240    pub fn query_accepts_text_input(&mut self) -> bool {
1241        self.cx
1242            .update(|window, cx| self.handler.accepts_text_input(window, cx))
1243            .unwrap_or(true)
1244    }
1245
1246    #[allow(dead_code)]
1247    pub fn query_prefers_ime_for_printable_keys(&mut self) -> bool {
1248        self.cx
1249            .update(|window, cx| self.handler.prefers_ime_for_printable_keys(window, cx))
1250            .unwrap_or(false)
1251    }
1252}
1253
1254/// A struct representing a selection in a text buffer, in UTF16 characters.
1255/// This is different from a range because the head may be before the tail.
1256#[derive(Debug)]
1257pub struct UTF16Selection {
1258    /// The range of text in the document this selection corresponds to
1259    /// in UTF16 characters.
1260    pub range: Range<usize>,
1261    /// Whether the head of this selection is at the start (true), or end (false)
1262    /// of the range
1263    pub reversed: bool,
1264}
1265
1266/// Zed's interface for handling text input from the platform's IME system
1267/// This is currently a 1:1 exposure of the NSTextInputClient API:
1268///
1269/// <https://developer.apple.com/documentation/appkit/nstextinputclient>
1270pub trait InputHandler: 'static {
1271    /// Get the range of the user's currently selected text, if any
1272    /// Corresponds to [selectedRange()](https://developer.apple.com/documentation/appkit/nstextinputclient/1438242-selectedrange)
1273    ///
1274    /// Return value is in terms of UTF-16 characters, from 0 to the length of the document
1275    fn selected_text_range(
1276        &mut self,
1277        ignore_disabled_input: bool,
1278        window: &mut Window,
1279        cx: &mut App,
1280    ) -> Option<UTF16Selection>;
1281
1282    /// Get the range of the currently marked text, if any
1283    /// Corresponds to [markedRange()](https://developer.apple.com/documentation/appkit/nstextinputclient/1438250-markedrange)
1284    ///
1285    /// Return value is in terms of UTF-16 characters, from 0 to the length of the document
1286    fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>>;
1287
1288    /// Get the text for the given document range in UTF-16 characters
1289    /// Corresponds to [attributedSubstring(forProposedRange: actualRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438238-attributedsubstring)
1290    ///
1291    /// range_utf16 is in terms of UTF-16 characters
1292    fn text_for_range(
1293        &mut self,
1294        range_utf16: Range<usize>,
1295        adjusted_range: &mut Option<Range<usize>>,
1296        window: &mut Window,
1297        cx: &mut App,
1298    ) -> Option<String>;
1299
1300    /// Replace the text in the given document range with the given text
1301    /// Corresponds to [insertText(_:replacementRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438258-inserttext)
1302    ///
1303    /// replacement_range is in terms of UTF-16 characters
1304    fn replace_text_in_range(
1305        &mut self,
1306        replacement_range: Option<Range<usize>>,
1307        text: &str,
1308        window: &mut Window,
1309        cx: &mut App,
1310    );
1311
1312    /// Replace the text in the given document range with the given text,
1313    /// and mark the given text as part of an IME 'composing' state
1314    /// Corresponds to [setMarkedText(_:selectedRange:replacementRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438246-setmarkedtext)
1315    ///
1316    /// range_utf16 is in terms of UTF-16 characters
1317    /// new_selected_range is in terms of UTF-16 characters
1318    fn replace_and_mark_text_in_range(
1319        &mut self,
1320        range_utf16: Option<Range<usize>>,
1321        new_text: &str,
1322        new_selected_range: Option<Range<usize>>,
1323        window: &mut Window,
1324        cx: &mut App,
1325    );
1326
1327    /// Remove the IME 'composing' state from the document
1328    /// Corresponds to [unmarkText()](https://developer.apple.com/documentation/appkit/nstextinputclient/1438239-unmarktext)
1329    fn unmark_text(&mut self, window: &mut Window, cx: &mut App);
1330
1331    /// Get the bounds of the given document range in screen coordinates
1332    /// Corresponds to [firstRect(forCharacterRange:actualRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438240-firstrect)
1333    ///
1334    /// This is used for positioning the IME candidate window
1335    fn bounds_for_range(
1336        &mut self,
1337        range_utf16: Range<usize>,
1338        window: &mut Window,
1339        cx: &mut App,
1340    ) -> Option<Bounds<Pixels>>;
1341
1342    /// Get the character offset for the given point in terms of UTF16 characters
1343    ///
1344    /// Corresponds to [characterIndexForPoint:](https://developer.apple.com/documentation/appkit/nstextinputclient/characterindex(for:))
1345    fn character_index_for_point(
1346        &mut self,
1347        point: Point<Pixels>,
1348        window: &mut Window,
1349        cx: &mut App,
1350    ) -> Option<usize>;
1351
1352    /// Allows a given input context to opt into getting raw key repeats instead of
1353    /// sending these to the platform.
1354    /// TODO: Ideally we should be able to set ApplePressAndHoldEnabled in NSUserDefaults
1355    /// (which is how iTerm does it) but it doesn't seem to work for me.
1356    #[allow(dead_code)]
1357    fn apple_press_and_hold_enabled(&mut self) -> bool {
1358        true
1359    }
1360
1361    /// Returns whether this handler is accepting text input to be inserted.
1362    fn accepts_text_input(&mut self, _window: &mut Window, _cx: &mut App) -> bool {
1363        true
1364    }
1365
1366    /// Returns whether printable keys should be routed to the IME before keybinding
1367    /// matching when a non-ASCII input source (e.g. Japanese, Korean, Chinese IME)
1368    /// is active. This prevents multi-stroke keybindings like `jj` from intercepting
1369    /// keys that the IME should compose.
1370    ///
1371    /// Defaults to `false`. The editor overrides this based on whether it expects
1372    /// character input (e.g. Vim insert mode returns `true`, normal mode returns `false`).
1373    /// The terminal keeps the default `false` so that raw keys reach the terminal process.
1374    fn prefers_ime_for_printable_keys(&mut self, _window: &mut Window, _cx: &mut App) -> bool {
1375        false
1376    }
1377}
1378
1379/// The variables that can be configured when creating a new window
1380#[derive(Debug)]
1381pub struct WindowOptions {
1382    /// Specifies the state and bounds of the window in screen coordinates.
1383    /// - `None`: Inherit the bounds.
1384    /// - `Some(WindowBounds)`: Open a window with corresponding state and its restore size.
1385    pub window_bounds: Option<WindowBounds>,
1386
1387    /// The titlebar configuration of the window
1388    pub titlebar: Option<TitlebarOptions>,
1389
1390    /// Whether the window should be focused when created
1391    pub focus: bool,
1392
1393    /// Whether the window should be shown when created
1394    pub show: bool,
1395
1396    /// The kind of window to create
1397    pub kind: WindowKind,
1398
1399    /// Whether the window should be movable by the user
1400    pub is_movable: bool,
1401
1402    /// Whether the window should be resizable by the user
1403    pub is_resizable: bool,
1404
1405    /// Whether the window should be minimized by the user
1406    pub is_minimizable: bool,
1407
1408    /// The display to create the window on, if this is None,
1409    /// the window will be created on the main display
1410    pub display_id: Option<DisplayId>,
1411
1412    /// The appearance of the window background.
1413    pub window_background: WindowBackgroundAppearance,
1414
1415    /// Application identifier of the window. Can by used by desktop environments to group applications together.
1416    pub app_id: Option<String>,
1417
1418    /// Window minimum size
1419    pub window_min_size: Option<Size<Pixels>>,
1420
1421    /// Whether to use client or server side decorations. Wayland only
1422    /// Note that this may be ignored.
1423    pub window_decorations: Option<WindowDecorations>,
1424
1425    /// Tab group name, allows opening the window as a native tab on macOS 10.12+. Windows with the same tabbing identifier will be grouped together.
1426    pub tabbing_identifier: Option<String>,
1427}
1428
1429/// The variables that can be configured when creating a new window
1430#[derive(Debug)]
1431#[cfg_attr(
1432    all(
1433        any(target_os = "linux", target_os = "freebsd"),
1434        not(any(feature = "x11", feature = "wayland"))
1435    ),
1436    allow(dead_code)
1437)]
1438#[allow(missing_docs)]
1439pub struct WindowParams {
1440    pub bounds: Bounds<Pixels>,
1441
1442    /// The titlebar configuration of the window
1443    #[cfg_attr(feature = "wayland", allow(dead_code))]
1444    pub titlebar: Option<TitlebarOptions>,
1445
1446    /// The kind of window to create
1447    #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1448    pub kind: WindowKind,
1449
1450    /// Whether the window should be movable by the user
1451    #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1452    pub is_movable: bool,
1453
1454    /// Whether the window should be resizable by the user
1455    #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1456    pub is_resizable: bool,
1457
1458    /// Whether the window should be minimized by the user
1459    #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1460    pub is_minimizable: bool,
1461
1462    #[cfg_attr(
1463        any(target_os = "linux", target_os = "freebsd", target_os = "windows"),
1464        allow(dead_code)
1465    )]
1466    pub focus: bool,
1467
1468    #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1469    pub show: bool,
1470
1471    #[cfg_attr(feature = "wayland", allow(dead_code))]
1472    pub display_id: Option<DisplayId>,
1473
1474    pub window_min_size: Option<Size<Pixels>>,
1475    #[cfg(target_os = "macos")]
1476    pub tabbing_identifier: Option<String>,
1477}
1478
1479/// Represents the status of how a window should be opened.
1480#[derive(Debug, Copy, Clone, PartialEq)]
1481pub enum WindowBounds {
1482    /// Indicates that the window should open in a windowed state with the given bounds.
1483    Windowed(Bounds<Pixels>),
1484    /// Indicates that the window should open in a maximized state.
1485    /// The bounds provided here represent the restore size of the window.
1486    Maximized(Bounds<Pixels>),
1487    /// Indicates that the window should open in fullscreen mode.
1488    /// The bounds provided here represent the restore size of the window.
1489    Fullscreen(Bounds<Pixels>),
1490}
1491
1492impl Default for WindowBounds {
1493    fn default() -> Self {
1494        WindowBounds::Windowed(Bounds::default())
1495    }
1496}
1497
1498impl WindowBounds {
1499    /// Retrieve the inner bounds
1500    pub fn get_bounds(&self) -> Bounds<Pixels> {
1501        match self {
1502            WindowBounds::Windowed(bounds) => *bounds,
1503            WindowBounds::Maximized(bounds) => *bounds,
1504            WindowBounds::Fullscreen(bounds) => *bounds,
1505        }
1506    }
1507
1508    /// Creates a new window bounds that centers the window on the screen.
1509    pub fn centered(size: Size<Pixels>, cx: &App) -> Self {
1510        WindowBounds::Windowed(Bounds::centered(None, size, cx))
1511    }
1512}
1513
1514impl Default for WindowOptions {
1515    fn default() -> Self {
1516        Self {
1517            window_bounds: None,
1518            titlebar: Some(TitlebarOptions {
1519                title: Default::default(),
1520                appears_transparent: Default::default(),
1521                traffic_light_position: Default::default(),
1522            }),
1523            focus: true,
1524            show: true,
1525            kind: WindowKind::Normal,
1526            is_movable: true,
1527            is_resizable: true,
1528            is_minimizable: true,
1529            display_id: None,
1530            window_background: WindowBackgroundAppearance::default(),
1531            app_id: None,
1532            window_min_size: None,
1533            window_decorations: None,
1534            tabbing_identifier: None,
1535        }
1536    }
1537}
1538
1539/// The options that can be configured for a window's titlebar
1540#[derive(Debug, Default)]
1541pub struct TitlebarOptions {
1542    /// The initial title of the window
1543    pub title: Option<SharedString>,
1544
1545    /// Should the default system titlebar be hidden to allow for a custom-drawn titlebar? (macOS and Windows only)
1546    /// Refer to [`WindowOptions::window_decorations`] on Linux
1547    pub appears_transparent: bool,
1548
1549    /// The position of the macOS traffic light buttons
1550    pub traffic_light_position: Option<Point<Pixels>>,
1551}
1552
1553/// The kind of window to create
1554#[derive(Clone, Debug, PartialEq, Eq)]
1555pub enum WindowKind {
1556    /// A normal application window
1557    Normal,
1558
1559    /// A window that appears above all other windows, usually used for alerts or popups
1560    /// use sparingly!
1561    PopUp,
1562
1563    /// A floating window that appears on top of its parent window
1564    Floating,
1565
1566    /// A Wayland LayerShell window, used to draw overlays or backgrounds for applications such as
1567    /// docks, notifications or wallpapers.
1568    #[cfg(all(target_os = "linux", feature = "wayland"))]
1569    LayerShell(layer_shell::LayerShellOptions),
1570
1571    /// A window that appears on top of its parent window and blocks interaction with it
1572    /// until the modal window is closed
1573    Dialog,
1574}
1575
1576/// The appearance of the window, as defined by the operating system.
1577///
1578/// On macOS, this corresponds to named [`NSAppearance`](https://developer.apple.com/documentation/appkit/nsappearance)
1579/// values.
1580#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
1581pub enum WindowAppearance {
1582    /// A light appearance.
1583    ///
1584    /// On macOS, this corresponds to the `aqua` appearance.
1585    #[default]
1586    Light,
1587
1588    /// A light appearance with vibrant colors.
1589    ///
1590    /// On macOS, this corresponds to the `NSAppearanceNameVibrantLight` appearance.
1591    VibrantLight,
1592
1593    /// A dark appearance.
1594    ///
1595    /// On macOS, this corresponds to the `darkAqua` appearance.
1596    Dark,
1597
1598    /// A dark appearance with vibrant colors.
1599    ///
1600    /// On macOS, this corresponds to the `NSAppearanceNameVibrantDark` appearance.
1601    VibrantDark,
1602}
1603
1604/// The appearance of the background of the window itself, when there is
1605/// no content or the content is transparent.
1606#[derive(Copy, Clone, Debug, Default, PartialEq)]
1607pub enum WindowBackgroundAppearance {
1608    /// Opaque.
1609    ///
1610    /// This lets the window manager know that content behind this
1611    /// window does not need to be drawn.
1612    ///
1613    /// Actual color depends on the system and themes should define a fully
1614    /// opaque background color instead.
1615    #[default]
1616    Opaque,
1617    /// Plain alpha transparency.
1618    Transparent,
1619    /// Transparency, but the contents behind the window are blurred.
1620    ///
1621    /// Not always supported.
1622    Blurred,
1623    /// The Mica backdrop material, supported on Windows 11.
1624    MicaBackdrop,
1625    /// The Mica Alt backdrop material, supported on Windows 11.
1626    MicaAltBackdrop,
1627}
1628
1629/// The text rendering mode to use for drawing glyphs.
1630#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
1631pub enum TextRenderingMode {
1632    /// Use the platform's default text rendering mode.
1633    #[default]
1634    PlatformDefault,
1635    /// Use subpixel (ClearType-style) text rendering.
1636    Subpixel,
1637    /// Use grayscale text rendering.
1638    Grayscale,
1639}
1640
1641/// The options that can be configured for a file dialog prompt
1642#[derive(Clone, Debug)]
1643pub struct PathPromptOptions {
1644    /// Should the prompt allow files to be selected?
1645    pub files: bool,
1646    /// Should the prompt allow directories to be selected?
1647    pub directories: bool,
1648    /// Should the prompt allow multiple files to be selected?
1649    pub multiple: bool,
1650    /// The prompt to show to a user when selecting a path
1651    pub prompt: Option<SharedString>,
1652}
1653
1654/// What kind of prompt styling to show
1655#[derive(Copy, Clone, Debug, PartialEq)]
1656pub enum PromptLevel {
1657    /// A prompt that is shown when the user should be notified of something
1658    Info,
1659
1660    /// A prompt that is shown when the user needs to be warned of a potential problem
1661    Warning,
1662
1663    /// A prompt that is shown when a critical problem has occurred
1664    Critical,
1665}
1666
1667/// Prompt Button
1668#[derive(Clone, Debug, PartialEq)]
1669pub enum PromptButton {
1670    /// Ok button
1671    Ok(SharedString),
1672    /// Cancel button
1673    Cancel(SharedString),
1674    /// Other button
1675    Other(SharedString),
1676}
1677
1678impl PromptButton {
1679    /// Create a button with label
1680    pub fn new(label: impl Into<SharedString>) -> Self {
1681        PromptButton::Other(label.into())
1682    }
1683
1684    /// Create an Ok button
1685    pub fn ok(label: impl Into<SharedString>) -> Self {
1686        PromptButton::Ok(label.into())
1687    }
1688
1689    /// Create a Cancel button
1690    pub fn cancel(label: impl Into<SharedString>) -> Self {
1691        PromptButton::Cancel(label.into())
1692    }
1693
1694    /// Returns true if this button is a cancel button.
1695    #[allow(dead_code)]
1696    pub fn is_cancel(&self) -> bool {
1697        matches!(self, PromptButton::Cancel(_))
1698    }
1699
1700    /// Returns the label of the button
1701    pub fn label(&self) -> &SharedString {
1702        match self {
1703            PromptButton::Ok(label) => label,
1704            PromptButton::Cancel(label) => label,
1705            PromptButton::Other(label) => label,
1706        }
1707    }
1708}
1709
1710impl From<&str> for PromptButton {
1711    fn from(value: &str) -> Self {
1712        match value.to_lowercase().as_str() {
1713            "ok" => PromptButton::Ok("Ok".into()),
1714            "cancel" => PromptButton::Cancel("Cancel".into()),
1715            _ => PromptButton::Other(SharedString::from(value.to_owned())),
1716        }
1717    }
1718}
1719
1720/// The style of the cursor (pointer)
1721#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
1722pub enum CursorStyle {
1723    /// The default cursor
1724    #[default]
1725    Arrow,
1726
1727    /// A text input cursor
1728    /// corresponds to the CSS cursor value `text`
1729    IBeam,
1730
1731    /// A crosshair cursor
1732    /// corresponds to the CSS cursor value `crosshair`
1733    Crosshair,
1734
1735    /// A closed hand cursor
1736    /// corresponds to the CSS cursor value `grabbing`
1737    ClosedHand,
1738
1739    /// An open hand cursor
1740    /// corresponds to the CSS cursor value `grab`
1741    OpenHand,
1742
1743    /// A pointing hand cursor
1744    /// corresponds to the CSS cursor value `pointer`
1745    PointingHand,
1746
1747    /// A resize left cursor
1748    /// corresponds to the CSS cursor value `w-resize`
1749    ResizeLeft,
1750
1751    /// A resize right cursor
1752    /// corresponds to the CSS cursor value `e-resize`
1753    ResizeRight,
1754
1755    /// A resize cursor to the left and right
1756    /// corresponds to the CSS cursor value `ew-resize`
1757    ResizeLeftRight,
1758
1759    /// A resize up cursor
1760    /// corresponds to the CSS cursor value `n-resize`
1761    ResizeUp,
1762
1763    /// A resize down cursor
1764    /// corresponds to the CSS cursor value `s-resize`
1765    ResizeDown,
1766
1767    /// A resize cursor directing up and down
1768    /// corresponds to the CSS cursor value `ns-resize`
1769    ResizeUpDown,
1770
1771    /// A resize cursor directing up-left and down-right
1772    /// corresponds to the CSS cursor value `nesw-resize`
1773    ResizeUpLeftDownRight,
1774
1775    /// A resize cursor directing up-right and down-left
1776    /// corresponds to the CSS cursor value `nwse-resize`
1777    ResizeUpRightDownLeft,
1778
1779    /// A cursor indicating that the item/column can be resized horizontally.
1780    /// corresponds to the CSS cursor value `col-resize`
1781    ResizeColumn,
1782
1783    /// A cursor indicating that the item/row can be resized vertically.
1784    /// corresponds to the CSS cursor value `row-resize`
1785    ResizeRow,
1786
1787    /// A text input cursor for vertical layout
1788    /// corresponds to the CSS cursor value `vertical-text`
1789    IBeamCursorForVerticalLayout,
1790
1791    /// A cursor indicating that the operation is not allowed
1792    /// corresponds to the CSS cursor value `not-allowed`
1793    OperationNotAllowed,
1794
1795    /// A cursor indicating that the operation will result in a link
1796    /// corresponds to the CSS cursor value `alias`
1797    DragLink,
1798
1799    /// A cursor indicating that the operation will result in a copy
1800    /// corresponds to the CSS cursor value `copy`
1801    DragCopy,
1802
1803    /// A cursor indicating that the operation will result in a context menu
1804    /// corresponds to the CSS cursor value `context-menu`
1805    ContextualMenu,
1806
1807    /// Hide the cursor
1808    None,
1809}
1810
1811/// A clipboard item that should be copied to the clipboard
1812#[derive(Clone, Debug, Eq, PartialEq)]
1813pub struct ClipboardItem {
1814    /// The entries in this clipboard item.
1815    pub entries: Vec<ClipboardEntry>,
1816}
1817
1818/// Either a ClipboardString or a ClipboardImage
1819#[derive(Clone, Debug, Eq, PartialEq)]
1820pub enum ClipboardEntry {
1821    /// A string entry
1822    String(ClipboardString),
1823    /// An image entry
1824    Image(Image),
1825    /// A file entry
1826    ExternalPaths(crate::ExternalPaths),
1827}
1828
1829impl ClipboardItem {
1830    /// Create a new ClipboardItem::String with no associated metadata
1831    pub fn new_string(text: String) -> Self {
1832        Self {
1833            entries: vec![ClipboardEntry::String(ClipboardString::new(text))],
1834        }
1835    }
1836
1837    /// Create a new ClipboardItem::String with the given text and associated metadata
1838    pub fn new_string_with_metadata(text: String, metadata: String) -> Self {
1839        Self {
1840            entries: vec![ClipboardEntry::String(ClipboardString {
1841                text,
1842                metadata: Some(metadata),
1843            })],
1844        }
1845    }
1846
1847    /// Create a new ClipboardItem::String with the given text and associated metadata
1848    pub fn new_string_with_json_metadata<T: Serialize>(text: String, metadata: T) -> Self {
1849        Self {
1850            entries: vec![ClipboardEntry::String(
1851                ClipboardString::new(text).with_json_metadata(metadata),
1852            )],
1853        }
1854    }
1855
1856    /// Create a new ClipboardItem::Image with the given image with no associated metadata
1857    pub fn new_image(image: &Image) -> Self {
1858        Self {
1859            entries: vec![ClipboardEntry::Image(image.clone())],
1860        }
1861    }
1862
1863    /// Concatenates together all the ClipboardString entries in the item.
1864    /// Returns None if there were no ClipboardString entries.
1865    pub fn text(&self) -> Option<String> {
1866        let mut answer = String::new();
1867
1868        for entry in self.entries.iter() {
1869            if let ClipboardEntry::String(ClipboardString { text, metadata: _ }) = entry {
1870                answer.push_str(text);
1871            }
1872        }
1873
1874        if answer.is_empty() {
1875            for entry in self.entries.iter() {
1876                if let ClipboardEntry::ExternalPaths(paths) = entry {
1877                    for path in &paths.0 {
1878                        use std::fmt::Write as _;
1879                        _ = write!(answer, "{}", path.display());
1880                    }
1881                }
1882            }
1883        }
1884
1885        if !answer.is_empty() {
1886            Some(answer)
1887        } else {
1888            None
1889        }
1890    }
1891
1892    /// If this item is one ClipboardEntry::String, returns its metadata.
1893    #[cfg_attr(not(target_os = "windows"), allow(dead_code))]
1894    pub fn metadata(&self) -> Option<&String> {
1895        match self.entries().first() {
1896            Some(ClipboardEntry::String(clipboard_string)) if self.entries.len() == 1 => {
1897                clipboard_string.metadata.as_ref()
1898            }
1899            _ => None,
1900        }
1901    }
1902
1903    /// Get the item's entries
1904    pub fn entries(&self) -> &[ClipboardEntry] {
1905        &self.entries
1906    }
1907
1908    /// Get owned versions of the item's entries
1909    pub fn into_entries(self) -> impl Iterator<Item = ClipboardEntry> {
1910        self.entries.into_iter()
1911    }
1912}
1913
1914impl From<ClipboardString> for ClipboardEntry {
1915    fn from(value: ClipboardString) -> Self {
1916        Self::String(value)
1917    }
1918}
1919
1920impl From<String> for ClipboardEntry {
1921    fn from(value: String) -> Self {
1922        Self::from(ClipboardString::from(value))
1923    }
1924}
1925
1926impl From<Image> for ClipboardEntry {
1927    fn from(value: Image) -> Self {
1928        Self::Image(value)
1929    }
1930}
1931
1932impl From<ClipboardEntry> for ClipboardItem {
1933    fn from(value: ClipboardEntry) -> Self {
1934        Self {
1935            entries: vec![value],
1936        }
1937    }
1938}
1939
1940impl From<String> for ClipboardItem {
1941    fn from(value: String) -> Self {
1942        Self::from(ClipboardEntry::from(value))
1943    }
1944}
1945
1946impl From<Image> for ClipboardItem {
1947    fn from(value: Image) -> Self {
1948        Self::from(ClipboardEntry::from(value))
1949    }
1950}
1951
1952/// One of the editor's supported image formats (e.g. PNG, JPEG) - used when dealing with images in the clipboard
1953#[derive(Clone, Copy, Debug, Eq, PartialEq, EnumIter, Hash)]
1954pub enum ImageFormat {
1955    // Sorted from most to least likely to be pasted into an editor,
1956    // which matters when we iterate through them trying to see if
1957    // clipboard content matches them.
1958    /// .png
1959    Png,
1960    /// .jpeg or .jpg
1961    Jpeg,
1962    /// .webp
1963    Webp,
1964    /// .gif
1965    Gif,
1966    /// .svg
1967    Svg,
1968    /// .bmp
1969    Bmp,
1970    /// .tif or .tiff
1971    Tiff,
1972    /// .ico
1973    Ico,
1974}
1975
1976impl ImageFormat {
1977    /// Returns the mime type for the ImageFormat
1978    pub const fn mime_type(self) -> &'static str {
1979        match self {
1980            ImageFormat::Png => "image/png",
1981            ImageFormat::Jpeg => "image/jpeg",
1982            ImageFormat::Webp => "image/webp",
1983            ImageFormat::Gif => "image/gif",
1984            ImageFormat::Svg => "image/svg+xml",
1985            ImageFormat::Bmp => "image/bmp",
1986            ImageFormat::Tiff => "image/tiff",
1987            ImageFormat::Ico => "image/ico",
1988        }
1989    }
1990
1991    /// Returns the ImageFormat for the given mime type
1992    pub fn from_mime_type(mime_type: &str) -> Option<Self> {
1993        match mime_type {
1994            "image/png" => Some(Self::Png),
1995            "image/jpeg" | "image/jpg" => Some(Self::Jpeg),
1996            "image/webp" => Some(Self::Webp),
1997            "image/gif" => Some(Self::Gif),
1998            "image/svg+xml" => Some(Self::Svg),
1999            "image/bmp" => Some(Self::Bmp),
2000            "image/tiff" | "image/tif" => Some(Self::Tiff),
2001            "image/ico" => Some(Self::Ico),
2002            _ => None,
2003        }
2004    }
2005}
2006
2007/// An image, with a format and certain bytes
2008#[derive(Clone, Debug, PartialEq, Eq)]
2009pub struct Image {
2010    /// The image format the bytes represent (e.g. PNG)
2011    pub format: ImageFormat,
2012    /// The raw image bytes
2013    pub bytes: Vec<u8>,
2014    /// The unique ID for the image
2015    pub id: u64,
2016}
2017
2018impl Hash for Image {
2019    fn hash<H: Hasher>(&self, state: &mut H) {
2020        state.write_u64(self.id);
2021    }
2022}
2023
2024impl Image {
2025    /// An empty image containing no data
2026    pub fn empty() -> Self {
2027        Self::from_bytes(ImageFormat::Png, Vec::new())
2028    }
2029
2030    /// Create an image from a format and bytes
2031    pub fn from_bytes(format: ImageFormat, bytes: Vec<u8>) -> Self {
2032        Self {
2033            id: hash(&bytes),
2034            format,
2035            bytes,
2036        }
2037    }
2038
2039    /// Get this image's ID
2040    pub fn id(&self) -> u64 {
2041        self.id
2042    }
2043
2044    /// Use the GPUI `use_asset` API to make this image renderable
2045    pub fn use_render_image(
2046        self: Arc<Self>,
2047        window: &mut Window,
2048        cx: &mut App,
2049    ) -> Option<Arc<RenderImage>> {
2050        ImageSource::Image(self)
2051            .use_data(None, window, cx)
2052            .and_then(|result| result.ok())
2053    }
2054
2055    /// Use the GPUI `get_asset` API to make this image renderable
2056    pub fn get_render_image(
2057        self: Arc<Self>,
2058        window: &mut Window,
2059        cx: &mut App,
2060    ) -> Option<Arc<RenderImage>> {
2061        ImageSource::Image(self)
2062            .get_data(None, window, cx)
2063            .and_then(|result| result.ok())
2064    }
2065
2066    /// Use the GPUI `remove_asset` API to drop this image, if possible.
2067    pub fn remove_asset(self: Arc<Self>, cx: &mut App) {
2068        ImageSource::Image(self).remove_asset(cx);
2069    }
2070
2071    /// Convert the clipboard image to an `ImageData` object.
2072    pub fn to_image_data(&self, svg_renderer: SvgRenderer) -> Result<Arc<RenderImage>> {
2073        fn frames_for_image(
2074            bytes: &[u8],
2075            format: image::ImageFormat,
2076        ) -> Result<SmallVec<[Frame; 1]>> {
2077            let mut data = image::load_from_memory_with_format(bytes, format)?.into_rgba8();
2078
2079            // Convert from RGBA to BGRA.
2080            for pixel in data.chunks_exact_mut(4) {
2081                pixel.swap(0, 2);
2082            }
2083
2084            Ok(SmallVec::from_elem(Frame::new(data), 1))
2085        }
2086
2087        let frames = match self.format {
2088            ImageFormat::Gif => {
2089                let decoder = GifDecoder::new(Cursor::new(&self.bytes))?;
2090                let mut frames = SmallVec::new();
2091
2092                for frame in decoder.into_frames() {
2093                    let mut frame = frame?;
2094                    // Convert from RGBA to BGRA.
2095                    for pixel in frame.buffer_mut().chunks_exact_mut(4) {
2096                        pixel.swap(0, 2);
2097                    }
2098                    frames.push(frame);
2099                }
2100
2101                frames
2102            }
2103            ImageFormat::Png => frames_for_image(&self.bytes, image::ImageFormat::Png)?,
2104            ImageFormat::Jpeg => frames_for_image(&self.bytes, image::ImageFormat::Jpeg)?,
2105            ImageFormat::Webp => frames_for_image(&self.bytes, image::ImageFormat::WebP)?,
2106            ImageFormat::Bmp => frames_for_image(&self.bytes, image::ImageFormat::Bmp)?,
2107            ImageFormat::Tiff => frames_for_image(&self.bytes, image::ImageFormat::Tiff)?,
2108            ImageFormat::Ico => frames_for_image(&self.bytes, image::ImageFormat::Ico)?,
2109            ImageFormat::Svg => {
2110                return svg_renderer
2111                    .render_single_frame(&self.bytes, 1.0, false)
2112                    .map_err(Into::into);
2113            }
2114        };
2115
2116        Ok(Arc::new(RenderImage::new(frames)))
2117    }
2118
2119    /// Get the format of the clipboard image
2120    pub fn format(&self) -> ImageFormat {
2121        self.format
2122    }
2123
2124    /// Get the raw bytes of the clipboard image
2125    pub fn bytes(&self) -> &[u8] {
2126        self.bytes.as_slice()
2127    }
2128}
2129
2130/// A clipboard item that should be copied to the clipboard
2131#[derive(Clone, Debug, Eq, PartialEq)]
2132pub struct ClipboardString {
2133    /// The text content.
2134    pub text: String,
2135    /// Optional metadata associated with this clipboard string.
2136    pub metadata: Option<String>,
2137}
2138
2139impl ClipboardString {
2140    /// Create a new clipboard string with the given text
2141    pub fn new(text: String) -> Self {
2142        Self {
2143            text,
2144            metadata: None,
2145        }
2146    }
2147
2148    /// Return a new clipboard item with the metadata replaced by the given metadata,
2149    /// after serializing it as JSON.
2150    pub fn with_json_metadata<T: Serialize>(mut self, metadata: T) -> Self {
2151        self.metadata = Some(serde_json::to_string(&metadata).unwrap());
2152        self
2153    }
2154
2155    /// Get the text of the clipboard string
2156    pub fn text(&self) -> &String {
2157        &self.text
2158    }
2159
2160    /// Get the owned text of the clipboard string
2161    pub fn into_text(self) -> String {
2162        self.text
2163    }
2164
2165    /// Get the metadata of the clipboard string, formatted as JSON
2166    pub fn metadata_json<T>(&self) -> Option<T>
2167    where
2168        T: for<'a> Deserialize<'a>,
2169    {
2170        self.metadata
2171            .as_ref()
2172            .and_then(|m| serde_json::from_str(m).ok())
2173    }
2174
2175    #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
2176    /// Compute a hash of the given text for clipboard change detection.
2177    pub fn text_hash(text: &str) -> u64 {
2178        let mut hasher = SeaHasher::new();
2179        text.hash(&mut hasher);
2180        hasher.finish()
2181    }
2182}
2183
2184impl From<String> for ClipboardString {
2185    fn from(value: String) -> Self {
2186        Self {
2187            text: value,
2188            metadata: None,
2189        }
2190    }
2191}
2192
2193#[cfg(all(test, any(target_os = "linux", target_os = "freebsd")))]
2194mod tests {
2195    use super::*;
2196    use std::collections::HashSet;
2197
2198    #[test]
2199    fn test_window_button_layout_parse_standard() {
2200        let layout = WindowButtonLayout::parse("close,minimize:maximize").unwrap();
2201        assert_eq!(
2202            layout.left,
2203            [
2204                Some(WindowButton::Close),
2205                Some(WindowButton::Minimize),
2206                None
2207            ]
2208        );
2209        assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2210    }
2211
2212    #[test]
2213    fn test_window_button_layout_parse_right_only() {
2214        let layout = WindowButtonLayout::parse("minimize,maximize,close").unwrap();
2215        assert_eq!(layout.left, [None, None, None]);
2216        assert_eq!(
2217            layout.right,
2218            [
2219                Some(WindowButton::Minimize),
2220                Some(WindowButton::Maximize),
2221                Some(WindowButton::Close)
2222            ]
2223        );
2224    }
2225
2226    #[test]
2227    fn test_window_button_layout_parse_left_only() {
2228        let layout = WindowButtonLayout::parse("close,minimize,maximize:").unwrap();
2229        assert_eq!(
2230            layout.left,
2231            [
2232                Some(WindowButton::Close),
2233                Some(WindowButton::Minimize),
2234                Some(WindowButton::Maximize)
2235            ]
2236        );
2237        assert_eq!(layout.right, [None, None, None]);
2238    }
2239
2240    #[test]
2241    fn test_window_button_layout_parse_with_whitespace() {
2242        let layout = WindowButtonLayout::parse(" close , minimize : maximize ").unwrap();
2243        assert_eq!(
2244            layout.left,
2245            [
2246                Some(WindowButton::Close),
2247                Some(WindowButton::Minimize),
2248                None
2249            ]
2250        );
2251        assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2252    }
2253
2254    #[test]
2255    fn test_window_button_layout_parse_empty() {
2256        let layout = WindowButtonLayout::parse("").unwrap();
2257        assert_eq!(layout.left, [None, None, None]);
2258        assert_eq!(layout.right, [None, None, None]);
2259    }
2260
2261    #[test]
2262    fn test_window_button_layout_parse_intentionally_empty() {
2263        let layout = WindowButtonLayout::parse(":").unwrap();
2264        assert_eq!(layout.left, [None, None, None]);
2265        assert_eq!(layout.right, [None, None, None]);
2266    }
2267
2268    #[test]
2269    fn test_window_button_layout_parse_invalid_buttons() {
2270        let layout = WindowButtonLayout::parse("close,invalid,minimize:maximize,foo").unwrap();
2271        assert_eq!(
2272            layout.left,
2273            [
2274                Some(WindowButton::Close),
2275                Some(WindowButton::Minimize),
2276                None
2277            ]
2278        );
2279        assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2280    }
2281
2282    #[test]
2283    fn test_window_button_layout_parse_deduplicates_same_side_buttons() {
2284        let layout = WindowButtonLayout::parse("close,close,minimize").unwrap();
2285        assert_eq!(
2286            layout.right,
2287            [
2288                Some(WindowButton::Close),
2289                Some(WindowButton::Minimize),
2290                None
2291            ]
2292        );
2293        assert_eq!(layout.format(), ":close,minimize");
2294    }
2295
2296    #[test]
2297    fn test_window_button_layout_parse_deduplicates_buttons_across_sides() {
2298        let layout = WindowButtonLayout::parse("close:maximize,close,minimize").unwrap();
2299        assert_eq!(layout.left, [Some(WindowButton::Close), None, None]);
2300        assert_eq!(
2301            layout.right,
2302            [
2303                Some(WindowButton::Maximize),
2304                Some(WindowButton::Minimize),
2305                None
2306            ]
2307        );
2308
2309        let button_ids: Vec<_> = layout
2310            .left
2311            .iter()
2312            .chain(layout.right.iter())
2313            .flatten()
2314            .map(WindowButton::id)
2315            .collect();
2316        let unique_button_ids = button_ids.iter().copied().collect::<HashSet<_>>();
2317        assert_eq!(unique_button_ids.len(), button_ids.len());
2318        assert_eq!(layout.format(), "close:maximize,minimize");
2319    }
2320
2321    #[test]
2322    fn test_window_button_layout_parse_gnome_style() {
2323        let layout = WindowButtonLayout::parse("close").unwrap();
2324        assert_eq!(layout.left, [None, None, None]);
2325        assert_eq!(layout.right, [Some(WindowButton::Close), None, None]);
2326    }
2327
2328    #[test]
2329    fn test_window_button_layout_parse_elementary_style() {
2330        let layout = WindowButtonLayout::parse("close:maximize").unwrap();
2331        assert_eq!(layout.left, [Some(WindowButton::Close), None, None]);
2332        assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2333    }
2334
2335    #[test]
2336    fn test_window_button_layout_round_trip() {
2337        let cases = [
2338            "close:minimize,maximize",
2339            "minimize,maximize,close:",
2340            ":close",
2341            "close:",
2342            "close:maximize",
2343            ":",
2344        ];
2345
2346        for case in cases {
2347            let layout = WindowButtonLayout::parse(case).unwrap();
2348            assert_eq!(layout.format(), case, "Round-trip failed for: {}", case);
2349        }
2350    }
2351
2352    #[test]
2353    fn test_window_button_layout_linux_default() {
2354        let layout = WindowButtonLayout::linux_default();
2355        assert_eq!(layout.left, [None, None, None]);
2356        assert_eq!(
2357            layout.right,
2358            [
2359                Some(WindowButton::Minimize),
2360                Some(WindowButton::Maximize),
2361                Some(WindowButton::Close)
2362            ]
2363        );
2364
2365        let round_tripped = WindowButtonLayout::parse(&layout.format()).unwrap();
2366        assert_eq!(round_tripped, layout);
2367    }
2368
2369    #[test]
2370    fn test_window_button_layout_parse_all_invalid() {
2371        assert!(WindowButtonLayout::parse("asdfghjkl").is_err());
2372    }
2373}