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.
This commit is contained in:
1 parent
1fc6b064ae
commit
228a15b5b3
13 files changed
+36
-31
No files matched your search
+2
-2
@@ -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())
|
||||
{
|
||||
|
||||
+4
-2
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
+2
-1
@@ -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)
|
||||
|
||||
+10
-1
@@ -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;
|
||||
|
||||
+4
-9
@@ -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);
|
||||
|
||||
+1
-2
@@ -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&);
|
||||
|
||||
@@ -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(); },
|
||||
|
||||
+1
-1
@@ -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(); },
|
||||
|
||||
@@ -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,
|
||||
|
||||
+3
-3
@@ -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)
|
||||
|
||||
+1
-1
@@ -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))
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user