use crate::ui::Rect;
pub const FEATURE_VIEWS: &str = "views.1";
pub const FEATURE_MEDIA: &str = "media.1";
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct HostedView {
pub id: String,
pub rect: Rect,
#[serde(default)]
pub in_panel: bool,
pub content: Hosted,
#[serde(default)]
pub offscreen: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum Hosted {
Items {
layout: ViewLayout,
items: Vec<HostedItem>,
select: Option<u32>,
focused: bool,
tile: i32,
},
Media {
path: String,
autoplay: bool,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum ViewLayout {
#[default]
List,
Grid,
Carousel,
}
impl ViewLayout {
pub fn name(self) -> &'static str {
match self {
ViewLayout::List => "list",
ViewLayout::Grid => "grid",
ViewLayout::Carousel => "carousel",
}
}
pub fn from_name(s: &str) -> Option<Self> {
[ViewLayout::List, ViewLayout::Grid, ViewLayout::Carousel].into_iter().find(|l| l.name() == s)
}
pub fn next(self) -> Self {
match self {
ViewLayout::List => ViewLayout::Grid,
ViewLayout::Grid => ViewLayout::Carousel,
ViewLayout::Carousel => ViewLayout::List,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub struct HostedItem {
pub label: String,
#[serde(default)]
pub detail: String,
#[serde(default)]
pub image: Option<String>,
#[serde(default)]
pub icon: ItemIcon,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
pub enum ItemIcon {
#[default]
None,
Folder,
Badge(u8),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn layouts_cycle_and_round_trip() {
let mut l = ViewLayout::List;
for want in ["grid", "carousel", "list"] {
l = l.next();
assert_eq!(l.name(), want);
assert_eq!(ViewLayout::from_name(want), Some(l));
}
assert_eq!(ViewLayout::from_name("tree"), None);
}
}