mouser_mode_api/hosted.rs
1//! Views and media the host runs for a mode (mode-api 0.4, docs/mode-protocol.md
2//! §Host views and media). A mode registers them by id with
3//! [`crate::Host::hosted`] ([`crate::protocol::Out::Hosted`]): where each one
4//! goes and what it shows. Mouser then lays it out and draws it, loads the
5//! thumbnails, plays the media (pictures, video and sound, inline and
6//! fullscreen), takes its keys and clicks and redraws it as it animates or
7//! plays. The mode hears what happened as events:
8//!
9//! - `view.selected` `{id, index}`: the selection moved (keys, a click);
10//! - `view.activated` `{id, index}`: Enter or a double-click on an item;
11//! - `view.layout` `{id, layout}`: the layout key (`v`) switched it;
12//! - `media.state` `{id, playing, position, duration, fullscreen, error}`:
13//! a player started, paused, was sought, opened or closed fullscreen;
14//! - `media.ended` `{id}`: playback reached the end.
15//!
16//! Only send them when the host offers them: [`crate::protocol::Accepted::has`]
17//! with [`FEATURE_VIEWS`] / [`FEATURE_MEDIA`]. An older Mouser offers neither
18//! (and couldn't read the message), so draw the panel yourself there, e.g.
19//! with the `mouser-views` and `mouser-media` crates.
20//!
21//! Paths are on this computer and need `files.read`; without it Mouser
22//! draws the item's label and says so instead of reading the file.
23
24use crate::ui::Rect;
25
26/// [`crate::protocol::Accepted::features`]: the host runs [`Hosted::Items`].
27pub const FEATURE_VIEWS: &str = "views.1";
28/// [`crate::protocol::Accepted::features`]: the host runs [`Hosted::Media`].
29pub const FEATURE_MEDIA: &str = "media.1";
30
31/// One view or player the host runs for the mode.
32#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
33pub struct HostedView {
34 /// The mode's own name for it; events carry it back. Keep it stable
35 /// across views: selection, scroll and a playing video follow the id.
36 pub id: String,
37 /// Where it goes (window pixels, inside the mode's dock or panel).
38 pub rect: Rect,
39 /// Over the dock (`false`) or the floating panel (`true`).
40 #[serde(default)]
41 pub in_panel: bool,
42 pub content: Hosted,
43 /// Keep it registered without drawing it: an item view keeps its
44 /// selection and scroll, a player keeps playing (a clip scrolled out of
45 /// a transcript). It takes no keys while off screen.
46 #[serde(default)]
47 pub offscreen: bool,
48}
49
50/// What a hosted view shows.
51#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
52pub enum Hosted {
53 /// Items in a list, grid or carousel. Arrow keys, Home / End, Page Up /
54 /// Down move the selection while `focused`, Enter activates it and `v`
55 /// cycles the layout; clicks select, a second click activates.
56 Items {
57 layout: ViewLayout,
58 items: Vec<HostedItem>,
59 /// Move the selection here (applied when it changes from the last
60 /// one sent, so the user's own moves stick); `None`: keep it.
61 select: Option<u32>,
62 /// The view takes the navigation keys (one focused view at a time;
63 /// the last one wins).
64 focused: bool,
65 /// Grid tile side and carousel item height, px (`0`: Mouser picks).
66 tile: i32,
67 },
68 /// A picture, video or sound file: a thumbnail, or a player with play /
69 /// pause, progress and fullscreen (⤢ or a click). Fullscreen: space plays /
70 /// pauses, ← / → seek 10 s, `f` / Enter / Esc close.
71 Media {
72 /// Absolute path on this computer.
73 path: String,
74 /// Start playing as soon as it is shown (video and sound).
75 autoplay: bool,
76 },
77}
78
79/// How [`Hosted::Items`] lays out its items.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
81pub enum ViewLayout {
82 #[default]
83 List,
84 Grid,
85 Carousel,
86}
87
88impl ViewLayout {
89 pub fn name(self) -> &'static str {
90 match self {
91 ViewLayout::List => "list",
92 ViewLayout::Grid => "grid",
93 ViewLayout::Carousel => "carousel",
94 }
95 }
96
97 pub fn from_name(s: &str) -> Option<Self> {
98 [ViewLayout::List, ViewLayout::Grid, ViewLayout::Carousel].into_iter().find(|l| l.name() == s)
99 }
100
101 /// List → grid → carousel → list (the `v` key).
102 pub fn next(self) -> Self {
103 match self {
104 ViewLayout::List => ViewLayout::Grid,
105 ViewLayout::Grid => ViewLayout::Carousel,
106 ViewLayout::Carousel => ViewLayout::List,
107 }
108 }
109}
110
111/// One item of [`Hosted::Items`].
112#[derive(Debug, Clone, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
113pub struct HostedItem {
114 pub label: String,
115 /// A second column in the list (size, date ...).
116 #[serde(default)]
117 pub detail: String,
118 /// A picture file to show as its thumbnail (grid and carousel).
119 #[serde(default)]
120 pub image: Option<String>,
121 #[serde(default)]
122 pub icon: ItemIcon,
123}
124
125/// What a tile shows without a thumbnail.
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
127pub enum ItemIcon {
128 #[default]
129 None,
130 Folder,
131 /// A type badge (`PanelStyle::Badge` index).
132 Badge(u8),
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138
139 #[test]
140 fn layouts_cycle_and_round_trip() {
141 let mut l = ViewLayout::List;
142 for want in ["grid", "carousel", "list"] {
143 l = l.next();
144 assert_eq!(l.name(), want);
145 assert_eq!(ViewLayout::from_name(want), Some(l));
146 }
147 assert_eq!(ViewLayout::from_name("tree"), None);
148 }
149}