diff --git a/.gitignore b/.gitignore index b37a489..96fcc1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,21 @@ +# Python virtual environment and cache +.venv/ +__pycache__/ +*.pyc + +# IDE settings +.idea +.vscode +.code + +# AI agents configurations +.claude/ +.agents/ +.codex/ + +# Other dist/ src-tauri/target/ src-tauri/gen/ tests/kicad-demos/ *.tmp.png -__pycache__/ diff --git a/CLAUDE.md b/CLAUDE.md index 050cbe4..2f89673 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -137,6 +137,13 @@ README doesn't: conventions, environment and pitfalls. header, adds drag and drop, live reload on `folder-changed` (keeps tab, sheet and selection) and `index.html#folder=[&project=…][&commit=]`. +- Folder chooser (`pickFolder`, tauri.js → `pick_folder` in + `src-tauri/src/picker.rs`): on Linux GTK's `FileChooserNative` with + `GTK_USE_PORTAL=1` (set at start-up, removed for children by + `clean_child_env`), so the desktop's own dialog shows (KDE: Dolphin's); + the dialog plugin's rfd is GTK3-only. Elsewhere / on failure: the plugin. +- Gallery zoom: Ctrl + wheel in Projects scales the card grid (CSS `zoom`, + `src/desktop/card-zoom.js`, localStorage `kivi.cardZoom`), Ctrl+0 resets. - `src-tauri/src/lib.rs` commands: - `open_folder` (lists the folder and starts a 500 ms debounced watcher that emits `folder-changed` for KiCad and model files) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5c09304..dc0b58b 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2022,6 +2022,7 @@ name = "kivi-desktop" version = "0.1.0" dependencies = [ "git2", + "gtk", "notify", "notify-debouncer-mini", "reqwest 0.12.28", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 244cf31..25f04d9 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -29,6 +29,10 @@ git2 = { version = "0.20", default-features = false, features = ["vendored-libgi notify = "6" notify-debouncer-mini = "0.4" +# The folder chooser through the desktop portal (picker.rs). The same GTK as Tauri's. +[target.'cfg(target_os = "linux")'.dependencies] +gtk = "0.18" + [profile.release] codegen-units = 1 lto = true diff --git a/src-tauri/src/kicad.rs b/src-tauri/src/kicad.rs index d62c9df..f9ca725 100644 --- a/src-tauri/src/kicad.rs +++ b/src-tauri/src/kicad.rs @@ -182,8 +182,9 @@ pub fn kicad_open(program: String, project: String) -> Result<(), String> { /// The AppImage's launcher points GTK, GIO and XDG_DATA_DIRS into the image /// (`$APPDIR/usr/…`). KiCad must start with the system's own, so those entries -/// are dropped from what the child inherits. +/// are dropped from what the child inherits (and KiVi's own GTK_USE_PORTAL, picker.rs). fn outside_appimage(cmd: &mut Command) { + crate::picker::clean_child_env(cmd); let Some(appdir) = std::env::var("APPDIR").ok().filter(|d| !d.is_empty()) else { return }; for (k, v) in appimage_env_fixes(&appdir, std::env::vars()) { match v { diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 16ae28a..068daee 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -5,6 +5,7 @@ mod git; mod git_http; mod kicad; +mod picker; use std::path::{Path, PathBuf}; use std::sync::Mutex; @@ -269,11 +270,14 @@ async fn github_device_refresh(client_id: String, refresh_token: String) -> Resu #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + #[cfg(target_os = "linux")] + picker::use_portal(); // before any other thread starts tauri::Builder::default() .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_opener::init()) .manage(Workspace::default()) .invoke_handler(tauri::generate_handler![ + picker::pick_folder, open_folder, list_folder, read_file, diff --git a/src-tauri/src/picker.rs b/src-tauri/src/picker.rs new file mode 100644 index 0000000..3f72734 --- /dev/null +++ b/src-tauri/src/picker.rs @@ -0,0 +1,65 @@ +//! The folder chooser. On Linux the dialog plugin's rfd is built for GTK's own +//! file dialog, which looks the same on every desktop. GTK's *native* chooser +//! goes through the XDG desktop portal instead, so the desktop's own dialog +//! appears (KDE Plasma: its Dolphin-style dialog; GNOME: Nautilus's), and +//! falls back to GTK's dialog by itself when no portal is running. +//! On Windows and macOS the dialog plugin already shows the system dialog: +//! this command refuses there and `pickFolder` (tauri.js) uses the plugin. + +/// Make GTK 3 use the portal for native file choosers. GTK reads this once, on +/// the first chooser, and otherwise only uses the portal inside Flatpak or +/// Snap. Called at start-up, before any other thread exists (set_var isn't +/// thread-safe). Children don't inherit it: see `clean_child_env`. +#[cfg(target_os = "linux")] +pub fn use_portal() { + if std::env::var_os("GTK_USE_PORTAL").is_none() { + std::env::set_var("GTK_USE_PORTAL", "1"); + std::env::set_var("KIVI_SET_GTK_USE_PORTAL", "1"); + } +} + +/// Undo `use_portal` for a program KiVi starts (KiCad). +pub fn clean_child_env(cmd: &mut std::process::Command) { + if std::env::var_os("KIVI_SET_GTK_USE_PORTAL").is_some() { + cmd.env_remove("GTK_USE_PORTAL").env_remove("KIVI_SET_GTK_USE_PORTAL"); + } +} + +/// A folder picked by the user, or None when cancelled. +#[cfg(target_os = "linux")] +#[tauri::command] +pub async fn pick_folder(window: tauri::WebviewWindow, title: String, default_path: Option) -> Result, String> { + // GTK only works on the main thread; the dialog runs its own loop there until closed. + let (tx, rx) = std::sync::mpsc::channel(); + let w = window.clone(); + window + .run_on_main_thread(move || { + let _ = tx.send(run_dialog(&w, &title, default_path)); + }) + .map_err(|e| e.to_string())?; + tauri::async_runtime::spawn_blocking(move || rx.recv()) + .await + .map_err(|e| e.to_string())? + .map_err(|_| "the folder chooser closed unexpectedly".to_string()) +} + +#[cfg(target_os = "linux")] +fn run_dialog(window: &tauri::WebviewWindow, title: &str, default_path: Option) -> Option { + use gtk::prelude::*; + let parent = window.gtk_window().ok(); + let dialog = gtk::FileChooserNative::new(Some(title), parent.as_ref(), gtk::FileChooserAction::SelectFolder, Some("Select"), Some("Cancel")); + dialog.set_modal(true); + if let Some(dir) = default_path.filter(|d| std::path::Path::new(d).is_dir()) { + dialog.set_current_folder(&dir); + } + let picked = if dialog.run() == gtk::ResponseType::Accept { dialog.filename() } else { None }; + dialog.destroy(); + picked.map(|p| p.to_string_lossy().into_owned()) +} + +#[cfg(not(target_os = "linux"))] +#[tauri::command] +pub fn pick_folder(title: String, default_path: Option) -> Result, String> { + let _ = (title, default_path); + Err("the dialog plugin shows the system's chooser here".into()) +} diff --git a/src/desktop/card-zoom.js b/src/desktop/card-zoom.js new file mode 100644 index 0000000..598cbc0 --- /dev/null +++ b/src/desktop/card-zoom.js @@ -0,0 +1,61 @@ +// Ctrl + mouse wheel resizes the project gallery's cards (thumbnails and text +// together), like a file manager's icon size. CSS `zoom` on the grid scales +// the column width too, so bigger cards also mean fewer per row. Ctrl+0 resets. +// The size is a per-machine convenience, kept in localStorage. + +const KEY = "kivi.cardZoom"; +const MIN = 0.5; +const MAX = 2.5; + +function load() { + try { + const z = Number(localStorage.getItem(KEY)); + return z >= MIN && z <= MAX ? z : 1; + } catch { + return 1; + } +} + +function store(z) { + try { + localStorage.setItem(KEY, String(z)); + } catch { + /* storage unavailable: the size just isn't remembered */ + } +} + +/** + * Let Ctrl + wheel (and Ctrl+0) inside `area` zoom the card `grid`. + * @param {HTMLElement} area the projects view (where the wheel is listened for) + * @param {HTMLElement} grid the card grid that gets scaled + */ +export function initCardZoom(area, grid) { + let zoom = load(); + const apply = () => (grid.style.zoom = zoom === 1 ? "" : String(zoom)); + apply(); + + // Not passive: preventDefault stops the webview's own page zoom, where it has one. + area.addEventListener( + "wheel", + (e) => { + if (!e.ctrlKey) return; + e.preventDefault(); + // Line-mode wheels report lines, pixel-mode ~100 px per notch; touchpads send + // many small deltas, so scale smoothly rather than one step per event. + const dy = e.deltaMode === 1 ? e.deltaY * 33 : e.deltaY; + zoom = Math.min(MAX, Math.max(MIN, zoom * Math.exp(-dy * 0.0015))); + if (Math.abs(zoom - 1) < 0.04) zoom = 1; // snap back to the default size + apply(); + store(zoom); + }, + { passive: false }, + ); + + document.addEventListener("keydown", (e) => { + if (!e.ctrlKey || e.key !== "0" || area.hidden) return; + e.preventDefault(); + zoom = 1; + apply(); + store(zoom); + }); +} diff --git a/src/desktop/inbox.js b/src/desktop/inbox.js index 42cea19..c23e0be 100644 --- a/src/desktop/inbox.js +++ b/src/desktop/inbox.js @@ -15,6 +15,7 @@ import { forge } from "../forge/index.js"; import { initHeader } from "./account.js"; import { ICONS } from "./icons.js"; import { galleryRepos, initProjects, repoHasProjects } from "./projects.js"; +import { initCardZoom } from "./card-zoom.js"; import { localCopy } from "./git.js"; import { ISSUE_LABEL, decodeBody, anchorLabel } from "../review.js"; import { kicadFilesChanged } from "./pr-files.js"; @@ -196,6 +197,7 @@ const projects = initProjects({ }, }); const loaded = new Set(); // tabs fetched since the last refresh +initCardZoom($("#home-projects"), $("#projects")); function showHomeTab(name) { if (!TITLES[name]) name = "projects"; diff --git a/src/desktop/tauri.js b/src/desktop/tauri.js index 167c8c2..c070ae0 100644 --- a/src/desktop/tauri.js +++ b/src/desktop/tauri.js @@ -134,8 +134,18 @@ export function onDrop({ enter, leave, drop }) { }); } +/** + * Ask for a folder with the system's own chooser. On Linux that's the desktop + * portal's (KDE: the Dolphin-style dialog), through picker.rs, since the dialog + * plugin always shows GTK's; elsewhere, and if that fails, the dialog plugin. + * @returns {Promise} the folder, or null when cancelled + */ export async function pickFolder(title = "Open KiCad project folder", defaultPath = undefined) { - return T.dialog.open({ directory: true, title, defaultPath }); + try { + return await invoke("pick_folder", { title, defaultPath: defaultPath ?? null }); + } catch { + return T.dialog.open({ directory: true, title, defaultPath }); + } } // ---- KiCad -----------------------------------------------------------------------