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

feat: add option to change the top-most status of a window #5135

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Minor: Added icons for newer versions of macOS. (#5148)
- Minor: Added the `--incognito/--no-incognito` options to the `/openurl` command, allowing you to override the "Open links in incognito/private mode" setting. (#5149)
- Minor: Added support for the `{input.text}` placeholder in the **Split** -> **Run a command** hotkey. (#5130)
- Minor: Added the ability to pin a popup window. (#5135)
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
- Bugfix: Fixed an issue where certain emojis did not send to Twitch chat correctly. (#4840)
- Bugfix: Fixed capitalized channel names in log inclusion list not being logged. (#4848)
- Bugfix: Trimmed custom streamlink paths on all platforms making sure you don't accidentally add spaces at the beginning or end of its path. (#4834)
Expand Down
71 changes: 48 additions & 23 deletions src/widgets/BaseWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "widgets/BaseWindow.hpp"

#include "Application.hpp"
#include "common/QLogging.hpp"
#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp"
Expand Down Expand Up @@ -207,37 +208,52 @@ void BaseWindow::init()
// }
#endif

#ifdef USEWINSDK
// fourtf: don't ask me why we need to delay this
if (!this->flags_.has(TopMost))
{
QTimer::singleShot(1, this, [this] {
getSettings()->windowTopMost.connect(
[this](bool topMost, auto) {
::SetWindowPos(HWND(this->winId()),
topMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0,
0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
},
this->connections_);
});
}
#else
// TopMost flag overrides setting
if (!this->flags_.has(TopMost))
{
getSettings()->windowTopMost.connect(
[this](bool topMost, auto) {
auto isVisible = this->isVisible();
this->setWindowFlag(Qt::WindowStaysOnTopHint, topMost);
if (isVisible)
{
this->show();
}
[this](bool topMost) {
this->setTopMost(topMost);
},
this->connections_);
}
}

void BaseWindow::setTopMost(bool topMost)
{
if (this->flags_.has(TopMost))
{
qCWarning(chatterinoWidget)
<< "Called setTopMost on a window with the `TopMost` flag set.";
return;
}

if (this->isTopMost_ == topMost)
{
return;
}

#ifdef USEWINSDK
::SetWindowPos(reinterpret_cast<HWND>(this->winId()),
topMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

#else
auto isVisible = this->isVisible();
this->setWindowFlag(Qt::WindowStaysOnTopHint, topMost);
if (isVisible)
{
this->show();
}
#endif

this->isTopMost_ = topMost;
this->topMostChanged(this->isTopMost_);
}

bool BaseWindow::isTopMost() const
{
return this->isTopMost_ || this->flags_.has(TopMost);
}

void BaseWindow::setActionOnFocusLoss(ActionOnFocusLoss value)
Expand Down Expand Up @@ -559,6 +575,15 @@ void BaseWindow::showEvent(QShowEvent *)
{
this->moveTo(this->pos(), widgets::BoundsChecking::CursorPosition);
}

if (!this->flags_.has(TopMost))
{
QTimer::singleShot(1, this, [this] {
::SetWindowPos(reinterpret_cast<HWND>(this->winId()),
this->isTopMost_ ? HWND_TOPMOST : HWND_NOTOPMOST, 0,
0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
});
}
#endif
}

Expand Down
11 changes: 11 additions & 0 deletions src/widgets/BaseWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ class BaseWindow : public BaseWidget
float scale() const override;
float qtFontScale() const;

/// @returns true if the window is the top-most window.
/// Either #setTopMost was called or the `TopMost` flag is set which overrides this
bool isTopMost() const;
/// Updates the window's top-most status
/// If the `TopMost` flag is set, this is a no-op
void setTopMost(bool topMost);

pajlada::Signals::NoArgSignal closing;

static bool supportsCustomWindowFrame();

signals:
void topMostChanged(bool topMost);

protected:
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool nativeEvent(const QByteArray &eventType, void *message,
Expand Down Expand Up @@ -131,6 +141,7 @@ class BaseWindow : public BaseWidget
FlagsEnum<Flags> flags_;
float nativeScale_ = 1;
bool isResizeFixing_ = false;
bool isTopMost_ = false;

struct {
QLayout *windowLayout = nullptr;
Expand Down
24 changes: 24 additions & 0 deletions src/widgets/Notebook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ Notebook::Notebook(QWidget *parent)
});
this->updateTabVisibilityMenuAction();

this->toggleTopMostAction_ = new QAction("Top most window", this);
this->toggleTopMostAction_->setCheckable(true);
auto *window = dynamic_cast<BaseWindow *>(this->window());
if (window)
{
auto updateTopMost = [this, window] {
this->toggleTopMostAction_->setChecked(window->isTopMost());
};
updateTopMost();
QObject::connect(this->toggleTopMostAction_, &QAction::triggered,
window, [window] {
window->setTopMost(!window->isTopMost());
});
QObject::connect(window, &BaseWindow::topMostChanged, this,
updateTopMost);
}
else
{
qCWarning(chatterinoApp)
<< "Notebook must be created within a BaseWindow";
}

this->addNotebookActionsToMenu(&this->menu_);

// Manually resize the add button so the initial paint uses the correct
Expand Down Expand Up @@ -1181,6 +1203,8 @@ void Notebook::addNotebookActionsToMenu(QMenu *menu)
menu->addAction(this->showTabsAction_);

menu->addAction(this->lockNotebookLayoutAction_);

menu->addAction(this->toggleTopMostAction_);
}

NotebookButton *Notebook::getAddButton()
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/Notebook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ class Notebook : public BaseWidget
NotebookTabLocation tabLocation_ = NotebookTabLocation::Top;
QAction *lockNotebookLayoutAction_;
QAction *showTabsAction_;
QAction *toggleTopMostAction_;

// This filter, if set, is used to figure out the visibility of
// the tabs in this notebook.
Expand Down Expand Up @@ -224,7 +225,6 @@ class SplitNotebook : public Notebook

// Main window on Windows has basically a duplicate of this in Window
NotebookButton *streamerModeIcon_{};

void updateStreamerModeIcon();
};

Expand Down
Loading