mirror-chatterino2/src/widgets/Window.cpp

687 lines
22 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "widgets/Window.hpp"
2018-06-26 14:09:39 +02:00
#include "Application.hpp"
2019-09-13 19:26:52 +02:00
#include "common/Credentials.hpp"
2019-09-15 15:45:04 +02:00
#include "common/Modes.hpp"
#include "common/QLogging.hpp"
2018-06-26 15:33:51 +02:00
#include "common/Version.hpp"
2018-06-26 14:09:39 +02:00
#include "controllers/accounts/AccountController.hpp"
#include "controllers/hotkeys/HotkeyController.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
2018-07-05 11:42:40 +02:00
#include "singletons/Updates.hpp"
2018-06-26 14:09:39 +02:00
#include "singletons/WindowManager.hpp"
#include "util/InitUpdateButton.hpp"
#include "widgets/AccountSwitchPopup.hpp"
2018-06-26 15:11:45 +02:00
#include "widgets/dialogs/SettingsDialog.hpp"
#include "widgets/dialogs/switcher/QuickSwitcherPopup.hpp"
#include "widgets/dialogs/UpdateDialog.hpp"
2018-06-26 15:11:45 +02:00
#include "widgets/dialogs/WelcomeDialog.hpp"
#include "widgets/helper/EffectLabel.hpp"
2018-10-09 19:43:29 +02:00
#include "widgets/helper/NotebookTab.hpp"
#include "widgets/helper/TitlebarButton.hpp"
#include "widgets/Notebook.hpp"
2018-10-09 19:43:29 +02:00
#include "widgets/splits/ClosedSplits.hpp"
2018-06-26 15:33:51 +02:00
#include "widgets/splits/Split.hpp"
#include "widgets/splits/SplitContainer.hpp"
2016-12-29 17:31:07 +01:00
#ifndef NDEBUG
# include "providers/twitch/PubSubManager.hpp"
# include "providers/twitch/PubSubMessages.hpp"
# include "util/SampleData.hpp"
# include <rapidjson/document.h>
2019-09-08 19:40:30 +02:00
#endif
#include <QApplication>
2018-06-23 22:49:07 +02:00
#include <QDesktopServices>
#include <QHeaderView>
#include <QMenuBar>
2017-01-18 04:52:47 +01:00
#include <QPalette>
#include <QStandardItemModel>
#include <QVBoxLayout>
2017-04-14 17:52:22 +02:00
namespace chatterino {
2017-01-18 21:30:23 +01:00
Window::Window(WindowType type, QWidget *parent)
: BaseWindow(BaseWindow::EnableCustomFrame, parent)
2018-07-06 17:02:26 +02:00
, type_(type)
, notebook_(new SplitNotebook(this))
2016-12-29 17:31:07 +01:00
{
2018-07-06 17:02:26 +02:00
this->addCustomTitlebarButtons();
this->addShortcuts();
this->addLayout();
#ifdef Q_OS_MACOS
this->addMenuBar();
#endif
this->bSignals_.emplace_back(
getApp()->accounts->twitch.currentUserChanged.connect([this] {
this->onAccountSelected();
}));
2018-07-06 17:02:26 +02:00
this->onAccountSelected();
2018-05-25 18:23:13 +02:00
2018-10-21 13:43:02 +02:00
if (type == WindowType::Main)
{
2018-11-21 21:37:41 +01:00
this->resize(int(600 * this->scale()), int(500 * this->scale()));
2018-10-21 13:43:02 +02:00
}
else
{
2018-11-21 21:37:41 +01:00
this->resize(int(300 * this->scale()), int(500 * this->scale()));
2018-07-06 17:02:26 +02:00
}
this->signalHolder_.managedConnect(getApp()->hotkeys->onItemsUpdated,
[this]() {
this->clearShortcuts();
this->addShortcuts();
});
if (type == WindowType::Main || type == WindowType::Popup)
{
getSettings()->tabDirection.connect([this](int val) {
this->notebook_->setTabLocation(NotebookTabLocation(val));
});
}
2018-07-06 17:02:26 +02:00
}
WindowType Window::getType()
2018-07-06 17:02:26 +02:00
{
return this->type_;
}
SplitNotebook &Window::getNotebook()
{
return *this->notebook_;
2018-07-06 17:02:26 +02:00
}
bool Window::event(QEvent *event)
{
2018-10-21 13:43:02 +02:00
switch (event->type())
{
2018-07-06 17:02:26 +02:00
case QEvent::WindowActivate:
break;
2019-09-26 00:51:05 +02:00
case QEvent::WindowDeactivate: {
auto page = this->notebook_->getOrAddSelectedPage();
2018-07-06 17:02:26 +02:00
2018-10-21 13:43:02 +02:00
if (page != nullptr)
{
2018-07-06 17:02:26 +02:00
std::vector<Split *> splits = page->getSplits();
2018-10-21 13:43:02 +02:00
for (Split *split : splits)
{
2018-07-06 17:02:26 +02:00
split->updateLastReadMessage();
}
}
2018-05-25 18:23:13 +02:00
2018-08-06 21:17:03 +02:00
if (SplitContainer *container =
2018-10-21 13:43:02 +02:00
dynamic_cast<SplitContainer *>(page))
{
2018-07-06 17:02:26 +02:00
container->hideResizeHandles();
}
2018-10-21 13:43:02 +02:00
}
break;
2018-07-06 17:02:26 +02:00
default:;
2019-09-13 19:26:52 +02:00
}
2018-07-06 17:02:26 +02:00
return BaseWindow::event(event);
}
void Window::closeEvent(QCloseEvent *)
{
2018-10-21 13:43:02 +02:00
if (this->type_ == WindowType::Main)
{
2018-07-06 17:02:26 +02:00
auto app = getApp();
app->windows->save();
app->windows->closeAll();
}
2018-07-06 17:02:26 +02:00
this->closed.invoke();
2018-10-21 13:43:02 +02:00
if (this->type_ == WindowType::Main)
{
2018-07-06 17:02:26 +02:00
QApplication::exit();
}
2018-07-06 17:02:26 +02:00
}
2018-07-06 17:02:26 +02:00
void Window::addLayout()
{
2017-04-12 17:46:44 +02:00
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(this->notebook_);
this->getLayoutContainer()->setLayout(layout);
2017-04-12 17:46:44 +02:00
// set margin
layout->setContentsMargins(0, 0, 0, 0);
2016-12-30 12:20:26 +01:00
this->notebook_->setAllowUserTabManagement(true);
this->notebook_->setShowAddButton(true);
2018-07-06 17:02:26 +02:00
}
2018-07-06 17:02:26 +02:00
void Window::addCustomTitlebarButtons()
{
2018-10-21 13:43:02 +02:00
if (!this->hasCustomWindowFrame())
return;
if (this->type_ != WindowType::Main)
return;
2018-07-06 17:02:26 +02:00
// settings
this->addTitleBarButton(TitleBarButtonStyle::Settings, [this] {
getApp()->windows->showSettingsDialog(this);
});
2018-06-11 15:04:54 +02:00
2018-07-06 17:02:26 +02:00
// updates
auto update = this->addTitleBarButton(TitleBarButtonStyle::None, [] {});
initUpdateButton(*update, this->signalHolder_);
2018-07-06 17:02:26 +02:00
// account
this->userLabel_ = this->addTitleBarLabel([this] {
2018-08-06 21:17:03 +02:00
getApp()->windows->showAccountSelectPopup(this->userLabel_->mapToGlobal(
this->userLabel_->rect().bottomLeft()));
2018-07-06 17:02:26 +02:00
});
2018-11-21 21:37:41 +01:00
this->userLabel_->setMinimumWidth(20 * scale());
2018-07-06 17:02:26 +02:00
}
void Window::addDebugStuff(HotkeyController::HotkeyMap &actions)
2018-07-06 17:02:26 +02:00
{
#ifndef NDEBUG
actions.emplace("addMiscMessage", [=](std::vector<QString>) -> QString {
const auto &messages = getSampleMiscMessages();
static int index = 0;
const auto &msg = messages[index++ % messages.size()];
getApp()->twitch->addFakeMessage(msg);
return "";
});
2018-06-07 17:43:21 +02:00
actions.emplace("addCheerMessage", [=](std::vector<QString>) -> QString {
const auto &messages = getSampleCheerMessages();
static int index = 0;
const auto &msg = messages[index++ % messages.size()];
getApp()->twitch->addFakeMessage(msg);
return "";
});
2019-09-08 19:25:42 +02:00
actions.emplace("addLinkMessage", [=](std::vector<QString>) -> QString {
const auto &messages = getSampleLinkMessages();
2019-12-29 15:45:52 +01:00
static int index = 0;
const auto &msg = messages[index++ % messages.size()];
getApp()->twitch->addFakeMessage(msg);
return "";
2019-12-29 15:45:52 +01:00
});
actions.emplace("addRewardMessage", [=](std::vector<QString>) -> QString {
rapidjson::Document doc;
auto app = getApp();
static bool alt = true;
if (alt)
{
auto oMessage =
parsePubSubBaseMessage(getSampleChannelRewardMessage());
auto oInnerMessage =
oMessage->toInner<PubSubMessageMessage>()
->toInner<PubSubCommunityPointsChannelV1Message>();
app->twitch->addFakeMessage(getSampleChannelRewardIRCMessage());
app->twitch->pubsub->signals_.pointReward.redeemed.invoke(
oInnerMessage->data.value("redemption").toObject());
alt = !alt;
}
else
{
auto oMessage =
parsePubSubBaseMessage(getSampleChannelRewardMessage2());
auto oInnerMessage =
oMessage->toInner<PubSubMessageMessage>()
->toInner<PubSubCommunityPointsChannelV1Message>();
app->twitch->pubsub->signals_.pointReward.redeemed.invoke(
oInnerMessage->data.value("redemption").toObject());
alt = !alt;
}
return "";
2018-06-07 17:43:21 +02:00
});
2019-09-08 19:25:42 +02:00
actions.emplace("addEmoteMessage", [=](std::vector<QString>) -> QString {
const auto &messages = getSampleEmoteTestMessages();
static int index = 0;
const auto &msg = messages[index++ % messages.size()];
getApp()->twitch->addFakeMessage(msg);
return "";
});
actions.emplace("addSubMessage", [=](std::vector<QString>) -> QString {
const auto &messages = getSampleSubMessages();
static int index = 0;
const auto &msg = messages[index++ % messages.size()];
getApp()->twitch->addFakeMessage(msg);
return "";
});
#endif
}
2016-12-29 17:31:07 +01:00
2018-07-06 17:02:26 +02:00
void Window::addShortcuts()
2018-07-05 11:42:40 +02:00
{
HotkeyController::HotkeyMap actions{
{"openSettings", // Open settings
[this](std::vector<QString>) -> QString {
SettingsDialog::showDialog(this);
return "";
}},
{"newSplit", // Create a new split
[this](std::vector<QString>) -> QString {
this->notebook_->getOrAddSelectedPage()->appendNewSplit(true);
return "";
}},
{"openTab", // CTRL + 1-8 to open corresponding tab.
[this](std::vector<QString> arguments) -> QString {
if (arguments.size() == 0)
{
qCWarning(chatterinoHotkeys)
<< "openTab shortcut called without arguments. "
"Takes only "
"one argument: tab specifier";
return "openTab shortcut called without arguments. "
"Takes only "
"one argument: tab specifier";
}
auto target = arguments.at(0);
if (target == "last")
{
this->notebook_->selectLastTab();
}
else if (target == "next")
{
this->notebook_->selectNextTab();
}
else if (target == "previous")
{
this->notebook_->selectPreviousTab();
}
else
{
bool ok;
int result = target.toInt(&ok);
if (ok)
{
this->notebook_->selectIndex(result);
}
else
{
qCWarning(chatterinoHotkeys)
<< "Invalid argument for openTab shortcut";
return QString("Invalid argument for openTab "
"shortcut: \"%1\". Use \"last\", "
"\"next\", \"previous\" or an integer.")
.arg(target);
}
}
return "";
}},
{"popup",
[this](std::vector<QString> arguments) -> QString {
if (arguments.size() == 0)
{
return "popup action called without arguments. Takes only "
"one: \"split\" or \"window\".";
}
if (arguments.at(0) == "split")
{
if (auto page = dynamic_cast<SplitContainer *>(
this->notebook_->getSelectedPage()))
{
if (auto split = page->getSelectedSplit())
{
split->popup();
}
}
}
else if (arguments.at(0) == "window")
{
if (auto page = dynamic_cast<SplitContainer *>(
this->notebook_->getSelectedPage()))
{
page->popup();
}
}
else
{
return "Invalid popup target. Use \"split\" or \"window\".";
}
return "";
}},
{"zoom",
[](std::vector<QString> arguments) -> QString {
if (arguments.size() == 0)
{
qCWarning(chatterinoHotkeys)
<< "zoom shortcut called without arguments. Takes "
"only "
"one argument: \"in\", \"out\", or \"reset\"";
return "zoom shortcut called without arguments. Takes "
"only "
"one argument: \"in\", \"out\", or \"reset\"";
}
auto change = 0.0f;
auto direction = arguments.at(0);
if (direction == "reset")
{
getSettings()->uiScale.setValue(1);
return "";
}
if (direction == "in")
{
change = 0.1f;
}
else if (direction == "out")
{
change = -0.1f;
}
else
{
qCWarning(chatterinoHotkeys)
<< "Invalid zoom direction, use \"in\", \"out\", or "
"\"reset\"";
return "Invalid zoom direction, use \"in\", \"out\", or "
"\"reset\"";
}
getSettings()->setClampedUiScale(
getSettings()->getClampedUiScale() + change);
return "";
}},
{"newTab",
[this](std::vector<QString>) -> QString {
this->notebook_->addPage(true);
return "";
}},
{"removeTab",
[this](std::vector<QString>) -> QString {
this->notebook_->removeCurrentPage();
return "";
}},
{"reopenSplit",
[this](std::vector<QString>) -> QString {
if (ClosedSplits::empty())
{
return "";
}
ClosedSplits::SplitInfo si = ClosedSplits::pop();
SplitContainer *splitContainer{nullptr};
if (si.tab)
{
splitContainer = dynamic_cast<SplitContainer *>(si.tab->page);
}
if (!splitContainer)
{
splitContainer = this->notebook_->getOrAddSelectedPage();
}
Split *split = new Split(splitContainer);
split->setChannel(
getApp()->twitch->getOrAddChannel(si.channelName));
split->setFilters(si.filters);
splitContainer->appendSplit(split);
splitContainer->setSelected(split);
this->notebook_->select(splitContainer);
return "";
}},
{"toggleLocalR9K",
[](std::vector<QString>) -> QString {
getSettings()->hideSimilar.setValue(!getSettings()->hideSimilar);
getApp()->windows->forceLayoutChannelViews();
return "";
}},
{"openQuickSwitcher",
[](std::vector<QString>) -> QString {
auto quickSwitcher =
new QuickSwitcherPopup(&getApp()->windows->getMainWindow());
quickSwitcher->show();
return "";
}},
{"quit",
[](std::vector<QString>) -> QString {
QApplication::exit();
return "";
}},
{"moveTab",
[this](std::vector<QString> arguments) -> QString {
if (arguments.size() == 0)
{
qCWarning(chatterinoHotkeys)
<< "moveTab shortcut called without arguments. "
"Takes only one argument: new index (number, "
"\"next\" "
"or \"previous\")";
return "moveTab shortcut called without arguments. "
"Takes only one argument: new index (number, "
"\"next\" "
"or \"previous\")";
}
int newIndex = -1;
bool indexIsGenerated =
false; // indicates if `newIndex` was generated using target="next" or target="previous"
auto target = arguments.at(0);
qCDebug(chatterinoHotkeys) << target;
if (target == "next")
{
newIndex = this->notebook_->getSelectedIndex() + 1;
indexIsGenerated = true;
}
else if (target == "previous")
{
newIndex = this->notebook_->getSelectedIndex() - 1;
indexIsGenerated = true;
}
else
{
bool ok;
int result = target.toInt(&ok);
if (!ok)
{
qCWarning(chatterinoHotkeys)
<< "Invalid argument for moveTab shortcut";
return QString("Invalid argument for moveTab shortcut: "
"%1. Use \"next\" or \"previous\" or an "
"integer.")
.arg(target);
}
newIndex = result;
}
if (newIndex >= this->notebook_->getPageCount() || 0 > newIndex)
{
if (indexIsGenerated)
{
return ""; // don't error out on generated indexes, ie move tab right
}
qCWarning(chatterinoHotkeys)
<< "Invalid index for moveTab shortcut:" << newIndex;
return QString("Invalid index for moveTab shortcut: %1.")
.arg(newIndex);
}
this->notebook_->rearrangePage(this->notebook_->getSelectedPage(),
newIndex);
return "";
}},
{"setStreamerMode",
[](std::vector<QString> arguments) -> QString {
auto mode = 2;
if (arguments.size() != 0)
{
auto arg = arguments.at(0);
if (arg == "off")
{
mode = 0;
}
else if (arg == "on")
{
mode = 1;
}
else if (arg == "toggle")
{
mode = 2;
}
else if (arg == "auto")
{
mode = 3;
}
else
{
qCWarning(chatterinoHotkeys)
<< "Invalid argument for setStreamerMode hotkey: "
<< arg;
return QString("Invalid argument for setStreamerMode "
"hotkey: %1. Use \"on\", \"off\", "
"\"toggle\" or \"auto\".")
.arg(arg);
}
}
if (mode == 0)
{
getSettings()->enableStreamerMode.setValue(
StreamerModeSetting::Disabled);
}
else if (mode == 1)
{
getSettings()->enableStreamerMode.setValue(
StreamerModeSetting::Enabled);
}
else if (mode == 2)
{
if (isInStreamerMode())
{
getSettings()->enableStreamerMode.setValue(
StreamerModeSetting::Disabled);
}
else
{
getSettings()->enableStreamerMode.setValue(
StreamerModeSetting::Enabled);
}
}
else if (mode == 3)
{
getSettings()->enableStreamerMode.setValue(
StreamerModeSetting::DetectStreamingSoftware);
}
return "";
}},
{"setTabVisibility",
[this](std::vector<QString> arguments) -> QString {
auto mode = 2;
if (arguments.size() != 0)
{
auto arg = arguments.at(0);
if (arg == "off")
{
mode = 0;
}
else if (arg == "on")
{
mode = 1;
}
else if (arg == "toggle")
{
mode = 2;
}
else
{
qCWarning(chatterinoHotkeys)
<< "Invalid argument for setStreamerMode hotkey: "
<< arg;
return QString("Invalid argument for setTabVisibility "
"hotkey: %1. Use \"on\", \"off\" or "
"\"toggle\".")
.arg(arg);
}
}
if (mode == 0)
{
this->notebook_->setShowTabs(false);
}
else if (mode == 1)
{
this->notebook_->setShowTabs(true);
}
else if (mode == 2)
{
this->notebook_->setShowTabs(!this->notebook_->getShowTabs());
}
return "";
}},
};
this->addDebugStuff(actions);
this->shortcuts_ = getApp()->hotkeys->shortcutsForCategory(
HotkeyCategory::Window, actions, this);
}
void Window::addMenuBar()
{
QMenuBar *mainMenu = new QMenuBar();
mainMenu->setNativeMenuBar(true);
// First menu.
QMenu *menu = mainMenu->addMenu(QString());
QAction *prefs = menu->addAction(QString());
prefs->setMenuRole(QAction::PreferencesRole);
connect(prefs, &QAction::triggered, this, [this] {
SettingsDialog::showDialog(this);
});
// Window menu.
QMenu *windowMenu = mainMenu->addMenu(QString("Window"));
QAction *nextTab = windowMenu->addAction(QString("Select next tab"));
nextTab->setShortcuts({QKeySequence("Meta+Tab")});
connect(nextTab, &QAction::triggered, this, [=] {
this->notebook_->selectNextTab();
});
QAction *prevTab = windowMenu->addAction(QString("Select previous tab"));
prevTab->setShortcuts({QKeySequence("Meta+Shift+Tab")});
connect(prevTab, &QAction::triggered, this, [=] {
this->notebook_->selectPreviousTab();
});
}
2018-07-06 17:02:26 +02:00
void Window::onAccountSelected()
2018-01-23 22:48:33 +01:00
{
2018-07-06 17:02:26 +02:00
auto user = getApp()->accounts->twitch.getCurrent();
// update title (also append username on Linux and MacOS)
QString windowTitle = Version::instance().fullVersion();
2018-06-21 22:02:35 +02:00
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
2018-10-21 13:43:02 +02:00
if (user->isAnon())
{
windowTitle += " - not logged in";
2018-10-21 13:43:02 +02:00
}
else
{
windowTitle += " - " + user->getUserName();
}
#endif
this->setWindowTitle(windowTitle);
// update user
if (this->userLabel_)
{
if (user->isAnon())
{
this->userLabel_->getLabel().setText("anonymous");
}
else
2018-10-21 13:43:02 +02:00
{
2018-07-06 17:02:26 +02:00
this->userLabel_->getLabel().setText(user->getUserName());
}
}
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino