picoframe_core/lib.rs
1//! Core types and the base frame plugin for picoframe Tauri apps.
2//!
3//! Every picoframe plugin command returns a [`CliResult`] — a small, uniform
4//! envelope the frontend's typed `defineCommand` bindings unwrap. This mirrors
5//! the engineer-assist `CliResult` pattern but trimmed to the essentials.
6
7use serde::Serialize;
8use serde_json::Value;
9use tauri::{
10 plugin::{Builder, TauriPlugin},
11 AppHandle, Runtime,
12};
13
14#[cfg(target_os = "macos")]
15mod mouse_nav;
16
17/// Uniform result envelope returned by picoframe plugin commands.
18#[derive(Debug, Clone, Serialize)]
19pub struct CliResult {
20 pub success: bool,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 pub data: Option<Value>,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 pub error: Option<String>,
25}
26
27impl CliResult {
28 /// A successful result carrying a JSON payload.
29 pub fn ok(data: Value) -> Self {
30 Self { success: true, data: Some(data), error: None }
31 }
32
33 /// A failed result carrying an error message.
34 pub fn err(message: impl Into<String>) -> Self {
35 Self { success: false, data: None, error: Some(message.into()) }
36 }
37}
38
39/// The base frame plugin. Registered first in every picoframe app's builder.
40///
41/// On macOS its `setup` installs a native NSEvent monitor that emits `mouse-nav`
42/// for the X1/X2 mouse buttons (the frame's `useMouseNavigation` hook handles the
43/// DOM path on Windows/Linux). It registers no commands yet — window/theme
44/// commands land later — and anchors the generated `main.rs`
45/// `.plugin(picoframe_core::init())` call above the plugin markers.
46pub fn init<R: Runtime>() -> TauriPlugin<R> {
47 Builder::new("picoframe")
48 .setup(|_app, _api| {
49 #[cfg(target_os = "macos")]
50 mouse_nav::install(_app.clone());
51 Ok(())
52 })
53 .build()
54}
55
56/// Reveal the app's `main` window with a background colour matching the OS
57/// light/dark appearance, so no default-white webview frame flashes before the
58/// UI paints. The native window pane that appears first follows the OS theme, so
59/// matching the webview background to it keeps the two seamless.
60///
61/// The `main` window must be created hidden (`"visible": false` in
62/// `tauri.conf.json`): the webview paints white the instant it becomes visible,
63/// too early for any in-page script to prevent. Call this from the app's
64/// `.setup()` hook, where config windows already exist (they do not during
65/// plugin setup). A missing window or a failed colour/show call is ignored - the
66/// worst case is the unfixed flash, never a hidden window on the happy path.
67pub fn reveal_main_window<R: Runtime>(app: &AppHandle<R>) {
68 use tauri::{window::Color, Manager, Theme};
69
70 let Some(window) = app.get_webview_window("main") else {
71 return;
72 };
73
74 // Mirrors picoframe's `--background` tokens: dark is hsl(240 6% 7%), which is
75 // rgb(17, 17, 19); light is white.
76 let color = match window.theme() {
77 Ok(Theme::Dark) => Color(17, 17, 19, 255),
78 _ => Color(255, 255, 255, 255),
79 };
80 let _ = window.set_background_color(Some(color));
81 let _ = window.show();
82}
83
84/// The single disk store file picoframe apps share, in the app data dir. Both the JS
85/// side (`@picoframe/store`'s `createTauriStore`) and Rust plugins open this file, so
86/// no plugin needs to own "the" store. Namespace keys by convention, e.g. `"hello.draft"`.
87pub const STORE_PATH: &str = "picoframe.json";
88
89/// Handle to the shared picoframe store for the Rust side. Requires the app to register
90/// `tauri_plugin_store::Builder::default().build()` (the CLI app template does this).
91pub fn store<R: Runtime>(
92 app: &AppHandle<R>,
93) -> Result<std::sync::Arc<tauri_plugin_store::Store<R>>, tauri_plugin_store::Error> {
94 use tauri_plugin_store::StoreExt;
95 app.store(STORE_PATH)
96}