Add opening tab in popup (#3082)

Co-authored-by: zneix <zneix@zneix.eu>
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Tal Neoran 2021-08-21 15:16:00 +03:00 committed by GitHub
parent 773c4bb9e7
commit ad4a0c28d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 167 additions and 102 deletions

View file

@ -9,6 +9,7 @@
- Minor: Added a setting to hide similar messages by any user. (#2716) - Minor: Added a setting to hide similar messages by any user. (#2716)
- Minor: Duplicate spaces now count towards the display message length. (#3002) - Minor: Duplicate spaces now count towards the display message length. (#3002)
- Minor: Commands are now backed up. (#3168) - Minor: Commands are now backed up. (#3168)
- Minor: Added the ability to open an entire tab as a popup. (#3082)
- Minor: Added optional parameter to /usercard command for opening a usercard in a different channel context. (#3172) - Minor: Added optional parameter to /usercard command for opening a usercard in a different channel context. (#3172)
- Bugfix: Fixed colored usernames sometimes not working. (#3170) - Bugfix: Fixed colored usernames sometimes not working. (#3170)
- Bugfix: Restored ability to send duplicate `/me` messages. (#3166) - Bugfix: Restored ability to send duplicate `/me` messages. (#3166)

View file

@ -2,6 +2,8 @@
#include "common/Modes.hpp" #include "common/Modes.hpp"
#include <QFileInfo>
#define UGLYMACROHACK1(s) #s #define UGLYMACROHACK1(s) #s
#define FROM_EXTERNAL_DEFINE(s) UGLYMACROHACK1(s) #define FROM_EXTERNAL_DEFINE(s) UGLYMACROHACK1(s)

View file

@ -110,6 +110,42 @@ void SplitDescriptor::loadFromJSON(SplitDescriptor &descriptor,
descriptor.filters_ = loadFilters(root.value("filters")); descriptor.filters_ = loadFilters(root.value("filters"));
} }
TabDescriptor TabDescriptor::loadFromJSON(const QJsonObject &tabObj)
{
TabDescriptor tab;
// Load tab custom title
QJsonValue titleVal = tabObj.value("title");
if (titleVal.isString())
{
tab.customTitle_ = titleVal.toString();
}
// Load tab selected state
tab.selected_ = tabObj.value("selected").toBool(false);
// Load tab "highlightsEnabled" state
tab.highlightsEnabled_ = tabObj.value("highlightsEnabled").toBool(true);
QJsonObject splitRoot = tabObj.value("splits2").toObject();
// Load tab splits
if (!splitRoot.isEmpty())
{
// root type
auto nodeType = splitRoot.value("type").toString();
if (nodeType == "split")
{
tab.rootNode_ = loadNodes<SplitNodeDescriptor>(splitRoot);
}
else if (nodeType == "horizontal" || nodeType == "vertical")
{
tab.rootNode_ = loadNodes<ContainerNodeDescriptor>(splitRoot);
}
}
return tab;
}
WindowLayout WindowLayout::loadFromFile(const QString &path) WindowLayout WindowLayout::loadFromFile(const QString &path)
{ {
WindowLayout layout; WindowLayout layout;
@ -117,15 +153,15 @@ WindowLayout WindowLayout::loadFromFile(const QString &path)
bool hasSetAMainWindow = false; bool hasSetAMainWindow = false;
// "deserialize" // "deserialize"
for (const QJsonValue &window_val : loadWindowArray(path)) for (const QJsonValue &windowVal : loadWindowArray(path))
{ {
QJsonObject window_obj = window_val.toObject(); QJsonObject windowObj = windowVal.toObject();
WindowDescriptor window; WindowDescriptor window;
// Load window type // Load window type
QString type_val = window_obj.value("type").toString(); QString typeVal = windowObj.value("type").toString();
auto type = type_val == "main" ? WindowType::Main : WindowType::Popup; auto type = typeVal == "main" ? WindowType::Main : WindowType::Popup;
if (type == WindowType::Main) if (type == WindowType::Main)
{ {
@ -142,21 +178,21 @@ WindowLayout WindowLayout::loadFromFile(const QString &path)
window.type_ = type; window.type_ = type;
// Load window state // Load window state
if (window_obj.value("state") == "minimized") if (windowObj.value("state") == "minimized")
{ {
window.state_ = WindowDescriptor::State::Minimized; window.state_ = WindowDescriptor::State::Minimized;
} }
else if (window_obj.value("state") == "maximized") else if (windowObj.value("state") == "maximized")
{ {
window.state_ = WindowDescriptor::State::Maximized; window.state_ = WindowDescriptor::State::Maximized;
} }
// Load window geometry // Load window geometry
{ {
int x = window_obj.value("x").toInt(-1); int x = windowObj.value("x").toInt(-1);
int y = window_obj.value("y").toInt(-1); int y = windowObj.value("y").toInt(-1);
int width = window_obj.value("width").toInt(-1); int width = windowObj.value("width").toInt(-1);
int height = window_obj.value("height").toInt(-1); int height = windowObj.value("height").toInt(-1);
window.geometry_ = QRect(x, y, width, height); window.geometry_ = QRect(x, y, width, height);
} }
@ -164,23 +200,10 @@ WindowLayout WindowLayout::loadFromFile(const QString &path)
bool hasSetASelectedTab = false; bool hasSetASelectedTab = false;
// Load window tabs // Load window tabs
QJsonArray tabs = window_obj.value("tabs").toArray(); QJsonArray tabs = windowObj.value("tabs").toArray();
for (QJsonValue tab_val : tabs) for (QJsonValue tabVal : tabs)
{ {
TabDescriptor tab; TabDescriptor tab = TabDescriptor::loadFromJSON(tabVal.toObject());
QJsonObject tab_obj = tab_val.toObject();
// Load tab custom title
QJsonValue title_val = tab_obj.value("title");
if (title_val.isString())
{
tab.customTitle_ = title_val.toString();
}
// Load tab selected state
tab.selected_ = tab_obj.value("selected").toBool(false);
if (tab.selected_) if (tab.selected_)
{ {
if (hasSetASelectedTab) if (hasSetASelectedTab)
@ -192,34 +215,11 @@ WindowLayout WindowLayout::loadFromFile(const QString &path)
} }
hasSetASelectedTab = true; hasSetASelectedTab = true;
} }
// Load tab "highlightsEnabled" state
tab.highlightsEnabled_ =
tab_obj.value("highlightsEnabled").toBool(true);
QJsonObject splitRoot = tab_obj.value("splits2").toObject();
// Load tab splits
if (!splitRoot.isEmpty())
{
// root type
auto nodeType = splitRoot.value("type").toString();
if (nodeType == "split")
{
tab.rootNode_ = loadNodes<SplitNodeDescriptor>(splitRoot);
}
else if (nodeType == "horizontal" || nodeType == "vertical")
{
tab.rootNode_ =
loadNodes<ContainerNodeDescriptor>(splitRoot);
}
}
window.tabs_.emplace_back(std::move(tab)); window.tabs_.emplace_back(std::move(tab));
} }
// Load emote popup position // Load emote popup position
QJsonObject emote_popup_obj = window_obj.value("emotePopup").toObject(); QJsonObject emote_popup_obj = windowObj.value("emotePopup").toObject();
layout.emotePopupPos_ = QPoint(emote_popup_obj.value("x").toInt(), layout.emotePopupPos_ = QPoint(emote_popup_obj.value("x").toInt(),
emote_popup_obj.value("y").toInt()); emote_popup_obj.value("y").toInt());

View file

@ -67,6 +67,8 @@ struct ContainerNodeDescriptor {
}; };
struct TabDescriptor { struct TabDescriptor {
static TabDescriptor loadFromJSON(const QJsonObject &root);
QString customTitle_; QString customTitle_;
bool selected_{false}; bool selected_{false};
bool highlightsEnabled_{true}; bool highlightsEnabled_{true};

View file

@ -379,20 +379,20 @@ void WindowManager::save()
QJsonDocument document; QJsonDocument document;
// "serialize" // "serialize"
QJsonArray window_arr; QJsonArray windowArr;
for (Window *window : this->windows_) for (Window *window : this->windows_)
{ {
QJsonObject window_obj; QJsonObject windowObj;
// window type // window type
switch (window->getType()) switch (window->getType())
{ {
case WindowType::Main: case WindowType::Main:
window_obj.insert("type", "main"); windowObj.insert("type", "main");
break; break;
case WindowType::Popup: case WindowType::Popup:
window_obj.insert("type", "popup"); windowObj.insert("type", "popup");
break; break;
case WindowType::Attached:; case WindowType::Attached:;
@ -400,68 +400,48 @@ void WindowManager::save()
if (window->isMaximized()) if (window->isMaximized())
{ {
window_obj.insert("state", "maximized"); windowObj.insert("state", "maximized");
} }
else if (window->isMinimized()) else if (window->isMinimized())
{ {
window_obj.insert("state", "minimized"); windowObj.insert("state", "minimized");
} }
// window geometry // window geometry
auto rect = window->getBounds(); auto rect = window->getBounds();
window_obj.insert("x", rect.x()); windowObj.insert("x", rect.x());
window_obj.insert("y", rect.y()); windowObj.insert("y", rect.y());
window_obj.insert("width", rect.width()); windowObj.insert("width", rect.width());
window_obj.insert("height", rect.height()); windowObj.insert("height", rect.height());
QJsonObject emote_popup_obj; QJsonObject emotePopupObj;
emote_popup_obj.insert("x", this->emotePopupPos_.x()); emotePopupObj.insert("x", this->emotePopupPos_.x());
emote_popup_obj.insert("y", this->emotePopupPos_.y()); emotePopupObj.insert("y", this->emotePopupPos_.y());
window_obj.insert("emotePopup", emote_popup_obj); windowObj.insert("emotePopup", emotePopupObj);
// window tabs // window tabs
QJsonArray tabs_arr; QJsonArray tabsArr;
for (int tab_i = 0; tab_i < window->getNotebook().getPageCount(); for (int tabIndex = 0; tabIndex < window->getNotebook().getPageCount();
tab_i++) tabIndex++)
{ {
QJsonObject tab_obj; QJsonObject tabObj;
SplitContainer *tab = dynamic_cast<SplitContainer *>( SplitContainer *tab = dynamic_cast<SplitContainer *>(
window->getNotebook().getPageAt(tab_i)); window->getNotebook().getPageAt(tabIndex));
assert(tab != nullptr); assert(tab != nullptr);
// custom tab title bool isSelected = window->getNotebook().getSelectedPage() == tab;
if (tab->getTab()->hasCustomTitle()) WindowManager::encodeTab(tab, isSelected, tabObj);
{ tabsArr.append(tabObj);
tab_obj.insert("title", tab->getTab()->getCustomTitle());
}
// selected
if (window->getNotebook().getSelectedPage() == tab)
{
tab_obj.insert("selected", true);
}
// highlighting on new messages
tab_obj.insert("highlightsEnabled",
tab->getTab()->hasHighlightsEnabled());
// splits
QJsonObject splits;
this->encodeNodeRecursively(tab->getBaseNode(), splits);
tab_obj.insert("splits2", splits);
tabs_arr.append(tab_obj);
} }
window_obj.insert("tabs", tabs_arr); windowObj.insert("tabs", tabsArr);
window_arr.append(window_obj); windowArr.append(windowObj);
} }
QJsonObject obj; QJsonObject obj;
obj.insert("windows", window_arr); obj.insert("windows", windowArr);
document.setObject(obj); document.setObject(obj);
// save file // save file
@ -497,6 +477,32 @@ void WindowManager::queueSave()
this->saveTimer->start(10s); this->saveTimer->start(10s);
} }
void WindowManager::encodeTab(SplitContainer *tab, bool isSelected,
QJsonObject &obj)
{
// custom tab title
if (tab->getTab()->hasCustomTitle())
{
obj.insert("title", tab->getTab()->getCustomTitle());
}
// selected
if (isSelected)
{
obj.insert("selected", true);
}
// highlighting on new messages
obj.insert("highlightsEnabled", tab->getTab()->hasHighlightsEnabled());
// splits
QJsonObject splits;
WindowManager::encodeNodeRecursively(tab->getBaseNode(), splits);
obj.insert("splits2", splits);
}
void WindowManager::encodeNodeRecursively(SplitNode *node, QJsonObject &obj) void WindowManager::encodeNodeRecursively(SplitNode *node, QJsonObject &obj)
{ {
switch (node->getType()) switch (node->getType())
@ -506,11 +512,12 @@ void WindowManager::encodeNodeRecursively(SplitNode *node, QJsonObject &obj)
obj.insert("moderationMode", node->getSplit()->getModerationMode()); obj.insert("moderationMode", node->getSplit()->getModerationMode());
QJsonObject split; QJsonObject split;
encodeChannel(node->getSplit()->getIndirectChannel(), split); WindowManager::encodeChannel(node->getSplit()->getIndirectChannel(),
split);
obj.insert("data", split); obj.insert("data", split);
QJsonArray filters; QJsonArray filters;
encodeFilters(node->getSplit(), filters); WindowManager::encodeFilters(node->getSplit(), filters);
obj.insert("filters", filters); obj.insert("filters", filters);
} }
break; break;
@ -520,14 +527,14 @@ void WindowManager::encodeNodeRecursively(SplitNode *node, QJsonObject &obj)
? "horizontal" ? "horizontal"
: "vertical"); : "vertical");
QJsonArray items_arr; QJsonArray itemsArr;
for (const std::unique_ptr<SplitNode> &n : node->getChildren()) for (const std::unique_ptr<SplitNode> &n : node->getChildren())
{ {
QJsonObject subObj; QJsonObject subObj;
this->encodeNodeRecursively(n.get(), subObj); WindowManager::encodeNodeRecursively(n.get(), subObj);
items_arr.append(subObj); itemsArr.append(subObj);
} }
obj.insert("items", items_arr); obj.insert("items", itemsArr);
} }
break; break;
} }

View file

@ -30,6 +30,8 @@ public:
WindowManager(); WindowManager();
~WindowManager() override; ~WindowManager() override;
static void encodeTab(SplitContainer *tab, bool isSelected,
QJsonObject &obj);
static void encodeChannel(IndirectChannel channel, QJsonObject &obj); static void encodeChannel(IndirectChannel channel, QJsonObject &obj);
static void encodeFilters(Split *split, QJsonArray &arr); static void encodeFilters(Split *split, QJsonArray &arr);
static IndirectChannel decodeChannel(const SplitDescriptor &descriptor); static IndirectChannel decodeChannel(const SplitDescriptor &descriptor);
@ -99,7 +101,8 @@ public:
pajlada::Signals::Signal<SplitContainer *> selectSplitContainer; pajlada::Signals::Signal<SplitContainer *> selectSplitContainer;
private: private:
void encodeNodeRecursively(SplitContainer::Node *node, QJsonObject &obj); static void encodeNodeRecursively(SplitContainer::Node *node,
QJsonObject &obj);
// Load window layout from the window-layout.json file // Load window layout from the window-layout.json file
WindowLayout loadWindowLayoutFromFile() const; WindowLayout loadWindowLayoutFromFile() const;

View file

@ -338,6 +338,14 @@ void Window::addShortcuts()
} }
}); });
createWindowShortcut(this, "CTRL+SHIFT+N", [this] {
if (auto page = dynamic_cast<SplitContainer *>(
this->notebook_->getSelectedPage()))
{
page->popup();
}
});
// Zoom in // Zoom in
{ {
auto s = new QShortcut(QKeySequence::ZoomIn, this); auto s = new QShortcut(QKeySequence::ZoomIn, this);

View file

@ -64,6 +64,16 @@ NotebookTab::NotebookTab(Notebook *notebook)
this->notebook_->removePage(this->page); this->notebook_->removePage(this->page);
}); });
this->menu_.addAction(
"Popup Tab",
[=]() {
if (auto container = dynamic_cast<SplitContainer *>(this->page))
{
container->popup();
}
},
QKeySequence("Ctrl+Shift+N"));
highlightNewMessagesAction_ = highlightNewMessagesAction_ =
new QAction("Mark Tab as Unread on New Messages", &this->menu_); new QAction("Mark Tab as Unread on New Messages", &this->menu_);
highlightNewMessagesAction_->setCheckable(true); highlightNewMessagesAction_->setCheckable(true);

