Recent Files List
Overview: Added a Pop-up menu named 'Open Recent' under the 'File' menu bar item that lists 10 recently opened files. - It stores the paths of opened games inside a file named 'recent.txt' in the same folder where the xenia.exe is located. - If the file doesn't exits, the program creates it, then paths are stored when a new file is opened. - Each time a file is opened, the program compares stored paths with the new one, if it exists then the path is moved to the top of the list to indicate that it is the most recent file opened. - 'Open Recent' entries are refreshed once xenia is re-started.
This commit is contained in:
parent
3238fae758
commit
ccfe911567
src/xenia/app
|
@ -11,7 +11,6 @@
|
|||
|
||||
// Autogenerated by `xb premake`.
|
||||
#include "build/version.h"
|
||||
|
||||
#include "third_party/imgui/imgui.h"
|
||||
#include "xenia/base/clock.h"
|
||||
#include "xenia/base/debugging.h"
|
||||
|
@ -21,11 +20,14 @@
|
|||
#include "xenia/base/threading.h"
|
||||
#include "xenia/emulator.h"
|
||||
#include "xenia/gpu/graphics_system.h"
|
||||
|
||||
#include "xenia/ui/file_picker.h"
|
||||
#include "xenia/ui/imgui_dialog.h"
|
||||
#include "xenia/ui/imgui_drawer.h"
|
||||
|
||||
// For recent files R/W
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
namespace xe {
|
||||
namespace app {
|
||||
|
||||
|
@ -174,10 +176,37 @@ bool EmulatorWindow::Initialize() {
|
|||
// FIXME: This code is really messy.
|
||||
auto main_menu = MenuItem::Create(MenuItem::Type::kNormal);
|
||||
auto file_menu = MenuItem::Create(MenuItem::Type::kPopup, L"&File");
|
||||
// Recent Sub tab
|
||||
auto recent_list = MenuItem::Create(MenuItem::Type::kPopup, L"&Open Recent");
|
||||
//******Add Recent PATHS to menu***************************************
|
||||
std::string lines[10];
|
||||
std::ifstream recent_file_read("recent.txt");
|
||||
int i = 0;
|
||||
// Convert from UTF-8 to wide using helper function xe::to_wstring()
|
||||
if (recent_file_read.is_open()) {
|
||||
while ((i < 10) && getline(recent_file_read, lines[i])) {
|
||||
// Save paths to memory
|
||||
global_recent_paths_[i] = xe::to_wstring(lines[i]);
|
||||
// Add most recently opened files to menu
|
||||
if (lines[i] != "" && lines[i] != "\n") {
|
||||
recent_list->AddChild(
|
||||
MenuItem::Create(MenuItem::Type::kString, global_recent_paths_[i],
|
||||
std::bind(&EmulatorWindow::RecentList, this, i)));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
recent_file_read.close();
|
||||
}
|
||||
//****************************************************************************
|
||||
{
|
||||
file_menu->AddChild(
|
||||
MenuItem::Create(MenuItem::Type::kString, L"&Open...", L"Ctrl+O",
|
||||
std::bind(&EmulatorWindow::FileOpen, this)));
|
||||
// Add Recent List Tab to file tab
|
||||
file_menu->AddChild(std::move(recent_list));
|
||||
|
||||
file_menu->AddChild(
|
||||
MenuItem::Create(MenuItem::Type::kString, L"Close",
|
||||
std::bind(&EmulatorWindow::FileClose, this)));
|
||||
|
@ -297,6 +326,48 @@ void EmulatorWindow::FileDrop(wchar_t* filename) {
|
|||
}
|
||||
}
|
||||
|
||||
// Add new path to recent storage or prioritize if it already exist
|
||||
void EmulatorWindow::RecentListUpdater(std::wstring path) {
|
||||
//*******Save abs_path to recent as UTF-8******************************
|
||||
std::string u8_path = xe::to_string(path);
|
||||
|
||||
// Write to 'recent.txt' file
|
||||
std::string lines[10];
|
||||
int i = 0;
|
||||
int path_poss = 0;
|
||||
bool path_is_in = false;
|
||||
// Open file "recent.txt" to read values
|
||||
std::ifstream recent_file_read("recent.txt");
|
||||
if (recent_file_read.is_open()) {
|
||||
// Loop until end of document or 10 lines are read, which ever comes first
|
||||
while (getline(recent_file_read, lines[i]) && (i < 9)) {
|
||||
if (u8_path.compare(lines[i]) == 0) {
|
||||
path_is_in = true;
|
||||
path_poss = i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
recent_file_read.close();
|
||||
}
|
||||
// Move lines
|
||||
if (!path_is_in) {
|
||||
path_poss = 9;
|
||||
}
|
||||
for (i = path_poss; i > 0; i--) {
|
||||
lines[i] = lines[i - 1];
|
||||
}
|
||||
// Add all lines to file
|
||||
lines[0] = u8_path;
|
||||
std::ofstream recent_file_write("recent.txt");
|
||||
for (i = 0; i < 10; i++) {
|
||||
if (lines[i] != "" && lines[i] != "\n") {
|
||||
recent_file_write << lines[i] << std::endl;
|
||||
}
|
||||
}
|
||||
recent_file_write.close();
|
||||
//*****************************************************
|
||||
}
|
||||
|
||||
void EmulatorWindow::FileOpen() {
|
||||
std::wstring path;
|
||||
|
||||
|
@ -323,6 +394,8 @@ void EmulatorWindow::FileOpen() {
|
|||
// Normalize the path and make absolute.
|
||||
std::wstring abs_path = xe::to_absolute_path(path);
|
||||
|
||||
EmulatorWindow::RecentListUpdater(abs_path);
|
||||
|
||||
auto result = emulator_->LaunchPath(abs_path);
|
||||
if (XFAILED(result)) {
|
||||
// TODO: Display a message box.
|
||||
|
@ -331,6 +404,19 @@ void EmulatorWindow::FileOpen() {
|
|||
}
|
||||
}
|
||||
|
||||
// Launch recently opened files
|
||||
void EmulatorWindow::RecentList(int index) {
|
||||
if (!global_recent_paths_[index].empty()) {
|
||||
EmulatorWindow::RecentListUpdater(global_recent_paths_[index]);
|
||||
|
||||
auto result = emulator_->LaunchPath(global_recent_paths_[index]);
|
||||
if (XFAILED(result)) {
|
||||
// TODO: Display a message box.
|
||||
XELOGE("Failed to launch target: %.8X", result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EmulatorWindow::FileClose() {
|
||||
if (emulator_->is_title_open()) {
|
||||
emulator_->TerminateTitle();
|
||||
|
|
|
@ -45,6 +45,8 @@ class EmulatorWindow {
|
|||
|
||||
void FileDrop(wchar_t* filename);
|
||||
void FileOpen();
|
||||
void RecentListUpdater(std::wstring path);
|
||||
void RecentList(int index);
|
||||
void FileClose();
|
||||
void ShowContentDirectory();
|
||||
void CheckHideCursor();
|
||||
|
@ -61,6 +63,7 @@ class EmulatorWindow {
|
|||
std::unique_ptr<ui::Loop> loop_;
|
||||
std::unique_ptr<ui::Window> window_;
|
||||
std::wstring base_title_;
|
||||
std::wstring global_recent_paths_[10];
|
||||
uint64_t cursor_hide_time_ = 0;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue