fix: tristate toggle logic for tab visibilty (#5530)

Co-authored-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
pajlada
2024-08-24 11:42:42 +02:00
committed by GitHub
parent f36c73019d
commit 5170085d7c
14 changed files with 326 additions and 199 deletions
+91 -37
View File
@@ -8,8 +8,54 @@
#include <QShortcut>
namespace {
using namespace chatterino;
const std::map<HotkeyCategory, HotkeyCategoryData> HOTKEY_CATEGORIES = {
{HotkeyCategory::PopupWindow, {"popupWindow", "Popup Windows"}},
{HotkeyCategory::Split, {"split", "Split"}},
{HotkeyCategory::SplitInput, {"splitInput", "Split input box"}},
{HotkeyCategory::Window, {"window", "Window"}},
};
} // namespace
namespace chatterino {
const std::map<HotkeyCategory, HotkeyCategoryData> &hotkeyCategories()
{
return HOTKEY_CATEGORIES;
}
QString hotkeyCategoryName(HotkeyCategory category)
{
if (!HOTKEY_CATEGORIES.contains(category))
{
qCWarning(chatterinoHotkeys) << "Invalid HotkeyCategory passed to "
"categoryDisplayName function";
return {};
}
const auto &categoryData = HOTKEY_CATEGORIES.at(category);
return categoryData.name;
}
QString hotkeyCategoryDisplayName(HotkeyCategory category)
{
if (!HOTKEY_CATEGORIES.contains(category))
{
qCWarning(chatterinoHotkeys) << "Invalid HotkeyCategory passed to "
"categoryDisplayName function";
return {};
}
const auto &categoryData = HOTKEY_CATEGORIES.at(category);
return categoryData.displayName;
}
static bool hotkeySortCompare_(const std::shared_ptr<Hotkey> &a,
const std::shared_ptr<Hotkey> &b)
{
@@ -25,6 +71,9 @@ HotkeyController::HotkeyController()
: hotkeys_(hotkeySortCompare_)
{
this->loadHotkeys();
this->clearRemovedDefaults();
this->signalHolder_.managedConnect(
this->hotkeys_.delayedItemsChanged, [this]() {
qCDebug(chatterinoHotkeys) << "Reloading hotkeys!";
@@ -130,7 +179,7 @@ int HotkeyController::replaceHotkey(QString oldName,
std::optional<HotkeyCategory> HotkeyController::hotkeyCategoryFromName(
QString categoryName)
{
for (const auto &[category, data] : this->categories())
for (const auto &[category, data] : HOTKEY_CATEGORIES)
{
if (data.name == categoryName)
{
@@ -161,38 +210,9 @@ bool HotkeyController::isDuplicate(std::shared_ptr<Hotkey> hotkey,
return false;
}
QString HotkeyController::categoryDisplayName(HotkeyCategory category) const
const std::set<QString> &HotkeyController::removedOrDeprecatedHotkeys() const
{
if (this->hotkeyCategories_.count(category) == 0)
{
qCWarning(chatterinoHotkeys) << "Invalid HotkeyCategory passed to "
"categoryDisplayName function";
return QString();
}
const auto &categoryData = this->hotkeyCategories_.at(category);
return categoryData.displayName;
}
QString HotkeyController::categoryName(HotkeyCategory category) const
{
if (this->hotkeyCategories_.count(category) == 0)
{
qCWarning(chatterinoHotkeys) << "Invalid HotkeyCategory passed to "
"categoryName function";
return QString();
}
const auto &categoryData = this->hotkeyCategories_.at(category);
return categoryData.name;
}
const std::map<HotkeyCategory, HotkeyCategoryData> &
HotkeyController::categories() const
{
return this->hotkeyCategories_;
return this->removedOrDeprecatedHotkeys_;
}
void HotkeyController::loadHotkeys()
@@ -280,7 +300,7 @@ void HotkeyController::saveHotkeys()
pajlada::Settings::Setting<QString>::set(
section + "/keySequence", hotkey->keySequence().toString());
auto categoryName = this->categoryName(hotkey->category());
auto categoryName = hotkeyCategoryName(hotkey->category());
pajlada::Settings::Setting<QString>::set(section + "/category",
categoryName);
pajlada::Settings::Setting<std::vector<QString>>::set(
@@ -500,10 +520,6 @@ void HotkeyController::addDefaults(std::set<QString> &addedHotkeys)
this->tryAddDefault(addedHotkeys, HotkeyCategory::Window,
QKeySequence("Ctrl+U"), "setTabVisibility",
{"toggle"}, "toggle tab visibility");
this->tryAddDefault(addedHotkeys, HotkeyCategory::Window,
QKeySequence("Ctrl+Shift+L"), "setTabVisibility",
{"toggleLiveOnly"}, "toggle live tabs only");
}
}
@@ -524,6 +540,17 @@ void HotkeyController::resetToDefaults()
this->loadHotkeys();
}
void HotkeyController::clearRemovedDefaults()
{
// The "toggleLiveOnly" argument was removed 2024-08-04
this->tryRemoveDefault(HotkeyCategory::Window, QKeySequence("Ctrl+Shift+L"),
"setTabVisibility", {"toggleLiveOnly"},
"toggle live tabs only");
this->warnForRemovedHotkeyActions(HotkeyCategory::Window,
"setTabVisibility", {"toggleLiveOnly"});
}
void HotkeyController::tryAddDefault(std::set<QString> &addedHotkeys,
HotkeyCategory category,
QKeySequence keySequence, QString action,
@@ -541,6 +568,33 @@ void HotkeyController::tryAddDefault(std::set<QString> &addedHotkeys,
addedHotkeys.insert(name);
}
bool HotkeyController::tryRemoveDefault(HotkeyCategory category,
QKeySequence keySequence,
QString action,
std::vector<QString> args, QString name)
{
return this->hotkeys_.removeFirstMatching([&](const auto &hotkey) {
return hotkey->category() == category &&
hotkey->keySequence() == keySequence &&
hotkey->action() == action && hotkey->arguments() == args &&
hotkey->name() == name;
});
}
void HotkeyController::warnForRemovedHotkeyActions(HotkeyCategory category,
QString action,
std::vector<QString> args)
{
for (const auto &hotkey : this->hotkeys_)
{
if (hotkey->category() == category && hotkey->action() == action &&
hotkey->arguments() == args)
{
this->removedOrDeprecatedHotkeys_.insert(hotkey->name());
}
}
}
void HotkeyController::showHotkeyError(const std::shared_ptr<Hotkey> &hotkey,
QString warning)
{