godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.13k stars 75 forks source link

Add Windows File Explorer Context Menu Entries for Godot #5301

Open ianfabs opened 2 years ago

ianfabs commented 2 years ago

Describe the project you are working on

I am working on a myriad of side projects at the moment, and I found that it was cumbersome and cluttering to open the Godot project manager every time I wanted to open another project. Often times I will have 2-3 projects open at a time when I am copying from example code or moving a feature that was isolated in a demo project to the game I am working on that actively requires it

Describe the problem or limitation you are having in your project

I use windows explorer to navigate my project folders, when copying assets from my downloads, or copying GDScript files from one project to another and I was little annoyed that I couldn't just right click a folder and open it in Godot.

TD;LR: Can't right click on or in a folder to open it in Godot.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add Windows RegistryEditor entries to create File Explorer context menu items for opening a project or folder in Godot.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

When a user right clicks on or in a folder in the Windows File Explorer, a context-menu item to open the Godot project in the Godot editor will be available to them. This can be done through modifying the Windows Registry. Below are two examples

Advanced Example

Code

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Godot]
@="Open with &Godot"
"Icon"="C:\\PATH\\TO\\godot.exe"
"ExtendedSubCommandsKey"="Directory\\ContextMenus\\Godotx64"
"MUIVerb"="&Godot"

[HKEY_CLASSES_ROOT\Directory\ContextMenus\Godotx64]

[HKEY_CLASSES_ROOT\Directory\ContextMenus\Godotx64\shell]

[HKEY_CLASSES_ROOT\Directory\ContextMenus\Godotx64\shell\edit]
"Icon"="C:\\PATH\\TO\\godot.exe"
"MUIVerb"="Edit project with Godot"

[HKEY_CLASSES_ROOT\Directory\ContextMenus\Godotx64\shell\edit\command]
@="C:\\PATH\\TO\\godot.exe -e --path \"%V\""

[HKEY_CLASSES_ROOT\Directory\ContextMenus\Godotx64\shell\run]
"Icon"="C:\\PATH\\TO\\godot.exe"
"MUIVerb"="Run project with Godot"

[HKEY_CLASSES_ROOT\Directory\ContextMenus\Godotx64\shell\run\command]
@="C:\\PATH\\TO\\godot.exe --path \"%V\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\Godot]
@="Open with &Godot"
"Icon"="C:\\PATH\\TO\\godot.exe"

[HKEY_CLASSES_ROOT\Directory\Background\shell\Godot\command]
@="C:\\PATH\\TO\\godot.exe -e --path \"%V\""

Result image image

Simple Example

Code

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\Godot]
@="Open with &Godot"
"Icon"="C:\\PATH\\TO\\godot.exe"
[HKEY_CLASSES_ROOT\Directory\shell\Godot\command]
@="C:\\PATH\\TO\\godot.exe -e --path \"%V\""
[HKEY_CLASSES_ROOT\Directory\Background\shell\Godot]
@="Open with &Godot"
"Icon"="C:\\PATH\\TO\\godot.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Godot\command]
@="C:\\PATH\\TO\\godot.exe -e --path \"%V\""

Result image image

If this enhancement will not be used often, can it be worked around with a few lines of script?

This can be worked around with a simple PowerShell script that can be executed post-install via scoop, like this (will require Administrator access):

$GodotInstallDir = $args[0]
$hkcrExists = Get-PSDrive -PSProvider Registry -Name HKCR -ErrorAction SilentlyContinue

if ($hkcrExists) {
    Write-Output "HKCR Exists"
} else {
    Write-Output "HKCR Needs Creation";
    New-PSDrive -PSProvider registry -Root HKEY_CLASSES_ROOT -Name HKCR
    $hkcrExists = Get-PSDrive -PSProvider Registry -Name HKCR -ErrorAction SilentlyContinue
    if ($hkcrExists) {
        Write-Output "HKCR Was Created"
    } else {
        Write-Output "Failed to create HKCR"
    }
}

# Create registry entries

$mainKey = 'HKCR:\Directory\shell\Godot'

if (-not (Test-Path $mainKey)) {
    Write-Output "Creating key '${mainKey}'"
    New-Item -Path $mainKey -Force | Out-Null
    Write-Output "Creating key '${mainKey}\command'"
    New-Item -Path "${mainKey}\command" -Force | Out-Null
}
New-ItemProperty -Path $mainKey -Name 'Icon' -Value "$GodotInstallDir\godot.exe" -PropertyType String -Force
New-ItemProperty -Path $mainKey -Name '(Default)' -Value "Open with &Godot" -PropertyType String -Force

