Private
Public Access
0
0

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:
2026-06-03 12:24:51 -04:00
parent ab54d6b8a8
commit 55eb923bd9
2 changed files with 74 additions and 29 deletions
+45 -29
View File
@@ -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):
+29
View File
@@ -0,0 +1,29 @@
from unittest.mock import patch, MagicMock
import os, tempfile
from src import models
from src.gui_2 import render_files_and_media
def test_files_rendered_under_directory_grouping(app_instance):
with tempfile.TemporaryDirectory() as tmp:
sub = os.path.join(tmp, "sub")
os.makedirs(sub, exist_ok=True)
for p in [os.path.join(tmp, "a.py"), os.path.join(tmp, "b.py"), os.path.join(sub, "c.py")]:
open(p, "w").close()
app_instance.files = [models.FileItem(path=os.path.join(tmp, "a.py")), models.FileItem(path=os.path.join(tmp, "b.py")), models.FileItem(path=os.path.join(sub, "c.py"))]
with patch("src.gui_2.imgui") as mock_imgui, patch("src.gui_2.imscope") as mock_imscope, patch("src.gui_2.filedialog") as mock_filedialog, patch("src.gui_2.hide_tk_root", return_value=MagicMock()):
mock_imgui.collapsing_header.return_value = True
mock_imgui.TableFlags_ = type("T", (), {"resizable": 1, "borders": 2, "row_bg": 4})()
mock_imgui.TableColumnFlags_ = type("C", (), {"width_fixed": 1, "width_stretch": 2})()
mock_imgui.begin_table.return_value = True
mock_imgui.button.return_value = False
mock_imscope.group.return_value.__enter__.return_value = None
mock_imscope.tree_node_ex.return_value.__enter__.return_value = True
mock_filedialog.askopenfilenames.return_value = ()
mock_filedialog.askdirectory.return_value = ""
try:
render_files_and_media(app_instance)
except Exception as e:
import pytest
pytest.fail(f"render_files_and_media raised: {e}")
assert len(app_instance.files) == 3
assert mock_imscope.tree_node_ex.called, "render_files_and_media should group files under tree_node_ex by directory"