Compare commits

..

No commits in common. "292f9b97348d9a455b18ea8c658db870d33462f5" and "1554d7b6a4e184b69c9d34d6366899dcd27b91fe" have entirely different histories.

11 changed files with 226 additions and 317 deletions

View file

@ -65,8 +65,6 @@
- Bugfix: Fixed avatar in usercard and moderation button triggering when releasing the mouse outside their area. (#5052) - Bugfix: Fixed avatar in usercard and moderation button triggering when releasing the mouse outside their area. (#5052)
- Bugfix: Fixed moderator-only topics being subscribed to for non-moderators. (#5056) - Bugfix: Fixed moderator-only topics being subscribed to for non-moderators. (#5056)
- Bugfix: Fixed a bug where buttons would remain in a hovered state after leaving them. (#5077) - Bugfix: Fixed a bug where buttons would remain in a hovered state after leaving them. (#5077)
- Bugfix: Fixed popup windows not persisting between restarts. (#5081)
- Bugfix: Fixed splits not retaining their focus after minimizing. (#5080)
- Dev: Run miniaudio in a separate thread, and simplify it to not manage the device ourselves. There's a chance the simplification is a bad idea. (#4978) - Dev: Run miniaudio in a separate thread, and simplify it to not manage the device ourselves. There's a chance the simplification is a bad idea. (#4978)
- Dev: Change clang-format from v14 to v16. (#4929) - Dev: Change clang-format from v14 to v16. (#4929)
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791) - Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
@ -104,7 +102,6 @@
- Dev: Added Tests for Windows and MacOS in CI. (#4970, #5032) - Dev: Added Tests for Windows and MacOS in CI. (#4970, #5032)
- Dev: Move `clang-tidy` checker to its own CI job. (#4996) - Dev: Move `clang-tidy` checker to its own CI job. (#4996)
- Dev: Refactored the Image Uploader feature. (#4971) - Dev: Refactored the Image Uploader feature. (#4971)
- Dev: Refactored the SplitOverlay code. (#5082)
- Dev: Fixed deadlock and use-after-free in tests. (#4981) - Dev: Fixed deadlock and use-after-free in tests. (#4981)
- Dev: Moved all `.clang-format` files to the root directory. (#5037) - Dev: Moved all `.clang-format` files to the root directory. (#5037)
- Dev: Load less message history upon reconnects. (#5001, #5018) - Dev: Load less message history upon reconnects. (#5001, #5018)

View file

@ -696,7 +696,6 @@ set(SOURCE_FILES
widgets/splits/InputCompletionPopup.hpp widgets/splits/InputCompletionPopup.hpp
widgets/splits/Split.cpp widgets/splits/Split.cpp
widgets/splits/Split.hpp widgets/splits/Split.hpp
widgets/splits/SplitCommon.hpp
widgets/splits/SplitContainer.cpp widgets/splits/SplitContainer.cpp
widgets/splits/SplitContainer.hpp widgets/splits/SplitContainer.hpp
widgets/splits/SplitHeader.cpp widgets/splits/SplitHeader.cpp

View file

@ -50,6 +50,7 @@ const QString WindowManager::WINDOW_LAYOUT_FILENAME(
QStringLiteral("window-layout.json")); QStringLiteral("window-layout.json"));
using SplitNode = SplitContainer::Node; using SplitNode = SplitContainer::Node;
using SplitDirection = SplitContainer::Direction;
void WindowManager::showSettingsDialog(QWidget *parent, void WindowManager::showSettingsDialog(QWidget *parent,
SettingsDialogPreference preference) SettingsDialogPreference preference)
@ -421,14 +422,7 @@ void WindowManager::save()
{ {
return; return;
} }
qCDebug(chatterinoWindowmanager) << "[WindowManager] Saving";
if (this->shuttingDown_)
{
qCDebug(chatterinoWindowmanager) << "Skipping save (shutting down)";
return;
}
qCDebug(chatterinoWindowmanager) << "Saving";
assertInGuiThread(); assertInGuiThread();
QJsonDocument document; QJsonDocument document;
@ -707,9 +701,6 @@ void WindowManager::closeAll()
{ {
assertInGuiThread(); assertInGuiThread();
qCDebug(chatterinoWindowmanager) << "Shutting down (closing windows)";
this->shuttingDown_ = true;
for (Window *window : windows_) for (Window *window : windows_)
{ {
window->close(); window->close();

View file

@ -140,7 +140,6 @@ private:
const QString windowLayoutFilePath; const QString windowLayoutFilePath;
bool initialized_ = false; bool initialized_ = false;
bool shuttingDown_ = false;
QPoint emotePopupPos_; QPoint emotePopupPos_;

View file

@ -1332,19 +1332,13 @@ SplitNotebook::SplitNotebook(Window *parent)
}); });
} }
void SplitNotebook::showEvent(QShowEvent * /*event*/) void SplitNotebook::showEvent(QShowEvent *)
{ {
if (auto *page = this->getSelectedPage()) if (auto page = this->getSelectedPage())
{ {
auto *split = page->getSelectedSplit(); if (auto split = page->findChild<Split *>())
if (!split)
{ {
split = page->findChild<Split *>(); split->setFocus(Qt::FocusReason::OtherFocusReason);
}
if (split)
{
split->setFocus(Qt::OtherFocusReason);
} }
} }
} }

View file

@ -4,7 +4,6 @@
#include "common/Channel.hpp" #include "common/Channel.hpp"
#include "common/NullablePtr.hpp" #include "common/NullablePtr.hpp"
#include "widgets/BaseWidget.hpp" #include "widgets/BaseWidget.hpp"
#include "widgets/splits/SplitCommon.hpp"
#include <boost/signals2.hpp> #include <boost/signals2.hpp>
#include <pajlada/signals/signalholder.hpp> #include <pajlada/signals/signalholder.hpp>
@ -96,7 +95,8 @@ public:
pajlada::Signals::Signal<Action> actionRequested; pajlada::Signals::Signal<Action> actionRequested;
pajlada::Signals::Signal<ChannelPtr> openSplitRequested; pajlada::Signals::Signal<ChannelPtr> openSplitRequested;
pajlada::Signals::Signal<SplitDirection, Split *> insertSplitRequested; // args: (SplitContainer::Direction dir, Split* parent)
pajlada::Signals::Signal<int, Split *> insertSplitRequested;
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;

View file

@ -1,12 +0,0 @@
#pragma once
namespace chatterino {
enum class SplitDirection {
Left,
Above,
Right,
Below,
};
} // namespace chatterino

View file

@ -159,7 +159,7 @@ void SplitContainer::insertSplit(Split *split, InsertOptions &&options)
} }
auto *relativeTo = options.relativeNode; auto *relativeTo = options.relativeNode;
const auto direction = options.direction.value_or(SplitDirection::Right); const auto direction = options.direction.value_or(Direction::Right);
if (relativeTo == nullptr) if (relativeTo == nullptr)
{ {
@ -260,26 +260,27 @@ void SplitContainer::addSplit(Split *split)
break; break;
case Split::Action::SelectSplitLeft: case Split::Action::SelectSplitLeft:
this->selectNextSplit(SplitDirection::Left); this->selectNextSplit(SplitContainer::Left);
break; break;
case Split::Action::SelectSplitRight: case Split::Action::SelectSplitRight:
this->selectNextSplit(SplitDirection::Right); this->selectNextSplit(SplitContainer::Right);
break; break;
case Split::Action::SelectSplitAbove: case Split::Action::SelectSplitAbove:
this->selectNextSplit(SplitDirection::Above); this->selectNextSplit(SplitContainer::Above);
break; break;
case Split::Action::SelectSplitBelow: case Split::Action::SelectSplitBelow:
this->selectNextSplit(SplitDirection::Below); this->selectNextSplit(SplitContainer::Below);
break; break;
} }
}); });
conns.managedConnect( conns.managedConnect(
split->insertSplitRequested, [this](SplitDirection dir, Split *parent) { split->insertSplitRequested, [this](int dir, Split *parent) {
this->insertSplit(new Split(this), { this->insertSplit(new Split(this),
.relativeSplit = parent, {
.direction = dir, .relativeSplit = parent,
}); .direction = static_cast<Direction>(dir),
});
}); });
this->layout(); this->layout();
@ -354,7 +355,7 @@ SplitContainer::Position SplitContainer::deleteSplit(Split *split)
return releaseSplit(split); return releaseSplit(split);
} }
void SplitContainer::selectNextSplit(SplitDirection direction) void SplitContainer::selectNextSplit(Direction direction)
{ {
assertInGuiThread(); assertInGuiThread();
@ -364,7 +365,7 @@ void SplitContainer::selectNextSplit(SplitDirection direction)
} }
} }
void SplitContainer::selectSplitRecursive(Node *node, SplitDirection direction) void SplitContainer::selectSplitRecursive(Node *node, Direction direction)
{ {
if (node->parent_ != nullptr) if (node->parent_ != nullptr)
{ {
@ -378,8 +379,7 @@ void SplitContainer::selectSplitRecursive(Node *node, SplitDirection direction)
}); });
assert(it != siblings.end()); assert(it != siblings.end());
if (direction == SplitDirection::Left || if (direction == Direction::Left || direction == Direction::Above)
direction == SplitDirection::Above)
{ {
if (it == siblings.begin()) if (it == siblings.begin())
{ {
@ -507,20 +507,20 @@ void SplitContainer::layout()
// left // left
dropRects.emplace_back( dropRects.emplace_back(
QRect(g.left(), g.top(), g.width() / 3, g.height()), QRect(g.left(), g.top(), g.width() / 3, g.height()),
Position(node, SplitDirection::Left)); Position(node, Direction::Left));
// right // right
dropRects.emplace_back(QRect(g.right() - g.width() / 3, g.top(), dropRects.emplace_back(QRect(g.right() - g.width() / 3, g.top(),
g.width() / 3, g.height()), g.width() / 3, g.height()),
Position(node, SplitDirection::Right)); Position(node, Direction::Right));
// top // top
dropRects.emplace_back( dropRects.emplace_back(
QRect(g.left(), g.top(), g.width(), g.height() / 2), QRect(g.left(), g.top(), g.width(), g.height() / 2),
Position(node, SplitDirection::Above)); Position(node, Direction::Above));
// bottom // bottom
dropRects.emplace_back(QRect(g.left(), g.bottom() - g.height() / 2, dropRects.emplace_back(QRect(g.left(), g.bottom() - g.height() / 2,
g.width(), g.height() / 2), g.width(), g.height() / 2),
Position(node, SplitDirection::Below)); Position(node, Direction::Below));
} }
if (this->splits_.empty()) if (this->splits_.empty())
@ -528,7 +528,7 @@ void SplitContainer::layout()
QRect g = this->rect(); QRect g = this->rect();
dropRects.emplace_back( dropRects.emplace_back(
QRect(g.left(), g.top(), g.width() - 1, g.height() - 1), QRect(g.left(), g.top(), g.width() - 1, g.height() - 1),
Position(nullptr, SplitDirection::Below)); Position(nullptr, Direction::Below));
} }
this->overlay_.setRects(std::move(dropRects)); this->overlay_.setRects(std::move(dropRects));
@ -1027,7 +1027,7 @@ SplitContainer::Node *SplitContainer::Node::findNodeContainingSplit(
} }
void SplitContainer::Node::insertSplitRelative(Split *_split, void SplitContainer::Node::insertSplitRelative(Split *_split,
SplitDirection _direction) Direction _direction)
{ {
if (this->parent_ == nullptr) if (this->parent_ == nullptr)
{ {
@ -1066,7 +1066,7 @@ void SplitContainer::Node::insertSplitRelative(Split *_split,
} }
void SplitContainer::Node::nestSplitIntoCollection(Split *_split, void SplitContainer::Node::nestSplitIntoCollection(Split *_split,
SplitDirection _direction) Direction _direction)
{ {
if (toContainerType(_direction) == this->type_) if (toContainerType(_direction) == this->type_)
{ {
@ -1095,8 +1095,7 @@ void SplitContainer::Node::nestSplitIntoCollection(Split *_split,
} }
} }
void SplitContainer::Node::insertNextToThis(Split *_split, void SplitContainer::Node::insertNextToThis(Split *_split, Direction _direction)
SplitDirection _direction)
{ {
auto &siblings = this->parent_->children_; auto &siblings = this->parent_->children_;
@ -1116,8 +1115,7 @@ void SplitContainer::Node::insertNextToThis(Split *_split,
}); });
assert(it != siblings.end()); assert(it != siblings.end());
if (_direction == SplitDirection::Right || if (_direction == Direction::Right || _direction == Direction::Below)
_direction == SplitDirection::Below)
{ {
it++; it++;
} }
@ -1147,7 +1145,7 @@ SplitContainer::Position SplitContainer::Node::releaseSplit()
Position pos; Position pos;
pos.relativeNode_ = nullptr; pos.relativeNode_ = nullptr;
pos.direction_ = SplitDirection::Right; pos.direction_ = Direction::Right;
return pos; return pos;
} }
@ -1165,15 +1163,13 @@ SplitContainer::Position SplitContainer::Node::releaseSplit()
position.relativeNode_ = this->parent_; position.relativeNode_ = this->parent_;
if (this->parent_->type_ == Type::VerticalContainer) if (this->parent_->type_ == Type::VerticalContainer)
{ {
position.direction_ = siblings.begin() == it position.direction_ =
? SplitDirection::Above siblings.begin() == it ? Direction::Above : Direction::Below;
: SplitDirection::Below;
} }
else else
{ {
position.direction_ = siblings.begin() == it position.direction_ =
? SplitDirection::Left siblings.begin() == it ? Direction::Left : Direction::Right;
: SplitDirection::Right;
} }
auto *parent = this->parent_; auto *parent = this->parent_;
@ -1195,8 +1191,8 @@ SplitContainer::Position SplitContainer::Node::releaseSplit()
{ {
position.direction_ = position.direction_ =
this->parent_->type_ == Type::VerticalContainer this->parent_->type_ == Type::VerticalContainer
? SplitDirection::Below ? Direction::Below
: SplitDirection::Right; : Direction::Right;
siblings.erase(it); siblings.erase(it);
position.relativeNode_ = siblings.back().get(); position.relativeNode_ = siblings.back().get();
} }
@ -1205,8 +1201,8 @@ SplitContainer::Position SplitContainer::Node::releaseSplit()
position.relativeNode_ = (it + 1)->get(); position.relativeNode_ = (it + 1)->get();
position.direction_ = position.direction_ =
this->parent_->type_ == Type::VerticalContainer this->parent_->type_ == Type::VerticalContainer
? SplitDirection::Above ? Direction::Above
: SplitDirection::Left; : Direction::Left;
siblings.erase(it); siblings.erase(it);
} }
} }
@ -1286,8 +1282,8 @@ void SplitContainer::Node::layout(bool addSpacing, float _scale,
isVertical ? offset : this->geometry_.width(), isVertical ? offset : this->geometry_.width(),
isVertical ? this->geometry_.height() : offset) isVertical ? this->geometry_.height() : offset)
.toRect(), .toRect(),
Position(this, isVertical ? SplitDirection::Left Position(this,
: SplitDirection::Above)); isVertical ? Direction::Left : Direction::Above));
// droprect right / below // droprect right / below
if (isVertical) if (isVertical)
@ -1297,7 +1293,7 @@ void SplitContainer::Node::layout(bool addSpacing, float _scale,
this->geometry_.top(), offset, this->geometry_.top(), offset,
this->geometry_.height()) this->geometry_.height())
.toRect(), .toRect(),
Position(this, SplitDirection::Right)); Position(this, Direction::Right));
} }
else else
{ {
@ -1306,7 +1302,7 @@ void SplitContainer::Node::layout(bool addSpacing, float _scale,
this->geometry_.bottom() - offset, this->geometry_.bottom() - offset,
this->geometry_.width(), offset) this->geometry_.width(), offset)
.toRect(), .toRect(),
Position(this, SplitDirection::Below)); Position(this, Direction::Below));
} }
// shrink childRect // shrink childRect
@ -1395,10 +1391,9 @@ void SplitContainer::Node::clamp()
this->flexV_ = std::max(0.0, this->flexV_); this->flexV_ = std::max(0.0, this->flexV_);
} }
SplitContainer::Node::Type SplitContainer::Node::toContainerType( SplitContainer::Node::Type SplitContainer::Node::toContainerType(Direction _dir)
SplitDirection _dir)
{ {
return _dir == SplitDirection::Left || _dir == SplitDirection::Right return _dir == Direction::Left || _dir == Direction::Right
? Type::HorizontalContainer ? Type::HorizontalContainer
: Type::VerticalContainer; : Type::VerticalContainer;
} }

