Skip to content

Commit

Permalink
Support moving back entries in menu, other minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
XorTroll committed Sep 16, 2023
1 parent f1749f7 commit 01262e8
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 27 deletions.
2 changes: 2 additions & 0 deletions libs/uCommon/include/ul/menu/menu_Entries.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ namespace ul::menu {
void ReloadApplicationInfo();

void MoveTo(const std::string &new_folder_path);
void MoveToParentFolder();
void MoveToRoot();
void Save() const;
void Remove();

Expand Down
34 changes: 31 additions & 3 deletions libs/uCommon/source/ul/menu/menu_Entries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,25 @@ namespace ul::menu {
this->entry_path = new_entry_path;
}

void Entry::MoveToParentFolder() {
// Note: not moving the folder's actual fs folder (for folder entries) to avoid messy logic, this should work just fine

const auto cur_folder_path = fs::GetBaseDirectory(this->entry_path);
const auto cur_parent_folder_path = fs::GetBaseDirectory(cur_folder_path);

const auto new_entry_path = fs::JoinPath(cur_parent_folder_path, fs::GetBaseName(this->entry_path));
fs::RenameFile(this->entry_path, new_entry_path);
this->entry_path = new_entry_path;
}

void Entry::MoveToRoot() {
// Note: not moving the folder's actual fs folder (for folder entries) to avoid messy logic, this should work just fine

const auto new_entry_path = fs::JoinPath(MenuPath, fs::GetBaseName(this->entry_path));
fs::RenameFile(this->entry_path, new_entry_path);
this->entry_path = new_entry_path;
}

void Entry::Save() const {
auto entry_json = util::JSON::object();
entry_json["type"] = static_cast<u32>(this->type);
Expand Down Expand Up @@ -503,21 +522,30 @@ namespace ul::menu {
if(i < (entries.size() - 1)) {
auto &next_entry = entries.at(i + 1);

if((next_entry.index - cur_entry.index) == 1) {
if(next_entry.index == (cur_entry.index + 1)) {
needs_reindexing = true;
break;
}
if(cur_entry.index == (next_entry.index + 1)) {
needs_reindexing = true;
break;
}
if(next_entry.index == cur_entry.index) {
needs_reindexing = true;
break;
}
}
}

if(needs_reindexing) {
UL_LOG_INFO("Reindexing entries at '%s'...", path.c_str());
const auto index_gap = UINT32_MAX / entries.size();
u32 cur_start_idx = 0;
for(auto &entry : entries) {
entry.Remove();

u32 new_idx;
entry.entry_path = AllocateEntryPath(cur_start_idx, cur_start_idx + index_gap, path, new_idx);
const auto new_idx = cur_start_idx + index_gap / 2;
entry.entry_path = MakeEntryPath(path, new_idx);
entry.index = new_idx;
entry.Save();
cur_start_idx += index_gap;
Expand Down
4 changes: 4 additions & 0 deletions projects/uManager/include/ul/man/ui/ui_MainMenuLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace ul::man::ui {

pu::ui::elm::Menu::Ref options_menu;
pu::ui::elm::MenuItem::Ref activate_menu_item;
pu::ui::elm::MenuItem::Ref reset_menu_menu_item;
pu::ui::elm::MenuItem::Ref reset_cache_menu_item;
pu::ui::elm::MenuItem::Ref update_menu_item;

inline void ResetInfoText() {
Expand All @@ -24,6 +26,8 @@ namespace ul::man::ui {
PU_SMART_CTOR(MainMenuLayout)

void activate_DefaultKey();
void resetMenu_DefaultKey();
void resetCache_DefaultKey();
void update_DefaultKey();
};

Expand Down
10 changes: 9 additions & 1 deletion projects/uManager/romfs/lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
"status_active": "active",
"status_not_active": "not active",
"status_not_present": "not present",
"update_item": "Check for updates",
"activate_changes_title": "Changes",
"activate_changes": "The (de)activation of uLaunch needs a reboot for changes to take effect.",
"activate_continue": "Continue",
"activate_not_present": "uLaunch was not found in the SD card.",
"reset_menu_item": "Reset menu entries",
"reset_menu_title": "Reset menu",
"reset_menu_conf": "This will reset all the menu entries to default.\nAre you sure you want to continue?",
"reset_menu_success": "Menu entries were successfully reset.",
"reset_cache_item": "Reset menu cache",
"reset_cache_title": "Reset menu cache",
"reset_cache_conf": "This will reset all the menu cache (app/homebrew/user images, etc.).\nAre you sure you want to continue?",
"reset_cache_success": "Menu cache was successfully reset.",
"update_item": "Check for updates",
"update_title": "Update check",
"update_error": "Unable to retrieve last update information...",
"update_version": "Last release",
Expand Down
2 changes: 2 additions & 0 deletions projects/uManager/source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ul::man::ui::MainApplication::Ref g_MainApplication;

int main() {
ul::InitializeLogging("uManager");
UL_RC_ASSERT(nsInitialize());

auto renderer_opts = pu::ui::render::RendererInitOptions(SDL_INIT_EVERYTHING, pu::ui::render::RendererHardwareFlags);
renderer_opts.UseImage(pu::ui::render::IMGAllFlags);
Expand All @@ -17,5 +18,6 @@ int main() {
g_MainApplication->Prepare();
g_MainApplication->ShowWithFadeIn();

nsExit();
return 0;
}
37 changes: 37 additions & 0 deletions projects/uManager/source/ul/man/ui/ui_MainMenuLayout.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <ul/man/ui/ui_MainApplication.hpp>
#include <ul/menu/menu_Cache.hpp>
#include <ul/acc/acc_Accounts.hpp>
#include <ul/util/util_Json.hpp>
#include <ul/man/man_Manager.hpp>
#include <ul/man/man_Network.hpp>
#include <ul/cfg/cfg_Config.hpp>
#include <ul/os/os_Applications.hpp>
#include <zzip/lib.h>

extern ul::man::ui::MainApplication::Ref g_MainApplication;
Expand Down Expand Up @@ -63,6 +66,16 @@ namespace ul::man::ui {
this->activate_menu_item->AddOnKey(std::bind(&MainMenuLayout::activate_DefaultKey, this));
this->options_menu->AddItem(this->activate_menu_item);

this->reset_menu_menu_item = pu::ui::elm::MenuItem::New(GetLanguageString("reset_menu_item"));
this->reset_menu_menu_item->SetColor(pu::ui::Color(225, 225, 225, 255));
this->reset_menu_menu_item->AddOnKey(std::bind(&MainMenuLayout::resetMenu_DefaultKey, this));
this->options_menu->AddItem(this->reset_menu_menu_item);

this->reset_cache_menu_item = pu::ui::elm::MenuItem::New(GetLanguageString("reset_cache_item"));
this->reset_cache_menu_item->SetColor(pu::ui::Color(225, 225, 225, 255));
this->reset_cache_menu_item->AddOnKey(std::bind(&MainMenuLayout::resetCache_DefaultKey, this));
this->options_menu->AddItem(this->reset_cache_menu_item);

this->update_menu_item = pu::ui::elm::MenuItem::New(GetLanguageString("update_item"));
this->update_menu_item->SetColor(pu::ui::Color(225, 225, 225, 255));
this->update_menu_item->AddOnKey(std::bind(&MainMenuLayout::update_DefaultKey, this));
Expand Down Expand Up @@ -97,6 +110,30 @@ namespace ul::man::ui {
}
}

void MainMenuLayout::resetMenu_DefaultKey() {
const auto option = g_MainApplication->CreateShowDialog(GetLanguageString("reset_menu_title"), GetLanguageString("reset_menu_conf"), { GetLanguageString("yes"), GetLanguageString("cancel") }, true);
if(option == 0) {
fs::DeleteDirectory(MenuPath);

// When returning to uMenu it will automatically regenerate the menu entries

g_MainApplication->ShowNotification(GetLanguageString("reset_menu_success"));
}
}

void MainMenuLayout::resetCache_DefaultKey() {
const auto option = g_MainApplication->CreateShowDialog(GetLanguageString("reset_cache_title"), GetLanguageString("reset_cache_conf"), { GetLanguageString("yes"), GetLanguageString("cancel") }, true);
if(option == 0) {
// Regenerate cache
const auto cur_app_recs = os::ListApplicationRecords();
menu::CacheApplications(cur_app_recs);
menu::CacheHomebrew();
acc::CacheAccounts();

g_MainApplication->ShowNotification(GetLanguageString("reset_cache_success"));
}
}

void MainMenuLayout::update_DefaultKey() {
const auto json_data = man::RetrieveContent("https://api.github.com/repos/XorTroll/uLaunch/releases", "application/json");
if(json_data.empty()) {
Expand Down
6 changes: 6 additions & 0 deletions projects/uMenu/romfs/lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@
"entry_options": "Entry options",
"entry_action": "What would you like to do with the selected entry?",
"entry_move": "Move to/from folder",
"entry_rename": "Rename entry",
"entry_rename_conf": "Would you really like to rename the folder?",
"entry_rename_conf_sub": "New folder name",
"entry_remove": "Remove entry",
"entry_remove_conf": "Would you really like to remove this entry's access from main menu?",
"entry_remove_special": "This is a special homebrew entry and cannot be removed.",
"entry_remove_ok": "The entry was successfully removed.",
"entry_move_parent": "Move to parent folder",
"entry_move_root": "Move to root",
"hbmenu_launch": "Launch hbmenu",
"unknown": "Unknown",
"power_dialog": "Power dialog",
Expand All @@ -51,6 +56,7 @@
"folder_entry_mult": "entries",
"app_launch": "Title launch",
"app_not_launchable": "This application is not launchable (gamecard not inserted, archived, not downloaded, etc.)",
"app_take_over": "Select as homebrew donor title",
"app_no_take_over_title": "There is no title specified for homebrew to take over it.",
"app_take_over_title_select": "Select one by pressing up over it.",
"app_take_over_select": "Would you like to select this title for homebrew launching?",
Expand Down

0 comments on commit 01262e8

Please sign in to comment.