From 06a19e59ada8d932d68987206e704fe662cc8bdd Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Thu, 12 Jun 2025 14:25:16 +0200 Subject: [PATCH] slint: Change Platform.os to return a new enum OperatingSystemType (#8679) * slint: Change Platform.os to return a new enum OperatingSystemType cc #8631 --- api/cpp/include/slint.h | 7 ---- api/cpp/lib.rs | 5 +-- .../reference/global-namespaces/platform.mdx | 13 ++----- internal/common/enums.rs | 17 +++++++++ internal/compiler/builtins.slint | 3 +- internal/compiler/expression_tree.rs | 4 ++- internal/compiler/generator/cpp.rs | 2 +- internal/core/lib.rs | 35 +++++++++---------- tests/cases/platform.slint | 18 +++++++++- 9 files changed, 61 insertions(+), 43 deletions(-) diff --git a/api/cpp/include/slint.h b/api/cpp/include/slint.h index 965eac7c6..2477663d3 100644 --- a/api/cpp/include/slint.h +++ b/api/cpp/include/slint.h @@ -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 { diff --git a/api/cpp/lib.rs b/api/cpp/lib.rs index 983fc613d..93a3a49ae 100644 --- a/api/cpp/lib.rs +++ b/api/cpp/lib.rs @@ -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() } diff --git a/docs/astro/src/content/docs/reference/global-namespaces/platform.mdx b/docs/astro/src/content/docs/reference/global-namespaces/platform.mdx index 56a8515d3..5b288ac83 100644 --- a/docs/astro/src/content/docs/reference/global-namespaces/platform.mdx +++ b/docs/astro/src/content/docs/reference/global-namespaces/platform.mdx @@ -12,18 +12,9 @@ The **Platform** namespace contains properties that help deal with platform spec ## Properties ### os - -The name of the operating system. Possible values are: + -| 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. diff --git a/internal/common/enums.rs b/internal/common/enums.rs index 8e8f6f0d7..c624b9e1a 100644 --- a/internal/common/enums.rs +++ b/internal/common/enums.rs @@ -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, + } ]; }; } diff --git a/internal/compiler/builtins.slint b/internal/compiler/builtins.slint index 323e12acf..531f7ce3e 100644 --- a/internal/compiler/builtins.slint +++ b/internal/compiler/builtins.slint @@ -521,7 +521,7 @@ export global TextInputInterface { } export global Platform { - out property os; + out property os; out property style-name; } @@ -732,4 +732,3 @@ export global NativePalette { //-is_non_item_type //-is_internal } - diff --git a/internal/compiler/expression_tree.rs b/internal/compiler/expression_tree.rs index 35e0737bf..02a6e0e3c 100644 --- a/internal/compiler/expression_tree.rs +++ b/internal/compiler/expression_tree.rs @@ -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 { diff --git a/internal/compiler/generator/cpp.rs b/internal/compiler/generator/cpp.rs index b263cc2dc..9c911230d 100644 --- a/internal/compiler/generator/cpp.rs +++ b/internal/compiler/generator/cpp.rs @@ -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()") } } diff --git a/internal/core/lib.rs b/internal/core/lib.rs index 3f7ee29b4..d65850963 100644 --- a/internal/core/lib.rs +++ b/internal/core/lib.rs @@ -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() } diff --git a/tests/cases/platform.slint b/tests/cases/platform.slint index 14bf61065..776f28b70 100644 --- a/tests/cases/platform.slint +++ b/tests/cases/platform.slint @@ -5,7 +5,23 @@ // to fluent. export component TestCase inherits Window { - out property os: Platform.os; + out property 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 style-name: Platform.style-name; }