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 => IslandPresentationMode::FallbackNotification,
58            PlatformKind::MacOs
59            | PlatformKind::Windows
60            | PlatformKind::Linux
61            | PlatformKind::Web
62            | PlatformKind::Headless => IslandPresentationMode::FallbackStatusBar,
63        }
64    }
65
66    /// Starts a Dynamic Island / Live Activity session on iOS/iPadOS or a graceful fallback on other platforms.
67    ///
68    /// # Arguments
69    ///
70    /// - `&Self` - Mutable service hub.
71    /// - `String` - Unique activity identifier.
72    /// - `LiveActivityContent` - Declarative compact, minimal, and expanded content payload.
73    ///
74    /// # Returns
75    ///
76    /// - `LiveActivityHandle` - Handle describing the active live activity and resolved presentation mode.
77    pub fn start_live_activity(
78        &mut self,
79        activity_id: String,
80        content: LiveActivityContent,
81    ) -> LiveActivityHandle {
82        let platform: PlatformKind = self.get_platform();
83        let mode: IslandPresentationMode = Self::default_island_mode(platform);
84        let handle: LiveActivityHandle =
85            LiveActivityHandle::new(activity_id.clone(), platform, mode, content, true);
86        self.get_mut_live_activities()
87            .retain(|existing: &LiveActivityHandle| existing.get_activity_id() != &activity_id);
88        self.get_mut_live_activities().push(handle.clone());
89        let _: bool = dispatch_native_action(
90            NATIVE_ACTION_ISLAND,
91            handle.get_content().get_title(),
92            handle.get_content().get_subtitle(),
93        );
94        handle
95    }
96
97    /// Updates an active Dynamic Island / Live Activity in place.
98    ///
99    /// # Arguments
100    ///
101    /// - `&Self` - Mutable service hub.
102    /// - `&str` - Activity identifier.
103    /// - `LiveActivityContent` - Updated content payload.
104    ///
105    /// # Returns
106    ///
107    /// - `bool` - `true` when an active session matching `activity_id` was updated.
108    pub fn update_live_activity(
109        &mut self,
110        activity_id: &str,
111        content: LiveActivityContent,
112    ) -> bool {
113        for item in self.get_mut_live_activities().iter_mut() {
114            if item.get_activity_id() == activity_id && item.get_active() {
115                item.set_content(content);
116                return true;
117            }
118        }
119        false
120    }
121
122    /// Ends an active Dynamic Island / Live Activity session.
123    ///
124    /// # Arguments
125    ///
126    /// - `&Self` - Mutable service hub.
127    /// - `&str` - Activity identifier.
128    ///
129    /// # Returns
130    ///
131    /// - `bool` - `true` when an active session was terminated.
132    pub fn end_live_activity(&mut self, activity_id: &str) -> bool {
133        for item in self.get_mut_live_activities().iter_mut() {
134            if item.get_activity_id() == activity_id && item.get_active() {
135                item.set_active(false);
136                return true;
137            }
138        }
139        false
140    }
141
142    /// Triggers a tactile haptic pulse when supported by the active platform.
143    ///
144    /// # Arguments
145    ///
146    /// - `&Self` - Mutable service hub.
147    /// - `HapticStyle` - Requested tactile waveform.
148    ///
149    /// # Returns
150    ///
151    /// - `bool` - `true` when dispatched on a haptic-capable platform.
152    pub fn trigger_haptic(&mut self, style: HapticStyle) -> bool {
153        let _: HapticStyle = style;
154        let caps: PlatformCapabilities = PlatformCapabilities::for_platform(self.get_platform());
155        if !caps.get_haptics() {
156            return false;
157        }
158        let next: u64 = self.get_haptic_count().saturating_add(1);
159        self.set_haptic_count(next);
160        let _: bool = dispatch_native_action(NATIVE_ACTION_HAPTIC, "impact", "");
161        true
162    }
163
164    /// Returns the native biometric authentication provider available on the active platform.
165    ///
166    /// # Arguments
167    ///
168    /// - `&Self` - Service hub reference.
169    ///
170    /// # Returns
171    ///
172    /// - `BiometricKind` - Platform biometric authenticator kind.
173    #[must_use]
174    pub fn biometric_kind(&self) -> BiometricKind {
175        match self.get_platform() {
176            PlatformKind::Ios | PlatformKind::IpadOs => BiometricKind::FaceId,
177            PlatformKind::MacOs => BiometricKind::TouchId,
178            PlatformKind::Android => BiometricKind::Fingerprint,
179            PlatformKind::Windows => BiometricKind::WindowsHello,
180            PlatformKind::Web => BiometricKind::Passkey,
181            PlatformKind::Linux | PlatformKind::Headless => BiometricKind::None,
182        }
183    }
184
185    /// Installs or updates the desktop system tray / menu bar configuration.
186    ///
187    /// # Arguments
188    ///
189    /// - `&Self` - Mutable service hub.
190    /// - `SystemTrayConfig` - Tray icon tooltip, title, and menu items.
191    ///
192    /// # Returns
193    ///
194    /// - `Result<(), PlatformError>` - `Ok(())` on desktop targets, or `Unsupported` on mobile/Web.
195    pub fn configure_tray(&mut self, config: SystemTrayConfig) -> Result<(), PlatformError> {
196        let caps: PlatformCapabilities = PlatformCapabilities::for_platform(self.get_platform());
197        if !caps.get_system_tray() {
198            return Err(PlatformError::Unsupported(format!(
199                "System tray is unsupported on {}",
200                self.get_platform().as_str()
201            )));
202        }
203        self.set_tray(Some(config));
204        Ok(())
205    }
206
207    /// Returns the active desktop tray configuration, if installed.
208    ///
209    /// # Arguments
210    ///
211    /// - `&Self` - Service hub reference.
212    ///
213    /// # Returns
214    ///
215    /// - `Option<&SystemTrayConfig>` - Configured tray metadata.
216    #[must_use]
217    pub fn tray_config(&self) -> Option<&SystemTrayConfig> {
218        let Self { tray, .. } = self;
219        tray.as_ref()
220    }
221
222    /// Dispatches a native OS notification and records it in telemetry.
223    ///
224    /// # Arguments
225    ///
226    /// - `&Self` - Mutable service hub.
227    /// - `SystemNotification` - Notification payload.
228    pub fn send_notification(&mut self, notification: SystemNotification) {
229        let _: bool = dispatch_native_action(
230            NATIVE_ACTION_NOTIFY,
231            notification.get_title(),
232            notification.get_body(),
233        );
234        self.get_mut_notifications().push(notification);
235    }
236
237    /// Queues a screen reader accessibility live announcement.
238    ///
239    /// # Arguments
240    ///
241    /// - `&Self` - Mutable service hub.
242    /// - `String` - Message text to announce.
243    /// - `A11yLiveRegion` - Politeness priority.
244    pub fn announce_a11y(&mut self, message: String, politeness: A11yLiveRegion) {
245        self.get_mut_announcements()
246            .push(A11yAnnouncement::new(message, politeness));
247    }
248
249    /// Resolves the native OS share sheet API descriptor for `payload`.
250    ///
251    /// # Arguments
252    ///
253    /// - `&Self` - Service hub reference.
254    /// - `&SharePayload` - Share sheet title, text, and optional URL.
255    ///
256    /// # Returns
257    ///
258    /// - `Result<String, PlatformError>` - Native OS share dispatcher description.
259    pub fn share(&self, payload: &SharePayload) -> Result<String, PlatformError> {
260        let api: &str = match self.get_platform() {
261            PlatformKind::Ios | PlatformKind::IpadOs => "UIActivityViewController",
262            PlatformKind::MacOs => "NSSharingServicePicker",
263            PlatformKind::Android => "android.content.Intent.ACTION_SEND",
264            PlatformKind::Windows => "Windows.ApplicationModel.DataTransfer.DataTransferManager",
265            PlatformKind::Linux => "org.freedesktop.portal.OpenURI",
266            PlatformKind::Web | PlatformKind::Headless => "navigator.share",
267        };
268        Ok(format!("{api}: {}", payload.get_title()))
269    }
270}