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 WindowManager::getLastSelectedWindow() to replace getMainWindow() #4816

Merged
merged 4 commits into from
Sep 16, 2023
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.
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 @@ -18,6 +18,7 @@
- Dev: Fix clang-tidy `cppcoreguidelines-pro-type-member-init` warnings. (#4426)
- Dev: Immediate layout for invisible `ChannelView`s is skipped. (#4811)
- Dev: Refactor `Image` & Image's `Frames`. (#4773)
- Dev: Add `WindowManager::getLastSelectedWindow()` to replace `getMainWindow()`. (#4816)
- Dev: Clarify signal connection lifetimes where applicable. (#4818)

## 2.4.5
Expand Down
8 changes: 6 additions & 2 deletions src/singletons/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,15 @@ Window &WindowManager::getMainWindow()
return *this->mainWindow_;
}

Window &WindowManager::getSelectedWindow()
Window *WindowManager::getLastSelectedWindow() const
{
assertInGuiThread();
if (this->selectedWindow_ == nullptr)
{
return this->mainWindow_;
}

return *this->selectedWindow_;
return this->selectedWindow_;
}

Window &WindowManager::createWindow(WindowType type, bool show, QWidget *parent)
Expand Down
11 changes: 10 additions & 1 deletion src/singletons/WindowManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ class WindowManager final : public Singleton
void repaintGifEmotes();

Window &getMainWindow();
Window &getSelectedWindow();

// Returns a pointer to the last selected window.
// Edge cases:
// - If the application was not focused since the start, this will return a pointer to the main window.
// - If the window was closed this points to the main window.
// - If the window was unfocused since being selected, this function will still return it.
Window *getLastSelectedWindow() const;

Window &createWindow(WindowType type, bool show = true,
QWidget *parent = nullptr);

Expand Down Expand Up @@ -153,6 +160,8 @@ class WindowManager final : public Singleton

QTimer *saveTimer;
QTimer miscUpdateTimer_;

friend class Window; // this is for selectedWindow_
};

} // namespace chatterino
9 changes: 8 additions & 1 deletion src/widgets/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ bool Window::event(QEvent *event)
{
switch (event->type())
{
case QEvent::WindowActivate:
case QEvent::WindowActivate: {
getApp()->windows->selectedWindow_ = this;
break;
}

case QEvent::WindowDeactivate: {
auto page = this->notebook_->getOrAddSelectedPage();
Expand Down Expand Up @@ -142,6 +144,11 @@ void Window::closeEvent(QCloseEvent *)
app->windows->closeAll();
}

// Ensure selectedWindow_ is never an invalid pointer.
// WindowManager will return the main window if no window is pointed to by
// `selectedWindow_`.
getApp()->windows->selectedWindow_ = nullptr;

this->closed.invoke();

if (this->type_ == WindowType::Main)
Expand Down