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
@@ -459,6 +459,8 @@ CommandController::CommandController(const Paths &paths)
this->registerCommand("/debug-force-image-unload",
&commands::forceImageUnload);
this->registerCommand("/debug-test", &commands::debugTest);
this->registerCommand("/shield", &commands::shieldModeOn);
this->registerCommand("/shieldoff", &commands::shieldModeOff);
@@ -132,4 +132,16 @@ QString forceImageUnload(const CommandContext &ctx)
return "";
}
QString debugTest(const CommandContext &ctx)
{
if (!ctx.channel)
{
return "";
}
ctx.channel->addSystemMessage("debug-test called");
return "";
}
} // namespace chatterino::commands
@@ -22,4 +22,6 @@ QString forceImageGarbageCollection(const CommandContext &ctx);
QString forceImageUnload(const CommandContext &ctx);
QString debugTest(const CommandContext &ctx);
} // namespace chatterino::commands
+8 -7
View File
@@ -326,15 +326,16 @@ inline const std::map<HotkeyCategory, ActionDefinitionMap> actionNames{
{"setTabVisibility",
ActionDefinition{
.displayName = "Set tab visibility",
.argumentDescription = "[on, off, toggle, liveOnly, or "
"toggleLiveOnly. default: toggle]",
.argumentDescription =
"[on, off, toggle, or liveOnly. default: toggle]",
.minCountArguments = 0,
.maxCountArguments = 1,
.possibleArguments{{"Toggle", {}},
{"Set to on", {"on"}},
{"Set to off", {"off"}},
{"Live only on", {"liveOnly"}},
{"Live only toggle", {"toggleLiveOnly"}}},
.possibleArguments{
{"Toggle", {}},
{"Show all tabs", {"on"}},
{"Hide all tabs", {"off"}},
{"Only show live tabs", {"liveOnly"}},
},
.argumentsPrompt = "New value:",
.argumentsPromptHover = "Should the tabs be enabled, disabled, "
"toggled, or live-only.",
+1 -2
View File
@@ -1,6 +1,5 @@
#include "controllers/hotkeys/Hotkey.hpp"
#include "Application.hpp"
#include "common/QLogging.hpp"
#include "controllers/hotkeys/ActionNames.hpp"
#include "controllers/hotkeys/HotkeyController.hpp"
@@ -58,7 +57,7 @@ std::vector<QString> Hotkey::arguments() const
QString Hotkey::getCategory() const
{
return getApp()->getHotkeys()->categoryDisplayName(this->category_);
return hotkeyCategoryDisplayName(this->category_);
}
Qt::ShortcutContext Hotkey::getContext() const
+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)
{
+52 -27
View File
@@ -17,6 +17,26 @@ class Hotkey;
class HotkeyModel;
/**
* @returns a const map with the HotkeyCategory enum as its key, and HotkeyCategoryData as the value.
**/
[[nodiscard]] const std::map<HotkeyCategory, HotkeyCategoryData> &
hotkeyCategories();
/**
* @brief Returns the name of the given hotkey category
*
* @returns the name, or an empty string if an invalid hotkey category was given
**/
[[nodiscard]] QString hotkeyCategoryName(HotkeyCategory category);
/**
* @brief Returns the display name of the given hotkey category
*
* @returns the display name, or an empty string if an invalid hotkey category was given
**/
[[nodiscard]] QString hotkeyCategoryDisplayName(HotkeyCategory category);
class HotkeyController final
{
public:
@@ -63,28 +83,21 @@ public:
[[nodiscard]] bool isDuplicate(std::shared_ptr<Hotkey> hotkey,
QString ignoreNamed);
/**
* @brief Returns the display name of the given hotkey category
*
* @returns the display name, or an empty string if an invalid hotkey category was given
**/
[[nodiscard]] QString categoryDisplayName(HotkeyCategory category) const;
/**
* @brief Returns the name of the given hotkey category
*
* @returns the name, or an empty string if an invalid hotkey category was given
**/
[[nodiscard]] QString categoryName(HotkeyCategory category) const;
/**
* @returns a const map with the HotkeyCategory enum as its key, and HotkeyCategoryData as the value.
**/
[[nodiscard]] const std::map<HotkeyCategory, HotkeyCategoryData> &
categories() const;
pajlada::Signals::NoArgSignal onItemsUpdated;
/**
* @brief Removes hotkeys that were previously added as default hotkeys.
*
* This will potentially remove hotkeys that were explicitly added by the user if they added a hotkey
* with the exact same parameters as the default hotkey.
*/
void clearRemovedDefaults();
/// Returns the names of removed or deprecated hotkeys the user had at launch, if any
///
/// This is used to populate the on-launch warning in the hotkey dialog
const std::set<QString> &removedOrDeprecatedHotkeys() const;
private:
/**
* @brief load hotkeys from under the /hotkeys settings path
@@ -118,6 +131,22 @@ private:
QKeySequence keySequence, QString action,
std::vector<QString> args, QString name);
/**
* @brief try to remove a default hotkey if it hasn't already been modified by the user
*
* NOTE: This could also remove a user-added hotkey assuming it matches all parameters
*
* @returns true if the hotkey was removed
**/
bool tryRemoveDefault(HotkeyCategory category, QKeySequence keySequence,
QString action, std::vector<QString> args,
QString name);
/// Add hotkeys matching the given arguments to list of removed/deprecated hotkeys
/// that the user should remove
void warnForRemovedHotkeyActions(HotkeyCategory category, QString action,
std::vector<QString> args);
/**
* @brief show an error dialog about a hotkey in a standard format
**/
@@ -137,15 +166,11 @@ private:
friend class KeyboardSettingsPage;
/// Stores a list of names the user had at launch that contained deprecated or removed hotkey actions
std::set<QString> removedOrDeprecatedHotkeys_;
SignalVector<std::shared_ptr<Hotkey>> hotkeys_;
pajlada::Signals::SignalHolder signalHolder_;
const std::map<HotkeyCategory, HotkeyCategoryData> hotkeyCategories_ = {
{HotkeyCategory::PopupWindow, {"popupWindow", "Popup Windows"}},
{HotkeyCategory::Split, {"split", "Split"}},
{HotkeyCategory::SplitInput, {"splitInput", "Split input box"}},
{HotkeyCategory::Window, {"window", "Window"}},
};
};
} // namespace chatterino