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

Conversation

Mm2PL
Copy link
Collaborator

@Mm2PL Mm2PL commented Sep 14, 2023

Description

This is needed to fix /clearmessages. I will replace all usages of getMainWindow() in another PR.

Here is a simple fix for /clearmesages which can be used to test this.

Fixes #3353. Required to finally close issue #2406.

Patch
diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp
index 9ce784ba..68eb2939 100644
--- a/src/controllers/commands/CommandController.cpp
+++ b/src/controllers/commands/CommandController.cpp
@@ -1377,18 +1377,25 @@ void CommandController::initialize(Settings &, Paths &paths)
         return "";
     });
 
-    this->registerCommand("/clearmessages", [](const auto & /*words*/,
-                                               ChannelPtr channel) {
-        auto *currentPage = dynamic_cast<SplitContainer *>(
-            getApp()->windows->getMainWindow().getNotebook().getSelectedPage());
+    this->registerCommand(
+        "/clearmessages",
+        [](const auto & /*words*/, ChannelPtr channel) -> QString {
+            auto *win = getApp()->windows->getLastSelectedWindow();
+            if (win == nullptr)
+            {
+                return "";
+            }
 
-        if (auto split = currentPage->getSelectedSplit())
-        {
-            split->getChannelView().clearMessages();
-        }
+            auto *currentPage = dynamic_cast<SplitContainer *>(
+                win->getNotebook().getSelectedPage());
 
-        return "";
-    });
+            if (auto *split = currentPage->getSelectedSplit())
+            {
+                split->getChannelView().clearMessages();
+            }
+
+            return "";
+        });
 
     this->registerCommand("/settitle", [](const QStringList &words,
                                           ChannelPtr channel) {

@Mm2PL Mm2PL requested a review from pajlada September 14, 2023 21:43
@Mm2PL
Copy link
Collaborator Author

Mm2PL commented Sep 16, 2023

I will be making changes related to closing windows to this pr soon. I just can't do them now

@pajlada
Copy link
Member

pajlada commented Sep 16, 2023

I will be making changes related to closing windows to this pr soon. I just can't do them now

Sounds good, I will look into some of the questionable usages of getMainWIndow/getLastSelectedWindow

  1. No longer can return a nullptr if no window was ever focused
  2. When closing a window, it will no longer return an invalid pointer
Copy link
Member

@pajlada pajlada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for me - each use of the function should be weighed with possible alternatives

@pajlada pajlada enabled auto-merge (squash) September 16, 2023 20:14
@pajlada pajlada merged commit d752ce8 into master Sep 16, 2023
17 checks passed
@pajlada pajlada deleted the fix/internal/WindowManager--getSelectedWindow branch September 16, 2023 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

/clearmessages does not persist into popout windows
2 participants