Skip to main content

openlogi_core/binding/
effect.rs

1//! A platform-neutral synthesis IR.
2//!
3//! [`Action`] has one variant per user-facing behaviour (52 of them), but the
4//! three `openlogi-inject` backends don't care about most of that
5//! granularity — they care about *mechanism*: "press this chord", "click
6//! this mouse button", "fire this media key", "there is no portable way to
7//! do this, use the OS-specific path". [`Action::effect`] is the single
8//! exhaustive match that sorts every variant into one of those buckets, so
9//! each backend matches on ~10-variant [`Effect`] instead of re-deriving the
10//! full `Action` vocabulary three times.
11//!
12//! [`Shortcut`], [`MediaKey`], and [`NativeAction`] are semantic, not
13//! mechanical: the same named shortcut is not the same chord on every OS
14//! (`BrowserBack` is ⌘\[ on macOS, Alt+Left on Linux, and a dedicated
15//! virtual key with no modifier on Windows), so each backend owns its own
16//! lookup table from these enums to its platform's key/API. The
17//! exhaustiveness check on those enums is what keeps all three backends
18//! honest when a variant is added — a backend that forgets a case fails to
19//! compile rather than silently no-op-ing.
20
21use super::action::{Action, WorkflowStep};
22use super::key_combo::KeyCombo;
23
24/// What firing an [`Action`] should do, independent of platform.
25///
26/// `openlogi_inject`'s per-OS backends match on this instead of on
27/// [`Action`] directly.
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub enum Effect<'a> {
30    /// Suppress the input entirely: no OS event at all.
31    None,
32    /// Synthesise a physical mouse button click.
33    Click(MouseButton),
34    /// Fire a named shortcut. Each backend maps this to its own platform
35    /// chord (or, for the rare shortcut with no chord at all on some OS, a
36    /// dedicated native code path) — see that backend's `combo` table.
37    Shortcut(Shortcut),
38    /// Press an already-resolved keyboard chord: a user-recorded
39    /// [`Action::CustomShortcut`], or a workflow's `PressKey` step.
40    Key(&'a KeyCombo),
41    /// Synthesise one scroll tick. `dx`/`dy` are unit direction (-1/0/1);
42    /// each backend applies its own tick magnitude.
43    Scroll {
44        /// Horizontal direction: -1 left, 1 right, 0 none.
45        dx: i8,
46        /// Vertical direction: -1 down, 1 up, 0 none.
47        dy: i8,
48    },
49    /// Fire a media/volume key. Every backend reaches these through a
50    /// dedicated OS mechanism rather than an ordinary keyboard chord.
51    Media(MediaKey),
52    /// A window-manager or power action with no shared cross-platform
53    /// chord — each backend has its own dedicated handling, which may be a
54    /// debug-logged no-op where the OS has no equivalent at all.
55    Native(NativeAction),
56    /// A power-user scripting escape hatch.
57    Script(Script<'a>),
58    /// Type this text via unicode input.
59    Text(&'a str),
60    /// Handled entirely by the agent/hook layer — DPI presets, SmartShift,
61    /// the Actions Ring, and launching an application. The injector logs
62    /// and does nothing.
63    ///
64    /// [`Action::OpenApplication`] is included here even though
65    /// `openlogi_inject::execute` does open it: that happens in the
66    /// platform-independent dispatcher *before* a backend ever sees the
67    /// action (the config target is opened via the `opener` crate, not
68    /// through any per-OS synthesis path), so from a backend's point of
69    /// view it is exactly as much a no-op as the DPI actions.
70    AgentSide,
71}
72
73/// A physical mouse button an [`Effect::Click`] should press.
74#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
75pub enum MouseButton {
76    /// Primary button.
77    Left,
78    /// Secondary button.
79    Right,
80    /// Wheel-click button.
81    Middle,
82    /// Extra "back" side button (button 4).
83    Back,
84    /// Extra "forward" side button (button 5).
85    Forward,
86}
87
88/// A named shortcut whose chord is the same *concept* on every OS but not
89/// the same keys.
90///
91/// Each backend owns a `Shortcut -> KeyCombo` table (plus, for the couple of
92/// shortcuts a given OS has no ordinary chord for, a small override) rather
93/// than sharing one table — see the per-backend `combo`/`press_shortcut`.
94#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, strum::VariantArray)]
95pub enum Shortcut {
96    /// Copy the selection.
97    Copy,
98    /// Paste from the clipboard.
99    Paste,
100    /// Cut the selection.
101    Cut,
102    /// Undo the last action.
103    Undo,
104    /// Redo the last undone action.
105    Redo,
106    /// Select all content.
107    SelectAll,
108    /// Open find/search.
109    Find,
110    /// Save the current document.
111    Save,
112    /// Navigate backward in browser history.
113    BrowserBack,
114    /// Navigate forward in browser history.
115    BrowserForward,
116    /// Open a new tab.
117    NewTab,
118    /// Close the current tab.
119    CloseTab,
120    /// Reopen the last closed tab.
121    ReopenTab,
122    /// Switch to the next tab.
123    NextTab,
124    /// Switch to the previous tab.
125    PrevTab,
126    /// Reload the current page.
127    ReloadPage,
128}
129
130impl Shortcut {
131    /// Every named shortcut, in declaration order.
132    ///
133    /// `#[derive(strum::VariantArray)]` generates this straight from the
134    /// enum's variant list at compile time, so it cannot go stale the way a
135    /// hand-written array literal could: there is no second, independently
136    /// editable list for it to drift from — a variant that's missing here
137    /// would mean the enum itself doesn't have it. The single shared
138    /// iteration source for each backend's `Shortcut -> KeyCombo`
139    /// table-completeness test.
140    pub const ALL: &'static [Shortcut] = <Shortcut as strum::VariantArray>::VARIANTS;
141}
142
143/// A media/volume key.
144///
145/// Every backend reaches these through a dedicated OS mechanism — NX
146/// system-defined keys on macOS, MPRIS/XF86 keys on Linux, dedicated media
147/// virtual keys on Windows — rather than an ordinary keyboard chord.
148#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
149pub enum MediaKey {
150    /// Toggle play/pause.
151    PlayPause,
152    /// Skip to the next track.
153    NextTrack,
154    /// Go back to the previous track.
155    PrevTrack,
156    /// Increase system volume.
157    VolumeUp,
158    /// Decrease system volume.
159    VolumeDown,
160    /// Toggle system mute.
161    Mute,
162}
163
164/// A window-manager or power action with no shared cross-platform chord.
165///
166/// Each backend reaches it through its own dedicated OS API — or, where the
167/// OS has no equivalent at all, a debug-logged no-op.
168#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
169pub enum NativeAction {
170    /// Show all windows across spaces (macOS Mission Control).
171    MissionControl,
172    /// Show all windows of the frontmost app (macOS App Exposé).
173    AppExpose,
174    /// Switch to the previous desktop/space.
175    PreviousDesktop,
176    /// Switch to the next desktop/space.
177    NextDesktop,
178    /// Hide all windows to reveal the desktop.
179    ShowDesktop,
180    /// Open the application launcher.
181    LaunchpadShow,
182    /// Lock the screen.
183    LockScreen,
184    /// Capture a full-screen screenshot.
185    Screenshot,
186    /// Capture a selected screen region.
187    CaptureRegion,
188    /// Put the computer to sleep.
189    Sleep,
190}
191
192/// A power-user scripting escape hatch, borrowed from the originating
193/// [`Action::RunAppleScript`], [`Action::RunShellCommand`], or
194/// [`Action::Workflow`].
195#[derive(Clone, Copy, Debug, PartialEq, Eq)]
196pub enum Script<'a> {
197    /// Run an AppleScript source string. macOS-only; other backends warn.
198    AppleScript(&'a str),
199    /// Run a shell command string.
200    ShellCommand(&'a str),
201    /// Run an ordered sequence of workflow steps.
202    Workflow(&'a [WorkflowStep]),
203}
204
205impl Action {
206    /// Classify this action into the platform-neutral [`Effect`] IR that
207    /// `openlogi-inject`'s backends dispatch on.
208    ///
209    /// This is the single exhaustive match over the full [`Action`]
210    /// vocabulary that a new backend needs — see the module docs.
211    #[must_use]
212    pub fn effect(&self) -> Effect<'_> {
213        match self {
214            Action::None => Effect::None,
215
216            Action::LeftClick => Effect::Click(MouseButton::Left),
217            Action::RightClick => Effect::Click(MouseButton::Right),
218            Action::MiddleClick => Effect::Click(MouseButton::Middle),
219            Action::MouseBack => Effect::Click(MouseButton::Back),
220            Action::MouseForward => Effect::Click(MouseButton::Forward),
221
222            Action::Copy => Effect::Shortcut(Shortcut::Copy),
223            Action::Paste => Effect::Shortcut(Shortcut::Paste),
224            Action::Cut => Effect::Shortcut(Shortcut::Cut),
225            Action::Undo => Effect::Shortcut(Shortcut::Undo),
226            Action::Redo => Effect::Shortcut(Shortcut::Redo),
227            Action::SelectAll => Effect::Shortcut(Shortcut::SelectAll),
228            Action::Find => Effect::Shortcut(Shortcut::Find),
229            Action::Save => Effect::Shortcut(Shortcut::Save),
230
231            Action::BrowserBack => Effect::Shortcut(Shortcut::BrowserBack),
232            Action::BrowserForward => Effect::Shortcut(Shortcut::BrowserForward),
233            Action::NewTab => Effect::Shortcut(Shortcut::NewTab),
234            Action::CloseTab => Effect::Shortcut(Shortcut::CloseTab),
235            Action::ReopenTab => Effect::Shortcut(Shortcut::ReopenTab),
236            Action::NextTab => Effect::Shortcut(Shortcut::NextTab),
237            Action::PrevTab => Effect::Shortcut(Shortcut::PrevTab),
238            Action::ReloadPage => Effect::Shortcut(Shortcut::ReloadPage),
239
240            Action::MissionControl => Effect::Native(NativeAction::MissionControl),
241            Action::AppExpose => Effect::Native(NativeAction::AppExpose),
242            Action::PreviousDesktop => Effect::Native(NativeAction::PreviousDesktop),
243            Action::NextDesktop => Effect::Native(NativeAction::NextDesktop),
244            Action::ShowDesktop => Effect::Native(NativeAction::ShowDesktop),
245            Action::LaunchpadShow => Effect::Native(NativeAction::LaunchpadShow),
246
247            Action::LockScreen => Effect::Native(NativeAction::LockScreen),
248            Action::Screenshot => Effect::Native(NativeAction::Screenshot),
249            Action::CaptureRegion => Effect::Native(NativeAction::CaptureRegion),
250            Action::Sleep => Effect::Native(NativeAction::Sleep),
251
252            Action::PlayPause => Effect::Media(MediaKey::PlayPause),
253            Action::NextTrack => Effect::Media(MediaKey::NextTrack),
254            Action::PrevTrack => Effect::Media(MediaKey::PrevTrack),
255            Action::VolumeUp => Effect::Media(MediaKey::VolumeUp),
256            Action::VolumeDown => Effect::Media(MediaKey::VolumeDown),
257            Action::MuteVolume => Effect::Media(MediaKey::Mute),
258
259            // DPI/SmartShift/the Actions Ring/OpenApplication are all handled
260            // above (or beside) the injector — see `Effect::AgentSide`.
261            Action::CycleDpiPresets
262            | Action::SetDpiPreset(_)
263            | Action::ToggleSmartShift
264            | Action::ShowActionsRing
265            | Action::OpenApplication(_) => Effect::AgentSide,
266
267            Action::ScrollUp => Effect::Scroll { dx: 0, dy: 1 },
268            Action::ScrollDown => Effect::Scroll { dx: 0, dy: -1 },
269            Action::HorizontalScrollLeft => Effect::Scroll { dx: -1, dy: 0 },
270            Action::HorizontalScrollRight => Effect::Scroll { dx: 1, dy: 0 },
271
272            Action::CustomShortcut(combo) => Effect::Key(combo),
273
274            Action::TypeText(text) => Effect::Text(text),
275            Action::RunAppleScript(src) => Effect::Script(Script::AppleScript(src)),
276            Action::RunShellCommand(cmd) => Effect::Script(Script::ShellCommand(cmd)),
277            Action::Workflow(steps) => Effect::Script(Script::Workflow(steps)),
278        }
279    }
280}