New-ItemProperty -Path "${mainKey}\command" -Name '(Default)' -Value "$GodotInstallDir\godot.exe -e --path `"%V`"" -PropertyType String -Force

$bgKey = 'HKCR:\Directory\Background\shell\Godot'

if (-not (Test-Path $bgKey)) {
    Write-Output "Creating key '${bgKey}'"
    New-Item -Path $bgKey -Force | Out-Null
    Write-Output "Creating key '${bgKey}\command'"
    New-Item -Path "${bgKey}\command" -Force | Out-Null
}
New-ItemProperty -Path $bgKey -Name 'Icon' -Value "$GodotInstallDir\godot.exe" -PropertyType String -Force
New-ItemProperty -Path $bgKey -Name '(Default)' -Value "Open with &Godot" -PropertyType String -Force

New-ItemProperty -Path "${bgKey}\command" -Name '(Default)' -Value "$GodotInstallDir\godot.exe -e --path `"%V`"" -PropertyType String -Force

$submenuKey = 'HKCR:\Directory\ContextMenus\GodotSubMenu'
$runCmdKey = "${submenuKey}\shell\run"
$editCmdKey = "${submenuKey}\shell\edit"
if (-not (Test-Path $submenuKey)) {
  Write-Output "Creating key '${submenuKey}'"
  New-Item -Path $bgKey -Force | Out-Null
  Write-Output "Creating key '${submenuKey}\command'"
  New-Item -Path "${submenuKey}\shell" -Force | Out-Null

  # Create submenu items
  Write-Output "Creating key '${runCmdKey}'"
  New-Item -Path $runCmdKey -Force | Out-Null
  New-Item -Path "${runCmdKey}\command" -Force | Out-Null
  New-ItemProperty -Path $runCmdKey -Name 'Icon' -Value "$GodotInstallDir\godot.exe" -PropertyType String -Force
  New-ItemProperty -Path $runCmdKey -Name 'MUIVerb' -Value "&Run project" -PropertyType String -Force
  New-ItemProperty -Path "${runCmdKey}\command" -Name '(Default)' -Value "$GodotInstallDir\godot.exe --path `"%V`"" -PropertyType String -Force

  Write-Output "Creating key '${editCmdKey}'"
  New-Item -Path $editCmdKey -Force | Out-Null
  New-Item -Path "${editCmdKey}\command" -Force | Out-Null
  New-ItemProperty -Path $editCmdKey -Name 'Icon' -Value "$GodotInstallDir\godot.exe" -PropertyType String -Force
  New-ItemProperty -Path $editCmdKey -Name 'MUIVerb' -Value "&Edit project" -PropertyType String -Force
  New-ItemProperty -Path "${editCmdKey}\command" -Name '(Default)' -Value "$GodotInstallDir\godot.exe -e --path `"%V`"" -PropertyType String -Force
}

if (!!($args[1]) -and ($args[1] -eq 'true')) {
  New-ItemProperty -Path $mainKey -Name 'ExtendedSubCommandsKey' -Value "Directory\ContextMenus\GodotSubMenu" -PropertyType String -Force
  New-ItemProperty -Path $mainKey -Name 'MUIVerb' -Value "&Open with Godot" -PropertyType String -Force
  # only make submenu available on shift-rclick
  # New-ItemProperty -Path $mainKey -Name 'Extended' -Value "" -PropertyType String -Force
}

Or it can be done via C# using syntax like this, similar to the above code snippet :

RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Folder\\Shell", true);
RegistryKey newkey = _key.CreateSubKey("My Menu Item");
newkey.SetValue("AppliesTo", "under:T:");

RegistryKey subNewkey = newkey.CreateSubKey("Command");
subNewkey.SetValue("", "C:\\yourApplication.exe");
subNewkey.Close();

newkey.Close();
_key.Close();

Is there a reason why this should be core and not an add-on in the asset library?

There would be no way to make this an addon

TL;DR N/A

YuriSizov commented 2 years ago

You can just double-click on the godot.project file if you set up file association once...

Calinou commented 2 years ago

Related to https://github.com/godotengine/godot-proposals/issues/1432.

I find automated context menu registrations annoying, as they make an already-tall context menu even taller. I think your screenshot shows this well already :slightly_smiling_face:

If this is done, this should only be done explicitly on user request, not by default.

ianfabs commented 2 years ago

That's fair. Having it as an option would still be nice but I agree,

If this is done, this should only be done explicitly on user request, not by default.

Maybe instead of an addition to the editor, the process can be documented somewhere officially

Calinou commented 2 years ago

This can be worked around with a simple PowerShell script that can be executed post-install via scoop, like this (will require Administrator access):

What Scoop does for vscode is install a .reg file that is linked to in its notes section. If you paste the path to the .reg file in the command prompt, it'll register a context menu for VS Code.

That said, this is a third-party issue that should be discussed on the scoop-extras repository.