Skip to content

Commit

Permalink
Do bounds-checking on more windows (#4797)
Browse files Browse the repository at this point in the history
Co-authored-by: pajlada <[email protected]>
  • Loading branch information
Nerixyz and pajlada committed Dec 2, 2023
1 parent e327ed4 commit c4c9447
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- Bugfix: Fixed lookahead/-behind not working in _Ignores_. (#4965)
- Bugfix: Fixed Image Uploader accidentally deleting images with some hosts when link resolver was enabled. (#4971)
- Bugfix: Fixed rare crash with Image Uploader when closing a split right after starting an upload. (#4971)
- Bugfix: Fixed some windows appearing between screens. (#4797)
- 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: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
Expand Down
3 changes: 1 addition & 2 deletions src/singletons/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ void WindowManager::showAccountSelectPopup(QPoint point)

w->refresh();

QPoint buttonPos = point;
w->move(buttonPos.x() - 30, buttonPos.y());
w->moveTo(point - QPoint(30, 0), widgets::BoundsChecking::CursorPosition);
w->show();
w->setFocus();
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/InitUpdateButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void initUpdateButton(Button &button,
globalPoint.setX(0);
}

dialog->move(globalPoint);
dialog->moveTo(globalPoint, widgets::BoundsChecking::DesiredPosition);
dialog->show();
dialog->raise();

Expand Down
13 changes: 13 additions & 0 deletions src/util/WidgetHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ void moveWindowTo(QWidget *window, QPoint position, BoundsChecking mode)
}
}

void showAndMoveWindowTo(QWidget *window, QPoint position, BoundsChecking mode)
{
#ifdef Q_OS_WINDOWS
window->show();

moveWindowTo(window, position, mode);
#else
moveWindowTo(window, position, mode);

window->show();
#endif
}

} // namespace chatterino::widgets
10 changes: 10 additions & 0 deletions src/util/WidgetHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ enum class BoundsChecking {
void moveWindowTo(QWidget *window, QPoint position,
BoundsChecking mode = BoundsChecking::DesiredPosition);

/// Moves the `window` to the (global) `position`
/// while doing bounds-checking according to `mode` to ensure the window stays on one screen.
/// Will also call show on the `window`, order is dependant on platform.
///
/// @param window The window to move.
/// @param position The global position to move the window to.
/// @param mode The desired bounds checking.
void showAndMoveWindowTo(QWidget *window, QPoint position,
BoundsChecking mode = BoundsChecking::DesiredPosition);

} // namespace chatterino::widgets
11 changes: 11 additions & 0 deletions src/widgets/BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ void BaseWindow::moveTo(QPoint point, widgets::BoundsChecking mode)
widgets::moveWindowTo(this, point, mode);
}

void BaseWindow::showAndMoveTo(QPoint point, widgets::BoundsChecking mode)
{
widgets::showAndMoveWindowTo(this, point, mode);
}

void BaseWindow::resizeEvent(QResizeEvent *)
{
// Queue up save because: Window resized
Expand Down Expand Up @@ -559,6 +564,12 @@ void BaseWindow::closeEvent(QCloseEvent *)

void BaseWindow::showEvent(QShowEvent *)
{
#ifdef Q_OS_WIN
if (this->flags_.has(BoundsCheckOnShow))
{
this->moveTo(this->pos(), widgets::BoundsChecking::CursorPosition);
}
#endif
}

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
Expand Down
23 changes: 15 additions & 8 deletions src/widgets/BaseWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class BaseWindow : public BaseWidget
public:
enum Flags {
None = 0,
EnableCustomFrame = 1,
Frameless = 2,
TopMost = 4,
DisableCustomScaling = 8,
FramelessDraggable = 16,
DontFocus = 32,
Dialog = 64,
DisableLayoutSave = 128,
EnableCustomFrame = 1 << 0,
Frameless = 1 << 1,
TopMost = 1 << 2,
DisableCustomScaling = 1 << 3,
FramelessDraggable = 1 << 4,
DontFocus = 1 << 5,
Dialog = 1 << 6,
DisableLayoutSave = 1 << 7,
BoundsCheckOnShow = 1 << 8,
};

enum ActionOnFocusLoss { Nothing, Delete, Close, Hide };
Expand All @@ -57,6 +58,12 @@ class BaseWindow : public BaseWidget

void moveTo(QPoint point, widgets::BoundsChecking mode);

/**
* Moves the window to the given point and does bounds checking according to `mode`
* Depending on the platform, either the move or the show will take place first
**/
void showAndMoveTo(QPoint point, widgets::BoundsChecking mode);

float scale() const override;
float qtFontScale() const;

Expand Down
9 changes: 7 additions & 2 deletions src/widgets/dialogs/ColorPickerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
namespace chatterino {

ColorPickerDialog::ColorPickerDialog(const QColor &initial, QWidget *parent)
: BasePopup({BaseWindow::EnableCustomFrame, BaseWindow::DisableLayoutSave},
parent)
: BasePopup(
{
BaseWindow::EnableCustomFrame,
BaseWindow::DisableLayoutSave,
BaseWindow::BoundsCheckOnShow,
},
parent)
, color_()
, dialogConfirmed_(false)
{
Expand Down
8 changes: 6 additions & 2 deletions src/widgets/dialogs/QualityPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
namespace chatterino {

QualityPopup::QualityPopup(const QString &channelURL, QStringList options)
: BasePopup({BaseWindow::DisableLayoutSave},
static_cast<QWidget *>(&(getApp()->windows->getMainWindow())))
: BasePopup(
{
BaseWindow::DisableLayoutSave,
BaseWindow::BoundsCheckOnShow,
},
static_cast<QWidget *>(&(getApp()->windows->getMainWindow())))
, channelURL_(channelURL)
{
this->ui_.selector = new QComboBox(this);
Expand Down
11 changes: 8 additions & 3 deletions src/widgets/dialogs/SelectChannelDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@
namespace chatterino {

SelectChannelDialog::SelectChannelDialog(QWidget *parent)
: BaseWindow({BaseWindow::Flags::EnableCustomFrame,
BaseWindow::Flags::Dialog, BaseWindow::DisableLayoutSave},
parent)
: BaseWindow(
{
BaseWindow::Flags::EnableCustomFrame,
BaseWindow::Flags::Dialog,
BaseWindow::DisableLayoutSave,
BaseWindow::BoundsCheckOnShow,
},
parent)
, selectedChannel_(Channel::getEmpty())
{
this->setWindowTitle("Select a channel to join");
Expand Down
15 changes: 11 additions & 4 deletions src/widgets/dialogs/SettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "controllers/hotkeys/HotkeyController.hpp"
#include "singletons/Settings.hpp"
#include "util/LayoutCreator.hpp"
#include "widgets/BaseWindow.hpp"
#include "widgets/helper/Button.hpp"
#include "widgets/helper/SettingsDialogTab.hpp"
#include "widgets/settingspages/AboutPage.hpp"
Expand All @@ -29,9 +30,14 @@
namespace chatterino {

SettingsDialog::SettingsDialog(QWidget *parent)
: BaseWindow({BaseWindow::Flags::DisableCustomScaling,
BaseWindow::Flags::Dialog, BaseWindow::DisableLayoutSave},
parent)
: BaseWindow(
{
BaseWindow::Flags::DisableCustomScaling,
BaseWindow::Flags::Dialog,
BaseWindow::DisableLayoutSave,
BaseWindow::BoundsCheckOnShow,
},
parent)
{
this->setObjectName("SettingsDialog");
this->setWindowTitle("Chatterino Settings");
Expand Down Expand Up @@ -380,9 +386,10 @@ void SettingsDialog::themeChangedEvent()
this->setPalette(palette);
}

void SettingsDialog::showEvent(QShowEvent *)
void SettingsDialog::showEvent(QShowEvent *e)
{
this->ui_.search->setText("");
BaseWindow::showEvent(e);
}

///// Widget creation helpers
Expand Down
11 changes: 8 additions & 3 deletions src/widgets/dialogs/switcher/QuickSwitcherPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ QList<SplitContainer *> openPages(Window *window)
namespace chatterino {

QuickSwitcherPopup::QuickSwitcherPopup(Window *parent)
: BasePopup({BaseWindow::Flags::Frameless, BaseWindow::Flags::TopMost,
BaseWindow::DisableLayoutSave},
parent)
: BasePopup(
{
BaseWindow::Flags::Frameless,
BaseWindow::Flags::TopMost,
BaseWindow::DisableLayoutSave,
BaseWindow::BoundsCheckOnShow,
},
parent)
, switcherModel_(this)
, window(parent)
{
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2914,8 +2914,8 @@ void ChannelView::showReplyThreadPopup(const MessagePtr &message)
popup->setThread(message->replyThread);

QPoint offset(int(150 * this->scale()), int(70 * this->scale()));
popup->move(QCursor::pos() - offset);
popup->show();
popup->showAndMoveTo(QCursor::pos() - offset,
widgets::BoundsChecking::CursorPosition);
popup->giveFocus(Qt::MouseFocusReason);
}

Expand Down
10 changes: 8 additions & 2 deletions src/widgets/helper/SearchPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ ChannelPtr SearchPopup::filter(const QString &text, const QString &channelName,
}

SearchPopup::SearchPopup(QWidget *parent, Split *split)
: BasePopup({BaseWindow::DisableLayoutSave}, parent)
: BasePopup(
{
BaseWindow::DisableLayoutSave,
BaseWindow::BoundsCheckOnShow,
},
parent)
, split_(split)
{
this->initLayout();
Expand Down Expand Up @@ -180,9 +185,10 @@ void SearchPopup::updateWindowTitle()
this->setWindowTitle("Searching in " + historyName + " history");
}

void SearchPopup::showEvent(QShowEvent *)
void SearchPopup::showEvent(QShowEvent *e)
{
this->search();
BaseWindow::showEvent(e);
}

bool SearchPopup::eventFilter(QObject *object, QEvent *event)
Expand Down
10 changes: 7 additions & 3 deletions src/widgets/settingspages/AboutPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,13 @@ void AboutPage::addLicense(QFormLayout *form, const QString &name,
auto *b = new QLabel("<a href=\"" + licenseLink + "\">show license</a>");
QObject::connect(
b, &QLabel::linkActivated, [parent = this, name, licenseLink] {
auto window = new BasePopup({BaseWindow::Flags::EnableCustomFrame,
BaseWindow::DisableLayoutSave},
parent);
auto *window = new BasePopup(
{
BaseWindow::EnableCustomFrame,
BaseWindow::DisableLayoutSave,
BaseWindow::BoundsCheckOnShow,
},
parent);
window->setWindowTitle("Chatterino - License for " + name);
window->setAttribute(Qt::WA_DeleteOnClose);
auto layout = new QVBoxLayout();
Expand Down
5 changes: 2 additions & 3 deletions src/widgets/settingspages/FiltersPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ FiltersPage::FiltersPage()
});

// We can safely ignore this signal connection since we own the view
std::ignore = view->addButtonPressed.connect([] {
ChannelFilterEditorDialog d(
static_cast<QWidget *>(&(getApp()->windows->getMainWindow())));
std::ignore = view->addButtonPressed.connect([this] {
ChannelFilterEditorDialog d(this->window());
if (d.exec() == QDialog::Accepted)
{
getSettings()->filterRecords.append(
Expand Down
12 changes: 9 additions & 3 deletions src/widgets/splits/Split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,12 @@ namespace {
void showTutorialVideo(QWidget *parent, const QString &source,
const QString &title, const QString &description)
{
auto window =
new BasePopup(BaseWindow::Flags::EnableCustomFrame, parent);
auto *window = new BasePopup(
{
BaseWindow::EnableCustomFrame,
BaseWindow::BoundsCheckOnShow,
},
parent);
window->setWindowTitle("Chatterino - " + title);
window->setAttribute(Qt::WA_DeleteOnClose);
auto layout = new QVBoxLayout();
Expand Down Expand Up @@ -1393,7 +1397,9 @@ void Split::showChatterList()
multiWidget->setLayout(dockVbox);
chatterDock->setWidget(multiWidget);
chatterDock->setFloating(true);
chatterDock->show();
widgets::showAndMoveWindowTo(
chatterDock, this->mapToGlobal(QPoint{0, this->header_->height()}),
widgets::BoundsChecking::CursorPosition);
chatterDock->activateWindow();
}

Expand Down

0 comments on commit c4c9447

Please sign in to comment.