View file

@ -44,6 +44,8 @@ KeyboardSettingsPage::KeyboardSettingsPage()
form->addRow(new QLabel("Ctrl + Shift + T"), new QLabel("Create new tab")); form->addRow(new QLabel("Ctrl + Shift + T"), new QLabel("Create new tab"));
form->addRow(new QLabel("Ctrl + Shift + W"), form->addRow(new QLabel("Ctrl + Shift + W"),
new QLabel("Close current tab")); new QLabel("Close current tab"));
form->addRow(new QLabel("Ctrl + Shift + N"),
new QLabel("Open current tab as a popup"));
form->addRow(new QLabel("Ctrl + H"), form->addRow(new QLabel("Ctrl + H"),
new QLabel("Hide/Show similar messages (See General->R9K)")); new QLabel("Hide/Show similar messages (See General->R9K)"));

View file

@ -9,6 +9,7 @@
#include "util/Helpers.hpp" #include "util/Helpers.hpp"
#include "util/LayoutCreator.hpp" #include "util/LayoutCreator.hpp"
#include "widgets/Notebook.hpp" #include "widgets/Notebook.hpp"
#include "widgets/Window.hpp"
#include "widgets/helper/ChannelView.hpp" #include "widgets/helper/ChannelView.hpp"
#include "widgets/helper/NotebookTab.hpp" #include "widgets/helper/NotebookTab.hpp"
#include "widgets/splits/ClosedSplits.hpp" #include "widgets/splits/ClosedSplits.hpp"
@ -761,6 +762,33 @@ void SplitContainer::applyFromDescriptor(const NodeDescriptor &rootNode)
this->layout(); this->layout();
} }
void SplitContainer::popup()
{
Window &window = getApp()->windows->createWindow(WindowType::Popup);
auto popupContainer = window.getNotebook().getOrAddSelectedPage();
QJsonObject encodedTab;
WindowManager::encodeTab(this, true, encodedTab);
TabDescriptor tab = TabDescriptor::loadFromJSON(encodedTab);
// custom title
if (!tab.customTitle_.isEmpty())
{
popupContainer->getTab()->setCustomTitle(tab.customTitle_);
}
// highlighting on new messages
popupContainer->getTab()->setHighlightsEnabled(tab.highlightsEnabled_);
// splits
if (tab.rootNode_)
{
popupContainer->applyFromDescriptor(*tab.rootNode_);
}
window.show();
}
void SplitContainer::applyFromDescriptorRecursively( void SplitContainer::applyFromDescriptorRecursively(
const NodeDescriptor &rootNode, Node *node) const NodeDescriptor &rootNode, Node *node)
{ {

View file

@ -202,6 +202,8 @@ public:
void applyFromDescriptor(const NodeDescriptor &rootNode); void applyFromDescriptor(const NodeDescriptor &rootNode);
void popup();
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;