Skip to content

Commit

Permalink
issue #65
Browse files Browse the repository at this point in the history
Add a menu and file/folder selection commands
  • Loading branch information
pozitronik committed Nov 10, 2023
1 parent 0a839cb commit d4e98a9
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions sinner/gui/windows/SourcesLibraryForm.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from tkinter import Misc, NSEW
from tkinter import Misc, NSEW, Menu, filedialog, CASCADE, COMMAND
from typing import List, Callable

from customtkinter import CTkToplevel

from sinner.gui.controls.ThumbnailWidget import ThumbnailWidget
from sinner.utilities import is_image, is_dir, get_directory_file_list
from sinner.utilities import is_image, is_dir, get_directory_file_list, get_type_extensions


class SourcesLibraryForm:
Expand All @@ -30,6 +30,14 @@ def __init__(self, master: Misc, library: List[str], on_thumbnail_click_callback
self._on_thumbnail_click_callback = on_thumbnail_click_callback
self._on_window_close_callback = on_window_close_callback

self.MainMenu: Menu = Menu(self.SourcesLibraryWnd)
self.Library: Menu = Menu(self.MainMenu, tearoff=False)
self.MainMenu.add(CASCADE, menu=self.Library, label='Library')
self.Library.add(COMMAND, label='Add files', command=lambda: self.add_files())
self.Library.add(COMMAND, label='Add a folder', command=lambda: self.add_folder())

self.SourcesLibraryWnd.configure(menu=self.MainMenu, tearoff=False)

def show(self, show: bool = True) -> None:
if show is True:
self.SourcesLibraryWnd.deiconify()
Expand Down Expand Up @@ -60,3 +68,21 @@ def load(self, library: List[str] | None = None, callback: Callable[[str], None]
elif is_dir(item):
for dir_file in get_directory_file_list(item, is_image):
self.SourcesLibrary.add_thumbnail(image_path=dir_file, click_callback=lambda path: callback(path))

def add_files(self) -> None:
image_extensions = get_type_extensions('image/')
file_paths = filedialog.askopenfilenames(
title="Select files to add",
filetypes=[('Image files', image_extensions), ('All files', '*.*')],
initialdir="/", # Set the initial directory (you can change this)
)
if file_paths:
self.load(list(file_paths))

def add_folder(self) -> None:
directory = filedialog.askdirectory(
title="Select a directory to add",
initialdir="/",
)
if directory:
self.load([directory])

0 comments on commit d4e98a9

Please sign in to comment.