|
| 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 | +} |
0 commit comments