feat(files-media): group files by directory + add 'Add Directory' button
- render_files_and_media now wraps the per-file loop in directory groups
via aggregate.group_files_by_dir + imscope.tree_node_ex (mirrors the
Context Composition visual style at gui_2.py:3114)
- New 'Add Directory' button next to 'Add Files to Inventory':
uses filedialog.askdirectory() + os.walk to bulk-import a folder tree
- Button IDs (i, add_f_{i}, rem_f_{i}) preserve global uniqueness via
file_indices map (regression-safe across the directory wrap)
- Test uses mock button=False, mock filedialog.askopenfilenames/askdirectory
to avoid opening a real Tk dialog during test run
This commit is contained in:
+45
-29
@@ -2701,36 +2701,41 @@ def render_files_and_media(app: App) -> None:
|
||||
|
||||
to_remove_idx = -1
|
||||
app.files.sort(key=lambda f: f.path.lower() if hasattr(f, 'path') else str(f).lower())
|
||||
for i, f_item in enumerate(app.files):
|
||||
imgui.table_next_row()
|
||||
imgui.table_set_column_index(0)
|
||||
fpath = f_item.path if hasattr(f_item, 'path') else str(f_item)
|
||||
in_context = any((cf.path if hasattr(cf, 'path') else str(cf)) == fpath for cf in app.context_files)
|
||||
is_cached = any(fpath in c for c in getattr(app, '_cached_files', []))
|
||||
file_indices = {id(f): idx for idx, f in enumerate(app.files)}
|
||||
grouped = aggregate.group_files_by_dir(app.files)
|
||||
for dir_name, g_files in sorted(grouped.items()):
|
||||
with imscope.tree_node_ex(f"{dir_name}##files_dir", imgui.TreeNodeFlags_.default_open) as dir_open:
|
||||
if dir_open:
|
||||
for f_item in g_files:
|
||||
i = file_indices[id(f_item)]
|
||||
imgui.table_next_row()
|
||||
imgui.table_set_column_index(0)
|
||||
fpath = f_item.path if hasattr(f_item, 'path') else str(f_item)
|
||||
in_context = any((cf.path if hasattr(cf, 'path') else str(cf)) == fpath for cf in app.context_files)
|
||||
is_cached = any(fpath in c for c in getattr(app, '_cached_files', []))
|
||||
|
||||
if imgui.button(f"+##add_f_{i}"):
|
||||
if not in_context:
|
||||
from src import models
|
||||
new_item = models.FileItem(path=fpath)
|
||||
app.context_files.append(new_item)
|
||||
app._populate_auto_slices(new_item)
|
||||
|
||||
imgui.same_line()
|
||||
if imgui.button(f"x##rem_f_{i}"):
|
||||
to_remove_idx = i
|
||||
if imgui.button(f"+##add_f_{i}"):
|
||||
if not in_context:
|
||||
from src import models
|
||||
new_item = models.FileItem(path=fpath)
|
||||
app.context_files.append(new_item)
|
||||
app._populate_auto_slices(new_item)
|
||||
|
||||
imgui.same_line()
|
||||
if imgui.button(f"x##rem_f_{i}"):
|
||||
to_remove_idx = i
|
||||
|
||||
imgui.table_set_column_index(1)
|
||||
imgui.text(fpath)
|
||||
if imgui.is_item_hovered(): imgui.set_tooltip(fpath)
|
||||
|
||||
imgui.table_set_column_index(2)
|
||||
if in_context:
|
||||
imgui.text_colored(imgui.ImVec4(0.3, 0.8, 0.3, 1), "Active")
|
||||
elif is_cached:
|
||||
imgui.text_colored(imgui.ImVec4(0.3, 0.8, 1, 1), "Cached")
|
||||
else:
|
||||
imgui.text_disabled(" - ")
|
||||
|
||||
imgui.table_set_column_index(1)
|
||||
imgui.text(fpath)
|
||||
if imgui.is_item_hovered(): imgui.set_tooltip(fpath)
|
||||
|
||||
imgui.table_set_column_index(2)
|
||||
if in_context:
|
||||
imgui.text_colored(imgui.ImVec4(0.3, 0.8, 0.3, 1), "Active")
|
||||
elif is_cached:
|
||||
imgui.text_colored(imgui.ImVec4(0.3, 0.8, 1, 1), "Cached")
|
||||
else:
|
||||
imgui.text_disabled(" - ")
|
||||
imgui.end_table()
|
||||
if to_remove_idx != -1: app.files.pop(to_remove_idx)
|
||||
|
||||
@@ -2738,7 +2743,18 @@ def render_files_and_media(app: App) -> None:
|
||||
if imgui.button("Add Files to Inventory"):
|
||||
r = hide_tk_root(); paths = filedialog.askopenfilenames(); r.destroy()
|
||||
for p in paths:
|
||||
if p not in [f.path if hasattr(f, 'path') else f for f in app.files]: app.files.append(models.FileItem(path=p))
|
||||
if p not in [f.path if hasattr(f, "path") else f for f in app.files]: app.files.append(models.FileItem(path=p))
|
||||
imgui.same_line()
|
||||
if imgui.button("Add Directory"):
|
||||
r = hide_tk_root(); dirpath = filedialog.askdirectory(); r.destroy()
|
||||
if dirpath:
|
||||
existing = {f.path if hasattr(f, "path") else str(f) for f in app.files}
|
||||
for root, _dirs, files in os.walk(dirpath):
|
||||
for fname in files:
|
||||
full = os.path.join(root, fname)
|
||||
if full not in existing:
|
||||
app.files.append(models.FileItem(path=full))
|
||||
existing.add(full)
|
||||
|
||||
imgui.separator()
|
||||
if imgui.collapsing_header("Screenshots", imgui.TreeNodeFlags_.default_open):
|
||||
|
||||
Reference in New Issue
Block a user