use crate::prop::{PropEnum, PropValue};
use crate::ui::Ui;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScreenOrientation {
#[default]
Any,
Portrait,
Landscape,
SensorPortrait,
SensorLandscape,
}
impl ScreenOrientation {
pub const ALL: [ScreenOrientation; 5] =
[ScreenOrientation::Any, ScreenOrientation::Portrait, ScreenOrientation::Landscape, ScreenOrientation::SensorPortrait, ScreenOrientation::SensorLandscape];
pub fn parse(name: &str) -> Option<ScreenOrientation> {
let name = name.trim();
if name.is_empty() { Some(ScreenOrientation::Any) } else { <Self as PropEnum>::from_name(name) }
}
pub fn is_portrait(self) -> bool {
matches!(self, ScreenOrientation::Portrait | ScreenOrientation::SensorPortrait)
}
pub fn is_landscape(self) -> bool {
matches!(self, ScreenOrientation::Landscape | ScreenOrientation::SensorLandscape)
}
}
impl PropEnum for ScreenOrientation {
const NAMES: &'static [&'static str] = &["any", "portrait", "landscape", "sensor_portrait", "sensor_landscape"];
fn from_index(index: usize) -> Option<Self> {
Self::ALL.get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FormOrientation {
#[default]
Project,
Any,
Portrait,
Landscape,
SensorPortrait,
SensorLandscape,
}
impl FormOrientation {
pub const ALL: [FormOrientation; 6] = [
FormOrientation::Project,
FormOrientation::Any,
FormOrientation::Portrait,
FormOrientation::Landscape,
FormOrientation::SensorPortrait,
FormOrientation::SensorLandscape,
];
pub fn own(self) -> Option<ScreenOrientation> {
match self {
FormOrientation::Project => None,
FormOrientation::Any => Some(ScreenOrientation::Any),
FormOrientation::Portrait => Some(ScreenOrientation::Portrait),
FormOrientation::Landscape => Some(ScreenOrientation::Landscape),
FormOrientation::SensorPortrait => Some(ScreenOrientation::SensorPortrait),
FormOrientation::SensorLandscape => Some(ScreenOrientation::SensorLandscape),
}
}
}
impl PropEnum for FormOrientation {
const NAMES: &'static [&'static str] = &["project", "any", "portrait", "landscape", "sensor_portrait", "sensor_landscape"];
fn from_index(index: usize) -> Option<Self> {
Self::ALL.get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
#[derive(Default)]
struct OrientationState {
app: ScreenOrientation,
over: Option<ScreenOrientation>,
}
impl Ui {
pub fn set_app_orientation(&mut self, orientation: ScreenOrientation) {
if self.app_orientation() != orientation {
self.extension::<OrientationState>().app = orientation;
self.request_repaint();
}
}
pub fn app_orientation(&self) -> ScreenOrientation {
self.extension_ref::<OrientationState>().map_or_else(ScreenOrientation::default, |s| s.app)
}
pub fn set_orientation_override(&mut self, orientation: Option<ScreenOrientation>) {
self.extension::<OrientationState>().over = orientation;
self.request_repaint();
}
pub fn form_orientation(&self, form: crate::component::ComponentId) -> Option<ScreenOrientation> {
match self.get_prop(form, "orientation") {
Ok(PropValue::Enum(name)) => FormOrientation::from_name(&name).and_then(FormOrientation::own),
_ => None,
}
}
pub fn requested_orientation(&self) -> ScreenOrientation {
let state = self.extension_ref::<OrientationState>();
if let Some(over) = state.and_then(|s| s.over) {
return over;
}
let app = state.map_or_else(ScreenOrientation::default, |s| s.app);
self.open_forms.last().and_then(|f| self.form_orientation(f.form)).unwrap_or(app)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_names_round_trip() {
for o in ScreenOrientation::ALL {
assert_eq!(ScreenOrientation::parse(o.name()), Some(o));
}
for o in FormOrientation::ALL {
assert_eq!(FormOrientation::from_name(o.name()), Some(o));
}
assert_eq!(ScreenOrientation::parse(""), Some(ScreenOrientation::Any));
assert_eq!(ScreenOrientation::parse("sideways"), None);
}
}