refactor: Improve file dialog handling for cross-platform compatibility

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 11:08:39 -08:00
parent f010ee57cf
commit c4e107b40e

View File

@ -47,11 +47,11 @@ def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
from tkinter import filedialog
root = tk.Tk()
root.withdraw() # Hide the main window
root.attributes('-topmost', True) # Bring dialog to front
root.wm_attributes('-topmost', 1) # Bring dialog to front
path = filedialog.askopenfilename(title=title, filetypes=file_types)
root.destroy() # Explicitly destroy the tkinter instance
return path if path else None
elif platform.system() == "Linux":
# Use zenity for Linux systems
try:
result = subprocess.run(
['zenity', '--file-selection', '--title', title],
@ -60,6 +60,7 @@ def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
)
return result.stdout.strip() if result.returncode == 0 else None
except FileNotFoundError:
st.error("Zenity is not installed. Please install it for file dialog support.")
return None
elif platform.system() == "Darwin": # macOS
try:
@ -70,6 +71,7 @@ def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
)
return result.stdout.strip() if result.returncode == 0 else None
except FileNotFoundError:
st.error("AppleScript is not available for file dialog support.")
return None
except Exception as e:
st.error(f"Error opening file dialog: {str(e)}")
@ -84,11 +86,11 @@ def get_directory_path(title="Select Directory"):
from tkinter import filedialog
root = tk.Tk()
root.withdraw() # Hide the main window
root.attributes('-topmost', True) # Bring dialog to front
root.wm_attributes('-topmost', 1) # Bring dialog to front
path = filedialog.askdirectory(title=title)
root.destroy() # Explicitly destroy the tkinter instance
return path if path else None
elif platform.system() == "Linux":
# Use zenity for Linux systems
try:
result = subprocess.run(
['zenity', '--file-selection', '--directory', '--title', title],
@ -97,6 +99,7 @@ def get_directory_path(title="Select Directory"):
)
return result.stdout.strip() if result.returncode == 0 else None
except FileNotFoundError:
st.error("Zenity is not installed. Please install it for file dialog support.")
return None
elif platform.system() == "Darwin": # macOS
try:
@ -107,6 +110,7 @@ def get_directory_path(title="Select Directory"):
)
return result.stdout.strip() if result.returncode == 0 else None
except FileNotFoundError:
st.error("AppleScript is not available for file dialog support.")
return None
except Exception as e:
st.error(f"Error opening file dialog: {str(e)}")