mirror-chatterino2/src/widgets/dialogs/switcher/SwitchSplitItem.cpp

91 lines
2.7 KiB
C++
Raw Normal View History

Discord-like Quick Switcher (#1588) * Proof of Concept for Quick Switcher * Fix crash when suggestions are empty * QuickSwitcher: Use tab name instead of a single channel * Rebase later * Add missing include for <functional> * Move QuickSwitcher related classes into own subfolder * Refactor switcher list items Now, items are responsible for taking the right action when selected in the switcher list. This should allow for more focused code and responsibilities. * Add note about memory management * Add option to open channel in a new tab * Add support for using the mouse * Spawn switcher popup in the middle of the window Works reliably on i3 at least. Might need some additional testing on other WMs (and especially on Windows!). * Add some icons for switcher items Note that the final design of the list is not final but I do plan to incorporate these in the future. * Set Qt::Dialog window flag on switcher popup Prevents tiling window managers like i3 from trying to tile the window. * Rename "SwitcherItem" to "AbstractSwitcherItem" * Add comments about what items are inserted * Use custom model and view Still missing: Currently selected item is not highlighted yet. You can move between selected items with tab and arrow keys though. * Add helper function to convert QVariant to AbstractSwitcherItem * * Remove useless constant * Highlight currently selected switcher item * Use a different method for centering QuickSwitcherPopup window * QuickSwitcherModel: Add documentation * Add default parameter to QuickSwitcherModel::rowCount * QuickSwitcherPopup: Add comments * Remove outdated TODO * QuickSwitcherModel: Init vector with default capacity * Remove outdated comment * Add comment about 0 ms timeout interval * NewTabItem: Simplify interface * Only fetch opened splits once This is better than the prior approach since opened splits cannot change anyways while the switcher is open. * Use SplitContainer to pass information instead of custom type * Allow searching for tab titles as well Before this commit, only channel names could be searched. * Refactor switcher item interface to be more flexible Also show tab name and channel name in the switcher list. * Add documentation for AbstractSwitcherItem * Add documentation for NewTabItem * Add comments about {begin,end}{Insert,Remove}Rows * Remove unused method * Replace magic size with named constant * Add change log entry Co-authored-by: fourtf <tf.four@gmail.com>
2020-08-13 19:25:51 +02:00
#include "widgets/dialogs/switcher/SwitchSplitItem.hpp"
#include "Application.hpp"
#include "singletons/Fonts.hpp"
#include "singletons/Theme.hpp"
#include "widgets/helper/NotebookTab.hpp"
namespace chatterino {
SwitchSplitItem::SwitchSplitItem(Split *split)
: AbstractSwitcherItem(QIcon(":switcher/switch.svg"))
, split_(split)
, container_(split->getContainer())
{
}
SwitchSplitItem::SwitchSplitItem(SplitContainer *container)
: AbstractSwitcherItem(QIcon(":switcher/switch.svg"))
, container_(container)
{
}
void SwitchSplitItem::action()
{
auto &nb = getApp()->windows->getMainWindow().getNotebook();
nb.select(this->container_);
/*
* If the item is referring to a specific channel, select the
* corresponding split.
*/
if (this->split_)
{
this->container_->setSelected(this->split_);
}
}
void SwitchSplitItem::paint(QPainter *painter, const QRect &rect) const
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing, true);
// TODO(leon): Right pen/brush/font settings?
painter->setPen(getApp()->themes->splits.header.text);
painter->setBrush(Qt::SolidPattern);
painter->setFont(getApp()->fonts->getFont(FontStyle::UiMediumBold, 1.0));
QRect iconRect(rect.topLeft(), ICON_SIZE);
this->icon_.paint(painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter);
if (this->split_)
{
// Draw channel name and name of the containing tab
const auto availableTextWidth = rect.width() - iconRect.width();
QRect leftTextRect =
QRect(iconRect.topRight(),
QSize(0.3 * availableTextWidth, iconRect.height()));
painter->drawText(leftTextRect, Qt::AlignLeft | Qt::AlignVCenter,
this->split_->getChannel()->getName());
QRect rightTextRect =
QRect(leftTextRect.topRight(),
QSize(0.7 * availableTextWidth, iconRect.height()));
painter->setFont(getApp()->fonts->getFont(FontStyle::UiMedium, 1.0));
painter->drawText(rightTextRect, Qt::AlignRight | Qt::AlignVCenter,
this->container_->getTab()->getTitle());
}
else if (!this->split_ && this->container_)
{
// Only draw name of tab
QRect textRect =
QRect(iconRect.topRight(),
QSize(rect.width() - iconRect.width(), iconRect.height()));
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter,
this->container_->getTab()->getTitle());
}
painter->restore();
}
QSize SwitchSplitItem::sizeHint(const QRect &rect) const
{
return QSize(rect.width(), ICON_SIZE.height());
}
} // namespace chatterino