1use super::*;
2
3impl WearableVendor {
5 #[must_use]
15 pub const fn as_str(&self) -> &'static str {
16 match *self {
17 Self::WearOs => WEARABLE_VENDOR_WEAR_OS,
18 Self::WatchOs => WEARABLE_VENDOR_WATCH_OS,
19 Self::HarmonyWatch => WEARABLE_VENDOR_HARMONY_WATCH,
20 Self::HyperOsVela => WEARABLE_VENDOR_HYPEROS_VELA,
21 Self::OppoColorOsWatch => WEARABLE_VENDOR_OPPO_WATCH,
22 Self::VivoBlueOsWatch => WEARABLE_VENDOR_VIVO_WATCH,
23 Self::GarminConnectIq => WEARABLE_VENDOR_GARMIN,
24 Self::AmazfitZeppOs => WEARABLE_VENDOR_ZEPP_OS,
25 Self::GenericWearable => WEARABLE_VENDOR_GENERIC,
26 }
27 }
28
29 #[must_use]
39 pub fn from_str_id(name: &str) -> Option<Self> {
40 let lower: String = name.trim().to_ascii_lowercase();
41 match lower.as_str() {
42 WEARABLE_VENDOR_WEAR_OS
43 | WEARABLE_ALIAS_SAMSUNG_WATCH
44 | WEARABLE_ALIAS_GALAXY_WATCH
45 | WEARABLE_ALIAS_PIXEL_WATCH => Some(Self::WearOs),
46 WEARABLE_VENDOR_WATCH_OS | WEARABLE_ALIAS_APPLE_WATCH => Some(Self::WatchOs),
47 WEARABLE_VENDOR_HARMONY_WATCH
48 | WEARABLE_ALIAS_HARMONY_WEARABLE
49 | WEARABLE_ALIAS_HUAWEI_WATCH => Some(Self::HarmonyWatch),
50 WEARABLE_VENDOR_HYPEROS_VELA | WEARABLE_ALIAS_VELA | WEARABLE_ALIAS_MI_WATCH => {
51 Some(Self::HyperOsVela)
52 }
53 WEARABLE_VENDOR_OPPO_WATCH | WEARABLE_ALIAS_OPPO | WEARABLE_ALIAS_COLOROS_WATCH => {
54 Some(Self::OppoColorOsWatch)
55 }
56 WEARABLE_VENDOR_VIVO_WATCH
57 | WEARABLE_ALIAS_VIVO
58 | WEARABLE_ALIAS_BLUEOS
59 | WEARABLE_ALIAS_BLUEOS_WATCH
60 | WEARABLE_ALIAS_VIVO_BLUEOS => Some(Self::VivoBlueOsWatch),
61 WEARABLE_VENDOR_GARMIN => Some(Self::GarminConnectIq),
62 WEARABLE_VENDOR_ZEPP_OS | WEARABLE_ALIAS_AMAZFIT => Some(Self::AmazfitZeppOs),
63 WEARABLE_VENDOR_GENERIC | WEARABLE_ALIAS_GENERIC => Some(Self::GenericWearable),
64 _ => None,
65 }
66 }
67
68 #[must_use]
78 pub const fn runtime_ecosystem(&self) -> &'static str {
79 match *self {
80 Self::WearOs => WEARABLE_RUNTIME_WEAR_OS,
81 Self::WatchOs => WEARABLE_RUNTIME_WATCH_OS,
82 Self::HarmonyWatch => WEARABLE_RUNTIME_HARMONY_WATCH,
83 Self::HyperOsVela => WEARABLE_RUNTIME_HYPEROS_VELA,
84 Self::OppoColorOsWatch => WEARABLE_RUNTIME_OPPO_WATCH,
85 Self::VivoBlueOsWatch => WEARABLE_RUNTIME_VIVO_WATCH,
86 Self::GarminConnectIq => WEARABLE_RUNTIME_GARMIN,
87 Self::AmazfitZeppOs => WEARABLE_RUNTIME_ZEPP_OS,
88 Self::GenericWearable => WEARABLE_RUNTIME_GENERIC,
89 }
90 }
91
92 #[must_use]
102 pub const fn tag(&self) -> u8 {
103 match *self {
104 Self::WearOs => 1,
105 Self::WatchOs => 2,
106 Self::HarmonyWatch => 3,
107 Self::HyperOsVela => 4,
108 Self::OppoColorOsWatch => 5,
109 Self::VivoBlueOsWatch => 6,
110 Self::GarminConnectIq => 7,
111 Self::AmazfitZeppOs => 8,
112 Self::GenericWearable => 9,
113 }
114 }
115
116 #[must_use]
122 pub const fn all_vendors() -> [Self; 9] {
123 [
124 Self::WearOs,
125 Self::WatchOs,
126 Self::HarmonyWatch,
127 Self::HyperOsVela,
128 Self::OppoColorOsWatch,
129 Self::VivoBlueOsWatch,
130 Self::GarminConnectIq,
131 Self::AmazfitZeppOs,
132 Self::GenericWearable,
133 ]
134 }
135}
136
137impl WearableProfile {
139 #[must_use]
149 pub const fn for_vendor(vendor: WearableVendor) -> Self {
150 match vendor {
151 WearableVendor::WearOs => Self {
152 vendor,
153 shape: WearableShape::Round,
154 logical_width: 227,
155 logical_height: 227,
156 scale_factor: 2.0,
157 chin_height_dp: 0.0,
158 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
159 has_rotary_crown: true,
160 supports_aod: true,
161 },
162 WearableVendor::WatchOs => Self {
163 vendor,
164 shape: WearableShape::Squircle,
165 logical_width: 205,
166 logical_height: 251,
167 scale_factor: 2.0,
168 chin_height_dp: 0.0,
169 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
170 has_rotary_crown: true,
171 supports_aod: true,
172 },
173 WearableVendor::HarmonyWatch => Self {
174 vendor,
175 shape: WearableShape::Round,
176 logical_width: 233,
177 logical_height: 233,
178 scale_factor: 2.0,
179 chin_height_dp: 0.0,
180 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
181 has_rotary_crown: true,
182 supports_aod: true,
183 },
184 WearableVendor::HyperOsVela => Self {
185 vendor,
186 shape: WearableShape::Round,
187 logical_width: 233,
188 logical_height: 233,
189 scale_factor: 2.0,
190 chin_height_dp: 0.0,
191 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
192 has_rotary_crown: true,
193 supports_aod: true,
194 },
195 WearableVendor::OppoColorOsWatch => Self {
196 vendor,
197 shape: WearableShape::Squircle,
198 logical_width: 201,
199 logical_height: 238,
200 scale_factor: 2.0,
201 chin_height_dp: 0.0,
202 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
203 has_rotary_crown: true,
204 supports_aod: true,
205 },
206 WearableVendor::VivoBlueOsWatch => Self::vivo_watch_round(),
207 WearableVendor::GarminConnectIq => Self {
208 vendor,
209 shape: WearableShape::Round,
210 logical_width: 208,
211 logical_height: 208,
212 scale_factor: 2.0,
213 chin_height_dp: 0.0,
214 refresh_rate_hz: 30.0,
215 has_rotary_crown: false,
216 supports_aod: true,
217 },
218 WearableVendor::AmazfitZeppOs => Self {
219 vendor,
220 shape: WearableShape::Round,
221 logical_width: 240,
222 logical_height: 240,
223 scale_factor: 2.0,
224 chin_height_dp: 0.0,
225 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
226 has_rotary_crown: true,
227 supports_aod: true,
228 },
229 WearableVendor::GenericWearable => Self {
230 vendor,
231 shape: WearableShape::Rectangular,
232 logical_width: 184,
233 logical_height: 224,
234 scale_factor: 2.0,
235 chin_height_dp: 0.0,
236 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
237 has_rotary_crown: false,
238 supports_aod: true,
239 },
240 }
241 }
242
243 #[must_use]
249 pub const fn vivo_watch_round() -> Self {
250 Self {
251 vendor: WearableVendor::VivoBlueOsWatch,
252 shape: WearableShape::Round,
253 logical_width: 233,
254 logical_height: 233,
255 scale_factor: 2.0,
256 chin_height_dp: 0.0,
257 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
258 has_rotary_crown: true,
259 supports_aod: true,
260 }
261 }
262
263 #[must_use]
269 pub const fn vivo_watch_gt() -> Self {
270 Self {
271 vendor: WearableVendor::VivoBlueOsWatch,
272 shape: WearableShape::Squircle,
273 logical_width: 195,
274 logical_height: 225,
275 scale_factor: 2.0,
276 chin_height_dp: 0.0,
277 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
278 has_rotary_crown: true,
279 supports_aod: true,
280 }
281 }
282
283 #[must_use]
293 pub fn safe_area_insets(&self) -> SafeAreaInsets {
294 compute_wearable_safe_area(
295 self.get_shape(),
296 self.get_logical_width() as f32,
297 self.get_logical_height() as f32,
298 self.get_chin_height_dp(),
299 )
300 }
301
302 #[must_use]
312 pub fn compact_scale(&self) -> f32 {
313 compute_wearable_compact_scale(
314 self.get_logical_width() as f32,
315 self.get_logical_height() as f32,
316 )
317 }
318}
319
320impl Default for WearablePlatform {
322 fn default() -> Self {
328 Self::for_vendor(WearableVendor::WearOs)
329 }
330}
331
332impl WearablePlatform {
334 pub fn update_refresh_rate_hz(&mut self, hz: f32) {
341 if !hz.is_finite() || hz < MIN_REFRESH_RATE_HZ {
342 return;
343 }
344 self.get_mut_profile().set_refresh_rate_hz(hz);
345 let rounded_hz: u32 = hz.round() as u32;
346 for rec in self.get_mut_windows().values_mut() {
347 rec.get_mut_state()
348 .get_mut_display()
349 .set_refresh_rate_hz(rounded_hz);
350 }
351 }
352
353 #[must_use]
363 pub fn for_vendor(vendor: WearableVendor) -> Self {
364 Self::from_profile(WearableProfile::for_vendor(vendor))
365 }
366
367 #[must_use]
373 pub fn vivo_blueos() -> Self {
374 Self::from_profile(WearableProfile::vivo_watch_round())
375 }
376
377 #[must_use]
387 pub fn from_profile(profile: WearableProfile) -> Self {
388 let safe_area: SafeAreaInsets = profile.safe_area_insets();
389 let compact_scale: f32 = profile.compact_scale();
390 Self {
391 profile,
392 ambient_mode: WearableAmbientMode::Interactive,
393 compact_scale,
394 safe_area,
395 crown_accumulator: 0.0,
396 crown_scroll_step: WEARABLE_CROWN_PIXELS_PER_DETENT,
397 next_id: 1,
398 primary_window: None,
399 windows: HashMap::new(),
400 touch_tracker: TouchPointerTracker::default(),
401 pending_events: Vec::new(),
402 storage: HashMap::new(),
403 route_stack: vec![WEARABLE_DEFAULT_ROUTE.to_string()],
404 theme: ThemeMode::Dark,
405 pump_paused: false,
406 haptic_detent_count: 0,
407 capture_last_frame: false,
408 }
409 }
410
411 #[must_use]
421 pub fn with_capture_last_frame(mut self, enabled: bool) -> Self {
422 self.set_capture_last_frame(enabled);
423 self
424 }
425
426 #[must_use]
436 pub fn last_framebuffer(&self, id: WindowId) -> Option<&Framebuffer> {
437 if !self.get_capture_last_frame() {
438 return None;
439 }
440 self.get_windows()
441 .get(&id)
442 .map(WearableWindowRecord::get_last_frame)
443 }
444
445 #[must_use]
455 pub fn primary_window_id(&self) -> Option<WindowId> {
456 let Self { primary_window, .. } = self;
457 *primary_window
458 }
459
460 pub fn handle_crown_input(&mut self, input: WearableCrownInput) -> Vec<WindowEvent> {
471 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
472 if !input.get_detents().is_finite() || input.get_detents().abs() < 1e-5 {
473 return Vec::new();
474 }
475 let mut emitted: Vec<WindowEvent> = Vec::new();
476 match input.get_mode() {
477 WearableCrownMode::Scroll => {
478 let dy: f32 = input.get_detents() * self.get_crown_scroll_step();
479 emitted.push(WindowEvent::Scroll(wid, Point::new(0.0, dy)));
480 }
481 WearableCrownMode::FocusStep => {
482 let prev: f32 = self.get_crown_accumulator();
483 let base: f32 = if prev * input.get_detents() < 0.0 {
484 0.0
485 } else {
486 prev
487 };
488 let total: f32 = base + input.get_detents();
489 let whole: i32 = total.trunc() as i32;
490 self.set_crown_accumulator(total - (whole as f32));
491 let forward: bool = whole > 0;
492 let steps: usize = whole.unsigned_abs().min(16) as usize;
493 let mods: Modifiers = Modifiers::new(!forward, false, false, false);
494 for _ in 0..steps {
495 emitted.push(WindowEvent::KeyDown(
496 wid,
497 KeyEvent::new(KeyCode::Tab, mods, false),
498 ));
499 emitted.push(WindowEvent::KeyUp(
500 wid,
501 KeyEvent::new(KeyCode::Tab, mods, false),
502 ));
503 }
504 }
505 WearableCrownMode::DirectionalStep => {
506 let prev: f32 = self.get_crown_accumulator();
507 let base: f32 = if prev * input.get_detents() < 0.0 {
508 0.0
509 } else {
510 prev
511 };
512 let total: f32 = base + input.get_detents();
513 let whole: i32 = total.trunc() as i32;
514 self.set_crown_accumulator(total - (whole as f32));
515 let code: KeyCode = if whole > 0 {
516 KeyCode::ArrowDown
517 } else {
518 KeyCode::ArrowUp
519 };
520 let steps: usize = whole.unsigned_abs().min(16) as usize;
521 for _ in 0..steps {
522 emitted.push(WindowEvent::KeyDown(
523 wid,
524 KeyEvent::new(code, Modifiers::default(), false),
525 ));
526 emitted.push(WindowEvent::KeyUp(
527 wid,
528 KeyEvent::new(code, Modifiers::default(), false),
529 ));
530 }
531 }
532 }
533 if input.get_haptic_detent() && !emitted.is_empty() {
534 let next_h: u64 = self.get_haptic_detent_count().saturating_add(1);
535 self.set_haptic_detent_count(next_h);
536 }
537 self.get_mut_pending_events().extend(emitted.clone());
538 emitted
539 }
540
541 pub fn enter_ambient(&mut self) -> Vec<WindowEvent> {
551 if !self.get_profile().get_supports_aod() {
552 return self.enter_screen_off();
553 }
554 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
555 self.set_ambient_mode(WearableAmbientMode::AlwaysOnAmbient);
556 self.set_pump_paused(true);
557 let mut events: Vec<WindowEvent> =
558 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
559 events.push(WindowEvent::System(SystemEvent::LowPowerModeChanged(true)));
560 events.push(WindowEvent::System(SystemEvent::Inactive));
561 self.get_mut_pending_events().extend(events.clone());
562 events
563 }
564
565 pub fn pulse_ambient_tick(&mut self) -> Option<WindowEvent> {
575 if self.get_ambient_mode() != WearableAmbientMode::AlwaysOnAmbient {
576 return None;
577 }
578 let wid: WindowId = self.primary_window_id()?;
579 if let Some(rec) = self.get_mut_windows().get_mut(&wid) {
580 rec.set_ambient_tick_count(rec.get_ambient_tick_count().saturating_add(1));
581 }
582 let window_event: WindowEvent = WindowEvent::RedrawRequested(wid);
583 self.get_mut_pending_events().push(window_event.clone());
584 Some(window_event)
585 }
586
587 pub fn enter_screen_off(&mut self) -> Vec<WindowEvent> {
597 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
598 self.set_ambient_mode(WearableAmbientMode::ScreenOff);
599 self.set_pump_paused(true);
600 let mut events: Vec<WindowEvent> =
601 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
602 events.push(WindowEvent::System(SystemEvent::Disappear));
603 events.push(WindowEvent::System(SystemEvent::Background));
604 self.get_mut_pending_events().extend(events.clone());
605 events
606 }
607
608 pub fn exit_ambient(&mut self) -> Vec<WindowEvent> {
618 self.set_ambient_mode(WearableAmbientMode::Interactive);
619 self.set_pump_paused(false);
620 let events: Vec<WindowEvent> = vec![
621 WindowEvent::System(SystemEvent::LowPowerModeChanged(false)),
622 WindowEvent::System(SystemEvent::Appear),
623 WindowEvent::System(SystemEvent::Foreground),
624 ];
625 self.get_mut_pending_events().extend(events.clone());
626 events
627 }
628
629 pub fn handle_hardware_gesture(
640 &mut self,
641 gesture: WearableHardwareGesture,
642 ) -> Vec<WindowEvent> {
643 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
644 match gesture {
645 WearableHardwareGesture::WristRaise => self.exit_ambient(),
646 WearableHardwareGesture::WristLower => {
647 if self.get_profile().get_supports_aod() {
648 self.enter_ambient()
649 } else {
650 self.enter_screen_off()
651 }
652 }
653 WearableHardwareGesture::PalmCover => self.enter_screen_off(),
654 WearableHardwareGesture::DoublePinch => {
655 let events: Vec<WindowEvent> = vec![
656 WindowEvent::KeyDown(
657 wid,
658 KeyEvent::new(KeyCode::Enter, Modifiers::default(), false),
659 ),
660 WindowEvent::KeyUp(
661 wid,
662 KeyEvent::new(KeyCode::Enter, Modifiers::default(), false),
663 ),
664 ];
665 self.get_mut_pending_events().extend(events.clone());
666 events
667 }
668 WearableHardwareGesture::SideButtonPress => {
669 let events: Vec<WindowEvent> = vec![
670 WindowEvent::KeyDown(
671 wid,
672 KeyEvent::new(KeyCode::Back, Modifiers::default(), false),
673 ),
674 WindowEvent::System(SystemEvent::BackPressed),
675 ];
676 self.get_mut_pending_events().extend(events.clone());
677 events
678 }
679 WearableHardwareGesture::SideButtonLongPress => {
680 let events: Vec<WindowEvent> = vec![WindowEvent::System(SystemEvent::Inactive)];
681 self.get_mut_pending_events().extend(events.clone());
682 events
683 }
684 }
685 }
686}
687
688impl WindowController for WearablePlatform {
690 fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
701 match command {
702 WindowCommand::SetTitle(id, title) => {
703 let rec: &mut WearableWindowRecord = self
704 .get_mut_windows()
705 .get_mut(&id)
706 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
707 rec.get_mut_config().set_title(title);
708 Ok(())
709 }
710 WindowCommand::SetSize(id, width, height) => {
711 let shape: WearableShape = self.get_profile().get_shape();
712 let chin: f32 = self.get_profile().get_chin_height_dp();
713 let safe: SafeAreaInsets = compute_wearable_safe_area(
714 shape,
715 width.max(1) as f32,
716 height.max(1) as f32,
717 chin,
718 );
719 let compact: f32 =
720 compute_wearable_compact_scale(width.max(1) as f32, height.max(1) as f32);
721 self.set_safe_area(safe);
722 self.set_compact_scale(compact);
723 let scale: f32 = {
724 let rec: &mut WearableWindowRecord = self
725 .get_mut_windows()
726 .get_mut(&id)
727 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
728 let source_val: f32 = clamp_viewport_scale(rec.get_state().get_scale_factor());
729 let phys_w: u32 =
730 compute_lossless_physical_dim(f64::from(width.max(1)), source_val);
731 let phys_h: u32 =
732 compute_lossless_physical_dim(f64::from(height.max(1)), source_val);
733 rec.get_mut_config().set_size(width.max(1), height.max(1));
734 rec.get_mut_state()
735 .set_logical_width(width.max(1) as f32)
736 .set_logical_height(height.max(1) as f32)
737 .set_physical_width(phys_w)
738 .set_physical_height(phys_h)
739 .set_scale_factor(source_val)
740 .set_safe_area(safe);
741 source_val
742 };
743 self.get_mut_pending_events().push(WindowEvent::Resized(
744 id,
745 width.max(1),
746 height.max(1),
747 scale,
748 ));
749 self.get_mut_pending_events()
750 .push(WindowEvent::SafeAreaChanged(id, safe));
751 Ok(())
752 }
753 WindowCommand::SetRenderMode(id, mode) => {
754 let rec: &mut WearableWindowRecord = self
755 .get_mut_windows()
756 .get_mut(&id)
757 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
758 rec.get_mut_config().get_mut_render().set_mode(mode);
759 Ok(())
760 }
761 WindowCommand::RequestRedraw(id) => {
762 if !self.get_windows().contains_key(&id) {
763 return Err(PlatformError::WindowNotFound(id.get_value()));
764 }
765 self.get_mut_pending_events()
766 .push(WindowEvent::RedrawRequested(id));
767 Ok(())
768 }
769 WindowCommand::Close(id) => {
770 if self.get_mut_windows().remove(&id).is_none() {
771 return Err(PlatformError::WindowNotFound(id.get_value()));
772 }
773 Ok(())
774 }
775 WindowCommand::SetPosition(id, _, _)
776 | WindowCommand::SetResizable(id, _)
777 | WindowCommand::SetDecorations(id, _)
778 | WindowCommand::SetAlwaysOnTop(id, _)
779 | WindowCommand::SetFullscreen(id, _)
780 | WindowCommand::SetVisible(id, _)
781 | WindowCommand::SetCursor(id, _)
782 | WindowCommand::SetIme(id, _, _)
783 | WindowCommand::SetStatusBarStyle(id, _) => {
784 if !self.get_windows().contains_key(&id) {
785 return Err(PlatformError::WindowNotFound(id.get_value()));
786 }
787 Ok(())
788 }
789 }
790 }
791}
792
793impl PlatformBackend for WearablePlatform {
795 fn kind(&self) -> PlatformKind {
805 PlatformKind::Wearable
806 }
807
808 fn capabilities(&self) -> PlatformCapabilities {
818 let mut caps: PlatformCapabilities =
819 PlatformCapabilities::for_platform(PlatformKind::Wearable);
820 caps.rotary_input = self.get_profile().get_has_rotary_crown();
821 caps.always_on_display = self.get_profile().get_supports_aod();
822 caps
823 }
824
825 fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
836 let id: WindowId = WindowId::new(self.get_next_id());
837 self.set_next_id(self.get_next_id().wrapping_add(1));
838 let profile: WearableProfile = self.get_profile();
839 let mut watch_cfg: WindowConfig = config.clone();
840 watch_cfg.set_size(profile.get_logical_width(), profile.get_logical_height());
841 let mut state: WindowState =
842 WindowState::from_config(id, &watch_cfg, profile.get_scale_factor());
843 state.set_safe_area(self.get_safe_area());
844 state.set_system_theme(self.get_theme());
845 state
846 .get_mut_display()
847 .set_refresh_rate_hz(profile.get_refresh_rate_hz().round() as u32);
848 let record: WearableWindowRecord = WearableWindowRecord {
849 config: watch_cfg,
850 state,
851 native_surface: 0,
852 last_frame: Framebuffer::allocate(1, 1),
853 present_count: 0,
854 ambient_tick_count: 0,
855 };
856 self.get_mut_windows().insert(id, record);
857 if self.primary_window_id().is_none() {
858 self.set_primary_window(Some(id));
859 }
860 Ok(state)
861 }
862
863 fn window_state(&self, id: WindowId) -> Option<WindowState> {
874 self.get_windows()
875 .get(&id)
876 .map(WearableWindowRecord::get_state)
877 }
878
879 fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle {
890 match self.get_windows().get(&id) {
891 Some(rec) => RawWindowHandle::WearableSurface {
892 native_handle: rec.get_native_surface(),
893 vendor_tag: self.get_profile().get_vendor().tag(),
894 },
895 None => RawWindowHandle::Headless,
896 }
897 }
898
899 fn poll_events(&mut self) -> Vec<WindowEvent> {
909 take(self.get_mut_pending_events())
910 }
911
912 fn set_frame_pump_paused(&mut self, paused: bool) {
919 self.set_pump_paused(paused);
920 }
921
922 fn is_frame_pump_paused(&self) -> bool {
932 self.get_pump_paused()
933 }
934
935 fn present_framebuffer(
947 &mut self,
948 id: WindowId,
949 framebuffer: &Framebuffer,
950 ) -> Result<(), PlatformError> {
951 let capture: bool = self.get_capture_last_frame();
952 let rec: &mut WearableWindowRecord = self
953 .get_mut_windows()
954 .get_mut(&id)
955 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
956 if capture {
957 rec.set_last_frame(framebuffer.clone());
958 }
959 rec.set_present_count(rec.get_present_count().wrapping_add(1));
960 Ok(())
961 }
962
963 fn read_clipboard(&self) -> Option<String> {
973 None
974 }
975
976 fn write_clipboard(&mut self, _text: &str) -> Result<(), PlatformError> {
987 Err(PlatformError::Unsupported(format!(
988 "Clipboard is unsupported on {}",
989 self.get_profile().get_vendor().as_str()
990 )))
991 }
992
993 fn system_theme(&self) -> ThemeMode {
1003 self.get_theme()
1004 }
1005
1006 fn display_refresh_rate_hz(&self) -> f32 {
1016 if self.get_ambient_mode() == WearableAmbientMode::AlwaysOnAmbient {
1017 WEARABLE_AOD_REFRESH_HZ
1018 } else {
1019 self.get_profile().get_refresh_rate_hz()
1020 }
1021 }
1022}
1023
1024impl PlatformStorage for WearablePlatform {
1026 fn storage_get(&self, key: &str) -> Option<String> {
1037 self.get_storage().get(key).cloned()
1038 }
1039
1040 fn storage_set(&mut self, key: &str, value: &str) -> Result<(), PlatformError> {
1052 let _: Option<String> = self
1053 .get_mut_storage()
1054 .insert(key.to_string(), value.to_string());
1055 Ok(())
1056 }
1057
1058 fn storage_remove(&mut self, key: &str) -> Result<(), PlatformError> {
1069 let _: Option<String> = self.get_mut_storage().remove(key);
1070 Ok(())
1071 }
1072
1073 fn storage_clear(&mut self) -> Result<(), PlatformError> {
1083 self.get_mut_storage().clear();
1084 Ok(())
1085 }
1086}
1087
1088impl PlatformNavigation for WearablePlatform {
1090 fn navigate_to(&mut self, route: &str) -> Result<(), PlatformError> {
1101 if !route.is_empty() {
1102 self.get_mut_route_stack().push(route.to_string());
1103 }
1104 Ok(())
1105 }
1106
1107 fn navigate_back(&mut self, delta: u32) -> Result<(), PlatformError> {
1118 for _ in 0..delta.max(1) {
1119 if self.get_route_stack().len() > 1 {
1120 let _: Option<String> = self.get_mut_route_stack().pop();
1121 }
1122 }
1123 Ok(())
1124 }
1125
1126 fn current_route(&self) -> &str {
1136 self.get_route_stack()
1137 .last()
1138 .map(String::as_str)
1139 .unwrap_or(WEARABLE_DEFAULT_ROUTE)
1140 }
1141}
1142
1143impl PlatformLifecycleBridge for WearablePlatform {
1145 fn freeze_surface(&mut self) -> Vec<WindowEvent> {
1155 self.enter_screen_off()
1156 }
1157
1158 fn resume_surface(&mut self) -> Vec<WindowEvent> {
1168 self.exit_ambient()
1169 }
1170
1171 fn destroy_surface(&mut self) -> Vec<WindowEvent> {
1181 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
1182 self.set_ambient_mode(WearableAmbientMode::ScreenOff);
1183 self.set_pump_paused(true);
1184 let mut events: Vec<WindowEvent> =
1185 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
1186 events.push(WindowEvent::System(SystemEvent::Terminate));
1187 events
1188 }
1189}
1190
1191impl PlatformHostAdapter for WearablePlatform {
1193 fn device_form_factor(&self) -> DeviceFormFactor {
1203 DeviceFormFactor::Watch
1204 }
1205
1206 fn host_vendor_name(&self) -> &'static str {
1216 self.get_profile().get_vendor().as_str()
1217 }
1218}
1219
1220impl WearablePlatform {
1222 #[must_use]
1232 pub fn host_channel_capabilities(&self) -> HostChannelCapabilities {
1233 let has_live_surface: bool = self
1234 .get_windows()
1235 .values()
1236 .any(|rec: &WearableWindowRecord| rec.get_native_surface() != 0);
1237 let simulated: bool = !has_live_surface;
1238 let vendor: WearableVendor = self.get_profile().get_vendor();
1239 let (channels, preferred): (Vec<HostRenderChannel>, HostRenderChannel) = match vendor {
1240 WearableVendor::WearOs
1241 | WearableVendor::WatchOs
1242 | WearableVendor::HarmonyWatch
1243 | WearableVendor::OppoColorOsWatch => (
1244 vec![
1245 HostRenderChannel::GpuDirect,
1246 HostRenderChannel::SoftwareBuffer,
1247 ],
1248 if has_live_surface {
1249 HostRenderChannel::GpuDirect
1250 } else {
1251 HostRenderChannel::SoftwareBuffer
1252 },
1253 ),
1254 WearableVendor::HyperOsVela
1255 | WearableVendor::VivoBlueOsWatch
1256 | WearableVendor::GarminConnectIq
1257 | WearableVendor::AmazfitZeppOs
1258 | WearableVendor::GenericWearable => (
1259 vec![HostRenderChannel::SoftwareBuffer],
1260 HostRenderChannel::SoftwareBuffer,
1261 ),
1262 };
1263 let gpu_verified: bool =
1264 !simulated && channels.contains(&HostRenderChannel::GpuDirect) && has_live_surface;
1265 HostChannelCapabilities::from_host_declaration(
1266 self.host_vendor_name(),
1267 channels,
1268 preferred,
1269 HostVisualEquivalenceGate::default(),
1270 simulated,
1271 gpu_verified,
1272 )
1273 }
1274
1275 #[must_use]
1285 pub fn select_render_channel(
1286 &self,
1287 requested: Option<HostRenderChannel>,
1288 ) -> HostChannelSelection {
1289 self.host_channel_capabilities().select_channel(requested)
1290 }
1291}