1use std::fmt;
6
7use egui_pop_model::ids::{CommandId, HookId, MenuId, OwnerId, SettingId};
8use egui_pop_model::menus::MenuItem;
9use egui_pop_model::values::Descriptor;
10use serde::Serialize;
11
12use crate::app::AppHandle;
13
14#[derive(Clone, Debug, PartialEq, Serialize)]
16pub struct PaletteEntry {
17 pub command: CommandId,
18 pub title: String,
19 pub category: String,
20 pub keys: Vec<String>,
22}
23
24#[derive(Clone, Debug, PartialEq, Serialize)]
26pub struct SettingEntry {
27 pub id: SettingId,
28 pub path: String,
30 pub owner: OwnerId,
31 pub section: String,
33 pub descriptor: Descriptor,
34}
35
36pub type AlterFn<T> = Box<dyn Fn(&mut Vec<T>)>;
38
39pub enum Alter {
41 Menu(MenuId, AlterFn<MenuItem>),
42 Palette(AlterFn<PaletteEntry>),
43 Settings(AlterFn<SettingEntry>),
44}
45
46impl fmt::Debug for Alter {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 Alter::Menu(m, _) => write!(f, "Menu({m})"),
50 Alter::Palette(_) => write!(f, "Palette"),
51 Alter::Settings(_) => write!(f, "Settings"),
52 }
53 }
54}
55
56#[derive(Debug)]
59pub struct Hook {
60 pub id: HookId,
61 pub alter: Alter,
62 pub priority: i32,
63 pub after: Vec<HookId>,
64}
65
66impl Hook {
67 pub fn menu(id: HookId, menu: MenuId, f: impl Fn(&mut Vec<MenuItem>) + 'static) -> Hook {
69 Hook {
70 id,
71 alter: Alter::Menu(menu, Box::new(f)),
72 priority: 0,
73 after: Vec::new(),
74 }
75 }
76
77 pub fn palette(id: HookId, f: impl Fn(&mut Vec<PaletteEntry>) + 'static) -> Hook {
79 Hook {
80 id,
81 alter: Alter::Palette(Box::new(f)),
82 priority: 0,
83 after: Vec::new(),
84 }
85 }
86
87 pub fn settings(id: HookId, f: impl Fn(&mut Vec<SettingEntry>) + 'static) -> Hook {
89 Hook {
90 id,
91 alter: Alter::Settings(Box::new(f)),
92 priority: 0,
93 after: Vec::new(),
94 }
95 }
96
97 pub fn priority(mut self, priority: i32) -> Hook {
98 self.priority = priority;
99 self
100 }
101
102 pub fn after(mut self, hook: HookId) -> Hook {
104 self.after.push(hook);
105 self
106 }
107}
108
109#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
111#[serde(tag = "event", content = "plugin", rename_all = "snake_case")]
112pub enum Lifecycle {
113 Started,
115 Connected(egui_pop_model::ids::PluginId),
117 Disconnected(egui_pop_model::ids::PluginId),
119 Rebuilt,
121 Pass,
127}
128
129impl Lifecycle {
130 pub fn kind(&self) -> &'static str {
132 match self {
133 Lifecycle::Started => "started",
134 Lifecycle::Connected(_) => "connected",
135 Lifecycle::Disconnected(_) => "disconnected",
136 Lifecycle::Rebuilt => "rebuilt",
137 Lifecycle::Pass => "pass",
138 }
139 }
140}
141
142#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
144#[serde(rename_all = "snake_case")]
145pub enum On {
146 Started,
147 Connected,
148 Disconnected,
149 Rebuilt,
150 Pass,
153}
154
155impl On {
156 pub(crate) fn matches(self, e: &Lifecycle) -> bool {
157 matches!(
158 (self, e),
159 (On::Started, Lifecycle::Started)
160 | (On::Connected, Lifecycle::Connected(_))
161 | (On::Disconnected, Lifecycle::Disconnected(_))
162 | (On::Rebuilt, Lifecycle::Rebuilt)
163 | (On::Pass, Lifecycle::Pass)
164 )
165 }
166}
167
168pub type LifecycleFn = Box<dyn FnMut(&AppHandle, &Lifecycle)>;