Skip to content

Commit

Permalink
Improved categorization system
Browse files Browse the repository at this point in the history
- Categories are now sorted alphabetically
- New categories can be added through the Manage -> Category menu
- Unrecognized categories listed in db.json are added automatically
  * This may be changed...
- The Windows registry is finally populated properly with new entries
  • Loading branch information
Aemony committed Feb 11, 2024
1 parent 1d6e61f commit a853df9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 18 deletions.
11 changes: 6 additions & 5 deletions include/utility/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ struct SKIF_RegistrySettings {
};

public:
bool hasData (HKEY* hKey = nullptr);
_Tp getData (HKEY* hKey = nullptr);
bool putData (_Tp in)
bool hasData (HKEY* hKey = nullptr);
_Tp getData (HKEY* hKey = nullptr);
bool putDataMultiSZ (std::vector<std::wstring> in);
bool putData (_Tp in)
{
if ( ERROR_SUCCESS == _SetValue (&in) )
return true;
Expand Down Expand Up @@ -122,7 +123,7 @@ struct SKIF_RegistrySettings {
return lStat;
};

LSTATUS _GetValue ( _Tp* pVal, DWORD* pLen = nullptr, HKEY* hKey = nullptr )
LSTATUS _GetValue (_Tp* pVal, DWORD* pLen = nullptr, HKEY* hKey = nullptr)
{
LSTATUS lStat =
RegGetValueW ( (hKey != nullptr) ? *hKey : _desc.hKey,
Expand Down Expand Up @@ -500,7 +501,7 @@ struct SKIF_RegistrySettings {
std::wstring wsPath;
std::wstring wsAutoUpdateVersion; // Holds the version the auto-updater is trying to install

// Multi wide strings
// Multi strings
std::vector <std::string> mszCategories;

// Misc settings
Expand Down
61 changes: 53 additions & 8 deletions src/tabs/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ bool PopulatedGames = false;
char charFilter [MAX_PATH + 2] = { };
char charFilterTmp [MAX_PATH + 2] = { };
bool bFilterActive = false;
bool bNewCategory = false;
bool sort_changed = false;

app_record_s::launch_config_s*
Expand Down Expand Up @@ -201,7 +202,7 @@ Trie labelsFiltered;
static void
SearchAppsList (void)
{
if (bFilterActive)
if (bFilterActive || bNewCategory)
return;

static auto
Expand Down Expand Up @@ -1846,7 +1847,9 @@ DrawGameContextMenu (app_record_s* pApp)
// Category
if (ImGui::BeginMenu ("Category"))
{
ImGui::PushID ("###ManageCategories");
// Prevent the popup from being closed when selecting a category
ImGui::PushItemFlag (ImGuiItemFlags_SelectableDontClosePopup, true);
ImGui::PushID ("###ManageCategories");

bool isFavorite = (pApp->skif.pinned > 0 || (pApp->skif.pinned == -1 && pApp->steam.shared.favorite == 1));

Expand All @@ -1866,7 +1869,7 @@ DrawGameContextMenu (app_record_s* pApp)

for (auto& category : _registry.mszCategories)
{
if (ImGui::MenuItem (category.c_str(), "", (pApp->skif.category == category)))
if (ImGui::MenuItem (category.c_str(), "", (pApp->skif.category == category), ! isFavorite))
{
pApp->skif.category = (pApp->skif.category != category) ? category : "";
UpdateJsonMetaData (pApp, true);
Expand All @@ -1876,21 +1879,51 @@ DrawGameContextMenu (app_record_s* pApp)
}
}

/*
if (! _registry.mszCategories.empty())
ImGui::Separator ( );

if (ImGui::MenuItem ("Uncategorized", "", pApp->skif.category.empty(), ! isFavorite && ! pApp->skif.category.empty()))
constexpr int maxCategoryNameLen = 50;
static char charCategoryName[maxCategoryNameLen] = { };

if (isFavorite)
SKIF_ImGui_PushDisableState ( );

if (ImGui::InputTextEx ("###ManageCategoriesAddNew", "New category...", charCategoryName, 50,
ImVec2 (150.0f * SKIF_ImGui_GlobalDPIScale, 0.0f), ImGuiInputTextFlags_EnterReturnsTrue))
{
pApp->skif.category = "";
std::string szName = charCategoryName;
strncpy (charCategoryName, "\0", maxCategoryNameLen);

if (! szName.empty() && std::find(_registry.mszCategories.begin(), _registry.mszCategories.end(), szName) == _registry.mszCategories.end())
{
_registry.mszCategories.push_back (szName);

std::vector<std::wstring> _in;

for (auto& category : _registry.mszCategories)
_in.push_back (SK_UTF8ToWideChar (category));

_registry.regKVCategories.putDataMultiSZ (_in);
}

pApp->skif.category = szName;
UpdateJsonMetaData (pApp, true);

SKIF_GamingCollection::SortApps (&g_apps);
sort_changed = true;
}
*/

ImGui::PopID ( );
if (isFavorite)
SKIF_ImGui_PopDisableState ( );

bNewCategory = ImGui::IsItemActive ( );

if (bNewCategory)
allowShortcutCtrlA = false;


ImGui::PopID ( );
ImGui::PopItemFlag ( );

ImGui::EndMenu ( );
}
Expand Down Expand Up @@ -4913,6 +4946,18 @@ SKIF_UI_Tab_DrawLibrary (void)
app.second.skif.category = keyGroup;
app.second.skif.pinned = keyPinned;

// For unrecognized categories, add them to SKIF's internal database as well
if (! keyGroup.empty() && std::find(_registry.mszCategories.begin(), _registry.mszCategories.end(), keyGroup) == _registry.mszCategories.end())
{
_registry.mszCategories.push_back (keyGroup);
std::vector<std::wstring> _in;

for (auto& category : _registry.mszCategories)
_in.push_back (SK_UTF8ToWideChar (category));

_registry.regKVCategories.putDataMultiSZ (_in);
}

// Human-readable time format (local time)
time_t ltime = (time_t)strtol(app.second.skif.used.c_str(), NULL, 10);
app.second.skif.used_formatted = SK_WideCharToUTF8 (SKIF_Util_timeGetTimeAsWStr (ltime));
Expand Down
2 changes: 1 addition & 1 deletion src/utility/games.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ SKIF_GamingCollection::SortApps (std::vector <std::pair <std::string, app_record
);

// We need an iterator at the unpinned entries to sort the rest separately
auto it = apps->begin ();
auto it = apps->begin ();
while (it != apps->end ())
{
auto& item = *it;
Expand Down
64 changes: 60 additions & 4 deletions src/utility/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SKIF_RegistrySettings::KeyValue<_Tp>::hasData (HKEY* hKey)
return true;

return false;
};
}

std::vector <std::wstring>
SKIF_RegistrySettings::KeyValue<std::vector <std::wstring>>::getData (HKEY* hKey)
Expand Down Expand Up @@ -115,6 +115,49 @@ SKIF_RegistrySettings::KeyValue<std::wstring>::getData (HKEY* hKey)
return out;
}

bool
SKIF_RegistrySettings::KeyValue<std::vector <std::wstring>>::putDataMultiSZ (std::vector<std::wstring> _in)
{
LSTATUS lStat = STATUS_INVALID_DISPOSITION;
HKEY hKeyToSet = 0;
DWORD dwDisposition = 0;
size_t stDataSize = 0;

lStat =
RegCreateKeyExW (
_desc.hKey,
_desc.wszSubKey,
0x00, nullptr,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, nullptr,
&hKeyToSet, &dwDisposition );

_desc.dwType = REG_MULTI_SZ;

std::wstring wzData;

// Serialize into std::wstring
for (const auto& item : _in)
{
wzData += item + L'\0';
stDataSize += item.length ( ) + 1;
}

wzData += L'\0';
stDataSize++;

lStat =
RegSetKeyValueW ( hKeyToSet,
nullptr,
_desc.wszKeyValue,
_desc.dwType,
(LPBYTE) wzData.data ( ), (DWORD) stDataSize * sizeof(wchar_t));

RegCloseKey (hKeyToSet);

return (ERROR_SUCCESS == lStat);
}

template<class _Tp>
_Tp
SKIF_RegistrySettings::KeyValue<_Tp>::getData (HKEY* hKey)
Expand Down Expand Up @@ -148,7 +191,7 @@ SKIF_RegistrySettings::KeyValue<_Tp>::getData (HKEY* hKey)
_GetValue (&out, &dwOutLen, hKey) ) out = _Tp ();

return out;
};
}

template<class _Tp>
SKIF_RegistrySettings::KeyValue<_Tp>
Expand All @@ -168,7 +211,7 @@ SKIF_RegistrySettings::KeyValue<_Tp>::MakeKeyValue (const wchar_t* wszSubKey, co
kv._desc.dwFlags = dwFlags;

return kv;
};
}


SKIF_RegistrySettings::SKIF_RegistrySettings (void)
Expand Down Expand Up @@ -345,7 +388,20 @@ SKIF_RegistrySettings::SKIF_RegistrySettings (void)
mwzCategories = regKVCategories .getData (&hKey);

for (auto& wzCategory : mwzCategories)
mszCategories.push_back (SK_WideCharToUTF8 (wzCategory));
{
if (! wzCategory.empty() && wzCategory[0] != L'\0')
mszCategories.push_back (SK_WideCharToUTF8 (wzCategory));
}

// Sort categories in alphabetical order
std::stable_sort (mszCategories.begin (),
mszCategories.end (),
[]( const std::string& a,
const std::string& b ) -> int
{
return a.compare(b) < 0;
}
);

bDeveloperMode = regKVDeveloperMode .getData (&hKey);

Expand Down

0 comments on commit a853df9

Please sign in to comment.