Skip to content

Commit 8ca0dc9

Browse files
windows
1 parent 96727f8 commit 8ca0dc9

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

desktop/platform/win/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ path = "src/main.rs"
1515
[dependencies]
1616
graphite-desktop = { path = "../.." }
1717

18+
[target.'cfg(target_os = "windows")'.dependencies]
19+
windows-registry = "0.6"
20+
1821
[target.'cfg(target_os = "windows")'.build-dependencies]
1922
winres = "0.1"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::env;
2+
use std::error::Error;
3+
4+
use windows_registry::CURRENT_USER;
5+
6+
const PROG_ID: &str = "Graphite.Document";
7+
const EXECUTABLE_NAME: &str = "Graphite.exe";
8+
const APP_FRIENDLY_NAME: &str = "Graphite";
9+
const DOCUMENT_FRIENDLY_NAME: &str = "Graphite Document";
10+
const MIME_TYPE: &str = "application/graphite+json";
11+
const FILE_EXTENSION: &str = ".graphite";
12+
const SUPPORTED_EXTENSIONS: &[&str] = &[FILE_EXTENSION, ".svg", ".png", ".jpg", ".jpeg"];
13+
14+
pub fn register() {
15+
if let Err(e) = register_inner() {
16+
eprintln!("Failed to register file associations: {e}");
17+
}
18+
}
19+
20+
fn register_inner() -> Result<(), Box<dyn Error>> {
21+
let exe = env::current_exe()?;
22+
let exe_string = exe.to_string_lossy();
23+
let open_command = format!("\"{exe_string}\" \"%1\"");
24+
let icon_value = format!("{exe_string},0");
25+
26+
let prog_id = CURRENT_USER.create(format!("Software\\Classes\\{PROG_ID}"))?;
27+
prog_id.set_string("", DOCUMENT_FRIENDLY_NAME)?;
28+
let prog_id_icon = CURRENT_USER.create(format!("Software\\Classes\\{PROG_ID}\\DefaultIcon"))?;
29+
prog_id_icon.set_string("", &icon_value)?;
30+
let prog_id_command = CURRENT_USER.create(format!("Software\\Classes\\{PROG_ID}\\shell\\open\\command"))?;
31+
prog_id_command.set_string("", &open_command)?;
32+
33+
let app_base = format!("Software\\Classes\\Applications\\{EXECUTABLE_NAME}");
34+
let app = CURRENT_USER.create(&app_base)?;
35+
app.set_string("FriendlyAppName", APP_FRIENDLY_NAME)?;
36+
let app_command = CURRENT_USER.create(format!("{app_base}\\shell\\open\\command"))?;
37+
app_command.set_string("", &open_command)?;
38+
let supported = CURRENT_USER.create(format!("{app_base}\\SupportedTypes"))?;
39+
for extension in SUPPORTED_EXTENSIONS {
40+
supported.set_string(extension, "")?;
41+
}
42+
43+
let extension = CURRENT_USER.create(format!("Software\\Classes\\{FILE_EXTENSION}"))?;
44+
extension.set_string("", PROG_ID)?;
45+
extension.set_string("Content Type", MIME_TYPE)?;
46+
47+
Ok(())
48+
}

desktop/platform/win/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
#![windows_subsystem = "windows"]
2+
3+
#[cfg(target_os = "windows")]
4+
mod file_associations;
5+
26
fn main() {
7+
#[cfg(target_os = "windows")]
8+
file_associations::register();
9+
310
graphite_desktop::start();
411
}

0 commit comments

Comments
 (0)