Skip to main content

euv_core/system/capabilities/
impl.rs

1use super::*;
2
3/// Safe optional accessors for `LiveActivityContent`.
4impl LiveActivityContent {
5    /// Returns the optional progress percentage in `0..=100`.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Live activity content reference.
10    ///
11    /// # Returns
12    ///
13    /// - `Option<u8>` - Clamped progress percentage, if set.
14    #[must_use]
15    pub fn progress(&self) -> Option<u8> {
16        let Self {
17            progress_percent, ..
18        } = self;
19        progress_percent.map(|v: u8| v.min(100))
20    }
21}
22
23/// Safe optional accessors for `SystemNotification`.
24impl SystemNotification {
25    /// Returns the optional app icon badge count.
26    ///
27    /// # Arguments
28    ///
29    /// - `&Self` - System notification reference.
30    ///
31    /// # Returns
32    ///
33    /// - `Option<u32>` - Badge count if specified.
34    #[must_use]
35    pub fn badge(&self) -> Option<u32> {
36        let Self { badge_count, .. } = self;
37        *badge_count
38    }
39}
40
41/// Platform-aware Dynamic Island, permissions, haptics, tray, sharing, and A11y dispatch for `PlatformServiceHub`.
42impl PlatformServiceHub {
43    /// Resolves the optimal live-activity presentation surface for `platform`.
44    ///
45    /// # Arguments
46    ///
47    /// - `PlatformKind` - Target execution platform.
48    ///
49    /// # Returns
50    ///
51    /// - `IslandPresentationMode` - Native Dynamic Island or graceful platform fallback mode.
52    #[must_use]
53    pub const fn default_island_mode(platform: PlatformKind) -> IslandPresentationMode {
54        match platform {
55            PlatformKind::Ios => IslandPresentationMode::Compact,
56            PlatformKind::IpadOs => IslandPresentationMode::LockScreen,
57            PlatformKind::Android | PlatformKind::HarmonyOs | PlatformKind::Wearable => {
58                IslandPresentationMode::FallbackNotification
59            }
60            PlatformKind::MacOs
61            | PlatformKind::Windows
62            | PlatformKind::Linux
63            | PlatformKind::Web
64            | PlatformKind::MiniApp
65            | PlatformKind::QuickApp
66            | PlatformKind::Terminal
67            | PlatformKind::TerminalCli
68            | PlatformKind::Headless => IslandPresentationMode::FallbackStatusBar,
69        }
70    }
71
72    /// Starts a Dynamic Island / Live Activity session on iOS/iPadOS or a graceful fallback on other platforms.
73    ///
74    /// # Arguments
75    ///
76    /// - `&Self` - Mutable service hub.
77    /// - `String` - Unique activity identifier.
78    /// - `LiveActivityContent` - Declarative compact, minimal, and expanded content payload.
79    ///
80    /// # Returns
81    ///
82    /// - `LiveActivityHandle` - Handle describing the active live activity and resolved presentation mode.
83    pub fn start_live_activity(
84        &mut self,
85        activity_id: String,
86        content: LiveActivityContent,
87    ) -> LiveActivityHandle {
88        let platform: PlatformKind = self.get_platform();
89        let mode: IslandPresentationMode = Self::default_island_mode(platform);
90        let handle: LiveActivityHandle =
91            LiveActivityHandle::new(activity_id.clone(), platform, mode, content, true);
92        self.get_mut_live_activities()
93            .retain(|existing: &LiveActivityHandle| existing.get_activity_id() != &activity_id);
94        self.get_mut_live_activities().push(handle.clone());
95        let _: bool = dispatch_native_action(
96            NATIVE_ACTION_ISLAND,
97            handle.get_content().get_title(),
98            handle.get_content().get_subtitle(),
99        );
100        handle
101    }
102
103    /// Updates an active Dynamic Island / Live Activity in place.
104    ///
105    /// # Arguments
106    ///
107    /// - `&Self` - Mutable service hub.
108    /// - `&str` - Activity identifier.
109    /// - `LiveActivityContent` - Updated content payload.
110    ///
111    /// # Returns
112    ///
113    /// - `bool` - `true` when an active session matching `activity_id` was updated.
114    pub fn update_live_activity(
115        &mut self,
116        activity_id: &str,
117        content: LiveActivityContent,
118    ) -> bool {
119        for item in self.get_mut_live_activities().iter_mut() {
120            if item.get_activity_id() == activity_id && item.get_active() {
121                item.set_content(content);
122                return true;
123            }
124        }
125        false
126    }
127
128    /// Ends an active Dynamic Island / Live Activity session.
129    ///
130    /// # Arguments
131    ///
132    /// - `&Self` - Mutable service hub.
133    /// - `&str` - Activity identifier.
134    ///
135    /// # Returns
136    ///
137    /// - `bool` - `true` when an active session was terminated.
138    pub fn end_live_activity(&mut self, activity_id: &str) -> bool {
139        for item in self.get_mut_live_activities().iter_mut() {
140            if item.get_activity_id() == activity_id && item.get_active() {
141                item.set_active(false);
142                return true;
143            }
144        }
145        false
146    }
147
148    /// Triggers a tactile haptic pulse when supported by the active platform.
149    ///
150    /// # Arguments
151    ///
152    /// - `&Self` - Mutable service hub.
153    /// - `HapticStyle` - Requested tactile waveform.
154    ///
155    /// # Returns
156    ///
157    /// - `bool` - `true` when dispatched on a haptic-capable platform.
158    pub fn trigger_haptic(&mut self, style: HapticStyle) -> bool {
159        let _: HapticStyle = style;
160        let caps: PlatformCapabilities = PlatformCapabilities::for_platform(self.get_platform());
161        if !caps.get_haptics() {
162            return false;
163        }
164        let next: u64 = self.get_haptic_count().saturating_add(1);
165        self.set_haptic_count(next);
166        let _: bool = dispatch_native_action(NATIVE_ACTION_HAPTIC, "impact", "");
167        true
168    }
169
170    /// Returns the native biometric authentication provider available on the active platform.
171    ///
172    /// # Arguments
173    ///
174    /// - `&Self` - Service hub reference.
175    ///
176    /// # Returns
177    ///
178    /// - `BiometricKind` - Platform biometric authenticator kind.
179    #[must_use]
180    pub fn biometric_kind(&self) -> BiometricKind {
181        match self.get_platform() {
182            PlatformKind::Ios | PlatformKind::IpadOs => BiometricKind::FaceId,
183            PlatformKind::MacOs => BiometricKind::TouchId,
184            PlatformKind::Android | PlatformKind::HarmonyOs => BiometricKind::Fingerprint,
185            PlatformKind::Windows => BiometricKind::WindowsHello,
186            PlatformKind::Web => BiometricKind::Passkey,
187            PlatformKind::Linux
188            | PlatformKind::Wearable
189            | PlatformKind::MiniApp
190            | PlatformKind::QuickApp
191            | PlatformKind::Terminal
192            | PlatformKind::TerminalCli
193            | PlatformKind::Headless => BiometricKind::None,
194        }
195    }
196
197    /// Installs or updates the desktop system tray / menu bar configuration.
198    ///
199    /// # Arguments
200    ///
201    /// - `&Self` - Mutable service hub.
202    /// - `SystemTrayConfig` - Tray icon tooltip, title, and menu items.
203    ///
204    /// # Returns
205    ///
206    /// - `Result<(), PlatformError>` - `Ok(())` on desktop targets, or `Unsupported` on mobile/Web.
207    pub fn configure_tray(&mut self, config: SystemTrayConfig) -> Result<(), PlatformError> {
208        let caps: PlatformCapabilities = PlatformCapabilities::for_platform(self.get_platform());
209        if !caps.get_system_tray() {
210            return Err(PlatformError::Unsupported(format!(
211                "System tray is unsupported on {}",
212                self.get_platform().as_str()
213            )));
214        }
215        self.set_tray(Some(config));
216        Ok(())
217    }
218
219    /// Returns the active desktop tray configuration, if installed.
220    ///
221    /// # Arguments
222    ///
223    /// - `&Self` - Service hub reference.
224    ///
225    /// # Returns
226    ///
227    /// - `Option<&SystemTrayConfig>` - Configured tray metadata.
228    #[must_use]
229    pub fn tray_config(&self) -> Option<&SystemTrayConfig> {
230        let Self { tray, .. } = self;
231        tray.as_ref()
232    }
233
234    /// Dispatches a native OS notification and records it in telemetry.
235    ///
236    /// # Arguments
237    ///
238    /// - `&Self` - Mutable service hub.
239    /// - `SystemNotification` - Notification payload.
240    pub fn send_notification(&mut self, notification: SystemNotification) {
241        let _: bool = dispatch_native_action(
242            NATIVE_ACTION_NOTIFY,
243            notification.get_title(),
244            notification.get_body(),
245        );
246        self.get_mut_notifications().push(notification);
247    }
248
249    /// Queues a screen reader accessibility live announcement.
250    ///
251    /// # Arguments
252    ///
253    /// - `&Self` - Mutable service hub.
254    /// - `String` - Message text to announce.
255    /// - `A11yLiveRegion` - Politeness priority.
256    pub fn announce_a11y(&mut self, message: String, politeness: A11yLiveRegion) {
257        self.get_mut_announcements()
258            .push(A11yAnnouncement::new(message, politeness));
259    }
260
261    /// Resolves the native OS share sheet API descriptor for `payload`.
262    ///
263    /// # Arguments
264    ///
265    /// - `&Self` - Service hub reference.
266    /// - `&SharePayload` - Share sheet title, text, and optional URL.
267    ///
268    /// # Returns
269    ///
270    /// - `Result<String, PlatformError>` - Native OS share dispatcher description.
271    pub fn share(&self, payload: &SharePayload) -> Result<String, PlatformError> {
272        let api: &str = match self.get_platform() {
273            PlatformKind::Ios | PlatformKind::IpadOs => "UIActivityViewController",
274            PlatformKind::MacOs => "NSSharingServicePicker",
275            PlatformKind::Android => "android.content.Intent.ACTION_SEND",
276            PlatformKind::HarmonyOs => SHARE_API_HARMONY,
277            PlatformKind::Wearable => SHARE_API_WEARABLE,
278            PlatformKind::MiniApp => SHARE_API_MINIAPP,
279            PlatformKind::QuickApp => SHARE_API_QUICKAPP,
280            PlatformKind::Terminal | PlatformKind::TerminalCli => SHARE_API_TERMINAL,
281            PlatformKind::Windows => "Windows.ApplicationModel.DataTransfer.DataTransferManager",
282            PlatformKind::Linux => "org.freedesktop.portal.OpenURI",
283            PlatformKind::Web | PlatformKind::Headless => "navigator.share",
284        };
285        Ok(format!("{api}: {}", payload.get_title()))
286    }
287}