Skip to content

Commit

Permalink
Debug on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimergp committed Jul 2, 2024
1 parent 774ecfb commit b9dfa8e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
20 changes: 15 additions & 5 deletions menuinst/platforms/win.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .win_utils.knownfolders import folder_path as windows_folder_path
from .win_utils.knownfolders import windows_terminal_settings_files
from .win_utils.registry import (
notify_shell_changes,
register_file_extension,
register_url_protocol,
unregister_file_extension,
Expand Down Expand Up @@ -198,14 +199,19 @@ def create(self) -> Tuple[Path, ...]:

for location in self.menu.terminal_profile_locations:
self._add_remove_windows_terminal_profile(location, remove=False)
self._register_file_extensions()
self._register_url_protocols()
changed_extensions = self._register_file_extensions()
changed_protocols = self._register_url_protocols()
if changed_extensions or changed_protocols:
notify_shell_changes()

return paths

def remove(self) -> Tuple[Path, ...]:
self._unregister_file_extensions()
self._unregister_url_protocols()
changed_extensions = self._unregister_file_extensions()
changed_protocols = self._unregister_url_protocols()
if changed_extensions or changed_protocols:
notify_shell_changes()

for location in self.menu.terminal_profile_locations:
self._add_remove_windows_terminal_profile(location, remove=True)

Expand Down Expand Up @@ -433,7 +439,7 @@ def _cmd_ftype(identifier, command=None, query=False, remove=False) -> Completed
arg = f"{identifier}="
return logged_run(["cmd", "/D", "/C", f"assoc {arg}"], check=True)

def _register_file_extensions(self):
def _register_file_extensions(self) -> bool:
"""WIP"""
extensions = self.metadata["file_extensions"]
if not extensions:
Expand All @@ -453,6 +459,7 @@ def _register_file_extensions(self):
app_user_model_id=self._app_user_model_id(),
mode=self.menu.mode,
)
return True

def _unregister_file_extensions(self):
extensions = self.metadata["file_extensions"]
Expand All @@ -463,6 +470,7 @@ def _unregister_file_extensions(self):
for ext in exts:
identifier = self._ftype_identifier(ext)
unregister_file_extension(ext, identifier, mode=self.menu.mode)
return True

def _register_url_protocols(self):
"See https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)" # noqa
Expand All @@ -482,6 +490,7 @@ def _register_url_protocols(self):
app_user_model_id=self._app_user_model_id(),
mode=self.menu.mode,
)
return True

def _unregister_url_protocols(self):
protocols = self.metadata["url_protocols"]
Expand All @@ -490,6 +499,7 @@ def _unregister_url_protocols(self):
for protocol in protocols:
identifier = self._ftype_identifier(protocol)
unregister_url_protocol(protocol, identifier, mode=self.menu.mode)
return True

def _app_user_model_id(self):
aumi = self.render_key("app_user_model_id")
Expand Down
27 changes: 21 additions & 6 deletions menuinst/platforms/win_utils/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Mnemonic: SetValueEx for "excalars" (scalars, named values)
"""

import ctypes
import winreg
from logging import getLogger

Expand Down Expand Up @@ -60,22 +61,26 @@ def register_file_extension(
key = "HKEY_CURRENT_USER/Software/Classes" # HKCU

# First we associate an extension with a handler (presence of key is enough)
regvalue(f"{key}/{extension}/OpenWithProgids/{identifier}/@", "")
regvalue(f"{key}/{extension}/OpenWithProgids/{identifier}", "")

# Now we register the handler
regvalue(f"{key}/{identifier}/@", f"{extension} {identifier} handler")
regvalue(f"{key}/{identifier}/@", f"{extension} {identifier} file")

# Set the 'open' command
regvalue(f"{key}/{identifier}/shell/open/command/@", command)
if app_name:
regvalue(f"{key}/{identifier}/shell/open/@", app_name)
regvalue(f"{key}/{identifier}/FriendlyAppName/@", app_name)
regvalue(f"{key}/{identifier}/shell/open/FriendlyAppName", app_name)

if app_user_model_id:
regvalue(f"{key}/{identifier}/AppUserModelID", app_user_model_id)

if icon:
regvalue(f"{key}/{identifier}/DefaultIcon/@", icon)
regvalue(f"{key}/{identifier}/shell/open/Icon", icon)
# NOTE: This doesn't change the icon next in the Open With menu
# This defaults to whatever the command executable is shipping
regvalue(f"{key}/{identifier}/DefaultIcon/@", f'{icon},0')
regvalue(f"{key}/{identifier}/shell/open/Icon", f'{icon},0')

if friendly_type_name:
# NOTE: Windows <10 requires the string in a PE file, but that's too
Expand Down Expand Up @@ -130,9 +135,11 @@ def register_url_protocol(
regvalue(f"{key}/shell/open/command/@", command)
if app_name:
regvalue(f"{key}/shell/open/@", app_name)
regvalue(f"{key}/FriendlyAppName/@", app_name)
regvalue(f"{key}/shell/open/FriendlyAppName", app_name)
if icon:
regvalue(f"{key}/DefaultIcon/@", icon)
regvalue(f"{key}/shell/open/Icon", icon)
regvalue(f"{key}/DefaultIcon/@", f'"{icon}"')
regvalue(f"{key}/shell/open/Icon", f'"{icon}"')
if app_user_model_id:
regvalue(f"{key}/AppUserModelId", app_user_model_id)
if identifier:
Expand Down Expand Up @@ -195,3 +202,11 @@ def regvalue(key, value, value_type=winreg.REG_SZ, raise_on_errors=True):
if raise_on_errors:
raise
log.warning("Could not set %s to %s", original_key, value, exc_info=exc)


def notify_shell_changes():
shell32 = ctypes.OleDLL('shell32')
shell32.SHChangeNotify.restype = None
event = 0x08000000 # SHCNE_ASSOCCHANGED
flags = 0x0000 # SHCNF_IDLIST
shell32.SHChangeNotify(event, flags, None, None)

0 comments on commit b9dfa8e

Please sign in to comment.