Refactor recent files management: consolidate RecentFilesManager into project.h and remove redundant recent_files.h
This commit is contained in:
@@ -1,16 +1,20 @@
|
|||||||
#ifndef YAZE_APP_CORE_PROJECT_H
|
#ifndef YAZE_APP_CORE_PROJECT_H
|
||||||
#define YAZE_APP_CORE_PROJECT_H
|
#define YAZE_APP_CORE_PROJECT_H
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "absl/status/status.h"
|
#include "absl/status/status.h"
|
||||||
#include "app/core/common.h"
|
#include "app/core/common.h"
|
||||||
#include "app/core/constants.h"
|
#include "app/core/constants.h"
|
||||||
|
#include "app/core/utils/file_util.h"
|
||||||
|
|
||||||
namespace yaze {
|
namespace yaze {
|
||||||
namespace app {
|
namespace app {
|
||||||
|
|
||||||
|
const std::string kRecentFilesFilename = "recent_files.txt";
|
||||||
constexpr char kEndOfProjectFile[] = "EndOfProjectFile";
|
constexpr char kEndOfProjectFile[] = "EndOfProjectFile";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,13 +35,13 @@ struct Project : public core::ExperimentFlags {
|
|||||||
* @return An absl::Status indicating the success or failure of the project
|
* @return An absl::Status indicating the success or failure of the project
|
||||||
* creation.
|
* creation.
|
||||||
*/
|
*/
|
||||||
absl::Status Create(const std::string &project_name) {
|
absl::Status Create(const std::string& project_name) {
|
||||||
name = project_name;
|
name = project_name;
|
||||||
project_opened_ = true;
|
project_opened_ = true;
|
||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
absl::Status Open(const std::string &project_path);
|
absl::Status Open(const std::string& project_path);
|
||||||
absl::Status Save();
|
absl::Status Save();
|
||||||
|
|
||||||
absl::Status CheckForEmptyFields() {
|
absl::Status CheckForEmptyFields() {
|
||||||
@@ -61,6 +65,54 @@ struct Project : public core::ExperimentFlags {
|
|||||||
std::string keybindings_file = "";
|
std::string keybindings_file = "";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class RecentFilesManager {
|
||||||
|
public:
|
||||||
|
RecentFilesManager() : RecentFilesManager(kRecentFilesFilename) {}
|
||||||
|
RecentFilesManager(const std::string& filename) : filename_(filename) {}
|
||||||
|
|
||||||
|
void AddFile(const std::string& file_path) {
|
||||||
|
// Add a file to the list, avoiding duplicates
|
||||||
|
auto it = std::find(recent_files_.begin(), recent_files_.end(), file_path);
|
||||||
|
if (it == recent_files_.end()) {
|
||||||
|
recent_files_.push_back(file_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Save() {
|
||||||
|
std::ofstream file(filename_);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return; // Handle the error appropriately
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& file_path : recent_files_) {
|
||||||
|
file << file_path << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Load() {
|
||||||
|
std::ifstream file(filename_);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
recent_files_.clear();
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(file, line)) {
|
||||||
|
if (!line.empty()) {
|
||||||
|
recent_files_.push_back(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<std::string>& GetRecentFiles() const {
|
||||||
|
return recent_files_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string filename_;
|
||||||
|
std::vector<std::string> recent_files_;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace app
|
} // namespace app
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
#include "app/editor/sprite/sprite_editor.h"
|
#include "app/editor/sprite/sprite_editor.h"
|
||||||
#include "app/editor/utils/editor.h"
|
#include "app/editor/utils/editor.h"
|
||||||
#include "app/editor/utils/gfx_context.h"
|
#include "app/editor/utils/gfx_context.h"
|
||||||
#include "app/editor/utils/recent_files.h"
|
|
||||||
#include "app/emu/emulator.h"
|
#include "app/emu/emulator.h"
|
||||||
#include "app/gfx/snes_palette.h"
|
#include "app/gfx/snes_palette.h"
|
||||||
#include "app/gfx/snes_tile.h"
|
#include "app/gfx/snes_tile.h"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "absl/strings/match.h"
|
#include "absl/strings/match.h"
|
||||||
#include "app/core/constants.h"
|
#include "app/core/constants.h"
|
||||||
#include "app/core/platform/file_dialog.h"
|
#include "app/core/platform/file_dialog.h"
|
||||||
|
#include "app/core/project.h"
|
||||||
#include "app/editor/code/assembly_editor.h"
|
#include "app/editor/code/assembly_editor.h"
|
||||||
#include "app/editor/dungeon/dungeon_editor.h"
|
#include "app/editor/dungeon/dungeon_editor.h"
|
||||||
#include "app/editor/graphics/graphics_editor.h"
|
#include "app/editor/graphics/graphics_editor.h"
|
||||||
@@ -13,7 +14,6 @@
|
|||||||
#include "app/editor/overworld/overworld_editor.h"
|
#include "app/editor/overworld/overworld_editor.h"
|
||||||
#include "app/editor/sprite/sprite_editor.h"
|
#include "app/editor/sprite/sprite_editor.h"
|
||||||
#include "app/editor/utils/flags.h"
|
#include "app/editor/utils/flags.h"
|
||||||
#include "app/editor/utils/recent_files.h"
|
|
||||||
#include "app/emu/emulator.h"
|
#include "app/emu/emulator.h"
|
||||||
#include "app/gui/icons.h"
|
#include "app/gui/icons.h"
|
||||||
#include "app/gui/input.h"
|
#include "app/gui/input.h"
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#define IMGUI_DEFINE_MATH_OPERATORS
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
||||||
|
|
||||||
#include "absl/status/status.h"
|
#include "absl/status/status.h"
|
||||||
#include "app/core/message.h"
|
|
||||||
#include "app/core/project.h"
|
#include "app/core/project.h"
|
||||||
#include "app/editor/code/assembly_editor.h"
|
#include "app/editor/code/assembly_editor.h"
|
||||||
#include "app/editor/code/memory_editor.h"
|
#include "app/editor/code/memory_editor.h"
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
#ifndef YAZE_APP_EDITOR_UTILS_RECENT_FILES_H
|
|
||||||
#define YAZE_APP_EDITOR_UTILS_RECENT_FILES_H
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <fstream>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace yaze {
|
|
||||||
namespace app {
|
|
||||||
namespace editor {
|
|
||||||
|
|
||||||
class RecentFilesManager {
|
|
||||||
public:
|
|
||||||
RecentFilesManager(const std::string& filename) : filename_(filename) {}
|
|
||||||
|
|
||||||
void AddFile(const std::string& filePath) {
|
|
||||||
// Add a file to the list, avoiding duplicates
|
|
||||||
auto it = std::find(recentFiles_.begin(), recentFiles_.end(), filePath);
|
|
||||||
if (it == recentFiles_.end()) {
|
|
||||||
recentFiles_.push_back(filePath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Save() {
|
|
||||||
std::ofstream file(filename_);
|
|
||||||
if (!file.is_open()) {
|
|
||||||
return; // Handle the error appropriately
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& filePath : recentFiles_) {
|
|
||||||
file << filePath << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Load() {
|
|
||||||
std::ifstream file(filename_);
|
|
||||||
if (!file.is_open()) {
|
|
||||||
return; // Handle the error appropriately
|
|
||||||
}
|
|
||||||
|
|
||||||
recentFiles_.clear();
|
|
||||||
std::string line;
|
|
||||||
while (std::getline(file, line)) {
|
|
||||||
if (!line.empty()) {
|
|
||||||
recentFiles_.push_back(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<std::string>& GetRecentFiles() const {
|
|
||||||
return recentFiles_;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string filename_;
|
|
||||||
std::vector<std::string> recentFiles_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace editor
|
|
||||||
} // namespace app
|
|
||||||
} // namespace yaze
|
|
||||||
|
|
||||||
#endif // YAZE_APP_EDITOR_UTILS_RECENT_FILES_H
|
|
||||||
Reference in New Issue
Block a user