View file

@ -2,7 +2,6 @@
#include "common/WindowDescriptors.hpp" #include "common/WindowDescriptors.hpp"
#include "widgets/BaseWidget.hpp" #include "widgets/BaseWidget.hpp"
#include "widgets/splits/SplitCommon.hpp"
#include <pajlada/signals/signal.hpp> #include <pajlada/signals/signal.hpp>
#include <pajlada/signals/signalholder.hpp> #include <pajlada/signals/signalholder.hpp>
@ -36,17 +35,21 @@ class SplitContainer final : public BaseWidget
public: public:
struct Node; struct Node;
// fourtf: !!! preserve the order of left, up, right and down
// It's important to preserve since we cast from an int to this enum, so 0 = left, 1 = above etc
enum Direction { Left, Above, Right, Below };
struct Position final { struct Position final {
private: private:
Position() = default; Position() = default;
Position(Node *relativeNode, SplitDirection direction) Position(Node *relativeNode, Direction direcion)
: relativeNode_(relativeNode) : relativeNode_(relativeNode)
, direction_(direction) , direction_(direcion)
{ {
} }
Node *relativeNode_{nullptr}; Node *relativeNode_{nullptr};
SplitDirection direction_{SplitDirection::Right}; Direction direction_{Direction::Right};
friend struct Node; friend struct Node;
friend class SplitContainer; friend class SplitContainer;
@ -99,9 +102,9 @@ public:
bool isOrContainsNode(Node *_node); bool isOrContainsNode(Node *_node);
Node *findNodeContainingSplit(Split *_split); Node *findNodeContainingSplit(Split *_split);
void insertSplitRelative(Split *_split, SplitDirection _direction); void insertSplitRelative(Split *_split, Direction _direction);
void nestSplitIntoCollection(Split *_split, SplitDirection _direction); void nestSplitIntoCollection(Split *_split, Direction _direction);
void insertNextToThis(Split *_split, SplitDirection _direction); void insertNextToThis(Split *_split, Direction _direction);
void setSplit(Split *_split); void setSplit(Split *_split);
Position releaseSplit(); Position releaseSplit();
qreal getFlex(bool isVertical); qreal getFlex(bool isVertical);
@ -114,7 +117,7 @@ public:
// Clamps the flex values ensuring they're never below 0 // Clamps the flex values ensuring they're never below 0
void clamp(); void clamp();
static Type toContainerType(SplitDirection _dir); static Type toContainerType(Direction _dir);
Type type_; Type type_;
Split *split_; Split *split_;
@ -187,7 +190,7 @@ public:
Split *relativeSplit{nullptr}; Split *relativeSplit{nullptr};
Node *relativeNode{nullptr}; Node *relativeNode{nullptr};
std::optional<SplitDirection> direction{}; std::optional<Direction> direction{};
}; };
// Insert split into the base node of this container // Insert split into the base node of this container
@ -206,7 +209,7 @@ public:
Position releaseSplit(Split *split); Position releaseSplit(Split *split);
Position deleteSplit(Split *split); Position deleteSplit(Split *split);
void selectNextSplit(SplitDirection direction); void selectNextSplit(Direction direction);
void setSelected(Split *split); void setSelected(Split *split);
std::vector<Split *> getSplits() const; std::vector<Split *> getSplits() const;
@ -241,7 +244,7 @@ private:
Node *baseNode); Node *baseNode);
void layout(); void layout();
void selectSplitRecursive(Node *node, SplitDirection direction); void selectSplitRecursive(Node *node, Direction direction);
void focusSplitRecursive(Node *node); void focusSplitRecursive(Node *node);
void setPreferedTargetRecursive(Node *node); void setPreferedTargetRecursive(Node *node);

View file

@ -14,117 +14,14 @@
#include <QPainter> #include <QPainter>
#include <QPushButton> #include <QPushButton>
namespace {
using namespace chatterino;
class ButtonEventFilter : public QObject
{
SplitOverlay *parent;
SplitOverlayButton buttonType;
public:
ButtonEventFilter(SplitOverlay *_parent, SplitOverlayButton _buttonType);
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
};
ButtonEventFilter::ButtonEventFilter(SplitOverlay *_parent,
SplitOverlayButton _buttonType)
: QObject(_parent)
, parent(_parent)
, buttonType(_buttonType)
{
}
bool ButtonEventFilter::eventFilter(QObject *watched, QEvent *event)
{
switch (event->type())
{
case QEvent::Enter: {
auto *effect = dynamic_cast<QGraphicsOpacityEffect *>(
((QWidget *)watched)->graphicsEffect());
if (effect != nullptr)
{
effect->setOpacity(0.99);
}
this->parent->setHoveredButton(this->buttonType);
}
break;
case QEvent::Leave: {
auto *effect = dynamic_cast<QGraphicsOpacityEffect *>(
((QWidget *)watched)->graphicsEffect());
if (effect != nullptr)
{
effect->setOpacity(0.7);
}
this->parent->setHoveredButton({});
}
break;
case QEvent::MouseButtonPress: {
if (this->buttonType == SplitOverlayButton::Move)
{
auto *mouseEvent = dynamic_cast<QMouseEvent *>(event);
assert(mouseEvent != nullptr);
if (mouseEvent->button() == Qt::LeftButton)
{
this->parent->dragPressed();
}
return true;
}
}
break;
case QEvent::MouseButtonRelease: {
switch (this->buttonType)
{
case SplitOverlayButton::Move:
break;
case SplitOverlayButton::Left: {
this->parent->createSplitPressed(SplitDirection::Left);
}
break;
case SplitOverlayButton::Up: {
this->parent->createSplitPressed(SplitDirection::Above);
}
break;
case SplitOverlayButton::Right: {
this->parent->createSplitPressed(SplitDirection::Right);
}
break;
case SplitOverlayButton::Down: {
this->parent->createSplitPressed(SplitDirection::Below);
}
break;
}
}
break;
default:;
}
return QObject::eventFilter(watched, event);
}
} // namespace
namespace chatterino { namespace chatterino {
SplitOverlay::SplitOverlay(Split *parent) SplitOverlay::SplitOverlay(Split *parent)
: BaseWidget(parent) : BaseWidget(parent)
, split_(parent) , split_(parent)
{ {
auto *layout = new QGridLayout(this); QGridLayout *layout = new QGridLayout(this);
this->layout_ = layout;
layout->setContentsMargins(1, 1, 1, 1); layout->setContentsMargins(1, 1, 1, 1);
layout->setSpacing(1); layout->setSpacing(1);
@ -133,93 +30,69 @@ SplitOverlay::SplitOverlay(Split *parent)
layout->setColumnStretch(1, 1); layout->setColumnStretch(1, 1);
layout->setColumnStretch(3, 1); layout->setColumnStretch(3, 1);
this->move_ = new QPushButton(getResources().split.move, {}); auto *move = new QPushButton(getResources().split.move, QString());
this->left_ = new QPushButton(getResources().split.left, {}); auto *left = this->left_ =
this->right_ = new QPushButton(getResources().split.right, {}); new QPushButton(getResources().split.left, QString());
this->up_ = new QPushButton(getResources().split.up, {}); auto *right = this->right_ =
this->down_ = new QPushButton(getResources().split.down, {}); new QPushButton(getResources().split.right, QString());
auto *up = this->up_ = new QPushButton(getResources().split.up, QString());
auto *down = this->down_ =
new QPushButton(getResources().split.down, QString());
this->move_->setGraphicsEffect(new QGraphicsOpacityEffect(this)); move->setGraphicsEffect(new QGraphicsOpacityEffect(this));
this->left_->setGraphicsEffect(new QGraphicsOpacityEffect(this)); left->setGraphicsEffect(new QGraphicsOpacityEffect(this));
this->right_->setGraphicsEffect(new QGraphicsOpacityEffect(this)); right->setGraphicsEffect(new QGraphicsOpacityEffect(this));
this->up_->setGraphicsEffect(new QGraphicsOpacityEffect(this)); up->setGraphicsEffect(new QGraphicsOpacityEffect(this));
this->down_->setGraphicsEffect(new QGraphicsOpacityEffect(this)); down->setGraphicsEffect(new QGraphicsOpacityEffect(this));
this->move_->setFlat(true); move->setFlat(true);
this->left_->setFlat(true); left->setFlat(true);
this->right_->setFlat(true); right->setFlat(true);
this->up_->setFlat(true); up->setFlat(true);
this->down_->setFlat(true); down->setFlat(true);
layout->addWidget(this->move_, 2, 2); layout->addWidget(move, 2, 2);
layout->addWidget(this->left_, 2, 0); layout->addWidget(left, 2, 0);
layout->addWidget(this->right_, 2, 4); layout->addWidget(right, 2, 4);
layout->addWidget(this->up_, 0, 2); layout->addWidget(up, 0, 2);
layout->addWidget(this->down_, 4, 2); layout->addWidget(down, 4, 2);
this->move_->installEventFilter( move->installEventFilter(new ButtonEventFilter(this, SplitMove));
new ButtonEventFilter(this, SplitOverlayButton::Move)); left->installEventFilter(new ButtonEventFilter(this, SplitLeft));
this->left_->installEventFilter( right->installEventFilter(new ButtonEventFilter(this, SplitRight));
new ButtonEventFilter(this, SplitOverlayButton::Left)); up->installEventFilter(new ButtonEventFilter(this, SplitUp));
this->right_->installEventFilter( down->installEventFilter(new ButtonEventFilter(this, SplitDown));
new ButtonEventFilter(this, SplitOverlayButton::Right));
this->up_->installEventFilter(
new ButtonEventFilter(this, SplitOverlayButton::Up));
this->down_->installEventFilter(
new ButtonEventFilter(this, SplitOverlayButton::Down));
this->move_->setFocusPolicy(Qt::NoFocus); move->setFocusPolicy(Qt::NoFocus);
this->left_->setFocusPolicy(Qt::NoFocus); left->setFocusPolicy(Qt::NoFocus);
this->right_->setFocusPolicy(Qt::NoFocus); right->setFocusPolicy(Qt::NoFocus);
this->up_->setFocusPolicy(Qt::NoFocus); up->setFocusPolicy(Qt::NoFocus);
this->down_->setFocusPolicy(Qt::NoFocus); down->setFocusPolicy(Qt::NoFocus);
this->move_->setCursor(Qt::SizeAllCursor); move->setCursor(Qt::SizeAllCursor);
this->left_->setCursor(Qt::PointingHandCursor); left->setCursor(Qt::PointingHandCursor);
this->right_->setCursor(Qt::PointingHandCursor); right->setCursor(Qt::PointingHandCursor);
this->up_->setCursor(Qt::PointingHandCursor); up->setCursor(Qt::PointingHandCursor);
this->down_->setCursor(Qt::PointingHandCursor); down->setCursor(Qt::PointingHandCursor);
this->signalHolder_.managedConnect(this->scaleChanged, [=](float _scale) {
int a = int(_scale * 30);
QSize size(a, a);
move->setIconSize(size);
left->setIconSize(size);
right->setIconSize(size);
up->setIconSize(size);
down->setIconSize(size);
});
this->setMouseTracking(true); this->setMouseTracking(true);
this->setCursor(Qt::ArrowCursor); this->setCursor(Qt::ArrowCursor);
} }
void SplitOverlay::setHoveredButton( void SplitOverlay::paintEvent(QPaintEvent *)
std::optional<SplitOverlayButton> hoveredButton)
{ {
this->hoveredButton_ = hoveredButton;
this->update();
}
void SplitOverlay::dragPressed()
{
this->split_->drag();
}
void SplitOverlay::createSplitPressed(SplitDirection direction)
{
this->split_->insertSplitRequested.invoke(direction, this->split_);
this->hide();
}
void SplitOverlay::scaleChangedEvent(float newScale)
{
int a = int(newScale * 30);
QSize size(a, a);
this->move_->setIconSize(size);
this->left_->setIconSize(size);
this->right_->setIconSize(size);
this->up_->setIconSize(size);
this->down_->setIconSize(size);
BaseWidget::scaleChangedEvent(newScale);
}
void SplitOverlay::paintEvent(QPaintEvent *event)
{
(void)event;
QPainter painter(this); QPainter painter(this);
if (this->theme->isLightTheme()) if (this->theme->isLightTheme())
{ {
@ -230,32 +103,26 @@ void SplitOverlay::paintEvent(QPaintEvent *event)
painter.fillRect(this->rect(), QColor(0, 0, 0, 150)); painter.fillRect(this->rect(), QColor(0, 0, 0, 150));
} }
if (!this->hoveredButton_.has_value())
{
return;
}
QRect rect; QRect rect;
auto hoveredButton = this->hoveredButton_.value(); switch (this->hoveredElement_)
switch (hoveredButton)
{ {
case SplitOverlayButton::Left: { case SplitLeft: {
rect = QRect(0, 0, this->width() / 2, this->height()); rect = QRect(0, 0, this->width() / 2, this->height());
} }
break; break;
case SplitOverlayButton::Right: { case SplitRight: {
rect = rect =
QRect(this->width() / 2, 0, this->width() / 2, this->height()); QRect(this->width() / 2, 0, this->width() / 2, this->height());
} }
break; break;
case SplitOverlayButton::Up: { case SplitUp: {
rect = QRect(0, 0, this->width(), this->height() / 2); rect = QRect(0, 0, this->width(), this->height() / 2);
} }
break; break;
case SplitOverlayButton::Down: { case SplitDown: {
rect = rect =
QRect(0, this->height() / 2, this->width(), this->height() / 2); QRect(0, this->height() / 2, this->width(), this->height() / 2);
} }
@ -264,10 +131,11 @@ void SplitOverlay::paintEvent(QPaintEvent *event)
default:; default:;
} }
rect.setRight(rect.right() - 1);
rect.setBottom(rect.bottom() - 1);
if (!rect.isNull()) if (!rect.isNull())
{ {
rect.setRight(rect.right() - 1);
rect.setBottom(rect.bottom() - 1);
painter.setPen(getApp()->themes->splits.dropPreviewBorder); painter.setPen(getApp()->themes->splits.dropPreviewBorder);
painter.setBrush(getApp()->themes->splits.dropPreview); painter.setBrush(getApp()->themes->splits.dropPreview);
painter.drawRect(rect); painter.drawRect(rect);
@ -276,12 +144,9 @@ void SplitOverlay::paintEvent(QPaintEvent *event)
void SplitOverlay::resizeEvent(QResizeEvent *event) void SplitOverlay::resizeEvent(QResizeEvent *event)
{ {
const auto currentScale = this->scale(); float _scale = this->scale();
const auto minimumWidth = 150.F * currentScale; bool wideEnough = event->size().width() > 150 * _scale;
const auto minimumHeight = 150.F * currentScale; bool highEnough = event->size().height() > 150 * _scale;
const auto wideEnough = event->size().width() > minimumWidth;
const auto highEnough = event->size().height() > minimumHeight;
this->left_->setVisible(wideEnough); this->left_->setVisible(wideEnough);
this->right_->setVisible(wideEnough); this->right_->setVisible(wideEnough);
@ -299,4 +164,75 @@ void SplitOverlay::mouseMoveEvent(QMouseEvent *event)
// } // }
} }
SplitOverlay::ButtonEventFilter::ButtonEventFilter(SplitOverlay *_parent,
HoveredElement _element)
: QObject(_parent)
, parent(_parent)
, hoveredElement(_element)
{
}
bool SplitOverlay::ButtonEventFilter::eventFilter(QObject *watched,
QEvent *event)
{
switch (event->type())
{
case QEvent::Enter: {
QGraphicsOpacityEffect *effect =
dynamic_cast<QGraphicsOpacityEffect *>(
((QWidget *)watched)->graphicsEffect());
if (effect != nullptr)
{
effect->setOpacity(0.99);
}
this->parent->hoveredElement_ = this->hoveredElement;
this->parent->update();
}
break;
case QEvent::Leave: {
QGraphicsOpacityEffect *effect =
dynamic_cast<QGraphicsOpacityEffect *>(
((QWidget *)watched)->graphicsEffect());
if (effect != nullptr)
{
effect->setOpacity(0.7);
}
this->parent->hoveredElement_ = HoveredElement::None;
this->parent->update();
}
break;
case QEvent::MouseButtonPress: {
if (this->hoveredElement == HoveredElement::SplitMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::LeftButton)
{
this->parent->split_->drag();
}
return true;
}
}
break;
case QEvent::MouseButtonRelease: {
if (this->hoveredElement != HoveredElement::SplitMove)
{
auto dir = SplitContainer::Direction(
this->hoveredElement + SplitContainer::Left - SplitLeft);
this->parent->split_->insertSplitRequested.invoke(
static_cast<int>(dir), this->parent->split_);
this->parent->hide();
}
}
break;
default:;
}
return QObject::eventFilter(watched, event);
}
} // namespace chatterino } // namespace chatterino

View file

@ -1,53 +1,60 @@
#pragma once #pragma once
#include "widgets/BaseWidget.hpp" #include "widgets/BaseWidget.hpp"
#include "widgets/splits/SplitCommon.hpp"
#include <pajlada/signals/signalholder.hpp>
#include <QGridLayout>
#include <QPushButton> #include <QPushButton>
#include <optional>
namespace chatterino { namespace chatterino {
class Split; class Split;
/// Type of button in the split overlay (the overlay that appears when holding down ctrl+alt)
enum class SplitOverlayButton {
Move,
Left,
Up,
Right,
Down,
};
class SplitOverlay : public BaseWidget class SplitOverlay : public BaseWidget
{ {
public: public:
explicit SplitOverlay(Split *parent); explicit SplitOverlay(Split *parent = nullptr);
// Called from the Split Overlay's button when it gets hovered over
void setHoveredButton(std::optional<SplitOverlayButton> hoveredButton);
// Called from the Split Overlay's button when the move button is pressed
void dragPressed();
// Called from the Split Overlay's button when one of the direction buttons are pressed
void createSplitPressed(SplitDirection direction);
protected: protected:
void scaleChangedEvent(float newScale) override; // bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
private: private:
std::optional<SplitOverlayButton> hoveredButton_{}; // fourtf: !!! preserve the order of left, up, right and down
enum HoveredElement {
None,
SplitMove,
SplitLeft,
SplitUp,
SplitRight,
SplitDown
};
class ButtonEventFilter : public QObject
{
SplitOverlay *parent;
HoveredElement hoveredElement;
public:
ButtonEventFilter(SplitOverlay *parent, HoveredElement hoveredElement);
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
};
HoveredElement hoveredElement_ = None;
Split *split_; Split *split_;
QPushButton *move_; QGridLayout *layout_;
QPushButton *left_; QPushButton *left_;
QPushButton *up_; QPushButton *up_;
QPushButton *right_; QPushButton *right_;
QPushButton *down_; QPushButton *down_;
pajlada::Signals::SignalHolder signalHolder_;
friend class ButtonEventFilter;
}; };
} // namespace chatterino } // namespace chatterino