Handle OS file association: open .md files passed as CLI args

This commit is contained in:
Your Name
2026-02-14 19:45:58 +02:00
parent a3b241b4a1
commit f462ca8d03
5 changed files with 51 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "vesper"
version = "0.1.0"
version = "1.0.1"
description = "A beautiful markdown reader"
authors = ["you"]
edition = "2021"

View File

@@ -3,7 +3,7 @@ use std::fs;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use tauri::Manager;
use tauri::{Emitter, Manager};
#[derive(Serialize, Deserialize, Default)]
struct WindowState {
@@ -72,6 +72,27 @@ pub fn run() {
let _ = window.maximize();
}
// If launched with a file argument (e.g. double-clicking a .md file),
// emit the path to the frontend so it can open it as a tab.
let args: Vec<String> = env::args().collect();
if let Some(file_arg) = args.get(1) {
let path = PathBuf::from(file_arg);
if path.is_file() {
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
let ext_lower = ext.to_lowercase();
if ext_lower == "md" || ext_lower == "markdown" || ext_lower == "txt" {
let path_str = path.to_string_lossy().to_string();
let win = window.clone();
// Emit after a short delay so the frontend has time to set up listeners
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(500));
let _ = win.emit("open-file", path_str);
});
}
}
}
}
Ok(())
})
.on_window_event(|window, event| {

View File

@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "Vesper",
"version": "1.0.0",
"version": "1.0.1",
"identifier": "com.vesper.reader",
"build": {
"beforeDevCommand": "npm run dev",