Merge branch 'master' of https://github.com/ObsidianNA/xenia into canary

Recent Files Menu Bar PR 1396
This commit is contained in:
Cancerous 2019-09-04 23:20:02 -04:00 committed by illusion98
parent 9644587e2c
commit c60418018e
2 changed files with 90 additions and 3 deletions

View File

@ -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/cvar.h"
@ -22,13 +21,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"
DECLARE_bool(debug);
#include <fstream>
#include <string>
DECLARE_bool(debug);
DEFINE_int32(window_height, 720, "Window height", "UI");
DEFINE_int32(window_width, 1280, "Window width", "UI");
@ -189,10 +189,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)));
@ -312,6 +339,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;
@ -339,6 +408,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.
@ -347,6 +418,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);
}
}
}
std::wstring EmulatorWindow::SwapNext() {
std::wstring path = L"";

View File

@ -46,6 +46,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();
@ -62,6 +64,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;
};