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 const fn tag(&self) -> u8 {
40 match *self {
41 Self::WearOs => 1,
42 Self::WatchOs => 2,
43 Self::HarmonyWatch => 3,
44 Self::HyperOsVela => 4,
45 Self::OppoColorOsWatch => 5,
46 Self::VivoBlueOsWatch => 6,
47 Self::GarminConnectIq => 7,
48 Self::AmazfitZeppOs => 8,
49 Self::GenericWearable => 9,
50 }
51 }
52
53 #[must_use]
59 pub const fn all_vendors() -> [Self; 9] {
60 [
61 Self::WearOs,
62 Self::WatchOs,
63 Self::HarmonyWatch,
64 Self::HyperOsVela,
65 Self::OppoColorOsWatch,
66 Self::VivoBlueOsWatch,
67 Self::GarminConnectIq,
68 Self::AmazfitZeppOs,
69 Self::GenericWearable,
70 ]
71 }
72}
73
74impl WearableProfile {
76 #[must_use]
86 pub const fn for_vendor(vendor: WearableVendor) -> Self {
87 match vendor {
88 WearableVendor::WearOs => Self {
89 vendor,
90 shape: WearableShape::Round,
91 logical_width: 227,
92 logical_height: 227,
93 scale_factor: 2.0,
94 chin_height_dp: 0.0,
95 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
96 has_rotary_crown: true,
97 supports_aod: true,
98 },
99 WearableVendor::WatchOs => Self {
100 vendor,
101 shape: WearableShape::Squircle,
102 logical_width: 205,
103 logical_height: 251,
104 scale_factor: 2.0,
105 chin_height_dp: 0.0,
106 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
107 has_rotary_crown: true,
108 supports_aod: true,
109 },
110 WearableVendor::HarmonyWatch => Self {
111 vendor,
112 shape: WearableShape::Round,
113 logical_width: 233,
114 logical_height: 233,
115 scale_factor: 2.0,
116 chin_height_dp: 0.0,
117 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
118 has_rotary_crown: true,
119 supports_aod: true,
120 },
121 WearableVendor::HyperOsVela => Self {
122 vendor,
123 shape: WearableShape::Round,
124 logical_width: 233,
125 logical_height: 233,
126 scale_factor: 2.0,
127 chin_height_dp: 0.0,
128 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
129 has_rotary_crown: true,
130 supports_aod: true,
131 },
132 WearableVendor::OppoColorOsWatch => Self {
133 vendor,
134 shape: WearableShape::Squircle,
135 logical_width: 201,
136 logical_height: 238,
137 scale_factor: 2.0,
138 chin_height_dp: 0.0,
139 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
140 has_rotary_crown: true,
141 supports_aod: true,
142 },
143 WearableVendor::VivoBlueOsWatch => Self {
144 vendor,
145 shape: WearableShape::Round,
146 logical_width: 233,
147 logical_height: 233,
148 scale_factor: 2.0,
149 chin_height_dp: 0.0,
150 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
151 has_rotary_crown: true,
152 supports_aod: true,
153 },
154 WearableVendor::GarminConnectIq => Self {
155 vendor,
156 shape: WearableShape::Round,
157 logical_width: 208,
158 logical_height: 208,
159 scale_factor: 2.0,
160 chin_height_dp: 0.0,
161 refresh_rate_hz: 30.0,
162 has_rotary_crown: false,
163 supports_aod: true,
164 },
165 WearableVendor::AmazfitZeppOs => Self {
166 vendor,
167 shape: WearableShape::Round,
168 logical_width: 240,
169 logical_height: 240,
170 scale_factor: 2.0,
171 chin_height_dp: 0.0,
172 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
173 has_rotary_crown: true,
174 supports_aod: true,
175 },
176 WearableVendor::GenericWearable => Self {
177 vendor,
178 shape: WearableShape::Rectangular,
179 logical_width: 184,
180 logical_height: 224,
181 scale_factor: 2.0,
182 chin_height_dp: 0.0,
183 refresh_rate_hz: WEARABLE_INTERACTIVE_REFRESH_HZ,
184 has_rotary_crown: false,
185 supports_aod: true,
186 },
187 }
188 }
189
190 #[must_use]
200 pub fn safe_area_insets(&self) -> SafeAreaInsets {
201 compute_wearable_safe_area(
202 self.get_shape(),
203 self.get_logical_width() as f32,
204 self.get_logical_height() as f32,
205 self.get_chin_height_dp(),
206 )
207 }
208
209 #[must_use]
219 pub fn compact_scale(&self) -> f32 {
220 compute_wearable_compact_scale(
221 self.get_logical_width() as f32,
222 self.get_logical_height() as f32,
223 )
224 }
225}
226
227impl Default for WearablePlatform {
229 fn default() -> Self {
235 Self::for_vendor(WearableVendor::WearOs)
236 }
237}
238
239impl WearablePlatform {
241 #[must_use]
251 pub fn for_vendor(vendor: WearableVendor) -> Self {
252 Self::from_profile(WearableProfile::for_vendor(vendor))
253 }
254
255 #[must_use]
265 pub fn from_profile(profile: WearableProfile) -> Self {
266 let safe_area: SafeAreaInsets = profile.safe_area_insets();
267 let compact_scale: f32 = profile.compact_scale();
268 Self {
269 profile,
270 ambient_mode: WearableAmbientMode::Interactive,
271 compact_scale,
272 safe_area,
273 crown_accumulator: 0.0,
274 crown_scroll_step: WEARABLE_CROWN_PIXELS_PER_DETENT,
275 next_id: 1,
276 primary_window: None,
277 windows: HashMap::new(),
278 touch_tracker: TouchPointerTracker::default(),
279 pending_events: Vec::new(),
280 storage: HashMap::new(),
281 route_stack: vec![WEARABLE_DEFAULT_ROUTE.to_string()],
282 theme: ThemeMode::Dark,
283 pump_paused: false,
284 haptic_detent_count: 0,
285 }
286 }
287
288 #[must_use]
298 pub fn primary_window_id(&self) -> Option<WindowId> {
299 let Self { primary_window, .. } = self;
300 *primary_window
301 }
302
303 pub fn handle_crown_input(&mut self, input: WearableCrownInput) -> Vec<WindowEvent> {
314 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
315 if !input.get_detents().is_finite() || input.get_detents().abs() < 1e-5 {
316 return Vec::new();
317 }
318 let mut emitted: Vec<WindowEvent> = Vec::new();
319 match input.get_mode() {
320 WearableCrownMode::Scroll => {
321 let dy: f32 = input.get_detents() * self.get_crown_scroll_step();
322 emitted.push(WindowEvent::Scroll(wid, Point::new(0.0, dy)));
323 }
324 WearableCrownMode::FocusStep => {
325 let prev: f32 = self.get_crown_accumulator();
326 let base: f32 = if prev * input.get_detents() < 0.0 {
327 0.0
328 } else {
329 prev
330 };
331 let total: f32 = base + input.get_detents();
332 let whole: i32 = total.trunc() as i32;
333 self.set_crown_accumulator(total - (whole as f32));
334 let forward: bool = whole > 0;
335 let steps: usize = whole.unsigned_abs().min(16) as usize;
336 let mods: Modifiers = Modifiers::new(!forward, false, false, false);
337 for _ in 0..steps {
338 emitted.push(WindowEvent::KeyDown(
339 wid,
340 KeyEvent::new(KeyCode::Tab, mods, false),
341 ));
342 emitted.push(WindowEvent::KeyUp(
343 wid,
344 KeyEvent::new(KeyCode::Tab, mods, false),
345 ));
346 }
347 }
348 WearableCrownMode::DirectionalStep => {
349 let prev: f32 = self.get_crown_accumulator();
350 let base: f32 = if prev * input.get_detents() < 0.0 {
351 0.0
352 } else {
353 prev
354 };
355 let total: f32 = base + input.get_detents();
356 let whole: i32 = total.trunc() as i32;
357 self.set_crown_accumulator(total - (whole as f32));
358 let code: KeyCode = if whole > 0 {
359 KeyCode::ArrowDown
360 } else {
361 KeyCode::ArrowUp
362 };
363 let steps: usize = whole.unsigned_abs().min(16) as usize;
364 for _ in 0..steps {
365 emitted.push(WindowEvent::KeyDown(
366 wid,
367 KeyEvent::new(code, Modifiers::default(), false),
368 ));
369 emitted.push(WindowEvent::KeyUp(
370 wid,
371 KeyEvent::new(code, Modifiers::default(), false),
372 ));
373 }
374 }
375 }
376 if input.get_haptic_detent() && !emitted.is_empty() {
377 let next_h: u64 = self.get_haptic_detent_count().saturating_add(1);
378 self.set_haptic_detent_count(next_h);
379 }
380 self.get_mut_pending_events().extend(emitted.clone());
381 emitted
382 }
383
384 pub fn enter_ambient(&mut self) -> Vec<WindowEvent> {
394 if !self.get_profile().get_supports_aod() {
395 return self.enter_screen_off();
396 }
397 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
398 self.set_ambient_mode(WearableAmbientMode::AlwaysOnAmbient);
399 self.set_pump_paused(true);
400 let mut events: Vec<WindowEvent> =
401 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
402 events.push(WindowEvent::System(SystemEvent::LowPowerModeChanged(true)));
403 events.push(WindowEvent::System(SystemEvent::Inactive));
404 self.get_mut_pending_events().extend(events.clone());
405 events
406 }
407
408 pub fn pulse_ambient_tick(&mut self) -> Option<WindowEvent> {
418 if self.get_ambient_mode() != WearableAmbientMode::AlwaysOnAmbient {
419 return None;
420 }
421 let wid: WindowId = self.primary_window_id()?;
422 if let Some(rec) = self.get_mut_windows().get_mut(&wid) {
423 rec.set_ambient_tick_count(rec.get_ambient_tick_count().saturating_add(1));
424 }
425 let ev: WindowEvent = WindowEvent::RedrawRequested(wid);
426 self.get_mut_pending_events().push(ev.clone());
427 Some(ev)
428 }
429
430 pub fn enter_screen_off(&mut self) -> Vec<WindowEvent> {
440 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
441 self.set_ambient_mode(WearableAmbientMode::ScreenOff);
442 self.set_pump_paused(true);
443 let mut events: Vec<WindowEvent> =
444 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
445 events.push(WindowEvent::System(SystemEvent::Disappear));
446 events.push(WindowEvent::System(SystemEvent::Background));
447 self.get_mut_pending_events().extend(events.clone());
448 events
449 }
450
451 pub fn exit_ambient(&mut self) -> Vec<WindowEvent> {
461 self.set_ambient_mode(WearableAmbientMode::Interactive);
462 self.set_pump_paused(false);
463 let events: Vec<WindowEvent> = vec![
464 WindowEvent::System(SystemEvent::LowPowerModeChanged(false)),
465 WindowEvent::System(SystemEvent::Appear),
466 WindowEvent::System(SystemEvent::Foreground),
467 ];
468 self.get_mut_pending_events().extend(events.clone());
469 events
470 }
471
472 pub fn handle_hardware_gesture(
483 &mut self,
484 gesture: WearableHardwareGesture,
485 ) -> Vec<WindowEvent> {
486 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
487 match gesture {
488 WearableHardwareGesture::WristRaise => self.exit_ambient(),
489 WearableHardwareGesture::WristLower => {
490 if self.get_profile().get_supports_aod() {
491 self.enter_ambient()
492 } else {
493 self.enter_screen_off()
494 }
495 }
496 WearableHardwareGesture::PalmCover => self.enter_screen_off(),
497 WearableHardwareGesture::DoublePinch => {
498 let events: Vec<WindowEvent> = vec![
499 WindowEvent::KeyDown(
500 wid,
501 KeyEvent::new(KeyCode::Enter, Modifiers::default(), false),
502 ),
503 WindowEvent::KeyUp(
504 wid,
505 KeyEvent::new(KeyCode::Enter, Modifiers::default(), false),
506 ),
507 ];
508 self.get_mut_pending_events().extend(events.clone());
509 events
510 }
511 WearableHardwareGesture::SideButtonPress => {
512 let events: Vec<WindowEvent> = vec![
513 WindowEvent::KeyDown(
514 wid,
515 KeyEvent::new(KeyCode::Back, Modifiers::default(), false),
516 ),
517 WindowEvent::System(SystemEvent::BackPressed),
518 ];
519 self.get_mut_pending_events().extend(events.clone());
520 events
521 }
522 WearableHardwareGesture::SideButtonLongPress => {
523 let events: Vec<WindowEvent> = vec![WindowEvent::System(SystemEvent::Inactive)];
524 self.get_mut_pending_events().extend(events.clone());
525 events
526 }
527 }
528 }
529}
530
531impl WindowController for WearablePlatform {
533 fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
544 match command {
545 WindowCommand::SetTitle(id, title) => {
546 let rec: &mut WearableWindowRecord = self
547 .get_mut_windows()
548 .get_mut(&id)
549 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
550 rec.get_mut_config().set_title(title);
551 Ok(())
552 }
553 WindowCommand::SetSize(id, width, height) => {
554 let shape: WearableShape = self.get_profile().get_shape();
555 let chin: f32 = self.get_profile().get_chin_height_dp();
556 let safe: SafeAreaInsets = compute_wearable_safe_area(
557 shape,
558 width.max(1) as f32,
559 height.max(1) as f32,
560 chin,
561 );
562 let compact: f32 =
563 compute_wearable_compact_scale(width.max(1) as f32, height.max(1) as f32);
564 self.set_safe_area(safe);
565 self.set_compact_scale(compact);
566 let scale: f32 = {
567 let rec: &mut WearableWindowRecord = self
568 .get_mut_windows()
569 .get_mut(&id)
570 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
571 let s: f32 = rec.get_state().get_scale_factor();
572 rec.get_mut_config().set_size(width.max(1), height.max(1));
573 rec.get_mut_state()
574 .set_logical_width(width.max(1) as f32)
575 .set_logical_height(height.max(1) as f32)
576 .set_physical_width(((width.max(1) as f32) * s).round() as u32)
577 .set_physical_height(((height.max(1) as f32) * s).round() as u32)
578 .set_safe_area(safe);
579 s
580 };
581 self.get_mut_pending_events().push(WindowEvent::Resized(
582 id,
583 width.max(1),
584 height.max(1),
585 scale,
586 ));
587 self.get_mut_pending_events()
588 .push(WindowEvent::SafeAreaChanged(id, safe));
589 Ok(())
590 }
591 WindowCommand::SetRenderMode(id, mode) => {
592 let rec: &mut WearableWindowRecord = self
593 .get_mut_windows()
594 .get_mut(&id)
595 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
596 rec.get_mut_config().get_mut_render().set_mode(mode);
597 Ok(())
598 }
599 WindowCommand::RequestRedraw(id) => {
600 if !self.get_windows().contains_key(&id) {
601 return Err(PlatformError::WindowNotFound(id.get_value()));
602 }
603 self.get_mut_pending_events()
604 .push(WindowEvent::RedrawRequested(id));
605 Ok(())
606 }
607 WindowCommand::Close(id) => {
608 if self.get_mut_windows().remove(&id).is_none() {
609 return Err(PlatformError::WindowNotFound(id.get_value()));
610 }
611 Ok(())
612 }
613 WindowCommand::SetPosition(id, _, _)
614 | WindowCommand::SetResizable(id, _)
615 | WindowCommand::SetDecorations(id, _)
616 | WindowCommand::SetAlwaysOnTop(id, _)
617 | WindowCommand::SetFullscreen(id, _)
618 | WindowCommand::SetVisible(id, _)
619 | WindowCommand::SetCursor(id, _)
620 | WindowCommand::SetIme(id, _, _)
621 | WindowCommand::SetStatusBarStyle(id, _) => {
622 if !self.get_windows().contains_key(&id) {
623 return Err(PlatformError::WindowNotFound(id.get_value()));
624 }
625 Ok(())
626 }
627 }
628 }
629}
630
631impl PlatformBackend for WearablePlatform {
633 fn kind(&self) -> PlatformKind {
643 PlatformKind::Wearable
644 }
645
646 fn capabilities(&self) -> PlatformCapabilities {
656 let mut caps: PlatformCapabilities =
657 PlatformCapabilities::for_platform(PlatformKind::Wearable);
658 caps.rotary_input = self.get_profile().get_has_rotary_crown();
659 caps.always_on_display = self.get_profile().get_supports_aod();
660 caps
661 }
662
663 fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
674 let id: WindowId = WindowId::new(self.get_next_id());
675 self.set_next_id(self.get_next_id().wrapping_add(1));
676 let profile: WearableProfile = self.get_profile();
677 let mut watch_cfg: WindowConfig = config.clone();
678 watch_cfg.set_size(profile.get_logical_width(), profile.get_logical_height());
679 let mut state: WindowState =
680 WindowState::from_config(id, &watch_cfg, profile.get_scale_factor());
681 state.set_safe_area(self.get_safe_area());
682 state.set_system_theme(self.get_theme());
683 state
684 .get_mut_display()
685 .set_refresh_rate_hz(profile.get_refresh_rate_hz().round() as u32);
686 let record: WearableWindowRecord = WearableWindowRecord {
687 config: watch_cfg,
688 state,
689 native_surface: 0,
690 last_frame: Framebuffer::allocate(
691 state.get_physical_width(),
692 state.get_physical_height(),
693 ),
694 present_count: 0,
695 ambient_tick_count: 0,
696 };
697 self.get_mut_windows().insert(id, record);
698 if self.primary_window_id().is_none() {
699 self.set_primary_window(Some(id));
700 }
701 Ok(state)
702 }
703
704 fn window_state(&self, id: WindowId) -> Option<WindowState> {
715 self.get_windows()
716 .get(&id)
717 .map(WearableWindowRecord::get_state)
718 }
719
720 fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle {
731 match self.get_windows().get(&id) {
732 Some(rec) => RawWindowHandle::WearableSurface {
733 native_handle: rec.get_native_surface(),
734 vendor_tag: self.get_profile().get_vendor().tag(),
735 },
736 None => RawWindowHandle::Headless,
737 }
738 }
739
740 fn poll_events(&mut self) -> Vec<WindowEvent> {
750 take(self.get_mut_pending_events())
751 }
752
753 fn set_frame_pump_paused(&mut self, paused: bool) {
760 self.set_pump_paused(paused);
761 }
762
763 fn is_frame_pump_paused(&self) -> bool {
773 self.get_pump_paused()
774 }
775
776 fn present_framebuffer(
788 &mut self,
789 id: WindowId,
790 framebuffer: &Framebuffer,
791 ) -> Result<(), PlatformError> {
792 let rec: &mut WearableWindowRecord = self
793 .get_mut_windows()
794 .get_mut(&id)
795 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
796 rec.set_last_frame(framebuffer.clone());
797 rec.set_present_count(rec.get_present_count().wrapping_add(1));
798 Ok(())
799 }
800
801 fn read_clipboard(&self) -> Option<String> {
811 None
812 }
813
814 fn write_clipboard(&mut self, _text: &str) -> Result<(), PlatformError> {
825 Err(PlatformError::Unsupported(format!(
826 "Clipboard is unsupported on {}",
827 self.get_profile().get_vendor().as_str()
828 )))
829 }
830
831 fn system_theme(&self) -> ThemeMode {
841 self.get_theme()
842 }
843
844 fn display_refresh_rate_hz(&self) -> f32 {
854 if self.get_ambient_mode() == WearableAmbientMode::AlwaysOnAmbient {
855 WEARABLE_AOD_REFRESH_HZ
856 } else {
857 self.get_profile().get_refresh_rate_hz()
858 }
859 }
860}
861
862impl PlatformStorage for WearablePlatform {
864 fn storage_get(&self, key: &str) -> Option<String> {
875 self.get_storage().get(key).cloned()
876 }
877
878 fn storage_set(&mut self, key: &str, value: &str) -> Result<(), PlatformError> {
890 let _: Option<String> = self
891 .get_mut_storage()
892 .insert(key.to_string(), value.to_string());
893 Ok(())
894 }
895
896 fn storage_remove(&mut self, key: &str) -> Result<(), PlatformError> {
907 let _: Option<String> = self.get_mut_storage().remove(key);
908 Ok(())
909 }
910
911 fn storage_clear(&mut self) -> Result<(), PlatformError> {
921 self.get_mut_storage().clear();
922 Ok(())
923 }
924}
925
926impl PlatformNavigation for WearablePlatform {
928 fn navigate_to(&mut self, route: &str) -> Result<(), PlatformError> {
939 if !route.is_empty() {
940 self.get_mut_route_stack().push(route.to_string());
941 }
942 Ok(())
943 }
944
945 fn navigate_back(&mut self, delta: u32) -> Result<(), PlatformError> {
956 for _ in 0..delta.max(1) {
957 if self.get_route_stack().len() > 1 {
958 let _: Option<String> = self.get_mut_route_stack().pop();
959 }
960 }
961 Ok(())
962 }
963
964 fn current_route(&self) -> &str {
974 self.get_route_stack()
975 .last()
976 .map(String::as_str)
977 .unwrap_or(WEARABLE_DEFAULT_ROUTE)
978 }
979}
980
981impl PlatformLifecycleBridge for WearablePlatform {
983 fn freeze_surface(&mut self) -> Vec<WindowEvent> {
993 self.enter_screen_off()
994 }
995
996 fn resume_surface(&mut self) -> Vec<WindowEvent> {
1006 self.exit_ambient()
1007 }
1008
1009 fn destroy_surface(&mut self) -> Vec<WindowEvent> {
1019 let wid: WindowId = self.primary_window_id().unwrap_or(WindowId::new(1));
1020 self.set_ambient_mode(WearableAmbientMode::ScreenOff);
1021 self.set_pump_paused(true);
1022 let mut events: Vec<WindowEvent> =
1023 self.get_mut_touch_tracker().cancel_all_on_focus_loss(wid);
1024 events.push(WindowEvent::System(SystemEvent::Terminate));
1025 events
1026 }
1027}
1028
1029impl PlatformHostAdapter for WearablePlatform {
1031 fn device_form_factor(&self) -> DeviceFormFactor {
1041 DeviceFormFactor::Watch
1042 }
1043
1044 fn host_vendor_name(&self) -> &'static str {
1054 self.get_profile().get_vendor().as_str()
1055 }
1056}