From 228a15b5b3fb22ae391326d381689ff647812784 Mon Sep 17 00:00:00 2001 From: shweet Date: Sun, 22 Nov 2026 18:56:16 -0500 Subject: [PATCH] Open documents in one of three ways instead of two bools, and let dialogs know when they take many files DocumentOpen (open, new, or restored from an autosave) replaces isNew and isRecent and Manager::new_. The *_OPEN dialogs pick several files without being told. --- src/cli.cpp | 4 ++-- src/dialog.cpp | 6 ++++-- src/dialog.h | 2 +- src/document.cpp | 3 ++- src/document.h | 11 ++++++++++- src/manager.cpp | 13 ++++--------- src/manager.h | 3 +-- src/ui/libraries.cpp | 2 +- src/ui/sounds.cpp | 2 +- src/ui/spritesheets.cpp | 3 +-- src/ui/taskbar.cpp | 6 +++--- src/ui/welcome.cpp | 2 +- tests/render/render.cpp | 10 +++++----- 13 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/cli.cpp b/src/cli.cpp index 7f7dda0..f1e7b96 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -213,7 +213,7 @@ static int draw_run(const Arguments& arguments) } std::string error; - Document document(arguments.file, false, &error); + Document document(arguments.file, DOCUMENT_OPEN, &error); if (!document.is_valid()) { std::println(stderr, "could not load {}: {}", path_to_utf8(arguments.file), error); @@ -309,7 +309,7 @@ static int convert_run(const Arguments& arguments) if (format == "xml") gl.emplace(); std::string error; - Document document(arguments.file, false, &error); + Document document(arguments.file, DOCUMENT_OPEN, &error); auto fileUtf8 = path_to_utf8(arguments.file); if (!document.is_valid()) { diff --git a/src/dialog.cpp b/src/dialog.cpp index afbdc3d..985d903 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -42,14 +42,16 @@ Dialog::Dialog(SDL_Window* window) this->window = window; } -void Dialog::file_open(Type type, bool isMany) +// The *_OPEN dialogs pick any number of files. The others pick one. +void Dialog::file_open(Type type) { if (type == Dialog::NONE) return; path.clear(); paths.clear(); SDL_ShowOpenFileDialog(callback, this, window, FILTERS[TYPE_FILTERS[type]], std::size(FILTERS[TYPE_FILTERS[type]]), - nullptr, isMany); + nullptr, + type == ANM2_OPEN || type == SOUND_OPEN || type == LIBRARY_OPEN || type == SPRITESHEET_OPEN); this->type = type; } diff --git a/src/dialog.h b/src/dialog.h index 9e25a62..01616d2 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -87,7 +87,7 @@ public: Dialog() = default; Dialog(SDL_Window*); - void file_open(Type type, bool isMany = false); + void file_open(Type type); void file_save(Type type, const std::filesystem::path& defaultPath = {}); void folder_open(Type type); bool is_selected(Type type) const; diff --git a/src/document.cpp b/src/document.cpp index 41a9029..3a7a9e5 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -186,8 +186,9 @@ static void frame_time_sync(Document& document) document.frameTime = item && frameIndex >= 0 ? frame_time_from_index_get(*item, frameIndex) : 0.0f; } -Document::Document(const std::filesystem::path& path, bool isNew, std::string* errorString) +Document::Document(const std::filesystem::path& path, DocumentOpen mode, std::string* errorString) { + auto isNew = mode == DOCUMENT_NEW; tabId = document_tab_id_next(); model = model_make(); if (!isNew) diff --git a/src/document.h b/src/document.h index c251df6..84803e6 100644 --- a/src/document.h +++ b/src/document.h @@ -21,6 +21,15 @@ #include "shader.h" class Manager; + +// How a document comes to be open: read from its file, made new there, or restored from an autosave (kept out of the +// recent files). +enum DocumentOpen +{ + DOCUMENT_OPEN, + DOCUMENT_NEW, + DOCUMENT_RESTORE +}; struct Command; // Ids and labels for a combo box of content items. @@ -116,7 +125,7 @@ public: EditTarget editTarget = EditTarget::NONE; UidIndex index = {}; - Document(const std::filesystem::path&, bool = false, std::string* = nullptr); + Document(const std::filesystem::path&, DocumentOpen = DOCUMENT_OPEN, std::string* = nullptr); Document(const Document&) = delete; Document& operator=(const Document&) = delete; Document(Document&&) = default; diff --git a/src/manager.cpp b/src/manager.cpp index 91c7383..fb12af5 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -148,10 +148,10 @@ void Manager::commands_run() command.runManager(*this); } -Document* Manager::open(const std::filesystem::path& path, bool isNew, bool isRecent) +Document* Manager::open(const std::filesystem::path& path, DocumentOpen mode) { std::string errorString; - documents.emplace_back(path, isNew, &errorString); + documents.emplace_back(path, mode, &errorString); auto pathString = path_to_utf8(path); auto& document = documents.back(); @@ -162,7 +162,7 @@ Document* Manager::open(const std::filesystem::path& path, bool isNew, bool isRe return nullptr; } - if (isRecent) + if (mode != DOCUMENT_RESTORE) recent_file_add(path); selected = (int)documents.size() - 1; @@ -176,11 +176,6 @@ Document* Manager::open(const std::filesystem::path& path, bool isNew, bool isRe return &document; } -void Manager::new_(const std::filesystem::path& path) -{ - open(path, true, true); -} - bool Manager::save(int index, const std::filesystem::path& path) { if (auto document = get(index); document) @@ -482,7 +477,7 @@ void Manager::autosave_files_open() { for (auto& path : autosaveFiles) { - if (auto document = open(path, false, false)) + if (auto document = open(path, DOCUMENT_RESTORE)) { document->isForceDirty = true; document->path = document->path_from_autosave_get(path); diff --git a/src/manager.h b/src/manager.h index bbb28bf..7a393d5 100644 --- a/src/manager.h +++ b/src/manager.h @@ -88,8 +88,7 @@ public: Document* get(int = -1); void command_push(Command); void commands_run(); - Document* open(const std::filesystem::path&, bool = false, bool = true); - void new_(const std::filesystem::path&); + Document* open(const std::filesystem::path&, DocumentOpen = DOCUMENT_OPEN); bool save(int, const std::filesystem::path& = {}); bool save(const std::filesystem::path& = {}); void autosave(Document&); diff --git a/src/ui/libraries.cpp b/src/ui/libraries.cpp index fc23603..9b12821 100644 --- a/src/ui/libraries.cpp +++ b/src/ui/libraries.cpp @@ -47,7 +47,7 @@ void libraries_update(Panel& panel) actions.add(ACTION_OPEN, isOne, [&]() { open(*selection.begin()); }, TOOLTIP_LIBRARY_OPEN); actions.add(ACTION_OPEN_DIRECTORY, isOne, [&]() { directory_open(dialog, document, item_get(libraries, *selection.begin())->path); }); - actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::LIBRARY_OPEN, true); }, TOOLTIP_LIBRARY_ADD); + actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::LIBRARY_OPEN); }, TOOLTIP_LIBRARY_ADD); content_remove_action_add(actions, panel, ElementType::LIBRARY, SelectionKind::LIBRARIES); actions.add( ACTION_RELOAD, [selection]() { return !selection.empty(); }, diff --git a/src/ui/sounds.cpp b/src/ui/sounds.cpp index b9d7371..8c5bde0 100644 --- a/src/ui/sounds.cpp +++ b/src/ui/sounds.cpp @@ -59,7 +59,7 @@ void sounds_update(Panel& panel) Actions actions = {}; actions.add(ACTION_PLAY, isOne, [&]() { play(*selection.begin()); }, STRING_UNDEFINED, -1); actions.add(ACTION_OPEN_DIRECTORY, isOne, [&]() { open(*selection.begin()); }); - actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SOUND_OPEN, true); }, TOOLTIP_SOUND_ADD); + actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SOUND_OPEN); }, TOOLTIP_SOUND_ADD); content_remove_action_add(actions, panel, ElementType::SOUND_ELEMENT, SelectionKind::SOUNDS); actions.add( ACTION_RELOAD, [selection]() { return !selection.empty(); }, diff --git a/src/ui/spritesheets.cpp b/src/ui/spritesheets.cpp index 2f4a66b..e6b714f 100644 --- a/src/ui/spritesheets.cpp +++ b/src/ui/spritesheets.cpp @@ -241,8 +241,7 @@ void spritesheets_update(Panel& panel, SpritesheetsPanel& spritesheets) Actions actions = {}; actions.add(ACTION_OPEN_DIRECTORY, isOne, [&]() { open(*selection.begin()); }); actions.add(ACTION_SET_FILE_PATH, isOne, [&]() { dialog.file_save(Dialog::SPRITESHEET_PATH_SET); }); - actions.add( - ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SPRITESHEET_OPEN, true); }, TOOLTIP_ADD_SPRITESHEET); + actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SPRITESHEET_OPEN); }, TOOLTIP_ADD_SPRITESHEET); content_remove_action_add(actions, panel, ElementType::SPRITESHEET, SelectionKind::SPRITESHEETS); actions.add( ACTION_RELOAD, isAny, diff --git a/src/ui/taskbar.cpp b/src/ui/taskbar.cpp index 997f097..248782c 100644 --- a/src/ui/taskbar.cpp +++ b/src/ui/taskbar.cpp @@ -90,7 +90,7 @@ void Taskbar::update(Manager& manager, Settings& settings, Resources& resources, if (ImGui::MenuItem(localize.get(BASIC_NEW), settings.shortcutNew.c_str())) dialog.file_save(Dialog::ANM2_CREATE); if (ImGui::MenuItem(localize.get(BASIC_OPEN), settings.shortcutOpen.c_str())) - dialog.file_open(Dialog::ANM2_OPEN, true); + dialog.file_open(Dialog::ANM2_OPEN); auto recentFiles = manager.recent_files_ordered(); if (ImGui::BeginMenu(localize.get(LABEL_OPEN_RECENT), !recentFiles.empty())) @@ -134,7 +134,7 @@ void Taskbar::update(Manager& manager, Settings& settings, Resources& resources, if (dialog.is_selected(Dialog::ANM2_CREATE)) { auto path = dialog.path; - manager.command_push({.runManager = [path](Manager& manager) { manager.new_(path); }}); + manager.command_push({.runManager = [path](Manager& manager) { manager.open(path, DOCUMENT_NEW); }}); dialog.reset(); } @@ -446,7 +446,7 @@ void Taskbar::update(Manager& manager, Settings& settings, Resources& resources, if (shortcut(manager.chords[SHORTCUT_NEW], SCOPE_GLOBAL)) dialog.file_save(Dialog::ANM2_CREATE); if (shortcut(manager.chords[SHORTCUT_OPEN], SCOPE_GLOBAL)) - dialog.file_open(Dialog::ANM2_OPEN, true); + dialog.file_open(Dialog::ANM2_OPEN); if (shortcut(manager.chords[SHORTCUT_SAVE], SCOPE_GLOBAL)) { if (settings.fileIsWarnOverwrite) diff --git a/src/ui/welcome.cpp b/src/ui/welcome.cpp index 02b42b7..e7773b4 100644 --- a/src/ui/welcome.cpp +++ b/src/ui/welcome.cpp @@ -38,7 +38,7 @@ void welcome_update(Manager& manager, Resources& resources, Dialog& dialog, Task dialog.file_save(Dialog::ANM2_CREATE); // handled in taskbar.cpp ImGui::SameLine(); if (ImGui::Button(localize.get(BASIC_OPEN), widgetSize)) - dialog.file_open(Dialog::ANM2_OPEN, true); // handled in taskbar.cpp + dialog.file_open(Dialog::ANM2_OPEN); // handled in taskbar.cpp if (ImGui::BeginChild("##Recent Files Child", {}, ImGuiChildFlags_Borders)) { diff --git a/tests/render/render.cpp b/tests/render/render.cpp index 429e1ed..7337ed0 100644 --- a/tests/render/render.cpp +++ b/tests/render/render.cpp @@ -60,7 +60,7 @@ struct RenderFixture if (!gl.is_valid()) return; std::filesystem::create_directories(OUTPUT_DIR); - document.emplace(RENDER_TEST_DIR / file, false); + document.emplace(RENDER_TEST_DIR / file); auto& info = SHADERS[SHADER_TEXTURE]; shader.emplace(info.vertex, info.fragment); canvas.emplace(Vec2(1.0f)); @@ -210,7 +210,7 @@ TEST_CASE("converting SVG spritesheets writes PNGs beside them") for (auto name : {"vector.anm2", "vector.svg"}) std::filesystem::copy_file(RENDER_TEST_DIR / name, directory / name); - Document document(directory / "vector.anm2", false); + Document document(directory / "vector.anm2"); REQUIRE(document.is_valid()); document.spritesheets_vector_convert({0}); CHECK(document.model.content.spritesheets.front().path == "vector.png"); @@ -242,7 +242,7 @@ TEST_CASE("a Rebirth save flattens animation layers for the game") "Path=\"library/spin.anm2\""); std::ofstream(directory / "nested.anm2") << text; - Document document(directory / "nested.anm2", false); + Document document(directory / "nested.anm2"); REQUIRE(document.is_valid()); REQUIRE(document.save(directory / "rebirth.anm2")); std::ifstream file(directory / "rebirth.anm2"); @@ -278,7 +278,7 @@ TEST_CASE("the game's copy bakes skew and blur into a spritesheet that draws the "root": [ { "pos": [2, 3], "skew": [25, 10], "scale": [150, 100], "delay": 2 } ], "layers": [ { "layer": 1, "frames": [ { "crop": [16, 0], "size": [16, 16], "pivot": [8, 8], "delay": 2 } ] } ] } } })"; - Document document(directory / "bake.anm2", false); + Document document(directory / "bake.anm2"); REQUIRE(document.is_valid()); REQUIRE(document.save(directory / "saved.anm2")); CHECK(std::filesystem::exists(directory / "saved.baked.png")); @@ -291,7 +291,7 @@ TEST_CASE("the game's copy bakes skew and blur into a spritesheet that draws the CHECK(game.find("SkewX") == std::string::npos); CHECK(game.find("Blur") == std::string::npos); std::ofstream(directory / "game.anm2") << game; - Document gameDocument(directory / "game.anm2", false); + Document gameDocument(directory / "game.anm2"); REQUIRE(gameDocument.is_valid()); auto& info = SHADERS[SHADER_TEXTURE];