use std::rc::Rc;
use std::time::Duration;
use web_time::Instant;
use crate::fonts::FontSource;
use crate::geometry::{Color, Rect};
use crate::mouse_cursor::SystemMouseCursorKind;
use crate::restoration::{RestorationMap, RestorationUpdate};
use crate::system_context_menu::SystemContextMenuItem;
use crate::{View, ViewFocusDirection, ViewFocusState, ViewId};
pub type PlatformRef = Rc<dyn Platform>;
pub type ViewRef = Rc<dyn View>;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApplicationSwitcherDescription {
pub label: Option<String>,
pub primary_color: Option<u32>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SystemUiOverlayStyle {
pub system_navigation_bar_color: Option<Color>,
pub system_navigation_bar_divider_color: Option<Color>,
pub system_navigation_bar_icon_brightness: Option<Brightness>,
pub system_navigation_bar_contrast_enforced: Option<bool>,
pub status_bar_color: Option<Color>,
pub status_bar_brightness: Option<Brightness>,
pub status_bar_icon_brightness: Option<Brightness>,
pub system_status_bar_contrast_enforced: Option<bool>,
}
impl SystemUiOverlayStyle {
pub const LIGHT: SystemUiOverlayStyle = SystemUiOverlayStyle {
system_navigation_bar_color: Some(Color::new(0xFF000000)),
system_navigation_bar_icon_brightness: Some(Brightness::Light),
status_bar_icon_brightness: Some(Brightness::Light),
status_bar_brightness: Some(Brightness::Dark),
system_navigation_bar_divider_color: None,
system_navigation_bar_contrast_enforced: None,
status_bar_color: None,
system_status_bar_contrast_enforced: None,
};
pub const DARK: SystemUiOverlayStyle = SystemUiOverlayStyle {
system_navigation_bar_color: Some(Color::new(0xFF000000)),
system_navigation_bar_icon_brightness: Some(Brightness::Light),
status_bar_icon_brightness: Some(Brightness::Dark),
status_bar_brightness: Some(Brightness::Light),
system_navigation_bar_divider_color: None,
system_navigation_bar_contrast_enforced: None,
status_bar_color: None,
system_status_bar_contrast_enforced: None,
};
pub const fn new() -> SystemUiOverlayStyle {
SystemUiOverlayStyle {
system_navigation_bar_color: None,
system_navigation_bar_divider_color: None,
system_navigation_bar_icon_brightness: None,
system_navigation_bar_contrast_enforced: None,
status_bar_color: None,
status_bar_brightness: None,
status_bar_icon_brightness: None,
system_status_bar_contrast_enforced: None,
}
}
pub fn system_navigation_bar_color(mut self, color: Color) -> SystemUiOverlayStyle {
self.system_navigation_bar_color = Some(color);
self
}
pub fn system_navigation_bar_divider_color(mut self, color: Color) -> SystemUiOverlayStyle {
self.system_navigation_bar_divider_color = Some(color);
self
}
pub fn system_navigation_bar_icon_brightness(
mut self,
brightness: Brightness,
) -> SystemUiOverlayStyle {
self.system_navigation_bar_icon_brightness = Some(brightness);
self
}
pub fn system_navigation_bar_contrast_enforced(
mut self,
enforced: bool,
) -> SystemUiOverlayStyle {
self.system_navigation_bar_contrast_enforced = Some(enforced);
self
}
pub fn status_bar_color(mut self, color: Color) -> SystemUiOverlayStyle {
self.status_bar_color = Some(color);
self
}
pub fn status_bar_brightness(mut self, brightness: Brightness) -> SystemUiOverlayStyle {
self.status_bar_brightness = Some(brightness);
self
}
pub fn status_bar_icon_brightness(mut self, brightness: Brightness) -> SystemUiOverlayStyle {
self.status_bar_icon_brightness = Some(brightness);
self
}
pub fn system_status_bar_contrast_enforced(mut self, enforced: bool) -> SystemUiOverlayStyle {
self.system_status_bar_contrast_enforced = Some(enforced);
self
}
pub fn copy_with(&self) -> SystemUiOverlayStyle {
*self
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HapticFeedbackType {
Vibrate,
LightImpact,
MediumImpact,
HeavyImpact,
SelectionClick,
SuccessNotification,
WarningNotification,
ErrorNotification,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Brightness {
Dark,
Light,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AppLifecycleState {
Detached,
Resumed,
Inactive,
Hidden,
Paused,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AppExitResponse {
Exit,
Cancel,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum TargetPlatform {
Android,
Fuchsia,
IOS,
Linux,
MacOS,
Windows,
}
pub trait Platform: 'static {
fn target_platform(&self) -> TargetPlatform;
fn open_image_codec(&self, bytes: std::sync::Arc<[u8]>) -> crate::ImageCodecFuture {
let _ = bytes;
Box::pin(std::future::ready(Err(crate::ImageDecodeError::NoDecoder)))
}
fn handles_text_editing_keys(&self) -> bool {
false
}
fn locales(&self) -> Vec<crate::Locale> {
Vec::new()
}
fn set_application_locale(&self, locale: &crate::Locale) {
let _ = locale;
}
fn default_route_name(&self) -> String {
String::from("/")
}
fn set_application_switcher_description(&self, description: &ApplicationSwitcherDescription) {
let _ = description;
}
fn set_system_ui_overlay_style(&self, style: &SystemUiOverlayStyle) {
let _ = style;
}
fn haptic_feedback(&self, kind: HapticFeedbackType) {
let _ = kind;
}
fn clipboard_set_data(&self, text: &str) {
let _ = text;
}
fn clipboard_get_data(&self) -> Option<String> {
None
}
fn clipboard_has_strings(&self) -> bool {
false
}
fn supports_showing_system_context_menu(&self) -> bool {
false
}
fn show_system_context_menu(&self, target_rect: Rect, items: Option<&[SystemContextMenuItem]>) {
let _ = (target_rect, items);
}
fn hide_system_context_menu(&self) {}
fn look_up(&self, text: &str) {
let _ = text;
}
fn search_web(&self, text: &str) {
let _ = text;
}
fn share(&self, text: &str) {
let _ = text;
}
fn platform_brightness(&self) -> Brightness {
Brightness::Light
}
fn request_frame(&self);
fn now(&self) -> Instant;
fn wake_at(&self, deadline: Instant);
fn views(&self) -> Vec<ViewRef>;
fn view(&self, id: ViewId) -> Option<ViewRef>;
fn request_view_focus_change(
&self,
view_id: ViewId,
state: ViewFocusState,
direction: ViewFocusDirection,
) {
let _ = (view_id, state, direction);
}
fn implicit_view(&self) -> Option<ViewRef>;
fn font_source(&self) -> Option<Box<dyn FontSource>> {
None
}
fn activate_system_cursor(&self, device: i64, kind: SystemMouseCursorKind) {
let _ = (device, kind);
}
fn restoration_get(&self) -> Option<RestorationUpdate> {
None
}
fn restoration_put(&self, data: RestorationMap) {
let _ = data;
}
}
pub struct InertPlatform;
impl Platform for InertPlatform {
fn target_platform(&self) -> TargetPlatform {
TargetPlatform::Android
}
fn request_frame(&self) {}
fn now(&self) -> Instant {
Instant::now()
}
fn wake_at(&self, _deadline: Instant) {}
fn views(&self) -> Vec<ViewRef> {
Vec::new()
}
fn view(&self, _id: ViewId) -> Option<ViewRef> {
None
}
fn implicit_view(&self) -> Option<ViewRef> {
None
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Frame {
pub elapsed: Duration,
}
#[cfg(test)]
mod tests {
use super::{Brightness, InertPlatform, Platform, TargetPlatform};
#[test]
fn inert_platform_is_android() {
assert_eq!(InertPlatform.target_platform(), TargetPlatform::Android);
}
#[test]
fn inert_platform_brightness_is_light() {
assert_eq!(InertPlatform.platform_brightness(), Brightness::Light);
}
}