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 fn play_system_bell(&self) {}
693
694 #[cfg(any(test, feature = "test-support"))]
695 fn as_test(&mut self) -> Option<&mut TestWindow> {
696 None
697 }
698
699 /// Renders the given scene to a texture and returns the pixel data as an RGBA image.
700 /// This does not present the frame to screen - useful for visual testing where we want
701 /// to capture what would be rendered without displaying it or requiring the window to be visible.
702 #[cfg(any(test, feature = "test-support"))]
703 fn render_to_image(&self, _scene: &Scene) -> Result<RgbaImage> {
704 anyhow::bail!("render_to_image not implemented for this platform")
705 }
706}
707
708/// A renderer for headless windows that can produce real rendered output.
709#[cfg(any(test, feature = "test-support"))]
710pub trait PlatformHeadlessRenderer {
711 /// Render a scene and return the result as an RGBA image.
712 fn render_scene_to_image(
713 &mut self,
714 scene: &Scene,
715 size: Size<DevicePixels>,
716 ) -> Result<RgbaImage>;
717
718 /// Returns the sprite atlas used by this renderer.
719 fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas>;
720}
721
722/// Type alias for runnables with metadata.
723/// Previously an enum with a single variant, now simplified to a direct type alias.
724#[doc(hidden)]
725pub type RunnableVariant = Runnable<RunnableMeta>;
726
727#[doc(hidden)]
728pub type TimerResolutionGuard = gpui_util::Deferred<Box<dyn FnOnce() + Send>>;
729
730/// This type is public so that our test macro can generate and use it, but it should not
731/// be considered part of our public API.
732#[doc(hidden)]
733pub trait PlatformDispatcher: Send + Sync {
734 fn get_all_timings(&self) -> Vec<ThreadTaskTimings>;
735 fn get_current_thread_timings(&self) -> ThreadTaskTimings;
736 fn is_main_thread(&self) -> bool;
737 fn dispatch(&self, runnable: RunnableVariant, priority: Priority);
738 fn dispatch_on_main_thread(&self, runnable: RunnableVariant, priority: Priority);
739 fn dispatch_after(&self, duration: Duration, runnable: RunnableVariant);
740
741 fn spawn_realtime(&self, f: Box<dyn FnOnce() + Send>);
742
743 fn now(&self) -> Instant {
744 Instant::now()
745 }
746
747 fn increase_timer_resolution(&self) -> TimerResolutionGuard {
748 gpui_util::defer(Box::new(|| {}))
749 }
750
751 #[cfg(any(test, feature = "test-support"))]
752 fn as_test(&self) -> Option<&TestDispatcher> {
753 None
754 }
755}
756
757#[expect(missing_docs)]
758pub trait PlatformTextSystem: Send + Sync {
759 fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()>;
760 /// Get all available font names.
761 fn all_font_names(&self) -> Vec<String>;
762 /// Get the font ID for a font descriptor.
763 fn font_id(&self, descriptor: &Font) -> Result<FontId>;
764 /// Get metrics for a font.
765 fn font_metrics(&self, font_id: FontId) -> FontMetrics;
766 /// Get typographic bounds for a glyph.
767 fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>>;
768 /// Get the advance width for a glyph.
769 fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>>;
770 /// Get the glyph ID for a character.
771 fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
772 /// Get raster bounds for a glyph.
773 fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>>;
774 /// Rasterize a glyph.
775 fn rasterize_glyph(
776 &self,
777 params: &RenderGlyphParams,
778 raster_bounds: Bounds<DevicePixels>,
779 ) -> Result<(Size<DevicePixels>, Vec<u8>)>;
780 /// Layout a line of text with the given font runs.
781 fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout;
782 /// Returns the recommended text rendering mode for the given font and size.
783 fn recommended_rendering_mode(&self, _font_id: FontId, _font_size: Pixels)
784 -> TextRenderingMode;
785}
786
787#[expect(missing_docs)]
788pub struct NoopTextSystem;
789
790#[expect(missing_docs)]
791impl NoopTextSystem {
792 #[allow(dead_code)]
793 pub fn new() -> Self {
794 Self
795 }
796}
797
798impl PlatformTextSystem for NoopTextSystem {
799 fn add_fonts(&self, _fonts: Vec<Cow<'static, [u8]>>) -> Result<()> {
800 Ok(())
801 }
802
803 fn all_font_names(&self) -> Vec<String> {
804 Vec::new()
805 }
806
807 fn font_id(&self, _descriptor: &Font) -> Result<FontId> {
808 Ok(FontId(1))
809 }
810
811 fn font_metrics(&self, _font_id: FontId) -> FontMetrics {
812 FontMetrics {
813 units_per_em: 1000,
814 ascent: 1025.0,
815 descent: -275.0,
816 line_gap: 0.0,
817 underline_position: -95.0,
818 underline_thickness: 60.0,
819 cap_height: 698.0,
820 x_height: 516.0,
821 bounding_box: Bounds {
822 origin: Point {
823 x: -260.0,
824 y: -245.0,
825 },
826 size: Size {
827 width: 1501.0,
828 height: 1364.0,
829 },
830 },
831 }
832 }
833
834 fn typographic_bounds(&self, _font_id: FontId, _glyph_id: GlyphId) -> Result<Bounds<f32>> {
835 Ok(Bounds {
836 origin: Point { x: 54.0, y: 0.0 },
837 size: size(392.0, 528.0),
838 })
839 }
840
841 fn advance(&self, _font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> {
842 Ok(size(600.0 * glyph_id.0 as f32, 0.0))
843 }
844
845 fn glyph_for_char(&self, _font_id: FontId, ch: char) -> Option<GlyphId> {
846 Some(GlyphId(ch.len_utf16() as u32))
847 }
848
849 fn glyph_raster_bounds(&self, _params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
850 Ok(Default::default())
851 }
852
853 fn rasterize_glyph(
854 &self,
855 _params: &RenderGlyphParams,
856 raster_bounds: Bounds<DevicePixels>,
857 ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
858 Ok((raster_bounds.size, Vec::new()))
859 }
860
861 fn layout_line(&self, text: &str, font_size: Pixels, _runs: &[FontRun]) -> LineLayout {
862 let mut position = px(0.);
863 let metrics = self.font_metrics(FontId(0));
864 let em_width = font_size
865 * self
866 .advance(FontId(0), self.glyph_for_char(FontId(0), 'm').unwrap())
867 .unwrap()
868 .width
869 / metrics.units_per_em as f32;
870 let mut glyphs = Vec::new();
871 for (ix, c) in text.char_indices() {
872 if let Some(glyph) = self.glyph_for_char(FontId(0), c) {
873 glyphs.push(ShapedGlyph {
874 id: glyph,
875 position: point(position, px(0.)),
876 index: ix,
877 is_emoji: glyph.0 == 2,
878 });
879 if glyph.0 == 2 {
880 position += em_width * 2.0;
881 } else {
882 position += em_width;
883 }
884 } else {
885 position += em_width
886 }
887 }
888 let mut runs = Vec::default();
889 if !glyphs.is_empty() {
890 runs.push(ShapedRun {
891 font_id: FontId(0),
892 glyphs,
893 });
894 } else {
895 position = px(0.);
896 }
897
898 LineLayout {
899 font_size,
900 width: position,
901 ascent: font_size * (metrics.ascent / metrics.units_per_em as f32),
902 descent: font_size * (metrics.descent / metrics.units_per_em as f32),
903 runs,
904 len: text.len(),
905 }
906 }
907
908 fn recommended_rendering_mode(
909 &self,
910 _font_id: FontId,
911 _font_size: Pixels,
912 ) -> TextRenderingMode {
913 TextRenderingMode::Grayscale
914 }
915}
916
917// Adapted from https://github.com/microsoft/terminal/blob/1283c0f5b99a2961673249fa77c6b986efb5086c/src/renderer/atlas/dwrite.cpp
918// Copyright (c) Microsoft Corporation.
919// Licensed under the MIT license.
920/// Compute gamma correction ratios for subpixel text rendering.
921#[allow(dead_code)]
922pub fn get_gamma_correction_ratios(gamma: f32) -> [f32; 4] {
923 const GAMMA_INCORRECT_TARGET_RATIOS: [[f32; 4]; 13] = [
924 [0.0000 / 4.0, 0.0000 / 4.0, 0.0000 / 4.0, 0.0000 / 4.0], // gamma = 1.0
925 [0.0166 / 4.0, -0.0807 / 4.0, 0.2227 / 4.0, -0.0751 / 4.0], // gamma = 1.1
926 [0.0350 / 4.0, -0.1760 / 4.0, 0.4325 / 4.0, -0.1370 / 4.0], // gamma = 1.2
927 [0.0543 / 4.0, -0.2821 / 4.0, 0.6302 / 4.0, -0.1876 / 4.0], // gamma = 1.3
928 [0.0739 / 4.0, -0.3963 / 4.0, 0.8167 / 4.0, -0.2287 / 4.0], // gamma = 1.4
929 [0.0933 / 4.0, -0.5161 / 4.0, 0.9926 / 4.0, -0.2616 / 4.0], // gamma = 1.5
930 [0.1121 / 4.0, -0.6395 / 4.0, 1.1588 / 4.0, -0.2877 / 4.0], // gamma = 1.6
931 [0.1300 / 4.0, -0.7649 / 4.0, 1.3159 / 4.0, -0.3080 / 4.0], // gamma = 1.7
932 [0.1469 / 4.0, -0.8911 / 4.0, 1.4644 / 4.0, -0.3234 / 4.0], // gamma = 1.8
933 [0.1627 / 4.0, -1.0170 / 4.0, 1.6051 / 4.0, -0.3347 / 4.0], // gamma = 1.9
934 [0.1773 / 4.0, -1.1420 / 4.0, 1.7385 / 4.0, -0.3426 / 4.0], // gamma = 2.0
935 [0.1908 / 4.0, -1.2652 / 4.0, 1.8650 / 4.0, -0.3476 / 4.0], // gamma = 2.1
936 [0.2031 / 4.0, -1.3864 / 4.0, 1.9851 / 4.0, -0.3501 / 4.0], // gamma = 2.2
937 ];
938
939 const NORM13: f32 = ((0x10000 as f64) / (255.0 * 255.0) * 4.0) as f32;
940 const NORM24: f32 = ((0x100 as f64) / (255.0) * 4.0) as f32;
941
942 let index = ((gamma * 10.0).round() as usize).clamp(10, 22) - 10;
943 let ratios = GAMMA_INCORRECT_TARGET_RATIOS[index];
944
945 [
946 ratios[0] * NORM13,
947 ratios[1] * NORM24,
948 ratios[2] * NORM13,
949 ratios[3] * NORM24,
950 ]
951}
952
953#[derive(PartialEq, Eq, Hash, Clone)]
954#[expect(missing_docs)]
955pub enum AtlasKey {
956 Glyph(RenderGlyphParams),
957 Svg(RenderSvgParams),
958 Image(RenderImageParams),
959}
960
961impl AtlasKey {
962 #[cfg_attr(
963 all(
964 any(target_os = "linux", target_os = "freebsd"),
965 not(any(feature = "x11", feature = "wayland"))
966 ),
967 allow(dead_code)
968 )]
969 /// Returns the texture kind for this atlas key.
970 pub fn texture_kind(&self) -> AtlasTextureKind {
971 match self {
972 AtlasKey::Glyph(params) => {
973 if params.is_emoji {
974 AtlasTextureKind::Polychrome
975 } else if params.subpixel_rendering {
976 AtlasTextureKind::Subpixel
977 } else {
978 AtlasTextureKind::Monochrome
979 }
980 }
981 AtlasKey::Svg(_) => AtlasTextureKind::Monochrome,
982 AtlasKey::Image(_) => AtlasTextureKind::Polychrome,
983 }
984 }
985}
986
987impl From<RenderGlyphParams> for AtlasKey {
988 fn from(params: RenderGlyphParams) -> Self {
989 Self::Glyph(params)
990 }
991}
992
993impl From<RenderSvgParams> for AtlasKey {
994 fn from(params: RenderSvgParams) -> Self {
995 Self::Svg(params)
996 }
997}
998
999impl From<RenderImageParams> for AtlasKey {
1000 fn from(params: RenderImageParams) -> Self {
1001 Self::Image(params)
1002 }
1003}
1004
1005#[expect(missing_docs)]
1006pub trait PlatformAtlas {
1007 fn get_or_insert_with<'a>(
1008 &self,
1009 key: &AtlasKey,
1010 build: &mut dyn FnMut() -> Result<Option<(Size<DevicePixels>, Cow<'a, [u8]>)>>,
1011 ) -> Result<Option<AtlasTile>>;
1012 fn remove(&self, key: &AtlasKey);
1013}
1014
1015#[doc(hidden)]
1016pub struct AtlasTextureList<T> {
1017 pub textures: Vec<Option<T>>,
1018 pub free_list: Vec<usize>,
1019}
1020
1021impl<T> Default for AtlasTextureList<T> {
1022 fn default() -> Self {
1023 Self {
1024 textures: Vec::default(),
1025 free_list: Vec::default(),
1026 }
1027 }
1028}
1029
1030impl<T> ops::Index<usize> for AtlasTextureList<T> {
1031 type Output = Option<T>;
1032
1033 fn index(&self, index: usize) -> &Self::Output {
1034 &self.textures[index]
1035 }
1036}
1037
1038impl<T> AtlasTextureList<T> {
1039 #[allow(unused)]
1040 pub fn drain(&mut self) -> std::vec::Drain<'_, Option<T>> {
1041 self.free_list.clear();
1042 self.textures.drain(..)
1043 }
1044
1045 #[allow(dead_code)]
1046 pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> {
1047 self.textures.iter_mut().flatten()
1048 }
1049}
1050
1051#[derive(Clone, Debug, PartialEq, Eq)]
1052#[repr(C)]
1053#[expect(missing_docs)]
1054pub struct AtlasTile {
1055 /// The texture this tile belongs to.
1056 pub texture_id: AtlasTextureId,
1057 /// The unique ID of this tile within its texture.
1058 pub tile_id: TileId,
1059 /// Padding around the tile content in pixels.
1060 pub padding: u32,
1061 /// The bounds of this tile within the texture.
1062 pub bounds: Bounds<DevicePixels>,
1063}
1064
1065#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1066#[repr(C)]
1067#[expect(missing_docs)]
1068pub struct AtlasTextureId {
1069 // We use u32 instead of usize for Metal Shader Language compatibility
1070 /// The index of this texture in the atlas.
1071 pub index: u32,
1072 /// The kind of content stored in this texture.
1073 pub kind: AtlasTextureKind,
1074}
1075
1076#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1077#[repr(C)]
1078#[cfg_attr(
1079 all(
1080 any(target_os = "linux", target_os = "freebsd"),
1081 not(any(feature = "x11", feature = "wayland"))
1082 ),
1083 allow(dead_code)
1084)]
1085#[expect(missing_docs)]
1086pub enum AtlasTextureKind {
1087 Monochrome = 0,
1088 Polychrome = 1,
1089 Subpixel = 2,
1090}
1091
1092#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
1093#[repr(C)]
1094#[expect(missing_docs)]
1095pub struct TileId(pub u32);
1096
1097impl From<etagere::AllocId> for TileId {
1098 fn from(id: etagere::AllocId) -> Self {
1099 Self(id.serialize())
1100 }
1101}
1102
1103impl From<TileId> for etagere::AllocId {
1104 fn from(id: TileId) -> Self {
1105 Self::deserialize(id.0)
1106 }
1107}
1108
1109#[expect(missing_docs)]
1110pub struct PlatformInputHandler {
1111 cx: AsyncWindowContext,
1112 handler: Box<dyn InputHandler>,
1113}
1114
1115#[expect(missing_docs)]
1116#[cfg_attr(
1117 all(
1118 any(target_os = "linux", target_os = "freebsd"),
1119 not(any(feature = "x11", feature = "wayland"))
1120 ),
1121 allow(dead_code)
1122)]
1123impl PlatformInputHandler {
1124 pub fn new(cx: AsyncWindowContext, handler: Box<dyn InputHandler>) -> Self {
1125 Self { cx, handler }
1126 }
1127
1128 pub fn selected_text_range(&mut self, ignore_disabled_input: bool) -> Option<UTF16Selection> {
1129 self.cx
1130 .update(|window, cx| {
1131 self.handler
1132 .selected_text_range(ignore_disabled_input, window, cx)
1133 })
1134 .ok()
1135 .flatten()
1136 }
1137
1138 #[cfg_attr(target_os = "windows", allow(dead_code))]
1139 pub fn marked_text_range(&mut self) -> Option<Range<usize>> {
1140 self.cx
1141 .update(|window, cx| self.handler.marked_text_range(window, cx))
1142 .ok()
1143 .flatten()
1144 }
1145
1146 #[cfg_attr(
1147 any(target_os = "linux", target_os = "freebsd", target_os = "windows"),
1148 allow(dead_code)
1149 )]
1150 pub fn text_for_range(
1151 &mut self,
1152 range_utf16: Range<usize>,
1153 adjusted: &mut Option<Range<usize>>,
1154 ) -> Option<String> {
1155 self.cx
1156 .update(|window, cx| {
1157 self.handler
1158 .text_for_range(range_utf16, adjusted, window, cx)
1159 })
1160 .ok()
1161 .flatten()
1162 }
1163
1164 pub fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, text: &str) {
1165 self.cx
1166 .update(|window, cx| {
1167 self.handler
1168 .replace_text_in_range(replacement_range, text, window, cx);
1169 })
1170 .ok();
1171 }
1172
1173 pub fn replace_and_mark_text_in_range(
1174 &mut self,
1175 range_utf16: Option<Range<usize>>,
1176 new_text: &str,
1177 new_selected_range: Option<Range<usize>>,
1178 ) {
1179 self.cx
1180 .update(|window, cx| {
1181 self.handler.replace_and_mark_text_in_range(
1182 range_utf16,
1183 new_text,
1184 new_selected_range,
1185 window,
1186 cx,
1187 )
1188 })
1189 .ok();
1190 }
1191
1192 #[cfg_attr(target_os = "windows", allow(dead_code))]
1193 pub fn unmark_text(&mut self) {
1194 self.cx
1195 .update(|window, cx| self.handler.unmark_text(window, cx))
1196 .ok();
1197 }
1198
1199 pub fn bounds_for_range(&mut self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>> {
1200 self.cx
1201 .update(|window, cx| self.handler.bounds_for_range(range_utf16, window, cx))
1202 .ok()
1203 .flatten()
1204 }
1205
1206 #[allow(dead_code)]
1207 pub fn apple_press_and_hold_enabled(&mut self) -> bool {
1208 self.handler.apple_press_and_hold_enabled()
1209 }
1210
1211 pub fn dispatch_input(&mut self, input: &str, window: &mut Window, cx: &mut App) {
1212 self.handler.replace_text_in_range(None, input, window, cx);
1213 }
1214
1215 pub fn selected_bounds(&mut self, window: &mut Window, cx: &mut App) -> Option<Bounds<Pixels>> {
1216 let selection = self.handler.selected_text_range(true, window, cx)?;
1217 self.handler.bounds_for_range(
1218 if selection.reversed {
1219 selection.range.start..selection.range.start
1220 } else {
1221 selection.range.end..selection.range.end
1222 },
1223 window,
1224 cx,
1225 )
1226 }
1227
1228 #[allow(unused)]
1229 pub fn character_index_for_point(&mut self, point: Point<Pixels>) -> Option<usize> {
1230 self.cx
1231 .update(|window, cx| self.handler.character_index_for_point(point, window, cx))
1232 .ok()
1233 .flatten()
1234 }
1235
1236 #[allow(dead_code)]
1237 pub fn accepts_text_input(&mut self, window: &mut Window, cx: &mut App) -> bool {
1238 self.handler.accepts_text_input(window, cx)
1239 }
1240
1241 #[allow(dead_code)]
1242 pub fn query_accepts_text_input(&mut self) -> bool {
1243 self.cx
1244 .update(|window, cx| self.handler.accepts_text_input(window, cx))
1245 .unwrap_or(true)
1246 }
1247
1248 #[allow(dead_code)]
1249 pub fn query_prefers_ime_for_printable_keys(&mut self) -> bool {
1250 self.cx
1251 .update(|window, cx| self.handler.prefers_ime_for_printable_keys(window, cx))
1252 .unwrap_or(false)
1253 }
1254}
1255
1256/// A struct representing a selection in a text buffer, in UTF16 characters.
1257/// This is different from a range because the head may be before the tail.
1258#[derive(Debug)]
1259pub struct UTF16Selection {
1260 /// The range of text in the document this selection corresponds to
1261 /// in UTF16 characters.
1262 pub range: Range<usize>,
1263 /// Whether the head of this selection is at the start (true), or end (false)
1264 /// of the range
1265 pub reversed: bool,
1266}
1267
1268/// Zed's interface for handling text input from the platform's IME system
1269/// This is currently a 1:1 exposure of the NSTextInputClient API:
1270///
1271/// <https://developer.apple.com/documentation/appkit/nstextinputclient>
1272pub trait InputHandler: 'static {
1273 /// Get the range of the user's currently selected text, if any
1274 /// Corresponds to [selectedRange()](https://developer.apple.com/documentation/appkit/nstextinputclient/1438242-selectedrange)
1275 ///
1276 /// Return value is in terms of UTF-16 characters, from 0 to the length of the document
1277 fn selected_text_range(
1278 &mut self,
1279 ignore_disabled_input: bool,
1280 window: &mut Window,
1281 cx: &mut App,
1282 ) -> Option<UTF16Selection>;
1283
1284 /// Get the range of the currently marked text, if any
1285 /// Corresponds to [markedRange()](https://developer.apple.com/documentation/appkit/nstextinputclient/1438250-markedrange)
1286 ///
1287 /// Return value is in terms of UTF-16 characters, from 0 to the length of the document
1288 fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>>;
1289
1290 /// Get the text for the given document range in UTF-16 characters
1291 /// Corresponds to [attributedSubstring(forProposedRange: actualRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438238-attributedsubstring)
1292 ///
1293 /// range_utf16 is in terms of UTF-16 characters
1294 fn text_for_range(
1295 &mut self,
1296 range_utf16: Range<usize>,
1297 adjusted_range: &mut Option<Range<usize>>,
1298 window: &mut Window,
1299 cx: &mut App,
1300 ) -> Option<String>;
1301
1302 /// Replace the text in the given document range with the given text
1303 /// Corresponds to [insertText(_:replacementRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438258-inserttext)
1304 ///
1305 /// replacement_range is in terms of UTF-16 characters
1306 fn replace_text_in_range(
1307 &mut self,
1308 replacement_range: Option<Range<usize>>,
1309 text: &str,
1310 window: &mut Window,
1311 cx: &mut App,
1312 );
1313
1314 /// Replace the text in the given document range with the given text,
1315 /// and mark the given text as part of an IME 'composing' state
1316 /// Corresponds to [setMarkedText(_:selectedRange:replacementRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438246-setmarkedtext)
1317 ///
1318 /// range_utf16 is in terms of UTF-16 characters
1319 /// new_selected_range is in terms of UTF-16 characters
1320 fn replace_and_mark_text_in_range(
1321 &mut self,
1322 range_utf16: Option<Range<usize>>,
1323 new_text: &str,
1324 new_selected_range: Option<Range<usize>>,
1325 window: &mut Window,
1326 cx: &mut App,
1327 );
1328
1329 /// Remove the IME 'composing' state from the document
1330 /// Corresponds to [unmarkText()](https://developer.apple.com/documentation/appkit/nstextinputclient/1438239-unmarktext)
1331 fn unmark_text(&mut self, window: &mut Window, cx: &mut App);
1332
1333 /// Get the bounds of the given document range in screen coordinates
1334 /// Corresponds to [firstRect(forCharacterRange:actualRange:)](https://developer.apple.com/documentation/appkit/nstextinputclient/1438240-firstrect)
1335 ///
1336 /// This is used for positioning the IME candidate window
1337 fn bounds_for_range(
1338 &mut self,
1339 range_utf16: Range<usize>,
1340 window: &mut Window,
1341 cx: &mut App,
1342 ) -> Option<Bounds<Pixels>>;
1343
1344 /// Get the character offset for the given point in terms of UTF16 characters
1345 ///
1346 /// Corresponds to [characterIndexForPoint:](https://developer.apple.com/documentation/appkit/nstextinputclient/characterindex(for:))
1347 fn character_index_for_point(
1348 &mut self,
1349 point: Point<Pixels>,
1350 window: &mut Window,
1351 cx: &mut App,
1352 ) -> Option<usize>;
1353
1354 /// Allows a given input context to opt into getting raw key repeats instead of
1355 /// sending these to the platform.
1356 /// TODO: Ideally we should be able to set ApplePressAndHoldEnabled in NSUserDefaults
1357 /// (which is how iTerm does it) but it doesn't seem to work for me.
1358 #[allow(dead_code)]
1359 fn apple_press_and_hold_enabled(&mut self) -> bool {
1360 true
1361 }
1362
1363 /// Returns whether this handler is accepting text input to be inserted.
1364 fn accepts_text_input(&mut self, _window: &mut Window, _cx: &mut App) -> bool {
1365 true
1366 }
1367
1368 /// Returns whether printable keys should be routed to the IME before keybinding
1369 /// matching when a non-ASCII input source (e.g. Japanese, Korean, Chinese IME)
1370 /// is active. This prevents multi-stroke keybindings like `jj` from intercepting
1371 /// keys that the IME should compose.
1372 ///
1373 /// Defaults to `false`. The editor overrides this based on whether it expects
1374 /// character input (e.g. Vim insert mode returns `true`, normal mode returns `false`).
1375 /// The terminal keeps the default `false` so that raw keys reach the terminal process.
1376 fn prefers_ime_for_printable_keys(&mut self, _window: &mut Window, _cx: &mut App) -> bool {
1377 false
1378 }
1379}
1380
1381/// The variables that can be configured when creating a new window
1382#[derive(Debug)]
1383pub struct WindowOptions {
1384 /// Specifies the state and bounds of the window in screen coordinates.
1385 /// - `None`: Inherit the bounds.
1386 /// - `Some(WindowBounds)`: Open a window with corresponding state and its restore size.
1387 pub window_bounds: Option<WindowBounds>,
1388
1389 /// The titlebar configuration of the window
1390 pub titlebar: Option<TitlebarOptions>,
1391
1392 /// Whether the window should be focused when created
1393 pub focus: bool,
1394
1395 /// Whether the window should be shown when created
1396 pub show: bool,
1397
1398 /// The kind of window to create
1399 pub kind: WindowKind,
1400
1401 /// Whether the window should be movable by the user
1402 pub is_movable: bool,
1403
1404 /// Whether the window should be resizable by the user
1405 pub is_resizable: bool,
1406
1407 /// Whether the window should be minimized by the user
1408 pub is_minimizable: bool,
1409
1410 /// The display to create the window on, if this is None,
1411 /// the window will be created on the main display
1412 pub display_id: Option<DisplayId>,
1413
1414 /// The appearance of the window background.
1415 pub window_background: WindowBackgroundAppearance,
1416
1417 /// Application identifier of the window. Can by used by desktop environments to group applications together.
1418 pub app_id: Option<String>,
1419
1420 /// Window minimum size
1421 pub window_min_size: Option<Size<Pixels>>,
1422
1423 /// Whether to use client or server side decorations. Wayland only
1424 /// Note that this may be ignored.
1425 pub window_decorations: Option<WindowDecorations>,
1426
1427 /// Icon image (X11 only)
1428 pub icon: Option<Arc<image::RgbaImage>>,
1429
1430 /// 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.
1431 pub tabbing_identifier: Option<String>,
1432}
1433
1434/// The variables that can be configured when creating a new window
1435#[derive(Debug)]
1436#[cfg_attr(
1437 all(
1438 any(target_os = "linux", target_os = "freebsd"),
1439 not(any(feature = "x11", feature = "wayland"))
1440 ),
1441 allow(dead_code)
1442)]
1443#[allow(missing_docs)]
1444pub struct WindowParams {
1445 pub bounds: Bounds<Pixels>,
1446
1447 /// The titlebar configuration of the window
1448 #[cfg_attr(feature = "wayland", allow(dead_code))]
1449 pub titlebar: Option<TitlebarOptions>,
1450
1451 /// The kind of window to create
1452 #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1453 pub kind: WindowKind,
1454
1455 /// Whether the window should be movable by the user
1456 #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1457 pub is_movable: bool,
1458
1459 /// Whether the window should be resizable by the user
1460 #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1461 pub is_resizable: bool,
1462
1463 /// Whether the window should be minimized by the user
1464 #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1465 pub is_minimizable: bool,
1466
1467 #[cfg_attr(
1468 any(target_os = "linux", target_os = "freebsd", target_os = "windows"),
1469 allow(dead_code)
1470 )]
1471 pub focus: bool,
1472
1473 #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
1474 pub show: bool,
1475
1476 /// An image to set as the window icon (x11 only)
1477 #[cfg_attr(feature = "wayland", allow(dead_code))]
1478 pub icon: Option<Arc<image::RgbaImage>>,
1479
1480 #[cfg_attr(feature = "wayland", allow(dead_code))]
1481 pub display_id: Option<DisplayId>,
1482
1483 pub window_min_size: Option<Size<Pixels>>,
1484 #[cfg(target_os = "macos")]
1485 pub tabbing_identifier: Option<String>,
1486}
1487
1488/// Represents the status of how a window should be opened.
1489#[derive(Debug, Copy, Clone, PartialEq)]
1490pub enum WindowBounds {
1491 /// Indicates that the window should open in a windowed state with the given bounds.
1492 Windowed(Bounds<Pixels>),
1493 /// Indicates that the window should open in a maximized state.
1494 /// The bounds provided here represent the restore size of the window.
1495 Maximized(Bounds<Pixels>),
1496 /// Indicates that the window should open in fullscreen mode.
1497 /// The bounds provided here represent the restore size of the window.
1498 Fullscreen(Bounds<Pixels>),
1499}
1500
1501impl Default for WindowBounds {
1502 fn default() -> Self {
1503 WindowBounds::Windowed(Bounds::default())
1504 }
1505}
1506
1507impl WindowBounds {
1508 /// Retrieve the inner bounds
1509 pub fn get_bounds(&self) -> Bounds<Pixels> {
1510 match self {
1511 WindowBounds::Windowed(bounds) => *bounds,
1512 WindowBounds::Maximized(bounds) => *bounds,
1513 WindowBounds::Fullscreen(bounds) => *bounds,
1514 }
1515 }
1516
1517 /// Creates a new window bounds that centers the window on the screen.
1518 pub fn centered(size: Size<Pixels>, cx: &App) -> Self {
1519 WindowBounds::Windowed(Bounds::centered(None, size, cx))
1520 }
1521}
1522
1523impl Default for WindowOptions {
1524 fn default() -> Self {
1525 Self {
1526 window_bounds: None,
1527 titlebar: Some(TitlebarOptions {
1528 title: Default::default(),
1529 appears_transparent: Default::default(),
1530 traffic_light_position: Default::default(),
1531 }),
1532 focus: true,
1533 show: true,
1534 kind: WindowKind::Normal,
1535 is_movable: true,
1536 is_resizable: true,
1537 is_minimizable: true,
1538 display_id: None,
1539 window_background: WindowBackgroundAppearance::default(),
1540 icon: None,
1541 app_id: None,
1542 window_min_size: None,
1543 window_decorations: None,
1544 tabbing_identifier: None,
1545 }
1546 }
1547}
1548
1549/// The options that can be configured for a window's titlebar
1550#[derive(Debug, Default)]
1551pub struct TitlebarOptions {
1552 /// The initial title of the window
1553 pub title: Option<SharedString>,
1554
1555 /// Should the default system titlebar be hidden to allow for a custom-drawn titlebar? (macOS and Windows only)
1556 /// Refer to [`WindowOptions::window_decorations`] on Linux
1557 pub appears_transparent: bool,
1558
1559 /// The position of the macOS traffic light buttons
1560 pub traffic_light_position: Option<Point<Pixels>>,
1561}
1562
1563/// The kind of window to create
1564#[derive(Clone, Debug, PartialEq, Eq)]
1565pub enum WindowKind {
1566 /// A normal application window
1567 Normal,
1568
1569 /// A window that appears above all other windows, usually used for alerts or popups
1570 /// use sparingly!
1571 PopUp,
1572
1573 /// A floating window that appears on top of its parent window
1574 Floating,
1575
1576 /// A Wayland LayerShell window, used to draw overlays or backgrounds for applications such as
1577 /// docks, notifications or wallpapers.
1578 #[cfg(all(target_os = "linux", feature = "wayland"))]
1579 LayerShell(layer_shell::LayerShellOptions),
1580
1581 /// A window that appears on top of its parent window and blocks interaction with it
1582 /// until the modal window is closed
1583 Dialog,
1584}
1585
1586/// The appearance of the window, as defined by the operating system.
1587///
1588/// On macOS, this corresponds to named [`NSAppearance`](https://developer.apple.com/documentation/appkit/nsappearance)
1589/// values.
1590#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
1591pub enum WindowAppearance {
1592 /// A light appearance.
1593 ///
1594 /// On macOS, this corresponds to the `aqua` appearance.
1595 #[default]
1596 Light,
1597
1598 /// A light appearance with vibrant colors.
1599 ///
1600 /// On macOS, this corresponds to the `NSAppearanceNameVibrantLight` appearance.
1601 VibrantLight,
1602
1603 /// A dark appearance.
1604 ///
1605 /// On macOS, this corresponds to the `darkAqua` appearance.
1606 Dark,
1607
1608 /// A dark appearance with vibrant colors.
1609 ///
1610 /// On macOS, this corresponds to the `NSAppearanceNameVibrantDark` appearance.
1611 VibrantDark,
1612}
1613
1614/// The appearance of the background of the window itself, when there is
1615/// no content or the content is transparent.
1616#[derive(Copy, Clone, Debug, Default, PartialEq)]
1617pub enum WindowBackgroundAppearance {
1618 /// Opaque.
1619 ///
1620 /// This lets the window manager know that content behind this
1621 /// window does not need to be drawn.
1622 ///
1623 /// Actual color depends on the system and themes should define a fully
1624 /// opaque background color instead.
1625 #[default]
1626 Opaque,
1627 /// Plain alpha transparency.
1628 Transparent,
1629 /// Transparency, but the contents behind the window are blurred.
1630 ///
1631 /// Not always supported.
1632 Blurred,
1633 /// The Mica backdrop material, supported on Windows 11.
1634 MicaBackdrop,
1635 /// The Mica Alt backdrop material, supported on Windows 11.
1636 MicaAltBackdrop,
1637}
1638
1639/// The text rendering mode to use for drawing glyphs.
1640#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
1641pub enum TextRenderingMode {
1642 /// Use the platform's default text rendering mode.
1643 #[default]
1644 PlatformDefault,
1645 /// Use subpixel (ClearType-style) text rendering.
1646 Subpixel,
1647 /// Use grayscale text rendering.
1648 Grayscale,
1649}
1650
1651/// The options that can be configured for a file dialog prompt
1652#[derive(Clone, Debug)]
1653pub struct PathPromptOptions {
1654 /// Should the prompt allow files to be selected?
1655 pub files: bool,
1656 /// Should the prompt allow directories to be selected?
1657 pub directories: bool,
1658 /// Should the prompt allow multiple files to be selected?
1659 pub multiple: bool,
1660 /// The prompt to show to a user when selecting a path
1661 pub prompt: Option<SharedString>,
1662}
1663
1664/// What kind of prompt styling to show
1665#[derive(Copy, Clone, Debug, PartialEq)]
1666pub enum PromptLevel {
1667 /// A prompt that is shown when the user should be notified of something
1668 Info,
1669
1670 /// A prompt that is shown when the user needs to be warned of a potential problem
1671 Warning,
1672
1673 /// A prompt that is shown when a critical problem has occurred
1674 Critical,
1675}
1676
1677/// Prompt Button
1678#[derive(Clone, Debug, PartialEq)]
1679pub enum PromptButton {
1680 /// Ok button
1681 Ok(SharedString),
1682 /// Cancel button
1683 Cancel(SharedString),
1684 /// Other button
1685 Other(SharedString),
1686}
1687
1688impl PromptButton {
1689 /// Create a button with label
1690 pub fn new(label: impl Into<SharedString>) -> Self {
1691 PromptButton::Other(label.into())
1692 }
1693
1694 /// Create an Ok button
1695 pub fn ok(label: impl Into<SharedString>) -> Self {
1696 PromptButton::Ok(label.into())
1697 }
1698
1699 /// Create a Cancel button
1700 pub fn cancel(label: impl Into<SharedString>) -> Self {
1701 PromptButton::Cancel(label.into())
1702 }
1703
1704 /// Returns true if this button is a cancel button.
1705 #[allow(dead_code)]
1706 pub fn is_cancel(&self) -> bool {
1707 matches!(self, PromptButton::Cancel(_))
1708 }
1709
1710 /// Returns the label of the button
1711 pub fn label(&self) -> &SharedString {
1712 match self {
1713 PromptButton::Ok(label) => label,
1714 PromptButton::Cancel(label) => label,
1715 PromptButton::Other(label) => label,
1716 }
1717 }
1718}
1719
1720impl From<&str> for PromptButton {
1721 fn from(value: &str) -> Self {
1722 match value.to_lowercase().as_str() {
1723 "ok" => PromptButton::Ok("Ok".into()),
1724 "cancel" => PromptButton::Cancel("Cancel".into()),
1725 _ => PromptButton::Other(SharedString::from(value.to_owned())),
1726 }
1727 }
1728}
1729
1730/// The style of the cursor (pointer)
1731#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
1732pub enum CursorStyle {
1733 /// The default cursor
1734 #[default]
1735 Arrow,
1736
1737 /// A text input cursor
1738 /// corresponds to the CSS cursor value `text`
1739 IBeam,
1740
1741 /// A crosshair cursor
1742 /// corresponds to the CSS cursor value `crosshair`
1743 Crosshair,
1744
1745 /// A closed hand cursor
1746 /// corresponds to the CSS cursor value `grabbing`
1747 ClosedHand,
1748
1749 /// An open hand cursor
1750 /// corresponds to the CSS cursor value `grab`
1751 OpenHand,
1752
1753 /// A pointing hand cursor
1754 /// corresponds to the CSS cursor value `pointer`
1755 PointingHand,
1756
1757 /// A resize left cursor
1758 /// corresponds to the CSS cursor value `w-resize`
1759 ResizeLeft,
1760
1761 /// A resize right cursor
1762 /// corresponds to the CSS cursor value `e-resize`
1763 ResizeRight,
1764
1765 /// A resize cursor to the left and right
1766 /// corresponds to the CSS cursor value `ew-resize`
1767 ResizeLeftRight,
1768
1769 /// A resize up cursor
1770 /// corresponds to the CSS cursor value `n-resize`
1771 ResizeUp,
1772
1773 /// A resize down cursor
1774 /// corresponds to the CSS cursor value `s-resize`
1775 ResizeDown,
1776
1777 /// A resize cursor directing up and down
1778 /// corresponds to the CSS cursor value `ns-resize`
1779 ResizeUpDown,
1780
1781 /// A resize cursor directing up-left and down-right
1782 /// corresponds to the CSS cursor value `nesw-resize`
1783 ResizeUpLeftDownRight,
1784
1785 /// A resize cursor directing up-right and down-left
1786 /// corresponds to the CSS cursor value `nwse-resize`
1787 ResizeUpRightDownLeft,
1788
1789 /// A cursor indicating that the item/column can be resized horizontally.
1790 /// corresponds to the CSS cursor value `col-resize`
1791 ResizeColumn,
1792
1793 /// A cursor indicating that the item/row can be resized vertically.
1794 /// corresponds to the CSS cursor value `row-resize`
1795 ResizeRow,
1796
1797 /// A text input cursor for vertical layout
1798 /// corresponds to the CSS cursor value `vertical-text`
1799 IBeamCursorForVerticalLayout,
1800
1801 /// A cursor indicating that the operation is not allowed
1802 /// corresponds to the CSS cursor value `not-allowed`
1803 OperationNotAllowed,
1804
1805 /// A cursor indicating that the operation will result in a link
1806 /// corresponds to the CSS cursor value `alias`
1807 DragLink,
1808
1809 /// A cursor indicating that the operation will result in a copy
1810 /// corresponds to the CSS cursor value `copy`
1811 DragCopy,
1812
1813 /// A cursor indicating that the operation will result in a context menu
1814 /// corresponds to the CSS cursor value `context-menu`
1815 ContextualMenu,
1816
1817 /// Hide the cursor
1818 None,
1819}
1820
1821/// A clipboard item that should be copied to the clipboard
1822#[derive(Clone, Debug, Eq, PartialEq)]
1823pub struct ClipboardItem {
1824 /// The entries in this clipboard item.
1825 pub entries: Vec<ClipboardEntry>,
1826}
1827
1828/// Either a ClipboardString or a ClipboardImage
1829#[derive(Clone, Debug, Eq, PartialEq)]
1830pub enum ClipboardEntry {
1831 /// A string entry
1832 String(ClipboardString),
1833 /// An image entry
1834 Image(Image),
1835 /// A file entry
1836 ExternalPaths(crate::ExternalPaths),
1837}
1838
1839impl ClipboardItem {
1840 /// Create a new ClipboardItem::String with no associated metadata
1841 pub fn new_string(text: String) -> Self {
1842 Self {
1843 entries: vec![ClipboardEntry::String(ClipboardString::new(text))],
1844 }
1845 }
1846
1847 /// Create a new ClipboardItem::String with the given text and associated metadata
1848 pub fn new_string_with_metadata(text: String, metadata: String) -> Self {
1849 Self {
1850 entries: vec![ClipboardEntry::String(ClipboardString {
1851 text,
1852 metadata: Some(metadata),
1853 })],
1854 }
1855 }
1856
1857 /// Create a new ClipboardItem::String with the given text and associated metadata
1858 pub fn new_string_with_json_metadata<T: Serialize>(text: String, metadata: T) -> Self {
1859 Self {
1860 entries: vec![ClipboardEntry::String(
1861 ClipboardString::new(text).with_json_metadata(metadata),
1862 )],
1863 }
1864 }
1865
1866 /// Create a new ClipboardItem::Image with the given image with no associated metadata
1867 pub fn new_image(image: &Image) -> Self {
1868 Self {
1869 entries: vec![ClipboardEntry::Image(image.clone())],
1870 }
1871 }
1872
1873 /// Concatenates together all the ClipboardString entries in the item.
1874 /// Returns None if there were no ClipboardString entries.
1875 pub fn text(&self) -> Option<String> {
1876 let mut answer = String::new();
1877
1878 for entry in self.entries.iter() {
1879 if let ClipboardEntry::String(ClipboardString { text, metadata: _ }) = entry {
1880 answer.push_str(text);
1881 }
1882 }
1883
1884 if answer.is_empty() {
1885 for entry in self.entries.iter() {
1886 if let ClipboardEntry::ExternalPaths(paths) = entry {
1887 for path in &paths.0 {
1888 use std::fmt::Write as _;
1889 _ = write!(answer, "{}", path.display());
1890 }
1891 }
1892 }
1893 }
1894
1895 if !answer.is_empty() {
1896 Some(answer)
1897 } else {
1898 None
1899 }
1900 }
1901
1902 /// If this item is one ClipboardEntry::String, returns its metadata.
1903 #[cfg_attr(not(target_os = "windows"), allow(dead_code))]
1904 pub fn metadata(&self) -> Option<&String> {
1905 match self.entries().first() {
1906 Some(ClipboardEntry::String(clipboard_string)) if self.entries.len() == 1 => {
1907 clipboard_string.metadata.as_ref()
1908 }
1909 _ => None,
1910 }
1911 }
1912
1913 /// Get the item's entries
1914 pub fn entries(&self) -> &[ClipboardEntry] {
1915 &self.entries
1916 }
1917
1918 /// Get owned versions of the item's entries
1919 pub fn into_entries(self) -> impl Iterator<Item = ClipboardEntry> {
1920 self.entries.into_iter()
1921 }
1922}
1923
1924impl From<ClipboardString> for ClipboardEntry {
1925 fn from(value: ClipboardString) -> Self {
1926 Self::String(value)
1927 }
1928}
1929
1930impl From<String> for ClipboardEntry {
1931 fn from(value: String) -> Self {
1932 Self::from(ClipboardString::from(value))
1933 }
1934}
1935
1936impl From<Image> for ClipboardEntry {
1937 fn from(value: Image) -> Self {
1938 Self::Image(value)
1939 }
1940}
1941
1942impl From<ClipboardEntry> for ClipboardItem {
1943 fn from(value: ClipboardEntry) -> Self {
1944 Self {
1945 entries: vec![value],
1946 }
1947 }
1948}
1949
1950impl From<String> for ClipboardItem {
1951 fn from(value: String) -> Self {
1952 Self::from(ClipboardEntry::from(value))
1953 }
1954}
1955
1956impl From<Image> for ClipboardItem {
1957 fn from(value: Image) -> Self {
1958 Self::from(ClipboardEntry::from(value))
1959 }
1960}
1961
1962/// One of the editor's supported image formats (e.g. PNG, JPEG) - used when dealing with images in the clipboard
1963#[derive(Clone, Copy, Debug, Eq, PartialEq, EnumIter, Hash)]
1964pub enum ImageFormat {
1965 // Sorted from most to least likely to be pasted into an editor,
1966 // which matters when we iterate through them trying to see if
1967 // clipboard content matches them.
1968 /// .png
1969 Png,
1970 /// .jpeg or .jpg
1971 Jpeg,
1972 /// .webp
1973 Webp,
1974 /// .gif
1975 Gif,
1976 /// .svg
1977 Svg,
1978 /// .bmp
1979 Bmp,
1980 /// .tif or .tiff
1981 Tiff,
1982 /// .ico
1983 Ico,
1984}
1985
1986impl ImageFormat {
1987 /// Returns the mime type for the ImageFormat
1988 pub const fn mime_type(self) -> &'static str {
1989 match self {
1990 ImageFormat::Png => "image/png",
1991 ImageFormat::Jpeg => "image/jpeg",
1992 ImageFormat::Webp => "image/webp",
1993 ImageFormat::Gif => "image/gif",
1994 ImageFormat::Svg => "image/svg+xml",
1995 ImageFormat::Bmp => "image/bmp",
1996 ImageFormat::Tiff => "image/tiff",
1997 ImageFormat::Ico => "image/ico",
1998 }
1999 }
2000
2001 /// Returns the ImageFormat for the given mime type
2002 pub fn from_mime_type(mime_type: &str) -> Option<Self> {
2003 match mime_type {
2004 "image/png" => Some(Self::Png),
2005 "image/jpeg" | "image/jpg" => Some(Self::Jpeg),
2006 "image/webp" => Some(Self::Webp),
2007 "image/gif" => Some(Self::Gif),
2008 "image/svg+xml" => Some(Self::Svg),
2009 "image/bmp" => Some(Self::Bmp),
2010 "image/tiff" | "image/tif" => Some(Self::Tiff),
2011 "image/ico" => Some(Self::Ico),
2012 _ => None,
2013 }
2014 }
2015}
2016
2017/// An image, with a format and certain bytes
2018#[derive(Clone, Debug, PartialEq, Eq)]
2019pub struct Image {
2020 /// The image format the bytes represent (e.g. PNG)
2021 pub format: ImageFormat,
2022 /// The raw image bytes
2023 pub bytes: Vec<u8>,
2024 /// The unique ID for the image
2025 pub id: u64,
2026}
2027
2028impl Hash for Image {
2029 fn hash<H: Hasher>(&self, state: &mut H) {
2030 state.write_u64(self.id);
2031 }
2032}
2033
2034impl Image {
2035 /// An empty image containing no data
2036 pub fn empty() -> Self {
2037 Self::from_bytes(ImageFormat::Png, Vec::new())
2038 }
2039
2040 /// Create an image from a format and bytes
2041 pub fn from_bytes(format: ImageFormat, bytes: Vec<u8>) -> Self {
2042 Self {
2043 id: hash(&bytes),
2044 format,
2045 bytes,
2046 }
2047 }
2048
2049 /// Get this image's ID
2050 pub fn id(&self) -> u64 {
2051 self.id
2052 }
2053
2054 /// Use the GPUI `use_asset` API to make this image renderable
2055 pub fn use_render_image(
2056 self: Arc<Self>,
2057 window: &mut Window,
2058 cx: &mut App,
2059 ) -> Option<Arc<RenderImage>> {
2060 ImageSource::Image(self)
2061 .use_data(None, window, cx)
2062 .and_then(|result| result.ok())
2063 }
2064
2065 /// Use the GPUI `get_asset` API to make this image renderable
2066 pub fn get_render_image(
2067 self: Arc<Self>,
2068 window: &mut Window,
2069 cx: &mut App,
2070 ) -> Option<Arc<RenderImage>> {
2071 ImageSource::Image(self)
2072 .get_data(None, window, cx)
2073 .and_then(|result| result.ok())
2074 }
2075
2076 /// Use the GPUI `remove_asset` API to drop this image, if possible.
2077 pub fn remove_asset(self: Arc<Self>, cx: &mut App) {
2078 ImageSource::Image(self).remove_asset(cx);
2079 }
2080
2081 /// Convert the clipboard image to an `ImageData` object.
2082 pub fn to_image_data(&self, svg_renderer: SvgRenderer) -> Result<Arc<RenderImage>> {
2083 fn frames_for_image(
2084 bytes: &[u8],
2085 format: image::ImageFormat,
2086 ) -> Result<SmallVec<[Frame; 1]>> {
2087 let mut data = image::load_from_memory_with_format(bytes, format)?.into_rgba8();
2088
2089 // Convert from RGBA to BGRA.
2090 for pixel in data.chunks_exact_mut(4) {
2091 pixel.swap(0, 2);
2092 }
2093
2094 Ok(SmallVec::from_elem(Frame::new(data), 1))
2095 }
2096
2097 let frames = match self.format {
2098 ImageFormat::Gif => {
2099 let decoder = GifDecoder::new(Cursor::new(&self.bytes))?;
2100 let mut frames = SmallVec::new();
2101
2102 for frame in decoder.into_frames() {
2103 let mut frame = frame?;
2104 // Convert from RGBA to BGRA.
2105 for pixel in frame.buffer_mut().chunks_exact_mut(4) {
2106 pixel.swap(0, 2);
2107 }
2108 frames.push(frame);
2109 }
2110
2111 frames
2112 }
2113 ImageFormat::Png => frames_for_image(&self.bytes, image::ImageFormat::Png)?,
2114 ImageFormat::Jpeg => frames_for_image(&self.bytes, image::ImageFormat::Jpeg)?,
2115 ImageFormat::Webp => frames_for_image(&self.bytes, image::ImageFormat::WebP)?,
2116 ImageFormat::Bmp => frames_for_image(&self.bytes, image::ImageFormat::Bmp)?,
2117 ImageFormat::Tiff => frames_for_image(&self.bytes, image::ImageFormat::Tiff)?,
2118 ImageFormat::Ico => frames_for_image(&self.bytes, image::ImageFormat::Ico)?,
2119 ImageFormat::Svg => {
2120 return svg_renderer
2121 .render_single_frame(&self.bytes, 1.0)
2122 .map_err(Into::into);
2123 }
2124 };
2125
2126 Ok(Arc::new(RenderImage::new(frames)))
2127 }
2128
2129 /// Get the format of the clipboard image
2130 pub fn format(&self) -> ImageFormat {
2131 self.format
2132 }
2133
2134 /// Get the raw bytes of the clipboard image
2135 pub fn bytes(&self) -> &[u8] {
2136 self.bytes.as_slice()
2137 }
2138}
2139
2140/// A clipboard item that should be copied to the clipboard
2141#[derive(Clone, Debug, Eq, PartialEq)]
2142pub struct ClipboardString {
2143 /// The text content.
2144 pub text: String,
2145 /// Optional metadata associated with this clipboard string.
2146 pub metadata: Option<String>,
2147}
2148
2149impl ClipboardString {
2150 /// Create a new clipboard string with the given text
2151 pub fn new(text: String) -> Self {
2152 Self {
2153 text,
2154 metadata: None,
2155 }
2156 }
2157
2158 /// Return a new clipboard item with the metadata replaced by the given metadata,
2159 /// after serializing it as JSON.
2160 pub fn with_json_metadata<T: Serialize>(mut self, metadata: T) -> Self {
2161 self.metadata = Some(serde_json::to_string(&metadata).unwrap());
2162 self
2163 }
2164
2165 /// Get the text of the clipboard string
2166 pub fn text(&self) -> &String {
2167 &self.text
2168 }
2169
2170 /// Get the owned text of the clipboard string
2171 pub fn into_text(self) -> String {
2172 self.text
2173 }
2174
2175 /// Get the metadata of the clipboard string, formatted as JSON
2176 pub fn metadata_json<T>(&self) -> Option<T>
2177 where
2178 T: for<'a> Deserialize<'a>,
2179 {
2180 self.metadata
2181 .as_ref()
2182 .and_then(|m| serde_json::from_str(m).ok())
2183 }
2184
2185 #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))]
2186 /// Compute a hash of the given text for clipboard change detection.
2187 pub fn text_hash(text: &str) -> u64 {
2188 let mut hasher = SeaHasher::new();
2189 text.hash(&mut hasher);
2190 hasher.finish()
2191 }
2192}
2193
2194impl From<String> for ClipboardString {
2195 fn from(value: String) -> Self {
2196 Self {
2197 text: value,
2198 metadata: None,
2199 }
2200 }
2201}
2202
2203#[cfg(test)]
2204mod image_tests {
2205 use super::*;
2206 use std::sync::Arc;
2207
2208 #[test]
2209 fn test_svg_image_to_image_data_converts_to_bgra() {
2210 let image = Image::from_bytes(
2211 ImageFormat::Svg,
2212 br##"<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1">
2213<rect width="1" height="1" fill="#38BDF8"/>
2214</svg>"##
2215 .to_vec(),
2216 );
2217
2218 let render_image = image.to_image_data(SvgRenderer::new(Arc::new(()))).unwrap();
2219 let bytes = render_image.as_bytes(0).unwrap();
2220
2221 for pixel in bytes.chunks_exact(4) {
2222 assert_eq!(pixel, &[0xF8, 0xBD, 0x38, 0xFF]);
2223 }
2224 }
2225}
2226
2227#[cfg(all(test, any(target_os = "linux", target_os = "freebsd")))]
2228mod tests {
2229 use super::*;
2230 use std::collections::HashSet;
2231
2232 #[test]
2233 fn test_window_button_layout_parse_standard() {
2234 let layout = WindowButtonLayout::parse("close,minimize:maximize").unwrap();
2235 assert_eq!(
2236 layout.left,
2237 [
2238 Some(WindowButton::Close),
2239 Some(WindowButton::Minimize),
2240 None
2241 ]
2242 );
2243 assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2244 }
2245
2246 #[test]
2247 fn test_window_button_layout_parse_right_only() {
2248 let layout = WindowButtonLayout::parse("minimize,maximize,close").unwrap();
2249 assert_eq!(layout.left, [None, None, None]);
2250 assert_eq!(
2251 layout.right,
2252 [
2253 Some(WindowButton::Minimize),
2254 Some(WindowButton::Maximize),
2255 Some(WindowButton::Close)
2256 ]
2257 );
2258 }
2259
2260 #[test]
2261 fn test_window_button_layout_parse_left_only() {
2262 let layout = WindowButtonLayout::parse("close,minimize,maximize:").unwrap();
2263 assert_eq!(
2264 layout.left,
2265 [
2266 Some(WindowButton::Close),
2267 Some(WindowButton::Minimize),
2268 Some(WindowButton::Maximize)
2269 ]
2270 );
2271 assert_eq!(layout.right, [None, None, None]);
2272 }
2273
2274 #[test]
2275 fn test_window_button_layout_parse_with_whitespace() {
2276 let layout = WindowButtonLayout::parse(" close , minimize : maximize ").unwrap();
2277 assert_eq!(
2278 layout.left,
2279 [
2280 Some(WindowButton::Close),
2281 Some(WindowButton::Minimize),
2282 None
2283 ]
2284 );
2285 assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2286 }
2287
2288 #[test]
2289 fn test_window_button_layout_parse_empty() {
2290 let layout = WindowButtonLayout::parse("").unwrap();
2291 assert_eq!(layout.left, [None, None, None]);
2292 assert_eq!(layout.right, [None, None, None]);
2293 }
2294
2295 #[test]
2296 fn test_window_button_layout_parse_intentionally_empty() {
2297 let layout = WindowButtonLayout::parse(":").unwrap();
2298 assert_eq!(layout.left, [None, None, None]);
2299 assert_eq!(layout.right, [None, None, None]);
2300 }
2301
2302 #[test]
2303 fn test_window_button_layout_parse_invalid_buttons() {
2304 let layout = WindowButtonLayout::parse("close,invalid,minimize:maximize,foo").unwrap();
2305 assert_eq!(
2306 layout.left,
2307 [
2308 Some(WindowButton::Close),
2309 Some(WindowButton::Minimize),
2310 None
2311 ]
2312 );
2313 assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2314 }
2315
2316 #[test]
2317 fn test_window_button_layout_parse_deduplicates_same_side_buttons() {
2318 let layout = WindowButtonLayout::parse("close,close,minimize").unwrap();
2319 assert_eq!(
2320 layout.right,
2321 [
2322 Some(WindowButton::Close),
2323 Some(WindowButton::Minimize),
2324 None
2325 ]
2326 );
2327 assert_eq!(layout.format(), ":close,minimize");
2328 }
2329
2330 #[test]
2331 fn test_window_button_layout_parse_deduplicates_buttons_across_sides() {
2332 let layout = WindowButtonLayout::parse("close:maximize,close,minimize").unwrap();
2333 assert_eq!(layout.left, [Some(WindowButton::Close), None, None]);
2334 assert_eq!(
2335 layout.right,
2336 [
2337 Some(WindowButton::Maximize),
2338 Some(WindowButton::Minimize),
2339 None
2340 ]
2341 );
2342
2343 let button_ids: Vec<_> = layout
2344 .left
2345 .iter()
2346 .chain(layout.right.iter())
2347 .flatten()
2348 .map(WindowButton::id)
2349 .collect();
2350 let unique_button_ids = button_ids.iter().copied().collect::<HashSet<_>>();
2351 assert_eq!(unique_button_ids.len(), button_ids.len());
2352 assert_eq!(layout.format(), "close:maximize,minimize");
2353 }
2354
2355 #[test]
2356 fn test_window_button_layout_parse_gnome_style() {
2357 let layout = WindowButtonLayout::parse("close").unwrap();
2358 assert_eq!(layout.left, [None, None, None]);
2359 assert_eq!(layout.right, [Some(WindowButton::Close), None, None]);
2360 }
2361
2362 #[test]
2363 fn test_window_button_layout_parse_elementary_style() {
2364 let layout = WindowButtonLayout::parse("close:maximize").unwrap();
2365 assert_eq!(layout.left, [Some(WindowButton::Close), None, None]);
2366 assert_eq!(layout.right, [Some(WindowButton::Maximize), None, None]);
2367 }
2368
2369 #[test]
2370 fn test_window_button_layout_round_trip() {
2371 let cases = [
2372 "close:minimize,maximize",
2373 "minimize,maximize,close:",
2374 ":close",
2375 "close:",
2376 "close:maximize",
2377 ":",
2378 ];
2379
2380 for case in cases {
2381 let layout = WindowButtonLayout::parse(case).unwrap();
2382 assert_eq!(layout.format(), case, "Round-trip failed for: {}", case);
2383 }
2384 }
2385
2386 #[test]
2387 fn test_window_button_layout_linux_default() {
2388 let layout = WindowButtonLayout::linux_default();
2389 assert_eq!(layout.left, [None, None, None]);
2390 assert_eq!(
2391 layout.right,
2392 [
2393 Some(WindowButton::Minimize),
2394 Some(WindowButton::Maximize),
2395 Some(WindowButton::Close)
2396 ]
2397 );
2398
2399 let round_tripped = WindowButtonLayout::parse(&layout.format()).unwrap();
2400 assert_eq!(round_tripped, layout);
2401 }
2402
2403 #[test]
2404 fn test_window_button_layout_parse_all_invalid() {
2405 assert!(WindowButtonLayout::parse("asdfghjkl").is_err());
2406 }
2407}