Skip to content

Commit

Permalink
feat: support to create a shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
mika-f committed Jun 26, 2022
1 parent 80c05e6 commit 225d90b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/blender-launcher-ui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
ResizeMode="NoResize"
Title="Blender Launcher" Height="150" Width="500">
Title="Blender Launcher" Height="180" Width="500">
<StackPanel Margin="4">
<TextBlock FontSize="18" Margin="0,4" HorizontalAlignment="Center">
Blender Launcher for Drag-and-Drop Support
Expand All @@ -21,5 +21,6 @@
<Button Click="OnClickOpenFileButton" Grid.Column="2" HorizontalAlignment="Right" Content="Open File..." Width="120" Padding="8,4" />
</Grid>
<Button x:Name="LaunchButton" Click="OnClickLaunchButton" FontSize="16" Content="Launch Blender" Padding="8,4" />
<Button x:Name="CreateShortcutButton" Click="OnClickCreateShortcutButton" FontSize="16" Content="Create Shortcut" Padding="8,4" />
</StackPanel>
</Window>
41 changes: 41 additions & 0 deletions src/blender-launcher-ui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.
// ------------------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;

using Microsoft.Win32;
Expand All @@ -20,6 +22,7 @@ public MainWindow()
{
InitializeComponent();
LaunchButton.IsEnabled = false;
CreateShortcutButton.IsEnabled = false;
}

private void OnClickLaunchButton(object sender, RoutedEventArgs e)
Expand All @@ -45,10 +48,48 @@ private void OnClickOpenFileButton(object sender, RoutedEventArgs e)
if (!file.ToLowerInvariant().EndsWith("blender.exe"))
{
MessageBox.Show("Please select blender.exe");
LaunchButton.IsEnabled = false;
CreateShortcutButton.IsEnabled = false;
return;
}

BlenderLocation.Text = file;
LaunchButton.IsEnabled = true;
CreateShortcutButton.IsEnabled = true;
}

private void OnClickCreateShortcutButton(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog
{
FileName = "Blender.lnk",
CheckPathExists = true,
Filter = "Shortcut File (*.lnk)|*.lnk"
};

if (dialog.ShowDialog() == false)
return;

var file = dialog.FileName;
var t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8"));
if (t == null)
return;

dynamic? shell = Activator.CreateInstance(t);
if (shell == null)
return;

var shortcut = shell.CreateShortcut(file);
if (shortcut == null)
return;

var executable = Path.GetFullPath("./blender-launcher.exe");
shortcut.TargetPath = $"\"{executable}\"";
shortcut.Arguments = $"\"{BlenderLocation.Text}\"";
shortcut.IconLocation = $"{BlenderLocation.Text},0";
shortcut.Save();

Marshal.FinalReleaseComObject(shortcut);
Marshal.FinalReleaseComObject(shell);
}
}
13 changes: 11 additions & 2 deletions src/blender-launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
return 1;

var executable = args[0];
var self = Path.GetFullPath(Path.GetDirectoryName(typeof(ImportRequest).Assembly.Location)!);
var process = new Process
{
EnableRaisingEvents = true,
StartInfo =
{
FileName = executable,
Arguments = $"--python {Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(ImportRequest).Assembly.Location)!, "blender-server", "launch.py"))}",
Arguments = $"--python {Path.GetFullPath(Path.Combine(self, "blender-server", "launch.py"))}",
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
Expand All @@ -30,7 +31,15 @@
return 1;

var processId = process.Id;
Process.Start("./blender-hook.exe", processId.ToString()).WaitForExit();
var startInfo = new ProcessStartInfo
{
FileName = Path.Combine(self, "blender-hook.exe"),
Arguments = processId.ToString(),
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = self
};
Process.Start(startInfo)!.WaitForExit();

var client = new HttpClient();

Expand Down

0 comments on commit 225d90b

Please sign in to comment.