| 1 | mod app_menu; |
| 2 | mod keyboard; |
| 3 | mod keystroke; |
| 4 | |
| 5 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 6 | mod linux; |
| 7 | |
| 8 | #[cfg(target_os = "macos")] |
| 9 | mod mac; |
| 10 | |
| 11 | #[cfg(any( |
| 12 | all( |
| 13 | any(target_os = "linux", target_os = "freebsd"), |
| 14 | any(feature = "x11", feature = "wayland") |
| 15 | ), |
| 16 | all(target_os = "macos", feature = "macos-blade") |
| 17 | ))] |
| 18 | mod blade; |
| 19 | |
| 20 | #[cfg(any(test, feature = "test-support"))] |
| 21 | mod test; |
| 22 | |
| 23 | #[cfg(target_os = "windows")] |
| 24 | mod windows; |
| 25 | |
| 26 | #[cfg(all( |
| 27 | feature = "screen-capture", |
| 28 | any( |
| 29 | target_os = "windows", |
| 30 | all( |
| 31 | any(target_os = "linux", target_os = "freebsd"), |
| 32 | any(feature = "wayland", feature = "x11"), |
| 33 | ) |
| 34 | ) |
| 35 | ))] |
| 36 | pub(crate) mod scap_screen_capture; |
| 37 | |
| 38 | use crate::{ |
| 39 | Action, AnyWindowHandle, App, AsyncWindowContext, BackgroundExecutor, Bounds, |
| 40 | DEFAULT_WINDOW_SIZE, DevicePixels, DispatchEventResult, Font, FontId, FontMetrics, FontRun, |
| 41 | ForegroundExecutor, GlyphId, GpuSpecs, ImageSource, Keymap, LineLayout, Pixels, PlatformInput, |
| 42 | Point, RenderGlyphParams, RenderImage, RenderImageParams, RenderSvgParams, Scene, ShapedGlyph, |
| 43 | ShapedRun, SharedString, Size, SvgRenderer, SvgSize, SystemWindowTab, Task, TaskLabel, Window, |
| 44 | WindowControlArea, hash, point, px, size, |
| 45 | }; |
| 46 | use anyhow::Result; |
| 47 | use async_task::Runnable; |
| 48 | use futures::channel::oneshot; |
| 49 | use image::codecs::gif::GifDecoder; |
| 50 | use image::{AnimationDecoder as _, Frame}; |
| 51 | use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; |
| 52 | use schemars::JsonSchema; |
| 53 | use seahash::SeaHasher; |
| 54 | use serde::{Deserialize, Serialize}; |
| 55 | use smallvec::SmallVec; |
| 56 | use std::borrow::Cow; |
| 57 | use std::hash::{Hash, Hasher}; |
| 58 | use std::io::Cursor; |
| 59 | use std::ops; |
| 60 | use std::time::{Duration, Instant}; |
| 61 | use std::{ |
| 62 | fmt::{self, Debug}, |
| 63 | ops::Range, |
| 64 | path::{Path, PathBuf}, |
| 65 | rc::Rc, |
| 66 | sync::Arc, |
| 67 | }; |
| 68 | use strum::EnumIter; |
| 69 | use uuid::Uuid; |
| 70 | |
| 71 | pub use app_menu::*; |
| 72 | pub use keyboard::*; |
| 73 | pub use keystroke::*; |
| 74 | |
| 75 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 76 | pub(crate) use linux::*; |
| 77 | #[cfg(target_os = "macos")] |
| 78 | pub(crate) use mac::*; |
| 79 | pub use semantic_version::SemanticVersion; |
| 80 | #[cfg(any(test, feature = "test-support"))] |
| 81 | pub(crate) use test::*; |
| 82 | #[cfg(target_os = "windows")] |
| 83 | pub(crate) use windows::*; |
| 84 | |
| 85 | #[cfg(any(test, feature = "test-support"))] |
| 86 | pub use test::{TestDispatcher, TestScreenCaptureSource, TestScreenCaptureStream}; |
| 87 | |
| 88 | |
| 89 | pub fn background_executor() -> BackgroundExecutor { |
| 90 | current_platform(true).background_executor() |
| 91 | } |
| 92 | |
| 93 | #[cfg(target_os = "macos")] |
| 94 | pub(crate) fn current_platform(headless: bool) -> Rc<dyn Platform> { |
| 95 | Rc::new(MacPlatform::new(headless)) |
| 96 | } |
| 97 | |
| 98 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 99 | pub(crate) fn current_platform(headless: bool) -> Rc<dyn Platform> { |
| 100 | #[cfg(feature = "x11")] |
| 101 | use anyhow::Context as _; |
| 102 | |
| 103 | if headless { |
| 104 | return Rc::new(HeadlessClient::new()); |
| 105 | } |
| 106 | |
| 107 | match guess_compositor() { |
| 108 | #[cfg(feature = "wayland")] |
| 109 | "Wayland" => Rc::new(WaylandClient::new()), |
| 110 | |
| 111 | #[cfg(feature = "x11")] |
| 112 | "X11" => Rc::new( |
| 113 | X11Client::new() |
| 114 | .context("Failed to initialize X11 client.") |
| 115 | .unwrap(), |
| 116 | ), |
| 117 | |
| 118 | "Headless" => Rc::new(HeadlessClient::new()), |
| 119 | _ => unreachable!(), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 126 | #[inline] |
| 127 | pub fn guess_compositor() -> &'static str { |
| 128 | if std::env::var_os("ZED_HEADLESS").is_some() { |
| 129 | return "Headless"; |
| 130 | } |
| 131 | |
| 132 | #[cfg(feature = "wayland")] |
| 133 | let wayland_display = std::env::var_os("WAYLAND_DISPLAY"); |
| 134 | #[cfg(not(feature = "wayland"))] |
| 135 | let wayland_display: Option<std::ffi::OsString> = None; |
| 136 | |
| 137 | #[cfg(feature = "x11")] |
| 138 | let x11_display = std::env::var_os("DISPLAY"); |
| 139 | #[cfg(not(feature = "x11"))] |
| 140 | let x11_display: Option<std::ffi::OsString> = None; |
| 141 | |
| 142 | let use_wayland = wayland_display.is_some_and(|display| !display.is_empty()); |
| 143 | let use_x11 = x11_display.is_some_and(|display| !display.is_empty()); |
| 144 | |
| 145 | if use_wayland { |
| 146 | "Wayland" |
| 147 | } else if use_x11 { |
| 148 | "X11" |
| 149 | } else { |
| 150 | "Headless" |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[cfg(target_os = "windows")] |
| 155 | pub(crate) fn current_platform(_headless: bool) -> Rc<dyn Platform> { |
| 156 | Rc::new( |
| 157 | WindowsPlatform::new() |
| 158 | .inspect_err(|err| show_error("Failed to launch", err.to_string())) |
| 159 | .unwrap(), |
| 160 | ) |
| 161 | } |
| 162 | |
| 163 | pub(crate) trait Platform: 'static { |
| 164 | fn background_executor(&self) -> BackgroundExecutor; |
| 165 | fn foreground_executor(&self) -> ForegroundExecutor; |
| 166 | fn text_system(&self) -> Arc<dyn PlatformTextSystem>; |
| 167 | |
| 168 | fn run(&self, on_finish_launching: Box<dyn 'static + FnOnce()>); |
| 169 | fn quit(&self); |
| 170 | fn restart(&self, binary_path: Option<PathBuf>); |
| 171 | fn activate(&self, ignoring_other_apps: bool); |
| 172 | fn hide(&self); |
| 173 | fn hide_other_apps(&self); |
| 174 | fn unhide_other_apps(&self); |
| 175 | |
| 176 | fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>>; |
| 177 | fn primary_display(&self) -> Option<Rc<dyn PlatformDisplay>>; |
| 178 | fn active_window(&self) -> Option<AnyWindowHandle>; |
| 179 | fn window_stack(&self) -> Option<Vec<AnyWindowHandle>> { |
| 180 | None |
| 181 | } |
| 182 | |
| 183 | #[cfg(feature = "screen-capture")] |
| 184 | fn is_screen_capture_supported(&self) -> bool; |
| 185 | #[cfg(not(feature = "screen-capture"))] |
| 186 | fn is_screen_capture_supported(&self) -> bool { |
| 187 | false |
| 188 | } |
| 189 | #[cfg(feature = "screen-capture")] |
| 190 | fn screen_capture_sources(&self) |
| 191 | -> oneshot::Receiver<Result<Vec<Rc<dyn ScreenCaptureSource>>>>; |
| 192 | #[cfg(not(feature = "screen-capture"))] |
| 193 | fn screen_capture_sources( |
| 194 | &self, |
| 195 | ) -> oneshot::Receiver<anyhow::Result<Vec<Rc<dyn ScreenCaptureSource>>>> { |
| 196 | let (sources_tx, sources_rx) = oneshot::channel(); |
| 197 | sources_tx |
| 198 | .send(Err(anyhow::anyhow!( |
| 199 | "gpui was compiled without the screen-capture feature" |
| 200 | ))) |
| 201 | .ok(); |
| 202 | sources_rx |
| 203 | } |
| 204 | |
| 205 | fn open_window( |
| 206 | &self, |
| 207 | handle: AnyWindowHandle, |
| 208 | options: WindowParams, |
| 209 | ) -> anyhow::Result<Box<dyn PlatformWindow>>; |
| 210 | |
| 211 | |
| 212 | fn window_appearance(&self) -> WindowAppearance; |
| 213 | |
| 214 | fn open_url(&self, url: &str); |
| 215 | fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>); |
| 216 | fn register_url_scheme(&self, url: &str) -> Task<Result<()>>; |
| 217 | |
| 218 | fn prompt_for_paths( |
| 219 | &self, |
| 220 | options: PathPromptOptions, |
| 221 | ) -> oneshot::Receiver<Result<Option<Vec<PathBuf>>>>; |
| 222 | fn prompt_for_new_path( |
| 223 | &self, |
| 224 | directory: &Path, |
| 225 | suggested_name: Option<&str>, |
| 226 | ) -> oneshot::Receiver<Result<Option<PathBuf>>>; |
| 227 | fn can_select_mixed_files_and_dirs(&self) -> bool; |
| 228 | fn reveal_path(&self, path: &Path); |
| 229 | fn open_with_system(&self, path: &Path); |
| 230 | |
| 231 | fn on_quit(&self, callback: Box<dyn FnMut()>); |
| 232 | fn on_reopen(&self, callback: Box<dyn FnMut()>); |
| 233 | |
| 234 | fn set_menus(&self, menus: Vec<Menu>, keymap: &Keymap); |
| 235 | fn get_menus(&self) -> Option<Vec<OwnedMenu>> { |
| 236 | None |
| 237 | } |
| 238 | |
| 239 | fn set_dock_menu(&self, menu: Vec<MenuItem>, keymap: &Keymap); |
| 240 | fn perform_dock_menu_action(&self, _action: usize) {} |
| 241 | fn add_recent_document(&self, _path: &Path) {} |
| 242 | fn update_jump_list( |
| 243 | &self, |
| 244 | _menus: Vec<MenuItem>, |
| 245 | _entries: Vec<SmallVec<[PathBuf; 2]>>, |
| 246 | ) -> Vec<SmallVec<[PathBuf; 2]>> { |
| 247 | Vec::new() |
| 248 | } |
| 249 | fn on_app_menu_action(&self, callback: Box<dyn FnMut(&dyn Action)>); |
| 250 | fn on_will_open_app_menu(&self, callback: Box<dyn FnMut()>); |
| 251 | fn on_validate_app_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>); |
| 252 | |
| 253 | fn compositor_name(&self) -> &'static str { |
| 254 | "" |
| 255 | } |
| 256 | fn app_path(&self) -> Result<PathBuf>; |
| 257 | fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>; |
| 258 | |
| 259 | fn set_cursor_style(&self, style: CursorStyle); |
| 260 | fn should_auto_hide_scrollbars(&self) -> bool; |
| 261 | |
| 262 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 263 | fn write_to_primary(&self, item: ClipboardItem); |
| 264 | fn write_to_clipboard(&self, item: ClipboardItem); |
| 265 | #[cfg(any(target_os = "linux", target_os = "freebsd"))] |
| 266 | fn read_from_primary(&self) -> Option<ClipboardItem>; |
| 267 | fn read_from_clipboard(&self) -> Option<ClipboardItem>; |
| 268 | |
| 269 | fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Task<Result<()>>; |
| 270 | fn read_credentials(&self, url: &str) -> Task<Result<Option<(String, Vec<u8>)>>>; |
| 271 | fn delete_credentials(&self, url: &str) -> Task<Result<()>>; |
| 272 | |
| 273 | fn keyboard_layout(&self) -> Box<dyn PlatformKeyboardLayout>; |
| 274 | fn keyboard_mapper(&self) -> Rc<dyn PlatformKeyboardMapper>; |
| 275 | fn on_keyboard_layout_change(&self, callback: Box<dyn FnMut()>); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | pub trait PlatformDisplay: Send + Sync + Debug { |
| 280 | |
| 281 | fn id(&self) -> DisplayId; |
| 282 | |
| 283 | |
| 284 | |
| 285 | fn uuid(&self) -> Result<Uuid>; |
| 286 | |
| 287 | |
| 288 | fn bounds(&self) -> Bounds<Pixels>; |
| 289 | |
| 290 | |
| 291 | fn default_bounds(&self) -> Bounds<Pixels> { |
| 292 | let bounds = self.bounds(); |
| 293 | let center = bounds.center(); |
| 294 | let clipped_window_size = DEFAULT_WINDOW_SIZE.min(&bounds.size); |
| 295 | |
| 296 | let offset = clipped_window_size / 2.0; |
| 297 | let origin = point(center.x - offset.width, center.y - offset.height); |
| 298 | Bounds::new(origin, clipped_window_size) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | |
| 303 | #[derive(Clone)] |
| 304 | pub struct SourceMetadata { |
| 305 | |
| 306 | pub id: u64, |
| 307 | |
| 308 | pub label: Option<SharedString>, |
| 309 | |
| 310 | pub is_main: Option<bool>, |
| 311 | |
| 312 | pub resolution: Size<DevicePixels>, |
| 313 | } |
| 314 | |
| 315 | |
| 316 | pub trait ScreenCaptureSource { |
| 317 | |
| 318 | fn metadata(&self) -> Result<SourceMetadata>; |
| 319 | |
| 320 | |
| 321 | |
| 322 | fn stream( |
| 323 | &self, |
| 324 | foreground_executor: &ForegroundExecutor, |
| 325 | frame_callback: Box<dyn Fn(ScreenCaptureFrame) + Send>, |
| 326 | ) -> oneshot::Receiver<Result<Box<dyn ScreenCaptureStream>>>; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | pub trait ScreenCaptureStream { |
| 331 | |
| 332 | fn metadata(&self) -> Result<SourceMetadata>; |
| 333 | } |
| 334 | |
| 335 | |
| 336 | pub struct ScreenCaptureFrame(pub PlatformScreenCaptureFrame); |
| 337 | |
| 338 | |
| 339 | #[derive(PartialEq, Eq, Hash, Copy, Clone)] |
| 340 | pub struct DisplayId(pub(crate) u32); |
| 341 | |
| 342 | impl From<DisplayId> for u32 { |
| 343 | fn from(id: DisplayId) -> Self { |
| 344 | id.0 |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | impl Debug for DisplayId { |
| 349 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 350 | write!(f, "DisplayId({})", self.0) |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | |
| 355 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 356 | pub enum ResizeEdge { |
| 357 | |
| 358 | Top, |
| 359 | |
| 360 | TopRight, |
| 361 | |
| 362 | Right, |
| 363 | |
| 364 | BottomRight, |
| 365 | |
| 366 | Bottom, |
| 367 | |
| 368 | BottomLeft, |
| 369 | |
| 370 | Left, |
| 371 | |
| 372 | TopLeft, |
| 373 | } |
| 374 | |
| 375 | |
| 376 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)] |
| 377 | pub enum WindowDecorations { |
| 378 | #[default] |
| 379 | |
| 380 | Server, |
| 381 | |
| 382 | Client, |
| 383 | } |
| 384 | |
| 385 | |
| 386 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)] |
| 387 | pub enum Decorations { |
| 388 | |
| 389 | #[default] |
| 390 | Server, |
| 391 | |
| 392 | Client { |
| 393 | |
| 394 | tiling: Tiling, |
| 395 | }, |
| 396 | } |
| 397 | |
| 398 | |
| 399 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] |
| 400 | pub struct WindowControls { |
| 401 | |
| 402 | pub fullscreen: bool, |
| 403 | |
| 404 | pub maximize: bool, |
| 405 | |
| 406 | pub minimize: bool, |
| 407 | |
| 408 | pub window_menu: bool, |
| 409 | } |
| 410 | |
| 411 | impl Default for WindowControls { |
| 412 | fn default() -> Self { |
| 413 | |
| 414 | Self { |
| 415 | fullscreen: true, |
| 416 | maximize: true, |
| 417 | minimize: true, |
| 418 | window_menu: true, |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | |
| 424 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)] |
| 425 | pub struct Tiling { |
| 426 | |
| 427 | pub top: bool, |
| 428 | |
| 429 | pub left: bool, |
| 430 | |
| 431 | pub right: bool, |
| 432 | |
| 433 | pub bottom: bool, |
| 434 | } |
| 435 | |
| 436 | impl Tiling { |
| 437 | |
| 438 | pub fn tiled() -> Self { |
| 439 | Self { |
| 440 | top: true, |
| 441 | left: true, |
| 442 | right: true, |
| 443 | bottom: true, |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | |
| 448 | pub fn is_tiled(&self) -> bool { |
| 449 | self.top || self.left || self.right || self.bottom |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Default)] |
| 454 | pub(crate) struct RequestFrameOptions { |
| 455 | pub(crate) require_presentation: bool, |
| 456 | |
| 457 | pub(crate) force_render: bool, |
| 458 | } |
| 459 | |
| 460 | pub(crate) trait PlatformWindow: HasWindowHandle + HasDisplayHandle { |
| 461 | fn bounds(&self) -> Bounds<Pixels>; |
| 462 | fn is_maximized(&self) -> bool; |
| 463 | fn window_bounds(&self) -> WindowBounds; |
| 464 | fn content_size(&self) -> Size<Pixels>; |
| 465 | fn resize(&mut self, size: Size<Pixels>); |
| 466 | fn scale_factor(&self) -> f32; |
| 467 | fn appearance(&self) -> WindowAppearance; |
| 468 | fn display(&self) -> Option<Rc<dyn PlatformDisplay>>; |
| 469 | fn mouse_position(&self) -> Point<Pixels>; |
| 470 | fn modifiers(&self) -> Modifiers; |
| 471 | fn capslock(&self) -> Capslock; |
| 472 | fn set_input_handler(&mut self, input_handler: PlatformInputHandler); |
| 473 | fn take_input_handler(&mut self) -> Option<PlatformInputHandler>; |
| 474 | fn prompt( |
| 475 | &self, |
| 476 | level: PromptLevel, |
| 477 | msg: &str, |
| 478 | detail: Option<&str>, |
| 479 | answers: &[PromptButton], |
| 480 | ) -> Option<oneshot::Receiver<usize>>; |
| 481 | fn activate(&self); |
| 482 | fn is_active(&self) -> bool; |
| 483 | fn is_hovered(&self) -> bool; |
| 484 | fn set_title(&mut self, title: &str); |
| 485 | fn set_background_appearance(&self, background_appearance: WindowBackgroundAppearance); |
| 486 | fn minimize(&self); |
| 487 | fn zoom(&self); |
| 488 | fn toggle_fullscreen(&self); |
| 489 | fn is_fullscreen(&self) -> bool; |
| 490 | fn on_request_frame(&self, callback: Box<dyn FnMut(RequestFrameOptions)>); |
| 491 | fn on_input(&self, callback: Box<dyn FnMut(PlatformInput) -> DispatchEventResult>); |
| 492 | fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>); |
| 493 | fn on_hover_status_change(&self, callback: Box<dyn FnMut(bool)>); |
| 494 | fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>); |
| 495 | fn on_moved(&self, callback: Box<dyn FnMut()>); |
| 496 | fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>); |
| 497 | fn on_hit_test_window_control(&self, callback: Box<dyn FnMut() -> Option<WindowControlArea>>); |
| 498 | fn on_close(&self, callback: Box<dyn FnOnce()>); |
| 499 | fn on_appearance_changed(&self, callback: Box<dyn FnMut()>); |
| 500 | fn draw(&self, scene: &Scene); |
| 501 | fn completed_frame(&self) {} |
| 502 | fn sprite_atlas(&self) -> Arc<dyn PlatformAtlas>; |
| 503 | |
| 504 | |
| 505 | fn get_title(&self) -> String { |
| 506 | String::new() |
| 507 | } |
| 508 | fn tabbed_windows(&self) -> Option<Vec<SystemWindowTab>> { |
| 509 | None |
| 510 | } |
| 511 | fn tab_bar_visible(&self) -> bool { |
| 512 | false |
| 513 | } |
| 514 | fn set_edited(&mut self, _edited: bool) {} |
| 515 | fn show_character_palette(&self) {} |
| 516 | fn titlebar_double_click(&self) {} |
| 517 | fn on_move_tab_to_new_window(&self, _callback: Box<dyn FnMut()>) {} |
| 518 | fn on_merge_all_windows(&self, _callback: Box<dyn FnMut()>) {} |
| 519 | fn on_select_previous_tab(&self, _callback: Box<dyn FnMut()>) {} |
| 520 | fn on_select_next_tab(&self, _callback: Box<dyn FnMut()>) {} |
| 521 | fn on_toggle_tab_bar(&self, _callback: Box<dyn FnMut()>) {} |
| 522 | fn merge_all_windows(&self) {} |
| 523 | fn move_tab_to_new_window(&self) {} |
| 524 | fn toggle_window_tab_overview(&self) {} |
| 525 | fn set_tabbing_identifier(&self, _identifier: Option<String>) {} |
| 526 | |
| 527 | #[cfg(target_os = "windows")] |
| 528 | fn get_raw_handle(&self) -> windows::HWND; |
| 529 | |
| 530 | |
| 531 | fn inner_window_bounds(&self) -> WindowBounds { |
| 532 | self.window_bounds() |
| 533 | } |
| 534 | fn request_decorations(&self, _decorations: WindowDecorations) {} |
| 535 | fn show_window_menu(&self, _position: Point<Pixels>) {} |
| 536 | fn start_window_move(&self) {} |
| 537 | fn start_window_resize(&self, _edge: ResizeEdge) {} |
| 538 | fn window_decorations(&self) -> Decorations { |
| 539 | Decorations::Server |
| 540 | } |
| 541 | fn set_app_id(&mut self, _app_id: &str) {} |
| 542 | fn map_window(&mut self) -> anyhow::Result<()> { |
| 543 | Ok(()) |
| 544 | } |
| 545 | fn window_controls(&self) -> WindowControls { |
| 546 | WindowControls::default() |
| 547 | } |
| 548 | fn set_client_inset(&self, _inset: Pixels) {} |
| 549 | fn gpu_specs(&self) -> Option<GpuSpecs>; |
| 550 | |
| 551 | fn update_ime_position(&self, _bounds: Bounds<Pixels>); |
| 552 | |
| 553 | #[cfg(any(test, feature = "test-support"))] |
| 554 | fn as_test(&mut self) -> Option<&mut TestWindow> { |
| 555 | None |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | |
| 560 | |
| 561 | #[doc(hidden)] |
| 562 | pub trait PlatformDispatcher: Send + Sync { |
| 563 | fn is_main_thread(&self) -> bool; |
| 564 | fn dispatch(&self, runnable: Runnable, label: Option<TaskLabel>); |
| 565 | fn dispatch_on_main_thread(&self, runnable: Runnable); |
| 566 | fn dispatch_after(&self, duration: Duration, runnable: Runnable); |
| 567 | fn now(&self) -> Instant { |
| 568 | Instant::now() |
| 569 | } |
| 570 | |
| 571 | #[cfg(any(test, feature = "test-support"))] |
| 572 | fn as_test(&self) -> Option<&TestDispatcher> { |
| 573 | None |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | pub(crate) trait PlatformTextSystem: Send + Sync { |
| 578 | fn add_fonts(&self, fonts: Vec<Cow<'static, [u8]>>) -> Result<()>; |
| 579 | fn all_font_names(&self) -> Vec<String>; |
| 580 | fn font_id(&self, descriptor: &Font) -> Result<FontId>; |
| 581 | fn font_metrics(&self, font_id: FontId) -> FontMetrics; |
| 582 | fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Bounds<f32>>; |
| 583 | fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>>; |
| 584 | fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>; |
| 585 | fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>>; |
| 586 | fn rasterize_glyph( |
| 587 | &self, |
| 588 | params: &RenderGlyphParams, |
| 589 | raster_bounds: Bounds<DevicePixels>, |
| 590 | ) -> Result<(Size<DevicePixels>, Vec<u8>)>; |
| 591 | fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout; |
| 592 | } |
| 593 | |
| 594 | pub(crate) struct NoopTextSystem; |
| 595 | |
| 596 | impl NoopTextSystem { |
| 597 | #[allow(dead_code)] |
| 598 | pub fn new() -> Self { |
| 599 | Self |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | impl PlatformTextSystem for NoopTextSystem { |
| 604 | fn add_fonts(&self, _fonts: Vec<Cow<'static, [u8]>>) -> Result<()> { |
| 605 | Ok(()) |
| 606 | } |
| 607 | |
| 608 | fn all_font_names(&self) -> Vec<String> { |
| 609 | Vec::new() |
| 610 | } |
| 611 | |
| 612 | fn font_id(&self, _descriptor: &Font) -> Result<FontId> { |
| 613 | Ok(FontId(1)) |
| 614 | } |
| 615 | |
| 616 | fn font_metrics(&self, _font_id: FontId) -> FontMetrics { |
| 617 | FontMetrics { |
| 618 | units_per_em: 1000, |
| 619 | ascent: 1025.0, |
| 620 | descent: -275.0, |
| 621 | line_gap: 0.0, |
| 622 | underline_position: -95.0, |
| 623 | underline_thickness: 60.0, |
| 624 | cap_height: 698.0, |
| 625 | x_height: 516.0, |
| 626 | bounding_box: Bounds { |
| 627 | origin: Point { |
| 628 | x: -260.0, |
| 629 | y: -245.0, |
| 630 | }, |
| 631 | size: Size { |
| 632 | width: 1501.0, |
| 633 | height: 1364.0, |
| 634 | }, |
| 635 | }, |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | fn typographic_bounds(&self, _font_id: FontId, _glyph_id: GlyphId) -> Result<Bounds<f32>> { |
| 640 | Ok(Bounds { |
| 641 | origin: Point { x: 54.0, y: 0.0 }, |
| 642 | size: size(392.0, 528.0), |
| 643 | }) |
| 644 | } |
| 645 | |
| 646 | fn advance(&self, _font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>> { |
| 647 | Ok(size(600.0 * glyph_id.0 as f32, 0.0)) |
| 648 | } |
| 649 | |
| 650 | fn glyph_for_char(&self, _font_id: FontId, ch: char) -> Option<GlyphId> { |
| 651 | Some(GlyphId(ch.len_utf16() as u32)) |
| 652 | } |
| 653 | |
| 654 | fn glyph_raster_bounds(&self, _params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> { |
| 655 | Ok(Default::default()) |
| 656 | } |
| 657 | |
| 658 | fn rasterize_glyph( |
| 659 | &self, |
| 660 | _params: &RenderGlyphParams, |
| 661 | raster_bounds: Bounds<DevicePixels>, |
| 662 | ) -> Result<(Size<DevicePixels>, Vec<u8>)> { |
| 663 | Ok((raster_bounds.size, Vec::new())) |
| 664 | } |
| 665 | |
| 666 | fn layout_line(&self, text: &str, font_size: Pixels, _runs: &[FontRun]) -> LineLayout { |
| 667 | let mut position = px(0.); |
| 668 | let metrics = self.font_metrics(FontId(0)); |
| 669 | let em_width = font_size |
| 670 | * self |
| 671 | .advance(FontId(0), self.glyph_for_char(FontId(0), 'm').unwrap()) |
| 672 | .unwrap() |
| 673 | .width |
| 674 | / metrics.units_per_em as f32; |
| 675 | let mut glyphs = Vec::new(); |
| 676 | for (ix, c) in text.char_indices() { |
| 677 | if let Some(glyph) = self.glyph_for_char(FontId(0), c) { |
| 678 | glyphs.push(ShapedGlyph { |
| 679 | id: glyph, |
| 680 | position: point(position, px(0.)), |
| 681 | index: ix, |
| 682 | is_emoji: glyph.0 == 2, |
| 683 | }); |
| 684 | if glyph.0 == 2 { |
| 685 | position += em_width * 2.0; |
| 686 | } else { |
| 687 | position += em_width; |
| 688 | } |
| 689 | } else { |
| 690 | position += em_width |
| 691 | } |
| 692 | } |
| 693 | let mut runs = Vec::default(); |
| 694 | if !glyphs.is_empty() { |
| 695 | runs.push(ShapedRun { |
| 696 | font_id: FontId(0), |
| 697 | glyphs, |
| 698 | }); |
| 699 | } else { |
| 700 | position = px(0.); |
| 701 | } |
| 702 | |
| 703 | LineLayout { |
| 704 | font_size, |
| 705 | width: position, |
| 706 | ascent: font_size * (metrics.ascent / metrics.units_per_em as f32), |
| 707 | descent: font_size * (metrics.descent / metrics.units_per_em as f32), |
| 708 | runs, |
| 709 | len: text.len(), |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | #[derive(PartialEq, Eq, Hash, Clone)] |
| 715 | pub(crate) enum AtlasKey { |
| 716 | Glyph(RenderGlyphParams), |
| 717 | Svg(RenderSvgParams), |
| 718 | Image(RenderImageParams), |
| 719 | } |
| 720 | |
| 721 | impl AtlasKey { |
| 722 | #[cfg_attr( |
| 723 | all( |
| 724 | any(target_os = "linux", target_os = "freebsd"), |
| 725 | not(any(feature = "x11", feature = "wayland")) |
| 726 | ), |
| 727 | allow(dead_code) |
| 728 | )] |
| 729 | pub(crate) fn texture_kind(&self) -> AtlasTextureKind { |
| 730 | match self { |
| 731 | AtlasKey::Glyph(params) => { |
| 732 | if params.is_emoji { |
| 733 | AtlasTextureKind::Polychrome |
| 734 | } else { |
| 735 | AtlasTextureKind::Monochrome |
| 736 | } |
| 737 | } |
| 738 | AtlasKey::Svg(_) => AtlasTextureKind::Monochrome, |
| 739 | AtlasKey::Image(_) => AtlasTextureKind::Polychrome, |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | impl From<RenderGlyphParams> for AtlasKey { |
| 745 | fn from(params: RenderGlyphParams) -> Self { |
| 746 | Self::Glyph(params) |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | impl From<RenderSvgParams> for AtlasKey { |
| 751 | fn from(params: RenderSvgParams) -> Self { |
| 752 | Self::Svg(params) |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | impl From<RenderImageParams> for AtlasKey { |
| 757 | fn from(params: RenderImageParams) -> Self { |
| 758 | Self::Image(params) |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | pub(crate) trait PlatformAtlas: Send + Sync { |
| 763 | fn get_or_insert_with<'a>( |
| 764 | &self, |
| 765 | key: &AtlasKey, |
| 766 | build: &mut dyn FnMut() -> Result<Option<(Size<DevicePixels>, Cow<'a, [u8]>)>>, |
| 767 | ) -> Result<Option<AtlasTile>>; |
| 768 | fn remove(&self, key: &AtlasKey); |
| 769 | } |
| 770 | |
| 771 | struct AtlasTextureList<T> { |
| 772 | textures: Vec<Option<T>>, |
| 773 | free_list: Vec<usize>, |
| 774 | } |
| 775 | |
| 776 | impl<T> Default for AtlasTextureList<T> { |
| 777 | fn default() -> Self { |
| 778 | Self { |
| 779 | textures: Vec::default(), |
| 780 | free_list: Vec::default(), |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | impl<T> ops::Index<usize> for AtlasTextureList<T> { |
| 786 | type Output = Option<T>; |
| 787 | |
| 788 | fn index(&self, index: usize) -> &Self::Output { |
| 789 | &self.textures[index] |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | impl<T> AtlasTextureList<T> { |
| 794 | #[allow(unused)] |
| 795 | fn drain(&mut self) -> std::vec::Drain<'_, Option<T>> { |
| 796 | self.free_list.clear(); |
| 797 | self.textures.drain(..) |
| 798 | } |
| 799 | |
| 800 | #[allow(dead_code)] |
| 801 | fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> { |
| 802 | self.textures.iter_mut().flatten() |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 807 | #[repr(C)] |
| 808 | pub(crate) struct AtlasTile { |
| 809 | pub(crate) texture_id: AtlasTextureId, |
| 810 | pub(crate) tile_id: TileId, |
| 811 | pub(crate) padding: u32, |
| 812 | pub(crate) bounds: Bounds<DevicePixels>, |
| 813 | } |
| 814 | |
| 815 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 816 | #[repr(C)] |
| 817 | pub(crate) struct AtlasTextureId { |
| 818 | |
| 819 | pub(crate) index: u32, |
| 820 | pub(crate) kind: AtlasTextureKind, |
| 821 | } |
| 822 | |
| 823 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 824 | #[repr(C)] |
| 825 | #[cfg_attr( |
| 826 | all( |
| 827 | any(target_os = "linux", target_os = "freebsd"), |
| 828 | not(any(feature = "x11", feature = "wayland")) |
| 829 | ), |
| 830 | allow(dead_code) |
| 831 | )] |
| 832 | pub(crate) enum AtlasTextureKind { |
| 833 | Monochrome = 0, |
| 834 | Polychrome = 1, |
| 835 | } |
| 836 | |
| 837 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 838 | #[repr(C)] |
| 839 | pub(crate) struct TileId(pub(crate) u32); |
| 840 | |
| 841 | impl From<etagere::AllocId> for TileId { |
| 842 | fn from(id: etagere::AllocId) -> Self { |
| 843 | Self(id.serialize()) |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | impl From<TileId> for etagere::AllocId { |
| 848 | fn from(id: TileId) -> Self { |
| 849 | Self::deserialize(id.0) |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | pub(crate) struct PlatformInputHandler { |
| 854 | cx: AsyncWindowContext, |
| 855 | handler: Box<dyn InputHandler>, |
| 856 | } |
| 857 | |
| 858 | #[cfg_attr( |
| 859 | all( |
| 860 | any(target_os = "linux", target_os = "freebsd"), |
| 861 | not(any(feature = "x11", feature = "wayland")) |
| 862 | ), |
| 863 | allow(dead_code) |
| 864 | )] |
| 865 | impl PlatformInputHandler { |
| 866 | pub fn new(cx: AsyncWindowContext, handler: Box<dyn InputHandler>) -> Self { |
| 867 | Self { cx, handler } |
| 868 | } |
| 869 | |
| 870 | fn selected_text_range(&mut self, ignore_disabled_input: bool) -> Option<UTF16Selection> { |
| 871 | self.cx |
| 872 | .update(|window, cx| { |
| 873 | self.handler |
| 874 | .selected_text_range(ignore_disabled_input, window, cx) |
| 875 | }) |
| 876 | .ok() |
| 877 | .flatten() |
| 878 | } |
| 879 | |
| 880 | #[cfg_attr(target_os = "windows", allow(dead_code))] |
| 881 | fn marked_text_range(&mut self) -> Option<Range<usize>> { |
| 882 | self.cx |
| 883 | .update(|window, cx| self.handler.marked_text_range(window, cx)) |
| 884 | .ok() |
| 885 | .flatten() |
| 886 | } |
| 887 | |
| 888 | #[cfg_attr( |
| 889 | any(target_os = "linux", target_os = "freebsd", target_os = "windows"), |
| 890 | allow(dead_code) |
| 891 | )] |
| 892 | fn text_for_range( |
| 893 | &mut self, |
| 894 | range_utf16: Range<usize>, |
| 895 | adjusted: &mut Option<Range<usize>>, |
| 896 | ) -> Option<String> { |
| 897 | self.cx |
| 898 | .update(|window, cx| { |
| 899 | self.handler |
| 900 | .text_for_range(range_utf16, adjusted, window, cx) |
| 901 | }) |
| 902 | .ok() |
| 903 | .flatten() |
| 904 | } |
| 905 | |
| 906 | fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, text: &str) { |
| 907 | self.cx |
| 908 | .update(|window, cx| { |
| 909 | self.handler |
| 910 | .replace_text_in_range(replacement_range, text, window, cx); |
| 911 | }) |
| 912 | .ok(); |
| 913 | } |
| 914 | |
| 915 | pub fn replace_and_mark_text_in_range( |
| 916 | &mut self, |
| 917 | range_utf16: Option<Range<usize>>, |
| 918 | new_text: &str, |
| 919 | new_selected_range: Option<Range<usize>>, |
| 920 | ) { |
| 921 | self.cx |
| 922 | .update(|window, cx| { |
| 923 | self.handler.replace_and_mark_text_in_range( |
| 924 | range_utf16, |
| 925 | new_text, |
| 926 | new_selected_range, |
| 927 | window, |
| 928 | cx, |
| 929 | ) |
| 930 | }) |
| 931 | .ok(); |
| 932 | } |
| 933 | |
| 934 | #[cfg_attr(target_os = "windows", allow(dead_code))] |
| 935 | fn unmark_text(&mut self) { |
| 936 | self.cx |
| 937 | .update(|window, cx| self.handler.unmark_text(window, cx)) |
| 938 | .ok(); |
| 939 | } |
| 940 | |
| 941 | fn bounds_for_range(&mut self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>> { |
| 942 | self.cx |
| 943 | .update(|window, cx| self.handler.bounds_for_range(range_utf16, window, cx)) |
| 944 | .ok() |
| 945 | .flatten() |
| 946 | } |
| 947 | |
| 948 | #[allow(dead_code)] |
| 949 | fn apple_press_and_hold_enabled(&mut self) -> bool { |
| 950 | self.handler.apple_press_and_hold_enabled() |
| 951 | } |
| 952 | |
| 953 | pub(crate) fn dispatch_input(&mut self, input: &str, window: &mut Window, cx: &mut App) { |
| 954 | self.handler.replace_text_in_range(None, input, window, cx); |
| 955 | } |
| 956 | |
| 957 | pub fn selected_bounds(&mut self, window: &mut Window, cx: &mut App) -> Option<Bounds<Pixels>> { |
| 958 | let selection = self.handler.selected_text_range(true, window, cx)?; |
| 959 | self.handler.bounds_for_range( |
| 960 | if selection.reversed { |
| 961 | selection.range.start..selection.range.start |
| 962 | } else { |
| 963 | selection.range.end..selection.range.end |
| 964 | }, |
| 965 | window, |
| 966 | cx, |
| 967 | ) |
| 968 | } |
| 969 | |
| 970 | #[allow(unused)] |
| 971 | pub fn character_index_for_point(&mut self, point: Point<Pixels>) -> Option<usize> { |
| 972 | self.cx |
| 973 | .update(|window, cx| self.handler.character_index_for_point(point, window, cx)) |
| 974 | .ok() |
| 975 | .flatten() |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | |
| 980 | |
| 981 | #[derive(Debug)] |
| 982 | pub struct UTF16Selection { |
| 983 | |
| 984 | |
| 985 | pub range: Range<usize>, |
| 986 | |
| 987 | |
| 988 | pub reversed: bool, |
| 989 | } |
| 990 | |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | pub trait InputHandler: 'static { |
| 996 | |
| 997 | |
| 998 | |
| 999 | |
| 1000 | fn selected_text_range( |
| 1001 | &mut self, |
| 1002 | ignore_disabled_input: bool, |
| 1003 | window: &mut Window, |
| 1004 | cx: &mut App, |
| 1005 | ) -> Option<UTF16Selection>; |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | fn marked_text_range(&mut self, window: &mut Window, cx: &mut App) -> Option<Range<usize>>; |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | fn text_for_range( |
| 1018 | &mut self, |
| 1019 | range_utf16: Range<usize>, |
| 1020 | adjusted_range: &mut Option<Range<usize>>, |
| 1021 | window: &mut Window, |
| 1022 | cx: &mut App, |
| 1023 | ) -> Option<String>; |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | fn replace_text_in_range( |
| 1030 | &mut self, |
| 1031 | replacement_range: Option<Range<usize>>, |
| 1032 | text: &str, |
| 1033 | window: &mut Window, |
| 1034 | cx: &mut App, |
| 1035 | ); |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | fn replace_and_mark_text_in_range( |
| 1044 | &mut self, |
| 1045 | range_utf16: Option<Range<usize>>, |
| 1046 | new_text: &str, |
| 1047 | new_selected_range: Option<Range<usize>>, |
| 1048 | window: &mut Window, |
| 1049 | cx: &mut App, |
| 1050 | ); |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | fn unmark_text(&mut self, window: &mut Window, cx: &mut App); |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | fn bounds_for_range( |
| 1061 | &mut self, |
| 1062 | range_utf16: Range<usize>, |
| 1063 | window: &mut Window, |
| 1064 | cx: &mut App, |
| 1065 | ) -> Option<Bounds<Pixels>>; |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | fn character_index_for_point( |
| 1071 | &mut self, |
| 1072 | point: Point<Pixels>, |
| 1073 | window: &mut Window, |
| 1074 | cx: &mut App, |
| 1075 | ) -> Option<usize>; |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | #[allow(dead_code)] |
| 1082 | fn apple_press_and_hold_enabled(&mut self) -> bool { |
| 1083 | true |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | |
| 1088 | #[derive(Debug)] |
| 1089 | pub struct WindowOptions { |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | pub window_bounds: Option<WindowBounds>, |
| 1094 | |
| 1095 | |
| 1096 | pub titlebar: Option<TitlebarOptions>, |
| 1097 | |
| 1098 | |
| 1099 | pub focus: bool, |
| 1100 | |
| 1101 | |
| 1102 | pub show: bool, |
| 1103 | |
| 1104 | |
| 1105 | pub kind: WindowKind, |
| 1106 | |
| 1107 | |
| 1108 | pub is_movable: bool, |
| 1109 | |
| 1110 | |
| 1111 | pub is_resizable: bool, |
| 1112 | |
| 1113 | |
| 1114 | pub is_minimizable: bool, |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | pub display_id: Option<DisplayId>, |
| 1119 | |
| 1120 | |
| 1121 | pub window_background: WindowBackgroundAppearance, |
| 1122 | |
| 1123 | |
| 1124 | pub app_id: Option<String>, |
| 1125 | |
| 1126 | |
| 1127 | pub window_min_size: Option<Size<Pixels>>, |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | pub window_decorations: Option<WindowDecorations>, |
| 1132 | |
| 1133 | |
| 1134 | pub tabbing_identifier: Option<String>, |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | #[derive(Debug)] |
| 1139 | #[cfg_attr( |
| 1140 | all( |
| 1141 | any(target_os = "linux", target_os = "freebsd"), |
| 1142 | not(any(feature = "x11", feature = "wayland")) |
| 1143 | ), |
| 1144 | allow(dead_code) |
| 1145 | )] |
| 1146 | pub(crate) struct WindowParams { |
| 1147 | pub bounds: Bounds<Pixels>, |
| 1148 | |
| 1149 | |
| 1150 | #[cfg_attr(feature = "wayland", allow(dead_code))] |
| 1151 | pub titlebar: Option<TitlebarOptions>, |
| 1152 | |
| 1153 | |
| 1154 | #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))] |
| 1155 | pub kind: WindowKind, |
| 1156 | |
| 1157 | |
| 1158 | #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))] |
| 1159 | pub is_movable: bool, |
| 1160 | |
| 1161 | |
| 1162 | #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))] |
| 1163 | pub is_resizable: bool, |
| 1164 | |
| 1165 | |
| 1166 | #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))] |
| 1167 | pub is_minimizable: bool, |
| 1168 | |
| 1169 | #[cfg_attr( |
| 1170 | any(target_os = "linux", target_os = "freebsd", target_os = "windows"), |
| 1171 | allow(dead_code) |
| 1172 | )] |
| 1173 | pub focus: bool, |
| 1174 | |
| 1175 | #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))] |
| 1176 | pub show: bool, |
| 1177 | |
| 1178 | #[cfg_attr(feature = "wayland", allow(dead_code))] |
| 1179 | pub display_id: Option<DisplayId>, |
| 1180 | |
| 1181 | pub window_min_size: Option<Size<Pixels>>, |
| 1182 | #[cfg(target_os = "macos")] |
| 1183 | pub tabbing_identifier: Option<String>, |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | #[derive(Debug, Copy, Clone, PartialEq)] |
| 1188 | pub enum WindowBounds { |
| 1189 | |
| 1190 | Windowed(Bounds<Pixels>), |
| 1191 | |
| 1192 | |
| 1193 | Maximized(Bounds<Pixels>), |
| 1194 | |
| 1195 | |
| 1196 | Fullscreen(Bounds<Pixels>), |
| 1197 | } |
| 1198 | |
| 1199 | impl Default for WindowBounds { |
| 1200 | fn default() -> Self { |
| 1201 | WindowBounds::Windowed(Bounds::default()) |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | impl WindowBounds { |
| 1206 | |
| 1207 | pub fn get_bounds(&self) -> Bounds<Pixels> { |
| 1208 | match self { |
| 1209 | WindowBounds::Windowed(bounds) => *bounds, |
| 1210 | WindowBounds::Maximized(bounds) => *bounds, |
| 1211 | WindowBounds::Fullscreen(bounds) => *bounds, |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | pub fn centered(size: Size<Pixels>, cx: &App) -> Self { |
| 1217 | WindowBounds::Windowed(Bounds::centered(None, size, cx)) |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | impl Default for WindowOptions { |
| 1222 | fn default() -> Self { |
| 1223 | Self { |
| 1224 | window_bounds: None, |
| 1225 | titlebar: Some(TitlebarOptions { |
| 1226 | title: Default::default(), |
| 1227 | appears_transparent: Default::default(), |
| 1228 | traffic_light_position: Default::default(), |
| 1229 | }), |
| 1230 | focus: true, |
| 1231 | show: true, |
| 1232 | kind: WindowKind::Normal, |
| 1233 | is_movable: true, |
| 1234 | is_resizable: true, |
| 1235 | is_minimizable: true, |
| 1236 | display_id: None, |
| 1237 | window_background: WindowBackgroundAppearance::default(), |
| 1238 | app_id: None, |
| 1239 | window_min_size: None, |
| 1240 | window_decorations: None, |
| 1241 | tabbing_identifier: None, |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | #[derive(Debug, Default)] |
| 1248 | pub struct TitlebarOptions { |
| 1249 | |
| 1250 | pub title: Option<SharedString>, |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | pub appears_transparent: bool, |
| 1255 | |
| 1256 | |
| 1257 | pub traffic_light_position: Option<Point<Pixels>>, |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 1262 | pub enum WindowKind { |
| 1263 | |
| 1264 | Normal, |
| 1265 | |
| 1266 | |
| 1267 | |
| 1268 | PopUp, |
| 1269 | |
| 1270 | |
| 1271 | Floating, |
| 1272 | } |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 1279 | pub enum WindowAppearance { |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | Light, |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | |
| 1288 | VibrantLight, |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | Dark, |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | |
| 1298 | VibrantDark, |
| 1299 | } |
| 1300 | |
| 1301 | impl Default for WindowAppearance { |
| 1302 | fn default() -> Self { |
| 1303 | Self::Light |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | |
| 1309 | #[derive(Copy, Clone, Debug, Default, PartialEq)] |
| 1310 | pub enum WindowBackgroundAppearance { |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | #[default] |
| 1319 | Opaque, |
| 1320 | |
| 1321 | Transparent, |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | Blurred, |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | #[derive(Clone, Debug)] |
| 1330 | pub struct PathPromptOptions { |
| 1331 | |
| 1332 | pub files: bool, |
| 1333 | |
| 1334 | pub directories: bool, |
| 1335 | |
| 1336 | pub multiple: bool, |
| 1337 | |
| 1338 | pub prompt: Option<SharedString>, |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | #[derive(Copy, Clone, Debug, PartialEq)] |
| 1343 | pub enum PromptLevel { |
| 1344 | |
| 1345 | Info, |
| 1346 | |
| 1347 | |
| 1348 | Warning, |
| 1349 | |
| 1350 | |
| 1351 | Critical, |
| 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | #[derive(Clone, Debug, PartialEq)] |
| 1356 | pub enum PromptButton { |
| 1357 | |
| 1358 | Ok(SharedString), |
| 1359 | |
| 1360 | Cancel(SharedString), |
| 1361 | |
| 1362 | Other(SharedString), |
| 1363 | } |
| 1364 | |
| 1365 | impl PromptButton { |
| 1366 | |
| 1367 | pub fn new(label: impl Into<SharedString>) -> Self { |
| 1368 | PromptButton::Other(label.into()) |
| 1369 | } |
| 1370 | |
| 1371 | |
| 1372 | pub fn ok(label: impl Into<SharedString>) -> Self { |
| 1373 | PromptButton::Ok(label.into()) |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | pub fn cancel(label: impl Into<SharedString>) -> Self { |
| 1378 | PromptButton::Cancel(label.into()) |
| 1379 | } |
| 1380 | |
| 1381 | #[allow(dead_code)] |
| 1382 | pub(crate) fn is_cancel(&self) -> bool { |
| 1383 | matches!(self, PromptButton::Cancel(_)) |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | pub fn label(&self) -> &SharedString { |
| 1388 | match self { |
| 1389 | PromptButton::Ok(label) => label, |
| 1390 | PromptButton::Cancel(label) => label, |
| 1391 | PromptButton::Other(label) => label, |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | impl From<&str> for PromptButton { |
| 1397 | fn from(value: &str) -> Self { |
| 1398 | match value.to_lowercase().as_str() { |
| 1399 | "ok" => PromptButton::Ok("Ok".into()), |
| 1400 | "cancel" => PromptButton::Cancel("Cancel".into()), |
| 1401 | _ => PromptButton::Other(SharedString::from(value.to_owned())), |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | |
| 1407 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] |
| 1408 | pub enum CursorStyle { |
| 1409 | |
| 1410 | Arrow, |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | IBeam, |
| 1415 | |
| 1416 | |
| 1417 | |
| 1418 | Crosshair, |
| 1419 | |
| 1420 | |
| 1421 | |
| 1422 | ClosedHand, |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | OpenHand, |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | PointingHand, |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | ResizeLeft, |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | ResizeRight, |
| 1439 | |
| 1440 | |
| 1441 | |
| 1442 | ResizeLeftRight, |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | ResizeUp, |
| 1447 | |
| 1448 | |
| 1449 | |
| 1450 | ResizeDown, |
| 1451 | |
| 1452 | |
| 1453 | |
| 1454 | ResizeUpDown, |
| 1455 | |
| 1456 | |
| 1457 | |
| 1458 | ResizeUpLeftDownRight, |
| 1459 | |
| 1460 | |
| 1461 | |
| 1462 | ResizeUpRightDownLeft, |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | ResizeColumn, |
| 1467 | |
| 1468 | |
| 1469 | |
| 1470 | ResizeRow, |
| 1471 | |
| 1472 | |
| 1473 | |
| 1474 | IBeamCursorForVerticalLayout, |
| 1475 | |
| 1476 | |
| 1477 | |
| 1478 | OperationNotAllowed, |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | DragLink, |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | DragCopy, |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | ContextualMenu, |
| 1491 | |
| 1492 | |
| 1493 | None, |
| 1494 | } |
| 1495 | |
| 1496 | impl Default for CursorStyle { |
| 1497 | fn default() -> Self { |
| 1498 | Self::Arrow |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1504 | pub struct ClipboardItem { |
| 1505 | entries: Vec<ClipboardEntry>, |
| 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1510 | pub enum ClipboardEntry { |
| 1511 | |
| 1512 | String(ClipboardString), |
| 1513 | |
| 1514 | Image(Image), |
| 1515 | } |
| 1516 | |
| 1517 | impl ClipboardItem { |
| 1518 | |
| 1519 | pub fn new_string(text: String) -> Self { |
| 1520 | Self { |
| 1521 | entries: vec![ClipboardEntry::String(ClipboardString::new(text))], |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | pub fn new_string_with_metadata(text: String, metadata: String) -> Self { |
| 1527 | Self { |
| 1528 | entries: vec![ClipboardEntry::String(ClipboardString { |
| 1529 | text, |
| 1530 | metadata: Some(metadata), |
| 1531 | })], |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | pub fn new_string_with_json_metadata<T: Serialize>(text: String, metadata: T) -> Self { |
| 1537 | Self { |
| 1538 | entries: vec![ClipboardEntry::String( |
| 1539 | ClipboardString::new(text).with_json_metadata(metadata), |
| 1540 | )], |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | pub fn new_image(image: &Image) -> Self { |
| 1546 | Self { |
| 1547 | entries: vec![ClipboardEntry::Image(image.clone())], |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | |
| 1552 | |
| 1553 | pub fn text(&self) -> Option<String> { |
| 1554 | let mut answer = String::new(); |
| 1555 | let mut any_entries = false; |
| 1556 | |
| 1557 | for entry in self.entries.iter() { |
| 1558 | if let ClipboardEntry::String(ClipboardString { text, metadata: _ }) = entry { |
| 1559 | answer.push_str(text); |
| 1560 | any_entries = true; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | if any_entries { Some(answer) } else { None } |
| 1565 | } |
| 1566 | |
| 1567 | |
| 1568 | #[cfg_attr(not(target_os = "windows"), allow(dead_code))] |
| 1569 | pub fn metadata(&self) -> Option<&String> { |
| 1570 | match self.entries().first() { |
| 1571 | Some(ClipboardEntry::String(clipboard_string)) if self.entries.len() == 1 => { |
| 1572 | clipboard_string.metadata.as_ref() |
| 1573 | } |
| 1574 | _ => None, |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | |
| 1579 | pub fn entries(&self) -> &[ClipboardEntry] { |
| 1580 | &self.entries |
| 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | pub fn into_entries(self) -> impl Iterator<Item = ClipboardEntry> { |
| 1585 | self.entries.into_iter() |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | impl From<ClipboardString> for ClipboardEntry { |
| 1590 | fn from(value: ClipboardString) -> Self { |
| 1591 | Self::String(value) |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | impl From<String> for ClipboardEntry { |
| 1596 | fn from(value: String) -> Self { |
| 1597 | Self::from(ClipboardString::from(value)) |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | impl From<Image> for ClipboardEntry { |
| 1602 | fn from(value: Image) -> Self { |
| 1603 | Self::Image(value) |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | impl From<ClipboardEntry> for ClipboardItem { |
| 1608 | fn from(value: ClipboardEntry) -> Self { |
| 1609 | Self { |
| 1610 | entries: vec![value], |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | impl From<String> for ClipboardItem { |
| 1616 | fn from(value: String) -> Self { |
| 1617 | Self::from(ClipboardEntry::from(value)) |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | impl From<Image> for ClipboardItem { |
| 1622 | fn from(value: Image) -> Self { |
| 1623 | Self::from(ClipboardEntry::from(value)) |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | #[derive(Clone, Copy, Debug, Eq, PartialEq, EnumIter, Hash)] |
| 1629 | pub enum ImageFormat { |
| 1630 | |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | Png, |
| 1635 | |
| 1636 | Jpeg, |
| 1637 | |
| 1638 | Webp, |
| 1639 | |
| 1640 | Gif, |
| 1641 | |
| 1642 | Svg, |
| 1643 | |
| 1644 | Bmp, |
| 1645 | |
| 1646 | Tiff, |
| 1647 | } |
| 1648 | |
| 1649 | impl ImageFormat { |
| 1650 | |
| 1651 | pub const fn mime_type(self) -> &'static str { |
| 1652 | match self { |
| 1653 | ImageFormat::Png => "image/png", |
| 1654 | ImageFormat::Jpeg => "image/jpeg", |
| 1655 | ImageFormat::Webp => "image/webp", |
| 1656 | ImageFormat::Gif => "image/gif", |
| 1657 | ImageFormat::Svg => "image/svg+xml", |
| 1658 | ImageFormat::Bmp => "image/bmp", |
| 1659 | ImageFormat::Tiff => "image/tiff", |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | |
| 1664 | pub fn from_mime_type(mime_type: &str) -> Option<Self> { |
| 1665 | match mime_type { |
| 1666 | "image/png" => Some(Self::Png), |
| 1667 | "image/jpeg" | "image/jpg" => Some(Self::Jpeg), |
| 1668 | "image/webp" => Some(Self::Webp), |
| 1669 | "image/gif" => Some(Self::Gif), |
| 1670 | "image/svg+xml" => Some(Self::Svg), |
| 1671 | "image/bmp" => Some(Self::Bmp), |
| 1672 | "image/tiff" | "image/tif" => Some(Self::Tiff), |
| 1673 | _ => None, |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | |
| 1679 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 1680 | pub struct Image { |
| 1681 | |
| 1682 | pub format: ImageFormat, |
| 1683 | |
| 1684 | pub bytes: Vec<u8>, |
| 1685 | |
| 1686 | id: u64, |
| 1687 | } |
| 1688 | |
| 1689 | impl Hash for Image { |
| 1690 | fn hash<H: Hasher>(&self, state: &mut H) { |
| 1691 | state.write_u64(self.id); |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | impl Image { |
| 1696 | |
| 1697 | pub fn empty() -> Self { |
| 1698 | Self::from_bytes(ImageFormat::Png, Vec::new()) |
| 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | pub fn from_bytes(format: ImageFormat, bytes: Vec<u8>) -> Self { |
| 1703 | Self { |
| 1704 | id: hash(&bytes), |
| 1705 | format, |
| 1706 | bytes, |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | |
| 1711 | pub fn id(&self) -> u64 { |
| 1712 | self.id |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | pub fn use_render_image( |
| 1717 | self: Arc<Self>, |
| 1718 | window: &mut Window, |
| 1719 | cx: &mut App, |
| 1720 | ) -> Option<Arc<RenderImage>> { |
| 1721 | ImageSource::Image(self) |
| 1722 | .use_data(None, window, cx) |
| 1723 | .and_then(|result| result.ok()) |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | pub fn get_render_image( |
| 1728 | self: Arc<Self>, |
| 1729 | window: &mut Window, |
| 1730 | cx: &mut App, |
| 1731 | ) -> Option<Arc<RenderImage>> { |
| 1732 | ImageSource::Image(self) |
| 1733 | .get_data(None, window, cx) |
| 1734 | .and_then(|result| result.ok()) |
| 1735 | } |
| 1736 | |
| 1737 | |
| 1738 | pub fn remove_asset(self: Arc<Self>, cx: &mut App) { |
| 1739 | ImageSource::Image(self).remove_asset(cx); |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | pub fn to_image_data(&self, svg_renderer: SvgRenderer) -> Result<Arc<RenderImage>> { |
| 1744 | fn frames_for_image( |
| 1745 | bytes: &[u8], |
| 1746 | format: image::ImageFormat, |
| 1747 | ) -> Result<SmallVec<[Frame; 1]>> { |
| 1748 | let mut data = image::load_from_memory_with_format(bytes, format)?.into_rgba8(); |
| 1749 | |
| 1750 | |
| 1751 | for pixel in data.chunks_exact_mut(4) { |
| 1752 | pixel.swap(0, 2); |
| 1753 | } |
| 1754 | |
| 1755 | Ok(SmallVec::from_elem(Frame::new(data), 1)) |
| 1756 | } |
| 1757 | |
| 1758 | let frames = match self.format { |
| 1759 | ImageFormat::Gif => { |
| 1760 | let decoder = GifDecoder::new(Cursor::new(&self.bytes))?; |
| 1761 | let mut frames = SmallVec::new(); |
| 1762 | |
| 1763 | for frame in decoder.into_frames() { |
| 1764 | let mut frame = frame?; |
| 1765 | |
| 1766 | for pixel in frame.buffer_mut().chunks_exact_mut(4) { |
| 1767 | pixel.swap(0, 2); |
| 1768 | } |
| 1769 | frames.push(frame); |
| 1770 | } |
| 1771 | |
| 1772 | frames |
| 1773 | } |
| 1774 | ImageFormat::Png => frames_for_image(&self.bytes, image::ImageFormat::Png)?, |
| 1775 | ImageFormat::Jpeg => frames_for_image(&self.bytes, image::ImageFormat::Jpeg)?, |
| 1776 | ImageFormat::Webp => frames_for_image(&self.bytes, image::ImageFormat::WebP)?, |
| 1777 | ImageFormat::Bmp => frames_for_image(&self.bytes, image::ImageFormat::Bmp)?, |
| 1778 | ImageFormat::Tiff => frames_for_image(&self.bytes, image::ImageFormat::Tiff)?, |
| 1779 | ImageFormat::Svg => { |
| 1780 | let pixmap = svg_renderer.render_pixmap(&self.bytes, SvgSize::ScaleFactor(1.0))?; |
| 1781 | |
| 1782 | let buffer = |
| 1783 | image::ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.take()) |
| 1784 | .unwrap(); |
| 1785 | |
| 1786 | SmallVec::from_elem(Frame::new(buffer), 1) |
| 1787 | } |
| 1788 | }; |
| 1789 | |
| 1790 | Ok(Arc::new(RenderImage::new(frames))) |
| 1791 | } |
| 1792 | |
| 1793 | |
| 1794 | pub fn format(&self) -> ImageFormat { |
| 1795 | self.format |
| 1796 | } |
| 1797 | |
| 1798 | |
| 1799 | pub fn bytes(&self) -> &[u8] { |
| 1800 | self.bytes.as_slice() |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | |
| 1805 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 1806 | pub struct ClipboardString { |
| 1807 | pub(crate) text: String, |
| 1808 | pub(crate) metadata: Option<String>, |
| 1809 | } |
| 1810 | |
| 1811 | impl ClipboardString { |
| 1812 | |
| 1813 | pub fn new(text: String) -> Self { |
| 1814 | Self { |
| 1815 | text, |
| 1816 | metadata: None, |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | |
| 1822 | pub fn with_json_metadata<T: Serialize>(mut self, metadata: T) -> Self { |
| 1823 | self.metadata = Some(serde_json::to_string(&metadata).unwrap()); |
| 1824 | self |
| 1825 | } |
| 1826 | |
| 1827 | |
| 1828 | pub fn text(&self) -> &String { |
| 1829 | &self.text |
| 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | pub fn into_text(self) -> String { |
| 1834 | self.text |
| 1835 | } |
| 1836 | |
| 1837 | |
| 1838 | pub fn metadata_json<T>(&self) -> Option<T> |
| 1839 | where |
| 1840 | T: for<'a> Deserialize<'a>, |
| 1841 | { |
| 1842 | self.metadata |
| 1843 | .as_ref() |
| 1844 | .and_then(|m| serde_json::from_str(m).ok()) |
| 1845 | } |
| 1846 | |
| 1847 | #[cfg_attr(any(target_os = "linux", target_os = "freebsd"), allow(dead_code))] |
| 1848 | pub(crate) fn text_hash(text: &str) -> u64 { |
| 1849 | let mut hasher = SeaHasher::new(); |
| 1850 | text.hash(&mut hasher); |
| 1851 | hasher.finish() |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | impl From<String> for ClipboardString { |
| 1856 | fn from(value: String) -> Self { |
| 1857 | Self { |
| 1858 | text: value, |
| 1859 | metadata: None, |
| 1860 | } |
| 1861 | } |
| 1862 | } |