Skip to content
Open
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
5 changes: 4 additions & 1 deletion .nix/pkgs/graphite.nix
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ deps.crane.lib.buildPackage (
cp target/${if dev then "debug" else "release"}/graphite $out/bin/graphite
mkdir -p $out/share/applications
cp $src/desktop/assets/*.desktop $out/share/applications/
cp $src/desktop/assets/art.graphite.Graphite.desktop $out/share/applications/
mkdir -p $out/share/mime/packages
cp $src/desktop/assets/art.graphite.Graphite.xml $out/share/mime/packages/
mkdir -p $out/share/icons/hicolor/scalable/apps
cp ${branding}/app-icons/graphite.svg $out/share/icons/hicolor/scalable/apps/art.graphite.Graphite.svg
Expand Down
15 changes: 14 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion desktop/assets/art.graphite.Graphite.desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
Name=Graphite
GenericName=Vector & Raster Graphics Editor
Comment=Open-source vector & raster graphics editor. Featuring node based procedural nondestructive editing workflow.
Exec=graphite
Exec=graphite %F
Terminal=false
Type=Application
Icon=art.graphite.Graphite
Categories=Graphics;VectorGraphics;RasterGraphics;
Keywords=graphite;editor;vector;raster;procedural;design;
StartupWMClass=art.graphite.Graphite
MimeType=application/graphite+json;image/svg+xml;image/png;image/jpeg;
9 changes: 9 additions & 0 deletions desktop/assets/art.graphite.Graphite.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/graphite+json">
<comment>Graphite Document</comment>
<sub-class-of type="application/json"/>
<glob pattern="*.graphite"/>
<icon name="art.graphite.Graphite"/>
</mime-type>
</mime-info>
4 changes: 4 additions & 0 deletions desktop/platform/win/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ path = "src/main.rs"
[dependencies]
graphite-desktop = { path = "../.." }

[target.'cfg(target_os = "windows")'.dependencies]
windows-registry = "0.6"
windows = { version = "0.62", features = ["Win32_UI_Shell"] }

[target.'cfg(target_os = "windows")'.build-dependencies]
winres = "0.1"
103 changes: 103 additions & 0 deletions desktop/platform/win/src/file_associations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// This is a bit of a hack to get file associations working without an installer.
// TODO: Replace this with a proper installer that can set up file associations.

use std::env;
use std::path::Path;

use windows::Win32::UI::Shell::{SHCNE_ASSOCCHANGED, SHCNF_IDLIST, SHChangeNotify};
use windows_registry::CURRENT_USER;

const PROG_ID: &str = "Graphite.Document";
const EXECUTABLE_NAME: &str = "Graphite.exe";
const APP_FRIENDLY_NAME: &str = "Graphite";
const DOCUMENT_FRIENDLY_NAME: &str = "Graphite Document";
const MIME_TYPE: &str = "application/graphite+json";
const FILE_EXTENSION: &str = ".graphite";
const SUPPORTED_EXTENSIONS: &[&str] = &[FILE_EXTENSION, ".svg", ".png", ".jpg", ".jpeg"];
Comment thread
timon-schelling marked this conversation as resolved.

pub fn write() {
if let Err(e) = FileAssociationWriter::new(&env::current_exe().unwrap())
Comment thread
timon-schelling marked this conversation as resolved.
.document_type(PROG_ID, DOCUMENT_FRIENDLY_NAME)
.application(EXECUTABLE_NAME, APP_FRIENDLY_NAME, SUPPORTED_EXTENSIONS)
.extension(FILE_EXTENSION, PROG_ID, MIME_TYPE)
.write()
{
eprintln!("Failed to register file associations: {e}");
}
}

struct FileAssociationWriter {
open_command: String,
icon_value: String,
entries: Vec<RegistryEntry>,
}

struct RegistryEntry {
path: String,
name: String,
value: String,
}

impl FileAssociationWriter {
fn new(executable: &Path) -> Self {
let exe_string = executable.to_string_lossy();
Self {
open_command: format!("\"{exe_string}\" \"%1\""),
icon_value: format!("{exe_string},0"),
entries: Vec::new(),
}
}

fn document_type(mut self, prog_id: &str, friendly_name: &str) -> Self {
let base = format!("Software\\Classes\\{prog_id}");
self.push(&base, "", friendly_name);
self.push(&format!("{base}\\DefaultIcon"), "", &self.icon_value.clone());
self.push(&format!("{base}\\shell\\open\\command"), "", &self.open_command.clone());
self
}

fn application(mut self, executable_name: &str, friendly_name: &str, supported_extensions: &[&str]) -> Self {
let base = format!("Software\\Classes\\Applications\\{executable_name}");
self.push(&base, "FriendlyAppName", friendly_name);
self.push(&format!("{base}\\shell\\open\\command"), "", &self.open_command.clone());

let supported_path = format!("{base}\\SupportedTypes");
for extension in supported_extensions {
self.push(&supported_path, extension, "");
}
self
}

fn extension(mut self, extension: &str, prog_id: &str, mime_type: &str) -> Self {
let path = format!("Software\\Classes\\{extension}");
self.push(&path, "", prog_id);
self.push(&path, "Content Type", mime_type);
self
}

fn push(&mut self, path: &str, name: &str, value: &str) {
self.entries.push(RegistryEntry {
path: path.to_owned(),
name: name.to_owned(),
value: value.to_owned(),
});
}

fn write(self) -> windows_registry::Result<()> {
let mut changed = false;

for entry in &self.entries {
let key = CURRENT_USER.create(&entry.path)?;
if key.get_string(&entry.name).ok().as_deref() == Some(entry.value.as_str()) {
continue;
}
key.set_string(&entry.name, &entry.value)?;
changed = true;
}

if changed {
unsafe { SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, None, None) };
}
Ok(())
}
}
7 changes: 7 additions & 0 deletions desktop/platform/win/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#![windows_subsystem = "windows"]

#[cfg(target_os = "windows")]
mod file_associations;

fn main() {
#[cfg(target_os = "windows")]
file_associations::write();

graphite_desktop::start();
}
Loading