i_slint_core/
lib.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4// cSpell: ignore sharedvector textlayout
5
6#![doc = include_str!("README.md")]
7#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
8#![deny(unsafe_code)]
9#![no_std]
10
11extern crate alloc;
12#[cfg(feature = "std")]
13extern crate std;
14
15#[cfg(all(not(feature = "std"), feature = "unsafe-single-threaded"))]
16pub(crate) mod unsafe_single_threaded;
17#[cfg(all(not(feature = "std"), not(feature = "unsafe-single-threaded")))]
18compile_error!(
19    "At least one of the following feature need to be enabled: `std` or `unsafe-single-threaded`"
20);
21use crate::items::OperatingSystemType;
22#[cfg(all(not(feature = "std"), feature = "unsafe-single-threaded"))]
23use crate::unsafe_single_threaded::thread_local;
24#[cfg(feature = "std")]
25use std::thread_local;
26
27pub mod accessibility;
28pub mod animations;
29pub mod api;
30pub mod callbacks;
31pub mod component_factory;
32pub mod context;
33pub mod date_time;
34pub mod future;
35pub mod graphics;
36pub mod input;
37pub mod item_focus;
38pub mod item_rendering;
39pub mod item_tree;
40pub mod items;
41pub mod layout;
42pub mod lengths;
43pub mod menus;
44pub mod model;
45pub mod partial_renderer;
46pub mod platform;
47pub mod properties;
48pub mod renderer;
49#[cfg(feature = "rtti")]
50pub mod rtti;
51pub mod sharedvector;
52pub mod slice;
53#[cfg(feature = "software-renderer")]
54pub mod software_renderer;
55pub mod string;
56pub mod tests;
57pub mod textlayout;
58pub mod timers;
59pub mod translations;
60pub mod window;
61
62#[doc(inline)]
63pub use string::SharedString;
64
65#[doc(inline)]
66pub use sharedvector::SharedVector;
67
68#[doc(inline)]
69pub use graphics::{ImageInner, StaticTextures};
70
71#[doc(inline)]
72pub use properties::Property;
73
74#[doc(inline)]
75pub use callbacks::Callback;
76
77#[doc(inline)]
78pub use graphics::Color;
79
80#[doc(inline)]
81pub use graphics::Brush;
82
83#[doc(inline)]
84pub use graphics::RgbaColor;
85
86#[cfg(feature = "std")]
87#[doc(inline)]
88pub use graphics::PathData;
89
90#[doc(inline)]
91pub use graphics::BorderRadius;
92
93pub use context::{with_global_context, SlintContext};
94
95#[cfg(not(slint_int_coord))]
96pub type Coord = f32;
97#[cfg(slint_int_coord)]
98pub type Coord = i32;
99
100/// This type is not exported from the public API crate, so function having this
101/// parameter cannot be called from the public API without naming it
102pub struct InternalToken;
103
104#[cfg(not(target_family = "wasm"))]
105pub fn detect_operating_system() -> OperatingSystemType {
106    if cfg!(target_os = "android") {
107        OperatingSystemType::Android
108    } else if cfg!(target_os = "ios") {
109        OperatingSystemType::Ios
110    } else if cfg!(target_os = "macos") {
111        OperatingSystemType::Macos
112    } else if cfg!(target_os = "windows") {
113        OperatingSystemType::Windows
114    } else if cfg!(target_os = "linux") {
115        OperatingSystemType::Linux
116    } else {
117        OperatingSystemType::Other
118    }
119}
120
121#[cfg(target_family = "wasm")]
122pub fn detect_operating_system() -> OperatingSystemType {
123    let mut user_agent =
124        web_sys::window().and_then(|w| w.navigator().user_agent().ok()).unwrap_or_default();
125    user_agent.make_ascii_lowercase();
126    let mut platform =
127        web_sys::window().and_then(|w| w.navigator().platform().ok()).unwrap_or_default();
128    platform.make_ascii_lowercase();
129
130    if user_agent.contains("ipad") || user_agent.contains("iphone") {
131        OperatingSystemType::Ios
132    } else if user_agent.contains("android") {
133        OperatingSystemType::Android
134    } else if platform.starts_with("mac") {
135        OperatingSystemType::Macos
136    } else if platform.starts_with("win") {
137        OperatingSystemType::Windows
138    } else if platform.starts_with("linux") {
139        OperatingSystemType::Linux
140    } else {
141        OperatingSystemType::Other
142    }
143}
144
145/// Returns true if the current platform is an Apple platform (macOS, iOS, iPadOS)
146pub fn is_apple_platform() -> bool {
147    matches!(detect_operating_system(), OperatingSystemType::Macos | OperatingSystemType::Ios)
148}