egui_pop_views/video/
plugin.rs1#[cfg(feature = "api")]
17use egui_pop_api::{Call, Ctx, Plugin, PluginError};
18#[cfg(feature = "api")]
19use egui_pop_core::debug::FnCollector;
20#[cfg(feature = "api")]
21use egui_pop_model::commands::CommandSpec;
22use egui_pop_model::ids::{CommandId, PluginId};
23#[cfg(feature = "api")]
24use serde_json::{Value as Json, json};
25
26#[cfg(feature = "api")]
27use super::Videos;
28
29pub const PLUGIN: PluginId = PluginId::from_static("egui_pop.video");
31
32pub const CMD_TOGGLE: CommandId = CommandId::from_static("video.toggle");
33pub const CMD_PLAY: CommandId = CommandId::from_static("video.play");
34pub const CMD_PAUSE: CommandId = CommandId::from_static("video.pause");
35pub const CMD_SEEK: CommandId = CommandId::from_static("video.seek");
36pub const CMD_VOLUME: CommandId = CommandId::from_static("video.volume");
37pub const CMD_MUTE: CommandId = CommandId::from_static("video.mute");
38
39#[cfg(feature = "api")]
43#[derive(Debug, Default)]
44pub struct VideoPlugin;
45
46#[cfg(feature = "api")]
47impl Plugin for VideoPlugin {
48 fn id(&self) -> PluginId {
49 PLUGIN
50 }
51
52 fn title(&self) -> String {
53 "Video".into()
54 }
55
56 fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
57 commands(ctx)?;
58 let egui = ctx.app().egui();
59 ctx.collector(
60 FnCollector::new(
61 "videos",
62 "The videos drawn with an id: their file, backend, controls, state, position and length (ms), volume, whether they play, loop or are muted, the loops made, the picture's size, a note or an error; the one that last had the focus, and how many have no id.",
63 )
64 .snapshot(move |_| Videos::of(&egui).describe()),
65 )?;
66 Ok(())
67 }
68}
69
70#[cfg(feature = "api")]
72fn target(args: &Json) -> Option<&str> {
73 args.get("video").and_then(Json::as_str)
74}
75
76#[cfg(feature = "api")]
77fn videos(call: &Call<'_>) -> Videos {
78 Videos::of(&call.app.egui())
79}
80
81#[cfg(feature = "api")]
82fn commands(ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
83 const CAT: &str = "Video";
84 let video = "tour (optional: the video that last had the focus)";
85 ctx.command(
86 CommandSpec::new(CMD_TOGGLE, "Play or pause a video", CAT)
87 .description(
88 "Plays or pauses a video (`video`, its id); from its end, plays it from the start.",
89 )
90 .args(json!({ "video": video })),
91 |call| videos(call).toggle(target(call.args)),
92 )?;
93 ctx.command(
94 CommandSpec::new(CMD_PLAY, "Play a video", CAT)
95 .description("Plays a video (`video`, its id).")
96 .args(json!({ "video": video })),
97 |call| videos(call).play(target(call.args)),
98 )?;
99 ctx.command(
100 CommandSpec::new(CMD_PAUSE, "Pause a video", CAT)
101 .description("Pauses a video (`video`, its id).")
102 .args(json!({ "video": video })),
103 |call| videos(call).pause(target(call.args)),
104 )?;
105 ctx.command(
106 CommandSpec::new(CMD_SEEK, "Move a video to a time", CAT)
107 .description("Moves a video (`video`) to a position (`seconds`), or by an amount (`by`, negative to go back), kept within it.")
108 .args(json!({ "video": video, "seconds": 12.5 })),
109 |call| {
110 let v = videos(call);
111 let video = target(call.args);
112 match (
113 call.args.get("seconds").and_then(Json::as_f64),
114 call.args.get("by").and_then(Json::as_f64),
115 ) {
116 (Some(s), _) => v.seek_to(video, s),
117 (None, Some(by)) => v.seek_by(video, by),
118 (None, None) => Err("`seconds` or `by` is a number".into()),
119 }
120 },
121 )?;
122 ctx.command(
123 CommandSpec::new(CMD_VOLUME, "Set a video's volume", CAT)
124 .description("Sets a video's volume (`volume`, from 0 to 1), or changes it (`by`); either gives the sound back.")
125 .args(json!({ "video": video, "volume": 0.5 })),
126 |call| {
127 let v = videos(call);
128 let video = target(call.args);
129 match (
130 call.args.get("volume").and_then(Json::as_f64),
131 call.args.get("by").and_then(Json::as_f64),
132 ) {
133 (Some(volume), _) => v.set_volume(video, volume as f32),
134 (None, Some(by)) => v.volume_by(video, by as f32),
135 (None, None) => Err("`volume` or `by` is a number".into()),
136 }
137 },
138 )?;
139 ctx.command(
140 CommandSpec::new(CMD_MUTE, "Mute a video", CAT)
141 .description("Mutes a video (`muted`: true) or gives its sound back (false); without `muted`, switches.")
142 .args(json!({ "video": video, "muted": true })),
143 |call| {
144 let muted = call.args.get("muted").and_then(Json::as_bool);
145 videos(call).set_muted(target(call.args), muted)
146 },
147 )?;
148 Ok(())
149}