slint: Change Platform.os to return a new enum OperatingSystemType (#8679)

* slint: Change Platform.os to return a new enum OperatingSystemType

cc #8631
This commit is contained in:
Simon Hausmann 2025-06-12 14:25:16 +02:00 committed by GitHub
parent 2d6ebc7df7
commit 06a19e59ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 61 additions and 43 deletions

View file

@ -91,13 +91,6 @@ inline void debug(const SharedString &str)
cbindgen_private::slint_debug(&str);
}
inline SharedString detect_operating_system()
{
SharedString result;
cbindgen_private::slint_detect_operating_system(&result);
return result;
}
} // namespace private_api
namespace cbindgen_private {

View file

@ -10,6 +10,7 @@ extern crate std;
use alloc::rc::Rc;
use core::ffi::c_void;
use i_slint_core::items::OperatingSystemType;
use i_slint_core::window::{ffi::WindowAdapterRcOpaque, WindowAdapter};
use i_slint_core::SharedString;
@ -226,6 +227,6 @@ pub unsafe extern "C" fn slint_set_xdg_app_id(_app_id: &SharedString) {
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn slint_detect_operating_system(out: &mut SharedString) {
*out = i_slint_core::detect_operating_system();
pub unsafe extern "C" fn slint_detect_operating_system() -> OperatingSystemType {
i_slint_core::detect_operating_system()
}

View file

@ -12,18 +12,9 @@ The **Platform** namespace contains properties that help deal with platform spec
## Properties
### os
<SlintProperty propName="os" typeName="string">
The name of the operating system. Possible values are:
<SlintProperty propName="os" typeName="enum" enumName="OperatingSystemType">
| Operating System | Value |
|------------------|-----------|
| Windows | `windows` |
| macOS | `macos` |
| Linux | `linux` |
| Android | `android` |
| iOS | `ios` |
If the operating system is none of the above, the value of this property is the empty string.
This property holds the type of the operating system detected at run-time.
:::note{Note}
When running in a web browser, the value of this property is computed at run-time by querying the web browser's navigator properties.

View file

@ -474,6 +474,23 @@ macro_rules! for_each_enums {
/// The stroke ends with a square projection beyond the path.
Square,
}
/// This enum describes the detected operating system types.
#[non_exhaustive]
enum OperatingSystemType {
/// This variant includes any version of Android running mobile phones, tablets, as well as embedded Android devices.
Android,
/// This variant covers iOS running on iPhones and iPads.
Ios,
/// This variant covers macOS running on Apple's Mac computers.
Macos,
/// This variant covers any version of Linux, except Android.
Linux,
/// This variant covers Microsoft Windows.
Windows,
/// This variant is reported when the operating system is none of the above.
Other,
}
];
};
}

View file

@ -521,7 +521,7 @@ export global TextInputInterface {
}
export global Platform {
out property <string> os;
out property <OperatingSystemType> os;
out property <string> style-name;
}
@ -732,4 +732,3 @@ export global NativePalette {
//-is_non_item_type
//-is_internal
}

View file

@ -260,7 +260,9 @@ declare_builtin_function_types!(
Translate: (Type::String, Type::String, Type::String, Type::Array(Type::String.into())) -> Type::String,
Use24HourFormat: () -> Type::Bool,
UpdateTimers: () -> Type::Void,
DetectOperatingSystem: () -> Type::String,
DetectOperatingSystem: () -> Type::Enumeration(
typeregister::BUILTIN.with(|e| e.enums.OperatingSystemType.clone()),
),
);
impl BuiltinFunction {

View file

@ -3992,7 +3992,7 @@ fn compile_builtin_function_call(
"self->update_timers()".into()
}
BuiltinFunction::DetectOperatingSystem => {
format!("slint::private_api::detect_operating_system()")
format!("slint::cbindgen_private::slint_detect_operating_system()")
}
}

View file

@ -18,6 +18,7 @@ pub(crate) mod unsafe_single_threaded;
compile_error!(
"At least one of the following feature need to be enabled: `std` or `unsafe-single-threaded`"
);
use crate::items::OperatingSystemType;
#[cfg(all(not(feature = "std"), feature = "unsafe-single-threaded"))]
use crate::unsafe_single_threaded::thread_local;
#[cfg(feature = "std")]
@ -100,25 +101,24 @@ pub type Coord = i32;
pub struct InternalToken;
#[cfg(not(target_family = "wasm"))]
pub fn detect_operating_system() -> SharedString {
let os = if cfg!(target_os = "android") {
"android"
pub fn detect_operating_system() -> OperatingSystemType {
if cfg!(target_os = "android") {
OperatingSystemType::Android
} else if cfg!(target_os = "ios") {
"ios"
OperatingSystemType::Ios
} else if cfg!(target_os = "macos") {
"macos"
OperatingSystemType::Macos
} else if cfg!(target_os = "windows") {
"windows"
OperatingSystemType::Windows
} else if cfg!(target_os = "linux") {
"linux"
OperatingSystemType::Linux
} else {
""
};
os.into()
OperatingSystemType::Other
}
}
#[cfg(target_family = "wasm")]
pub fn detect_operating_system() -> SharedString {
pub fn detect_operating_system() -> OperatingSystemType {
let mut user_agent =
web_sys::window().and_then(|w| w.navigator().user_agent().ok()).unwrap_or_default();
user_agent.make_ascii_lowercase();
@ -127,17 +127,16 @@ pub fn detect_operating_system() -> SharedString {
platform.make_ascii_lowercase();
if user_agent.contains("ipad") || user_agent.contains("iphone") {
"ios"
OperatingSystemType::Ios
} else if user_agent.contains("android") {
"android"
OperatingSystemType::Android
} else if platform.starts_with("mac") {
"macos"
OperatingSystemType::Macos
} else if platform.starts_with("win") {
"windows"
OperatingSystemType::Windows
} else if platform.starts_with("linux") {
"linux"
OperatingSystemType::Linux
} else {
""
OperatingSystemType::Other
}
.into()
}

View file

@ -5,7 +5,23 @@
// to fluent.
export component TestCase inherits Window {
out property <string> os: Platform.os;
out property <string> os: {
if Platform.os == OperatingSystemType.android {
"android"
} else if Platform.os == OperatingSystemType.ios {
"ios"
} else if Platform.os == OperatingSystemType.macos {
"macos"
} else if Platform.os == OperatingSystemType.linux {
"linux"
} else if Platform.os == OperatingSystemType.windows {
"windows"
} else if Platform.os == OperatingSystemType.other {
"other"
} else {
"error"
}
}
out property <string> style-name: Platform.style-name;
}