Skip to content

Commit

Permalink
Cache current file path in imgui.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
Smertig committed Nov 13, 2020
1 parent a34fafa commit 8727184
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion source/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

app::app(const std::string& app_name)
: m_window(sf::VideoMode::getFullscreenModes().front(), app_name)
, m_imgui_inited((ImGui::SFML::Init(m_window, false), true))
, m_camera(m_window)
, m_ui_file_dialog([this](std::filesystem::path replay_path) { open_replay(std::move(replay_path)); })
{
m_window.setVerticalSyncEnabled(true);

ImGui::SFML::Init(m_window, false);
::init_fonts();
}

Expand Down
1 change: 1 addition & 0 deletions source/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class app {
};

sf::RenderWindow m_window;
bool m_imgui_inited; // small hack to init ImGui just after m_window creation
scene::camera m_camera;
ui::file_dialog m_ui_file_dialog;
std::optional<map_state> m_current_map;
Expand Down
48 changes: 45 additions & 3 deletions source/ui/file_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <fmt/format.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <imgui_stdlib.h>

#include <fstream>
Expand Down Expand Up @@ -39,18 +40,22 @@ ImColor convert_color(int color_id) {
}
}

bool starts_with(std::string_view string, std::string_view prefix) {
return string.length() >= prefix.length() && string.substr(0, prefix.length()) == prefix;
}

} // unnamed namespace

namespace ui {

file_dialog::file_dialog(std::function<void(const std::filesystem::path&)> open_file_callback)
: m_open_file_callback(std::move(open_file_callback))
{
if (try_set_path(fs::current_path()) || try_set_path(fs::temp_directory_path())) {
return;
if (!try_set_path(fs::current_path()) && !try_set_path(fs::temp_directory_path())) {
throw std::runtime_error("Unable to set default path");
}

throw std::runtime_error("Unable to set default path");
add_settings_handler();
}

void file_dialog::update(int dt) {
Expand Down Expand Up @@ -208,4 +213,41 @@ void file_dialog::refresh_cache() {
});
}

void file_dialog::add_settings_handler() {
ImGuiSettingsHandler ini_handler;

ini_handler.TypeName = "FileDialog";
ini_handler.TypeHash = ImHashStr("FileDialog");
ini_handler.UserData = this; // TODO: lifetime

ini_handler.ReadOpenFn = [](ImGuiContext*, ImGuiSettingsHandler*, const char* name) -> void* {
static int dummy_var;
return &dummy_var;
};

ini_handler.ReadLineFn = [](ImGuiContext*, ImGuiSettingsHandler* handler, void* entry, const char* line) {
auto self = static_cast<file_dialog*>(handler->UserData);

auto try_extract = [line](std::string_view prefix) -> std::optional<std::string_view> {
if (starts_with(line, prefix)) {
return line + prefix.length();
}
return std::nullopt;
};

if (auto saved_path = try_extract("path=")) {
self->try_set_path(std::filesystem::path{ *saved_path, std::filesystem::path::format::generic_format });
}
};

ini_handler.WriteAllFn = [](ImGuiContext* ctx, ImGuiSettingsHandler* handler, ImGuiTextBuffer* out_buf) {
auto self = static_cast<file_dialog*>(handler->UserData);

out_buf->appendf("[%s][Settings]\n", handler->TypeName);
out_buf->appendf("path=%s\n", self->m_current_path.generic_u8string().c_str());
};

ImGui::GetCurrentContext()->SettingsHandlers.push_back(ini_handler);
}

} // namespace ui
2 changes: 2 additions & 0 deletions source/ui/file_dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class file_dialog {
bool try_set_path(std::filesystem::path path);

void refresh_cache();

void add_settings_handler();
};

} // namespace ui

0 comments on commit 8727184

Please sign in to comment.