Skip to main content

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#![cfg_attr(docsrs, feature(doc_cfg))]
9#![deny(unsafe_code)]
10#![cfg_attr(slint_nightly_test, feature(non_exhaustive_omitted_patterns_lint))]
11#![cfg_attr(slint_nightly_test, warn(non_exhaustive_omitted_patterns))]
12#![no_std]
13#![debugger_visualizer(gdb_script_file = "gdb_pretty_printers.py")]
14
15extern crate alloc;
16#[cfg(feature = "std")]
17extern crate std;
18
19#[cfg(all(not(feature = "std"), feature = "unsafe-single-threaded"))]
20pub mod unsafe_single_threaded;
21#[cfg(all(not(feature = "std"), not(feature = "unsafe-single-threaded")))]
22compile_error!(
23    "At least one of the following feature need to be enabled: `std` or `unsafe-single-threaded`"
24);
25use crate::items::OperatingSystemType;
26#[cfg(all(not(feature = "std"), feature = "unsafe-single-threaded"))]
27pub use crate::unsafe_single_threaded::thread_local;
28#[cfg(feature = "std")]
29pub use std::thread_local;
30
31pub mod accessibility;
32pub mod animations;
33pub mod api;
34pub mod callbacks;
35pub mod component_factory;
36pub mod context;
37pub mod date_time;
38pub mod future;
39pub mod graphics;
40pub mod input;
41pub mod item_focus;
42pub mod item_rendering;
43pub mod item_tree;
44pub mod items;
45pub mod layout;
46pub mod lengths;
47pub mod menus;
48pub mod model;
49pub mod partial_renderer;
50pub mod platform;
51pub mod properties;
52pub mod renderer;
53#[cfg(feature = "rtti")]
54pub mod rtti;
55pub mod sharedvector;
56pub mod slice;
57pub mod string;
58pub mod styled_text;
59pub mod tests;
60pub mod textlayout;
61pub mod timers;
62pub mod translations;
63pub mod window;
64
65#[doc(inline)]
66pub use string::SharedString;
67
68#[doc(inline)]
69pub use sharedvector::SharedVector;
70
71#[doc(inline)]
72pub use graphics::{ImageInner, StaticTextures};
73
74#[doc(inline)]
75pub use properties::Property;
76
77#[doc(inline)]
78pub use callbacks::Callback;
79
80#[doc(inline)]
81pub use graphics::Color;
82
83#[doc(inline)]
84pub use graphics::Brush;
85
86#[doc(inline)]
87pub use graphics::RgbaColor;
88
89#[cfg(feature = "std")]
90#[doc(inline)]
91pub use graphics::PathData;
92
93#[doc(inline)]
94pub use graphics::BorderRadius;
95
96pub use context::{SlintContext, with_global_context};
97
98#[cfg(not(slint_int_coord))]
99pub type Coord = f32;
100#[cfg(slint_int_coord)]
101pub type Coord = i32;
102
103/// This type is not exported from the public API crate, so function having this
104/// parameter cannot be called from the public API without naming it
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub struct InternalToken;
107
108#[cfg(feature = "std")]
109thread_local!(
110    /// Permit testing code to force an OS type
111    pub static OPERATING_SYSTEM_OVERRIDE: core::cell::Cell<Option<OperatingSystemType>> =
112        Default::default();
113);
114
115#[cfg(not(target_family = "wasm"))]
116pub fn detect_operating_system() -> OperatingSystemType {
117    #[cfg(feature = "std")]
118    if let Some(os_override) = OPERATING_SYSTEM_OVERRIDE.with(|os_override| os_override.get()) {
119        return os_override;
120    }
121
122    if cfg!(target_os = "android") {
123        OperatingSystemType::Android
124    } else if cfg!(target_os = "ios") {
125        OperatingSystemType::Ios
126    } else if cfg!(target_os = "macos") {
127        OperatingSystemType::Macos
128    } else if cfg!(target_os = "windows") {
129        OperatingSystemType::Windows
130    } else if cfg!(target_os = "linux") {
131        OperatingSystemType::Linux
132    } else {
133        OperatingSystemType::Other
134    }
135}
136
137#[cfg(target_family = "wasm")]
138pub fn detect_operating_system() -> OperatingSystemType {
139    if let Some(os_override) = OPERATING_SYSTEM_OVERRIDE.with(|os_override| os_override.get()) {
140        return os_override;
141    }
142
143    let mut user_agent =
144        web_sys::window().and_then(|w| w.navigator().user_agent().ok()).unwrap_or_default();
145    user_agent.make_ascii_lowercase();
146    let mut platform =
147        web_sys::window().and_then(|w| w.navigator().platform().ok()).unwrap_or_default();
148    platform.make_ascii_lowercase();
149
150    if user_agent.contains("ipad") || user_agent.contains("iphone") {
151        OperatingSystemType::Ios
152    } else if user_agent.contains("android") {
153        OperatingSystemType::Android
154    } else if platform.starts_with("mac") {
155        OperatingSystemType::Macos
156    } else if platform.starts_with("win") {
157        OperatingSystemType::Windows
158    } else if platform.starts_with("linux") {
159        OperatingSystemType::Linux
160    } else {
161        OperatingSystemType::Other
162    }
163}
164
165/// Returns true if the current platform is an Apple platform (macOS, iOS, iPadOS)
166pub fn is_apple_platform() -> bool {
167    matches!(detect_operating_system(), OperatingSystemType::Macos | OperatingSystemType::Ios)
168}