Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to duplicate tabs #5277

Merged
merged 17 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Checks: "-*,
-readability-magic-numbers,
-performance-noexcept-move-constructor,
-misc-non-private-member-variables-in-classes,
-misc-no-recursion,
-cppcoreguidelines-non-private-member-variables-in-classes,
-modernize-use-nodiscard,
-modernize-use-trailing-return-type,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Minor: Add option to customise Moderation buttons with images. (#5369)
- Minor: Colored usernames now update on the fly when changing the "Color @usernames" setting. (#5300)
- Minor: Added `flags.action` filter variable, allowing you to filter on `/me` messages. (#5397)
- Minor: Added the ability to duplicate tabs. (#5277)
- Bugfix: If a network request errors with 200 OK, Qt's error code is now reported instead of the HTTP status. (#5378)
- Bugfix: Fixed a crash that could occur when logging was enabled in IRC servers that were removed. (#5419)
- Dev: Use Qt's high DPI scaling. (#4868, #5400)
Expand Down
32 changes: 31 additions & 1 deletion src/common/Channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "controllers/completion/TabCompletionModel.hpp"
#include "messages/LimitedQueue.hpp"

#include <magic_enum/magic_enum.hpp>
#include <pajlada/signals/signal.hpp>
#include <QDate>
#include <QString>
Expand Down Expand Up @@ -45,7 +46,7 @@ class Channel : public std::enable_shared_from_this<Channel>
TwitchAutomod,
TwitchEnd,
Irc,
Misc
Misc,
};

explicit Channel(const QString &name, Type type);
Expand Down Expand Up @@ -151,3 +152,32 @@ class IndirectChannel
};

} // namespace chatterino

template <>
constexpr magic_enum::customize::customize_t
magic_enum::customize::enum_name<chatterino::Channel::Type>(
chatterino::Channel::Type value) noexcept
{
using Type = chatterino::Channel::Type;
switch (value)
{
case Type::Twitch:
return "twitch";
case Type::TwitchWhispers:
return "whispers";
case Type::TwitchWatching:
return "watching";
case Type::TwitchMentions:
return "mentions";
case Type::TwitchLive:
return "live";
case Type::TwitchAutomod:
return "automod";
case Type::Irc:
return "irc";
case Type::Misc:
return "misc";
default:
return default_tag;
}
}
59 changes: 58 additions & 1 deletion src/widgets/Notebook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <QUuid>
#include <QWidget>

#include <utility>

namespace chatterino {

Notebook::Notebook(QWidget *parent)
Expand Down Expand Up @@ -87,6 +89,12 @@ Notebook::Notebook(QWidget *parent)
}

NotebookTab *Notebook::addPage(QWidget *page, QString title, bool select)
{
return this->addPageAt(page, -1, std::move(title), select);
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
}

NotebookTab *Notebook::addPageAt(QWidget *page, int position, QString title,
pajlada marked this conversation as resolved.
Show resolved Hide resolved
bool select)
{
// Queue up save because: Tab added
getIApp()->getWindows()->queueSave();
Expand All @@ -101,7 +109,14 @@ NotebookTab *Notebook::addPage(QWidget *page, QString title, bool select)
item.page = page;
item.tab = tab;

this->items_.append(item);
if (position == -1)
{
this->items_.push_back(item);
}
else
{
this->items_.insert(position, item);
}

page->hide();
page->setParent(this);
Expand Down Expand Up @@ -165,6 +180,48 @@ void Notebook::removePage(QWidget *page)
this->performLayout(true);
}

void Notebook::duplicatePage(QWidget *page)
{
auto *item = this->findItem(page);
assert(item != nullptr);
if (item == nullptr)
{
return;
}

auto *container = dynamic_cast<SplitContainer *>(item->page);
if (!container)
{
return;
}

auto *newContainer = new SplitContainer(this);
if (!container->getSplits().empty())
{
auto descriptor = container->buildDescriptor();
newContainer->applyFromDescriptor(descriptor);
}

const auto tabPosition = this->indexOf(page);
auto newTabPosition = -1;
if (tabPosition != -1)
{
newTabPosition = tabPosition + 1;
}
auto newTabHighlightState = item->tab->highlightState();
QString newTabTitle = "";
if (item->tab->hasCustomTitle())
{
newTabTitle = item->tab->getCustomTitle();
}

auto *tab =
this->addPageAt(newContainer, newTabPosition, newTabTitle, false);
tab->setHighlightState(newTabHighlightState);

newContainer->setTab(tab);
}

void Notebook::removeCurrentPage()
{
if (this->selectedPage_ != nullptr)
Expand Down
9 changes: 9 additions & 0 deletions src/widgets/Notebook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ class Notebook : public BaseWidget

NotebookTab *addPage(QWidget *page, QString title = QString(),
bool select = false);

/**
* @brief Adds a page to the Notebook at a given position.
*
* @param position if set to -1, adds the page to the end
**/
NotebookTab *addPageAt(QWidget *page, int position,
QString title = QString(), bool select = false);
void removePage(QWidget *page);
void duplicatePage(QWidget *page);
void removeCurrentPage();

/**
Expand Down
4 changes: 4 additions & 0 deletions src/widgets/helper/NotebookTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ NotebookTab::NotebookTab(Notebook *notebook)
getIApp()->getHotkeys()->getDisplaySequence(HotkeyCategory::Window,
"popup", {{"window"}}));

this->menu_.addAction("Duplicate Tab", [this]() {
this->notebook_->duplicatePage(this->page);
});

highlightNewMessagesAction_ =
new QAction("Mark Tab as Unread on New Messages", &this->menu_);
highlightNewMessagesAction_->setCheckable(true);
Expand Down
53 changes: 52 additions & 1 deletion src/widgets/splits/SplitContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include "common/QLogging.hpp"
#include "common/WindowDescriptors.hpp"
#include "debug/AssertInGuiThread.hpp"
#include "providers/irc/IrcChannel2.hpp"
#include "providers/irc/IrcServer.hpp"
#include "singletons/Fonts.hpp"
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp"
#include "util/QMagicEnum.hpp"
#include "widgets/helper/ChannelView.hpp"
#include "widgets/helper/NotebookTab.hpp"
#include "widgets/Notebook.hpp"
Expand Down Expand Up @@ -762,6 +765,11 @@ SplitContainer::Node *SplitContainer::getBaseNode()
return &this->baseNode_;
}

NodeDescriptor SplitContainer::buildDescriptor() const
{
return this->buildDescriptorRecursively(&this->baseNode_);
}

void SplitContainer::applyFromDescriptor(const NodeDescriptor &rootNode)
{
assert(this->baseNode_.type_ == Node::Type::EmptyRoot);
Expand Down Expand Up @@ -799,6 +807,49 @@ void SplitContainer::popup()
window.show();
}

NodeDescriptor SplitContainer::buildDescriptorRecursively(
const Node *currentNode) const
{
if (currentNode->children_.empty())
{
const auto channelType =
currentNode->split_->getIndirectChannel().getType();

SplitNodeDescriptor result;
result.type_ = qmagicenum::enumNameString(channelType);
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved
pajlada marked this conversation as resolved.
Show resolved Hide resolved

switch (channelType)
{
case Channel::Type::Irc: {
if (auto *ircChannel = dynamic_cast<IrcChannel *>(
currentNode->split_->getChannel().get()))
{
if (ircChannel->server())
{
result.server_ = ircChannel->server()->id();
}
}
}
break;
}

result.channelName_ = currentNode->split_->getChannel()->getName();
result.filters_ = currentNode->split_->getFilters();
return result;
}

ContainerNodeDescriptor descriptor;
for (const auto &child : currentNode->children_)
{
descriptor.vertical_ =
currentNode->type_ == Node::Type::VerticalContainer;
descriptor.items_.push_back(
this->buildDescriptorRecursively(child.get()));
}

return descriptor;
}

void SplitContainer::applyFromDescriptorRecursively(
const NodeDescriptor &rootNode, Node *baseNode)
{
Expand Down Expand Up @@ -849,9 +900,9 @@ void SplitContainer::applyFromDescriptorRecursively(
}
const auto &splitNode = *inner;
auto *split = new Split(this);
split->setFilters(splitNode.filters_);
pajlada marked this conversation as resolved.
Show resolved Hide resolved
split->setChannel(WindowManager::decodeChannel(splitNode));
split->setModerationMode(splitNode.moderationMode_);
split->setFilters(splitNode.filters_);

auto *node = new Node();
node->parent_ = baseNode;
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/splits/SplitContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ class SplitContainer final : public BaseWidget
void hideResizeHandles();
void resetMouseStatus();

NodeDescriptor buildDescriptor() const;
void applyFromDescriptor(const NodeDescriptor &rootNode);

void popup();
Expand All @@ -237,6 +238,7 @@ class SplitContainer final : public BaseWidget
void resizeEvent(QResizeEvent *event) override;

private:
NodeDescriptor buildDescriptorRecursively(const Node *currentNode) const;
void applyFromDescriptorRecursively(const NodeDescriptor &rootNode,
Node *baseNode);

Expand Down