Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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__/
7 changes: 7 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<path>[&project=…][&commit=<sha>]`.
- 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)
Expand Down
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/kicad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod git;
mod git_http;
mod kicad;
mod picker;

use std::path::{Path, PathBuf};
use std::sync::Mutex;
Expand Down Expand Up @@ -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,
Expand Down
65 changes: 65 additions & 0 deletions src-tauri/src/picker.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> Result<Option<String>, 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<String>) -> Option<String> {
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<String>) -> Result<Option<String>, String> {
let _ = (title, default_path);
Err("the dialog plugin shows the system's chooser here".into())
}
61 changes: 61 additions & 0 deletions src/desktop/card-zoom.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
2 changes: 2 additions & 0 deletions src/desktop/inbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
12 changes: 11 additions & 1 deletion src/desktop/tauri.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|null>} 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 -----------------------------------------------------------------------
Expand Down
Loading