Skip to content

Commit

Permalink
Alternative method to get a dialog chooser
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Nov 18, 2023
1 parent 69b107e commit feabded
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/app/tk_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@

from tkinter import ttk
from tkinter import font as _tk_font
from tkinter import filedialog, commondialog, simpledialog, messagebox
from tkinter import filedialog, commondialog, messagebox
import tkinter as tk
import os.path

from srctools import logger
from idlelib.redirector import WidgetRedirector # type: ignore[import-not-found]
import trio

Expand All @@ -31,8 +32,7 @@
from ui_tk.wid_transtoken import set_text


# Set icons for the application.

LOGGER = logger.get_logger(__name__)
ICO_PATH = str(utils.bins_path('BEE2.ico'))
T = TypeVar('T')
AnyWidT = TypeVar('AnyWidT', bound=tk.Misc)
Expand Down Expand Up @@ -610,6 +610,37 @@ def validate(self) -> bool:
filedialog.Directory.command = '::tk::dialog::file::chooseDir::'


async def _folderbrowse_powershell() -> Optional[str]:
"""For Windows, the TK bindings don't work properly. Use Powershell to call this one API."""
result = await trio.run_process(
[
"powershell", "-NoProfile",
"-command", "-", # Run from stdin.
],
shell=True,
capture_stdout=True,
capture_stderr=True,
stdin=BROWSE_DIR_PS,
)
# An Ok or Cancel from ShowDialog, then the path.
[btn, poss_path] = result.stdout.splitlines()
if btn == b'Cancel':
return None
# Anything non-ASCII seems to just be dropped, or replaced by ?.
if b'?' in poss_path:
raise ValueError(poss_path)
return os.fsdecode(poss_path)


BROWSE_DIR_PS = b'''\
Add-Type -AssemblyName System.Windows.Forms
$Dialog = New-Object -TypeName System.Windows.Forms.FolderBrowserDialog
$Dialog.ShowNewFolderButton = true
$Dialog.ShowDialog()
Write-Output $Dialog.SelectedPath
'''


class FileField(ttk.Frame):
"""A text box which allows searching for a file or directory.
"""
Expand Down Expand Up @@ -657,15 +688,15 @@ def __init__(
)
self.textbox.grid(row=0, column=0, sticky='ew')
self.columnconfigure(0, weight=1)
bind_leftclick(self.textbox, self.browse)
bind_leftclick(self.textbox, lambda e: background_run(self.browse))
# The full location is displayed in a tooltip.
add_tooltip(self.textbox, TransToken.untranslated(self._location))
self.textbox.bind('<Configure>', self._text_configure)

self.browse_btn = ttk.Button(
self,
text="...",
command=self.browse,
command=lambda: background_run(self.browse),
)
self.browse_btn.grid(row=0, column=1)
# It should be this narrow, but perhaps this doesn't accept floats?
Expand All @@ -676,9 +707,17 @@ def __init__(

self._text_var.set(self._truncate(loc))

def browse(self, event: object = None) -> None:
async def browse(self) -> None:
"""Browse for a file."""
path = self.browser.show()
if utils.WIN and self.is_dir:
try:
path = await _folderbrowse_powershell()
except Exception as exc:
LOGGER.warning('Failed to browse for a directory:', exc_info=exc)
path = self.browser.show() # Fallback to generic widget.
else:
path = self.browser.show()

if path:
self.value = path

Expand Down

0 comments on commit feabded

Please sign in to comment.