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

perf: hide main window from alt tab program switcher #3241

Merged
merged 3 commits into from
Feb 15, 2025

Conversation

ZGGSONG
Copy link
Contributor

@ZGGSONG ZGGSONG commented Feb 14, 2025

As a tool, used as needed and simply come away, one indeed need not utilize Alt-Tab to switch pages, which also affects user usage of Alt-Tab for switching between applications.

This issue was also raised in issue #2356.

This comment has been minimized.

Copy link

gitstream-cm bot commented Feb 14, 2025

Be a legend 🏆 by adding a before and after screenshot of the changes you made, especially if they are around UI/UX.

Copy link
Contributor

coderabbitai bot commented Feb 14, 2025

📝 Walkthrough

Walkthrough

The pull request introduces enhancements for managing window visibility in the Alt+Tab interface. In the WindowsInteropHelper class, a new region is added with methods: HideFromAltTab, ShowInAltTab, and GetCurrentWindowStyle to control window styles. Additionally, the MainWindow.xaml file now includes a SourceInitialized event handler, and the corresponding code-behind in MainWindow.xaml.cs implements the OnSourceInitialized method that calls HideFromAltTab to modify the window’s appearance during startup.

Changes

File(s) Change Summary
Flow.Launcher/Helper/WindowsInteropHelper.cs Added new "Alt Tab" region with methods (HideFromAltTab, ShowInAltTab, GetCurrentWindowStyle) for managing window visibility in Alt+Tab.
Flow.Launcher/MainWindow.xaml & Flow.Launcher/MainWindow.xaml.cs Added the SourceInitialized event handler in XAML and implemented the OnSourceInitialized method that calls HideFromAltTab in code-behind.
Flow.Launcher/NativeMethods.txt Added constants (WM_EXITSIZEMOVE, WINDOW_EX_STYLE) and function (SetLastError); removed WM_EXITSIZEMOVE from exports.

Possibly related PRs

Suggested labels

bug, 1 min review

Suggested reviewers

  • jjw24
  • taooceros

Poem

I'm a little rabbit, coding through the night,
Hopping on keys, making windows light.
Alt+Tab now hides with a seamless trick,
My code dances like ASCII, so quick!
Bounding over bugs with a joyful flick!
🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
Flow.Launcher/Helper/WindowsInteropHelper.cs (1)

184-223: Add error handling for window style operations.

While the implementation is correct, consider adding error handling for failed window style operations. The SetWindowLong API can fail, and it's important to handle such cases gracefully.

 public static void HideFromAltTab(Window window)
 {
     var helper = new WindowInteropHelper(window);
     var exStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
+    if (exStyle == 0)
+    {
+        throw new Win32Exception(Marshal.GetLastPInvokeError());
+    }

     // Add TOOLWINDOW style, remove APPWINDOW style
     exStyle = (exStyle | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW;

-    SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
+    var result = SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
+    if (result == IntPtr.Zero)
+    {
+        throw new Win32Exception(Marshal.GetLastPInvokeError());
+    }
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 502129d and 5313229.

📒 Files selected for processing (3)
  • Flow.Launcher/Helper/WindowsInteropHelper.cs (1 hunks)
  • Flow.Launcher/MainWindow.xaml (1 hunks)
  • Flow.Launcher/MainWindow.xaml.cs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: gitStream.cm
🔇 Additional comments (4)
Flow.Launcher/Helper/WindowsInteropHelper.cs (2)

152-169: LGTM! Constants and PInvoke declarations are well-defined.

The constants and PInvoke declarations are correctly implemented with proper values and attributes.


170-182: LGTM! Architecture-aware implementation.

The SetWindowLong helper method correctly handles both 32-bit and 64-bit architectures with proper error handling.

Flow.Launcher/MainWindow.xaml (1)

23-23: LGTM! Event handler is properly integrated.

The SourceInitialized event handler is correctly added and follows the existing pattern of window event handlers.

Flow.Launcher/MainWindow.xaml.cs (1)

174-177: LGTM! Event handler is correctly implemented.

The OnSourceInitialized event handler is properly implemented to hide the window from Alt+Tab during initialization.

@ZGGSONG ZGGSONG changed the title perf: hide main window from alt tab program switcher #2356 perf: hide main window from alt tab program switcher Feb 14, 2025
@Jack251970 Jack251970 linked an issue Feb 14, 2025 that may be closed by this pull request
2 tasks

This comment has been minimized.

@Jack251970
Copy link
Contributor

LGTM. And I have improved code quality with CSWin32.

Jack251970
Jack251970 previously approved these changes Feb 14, 2025
Copy link

gitstream-cm bot commented Feb 14, 2025

🥷 Code experts: Jack251970, onesounds

Jack251970 has most 👩‍💻 activity in the files.
Jack251970, onesounds have most 🧠 knowledge in the files.

See details

Flow.Launcher/Helper/WindowsInteropHelper.cs

Activity based on git-commit:

Jack251970
FEB 31 additions & 46 deletions
JAN
DEC 32 additions & 59 deletions
NOV
OCT
SEP

Knowledge based on git-blame:
Jack251970: 22%

Flow.Launcher/MainWindow.xaml

Activity based on git-commit:

Jack251970
FEB 1 additions & 1 deletions
JAN
DEC
NOV 1 additions & 1 deletions
OCT
SEP

Knowledge based on git-blame:
onesounds: 90%

Flow.Launcher/MainWindow.xaml.cs

Activity based on git-commit:

Jack251970
FEB
JAN
DEC 5 additions & 10 deletions
NOV
OCT
SEP

Knowledge based on git-blame:
onesounds: 33%
Jack251970: 1%

Flow.Launcher/NativeMethods.txt

Activity based on git-commit:

Jack251970
FEB 4 additions & 1 deletions
JAN
DEC 18 additions & 1 deletions
NOV
OCT
SEP

Knowledge based on git-blame:
Jack251970: 100%

To learn more about /:\ gitStream - Visit our Docs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (1)
Flow.Launcher/Helper/WindowsInteropHelper.cs (1)

181-194: 🛠️ Refactor suggestion

Add error handling for SetWindowLong failure.

Similar to HideFromAltTab, this method should handle potential failures when modifying window styles.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5313229 and 829dbaa.

📒 Files selected for processing (2)
  • Flow.Launcher/Helper/WindowsInteropHelper.cs (1 hunks)
  • Flow.Launcher/NativeMethods.txt (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm
🔇 Additional comments (3)
Flow.Launcher/Helper/WindowsInteropHelper.cs (2)

154-160: LGTM! Good error handling practice.

The method properly clears any existing error before calling SetWindowLong, following best practices for P/Invoke error handling.


161-164: Verify potential data loss on 64-bit systems.

The unchecked conversion from IntPtr to Int32 could potentially lose data on 64-bit systems if the pointer value exceeds Int32.MaxValue.

Consider adding a check to ensure the value fits within Int32 range:

 private static int IntPtrToInt32(IntPtr intPtr)
 {
+    var int64Value = intPtr.ToInt64();
+    if (int64Value > int.MaxValue || int64Value < int.MinValue)
+    {
+        throw new OverflowException("IntPtr value is outside the range of Int32");
+    }
     return unchecked((int)intPtr.ToInt64());
 }
Flow.Launcher/NativeMethods.txt (1)

17-20: LGTM! Required constants and functions are properly declared.

The added constants and functions align with the implementation in WindowsInteropHelper.cs.

Copy link

@check-spelling-bot Report

🔴 Please review

See the 📂 files view, the 📜action log, or 📝 job summary for details.

❌ Errors Count
❌ forbidden-pattern 26
⚠️ ignored-expect-variant 1
⚠️ non-alpha-in-dictionary 19

See ❌ Event descriptions for more information.

Forbidden patterns 🙅 (1)

In order to address this, you could change the content to not match the forbidden patterns (comments before forbidden patterns may help explain why they're forbidden), add patterns for acceptable instances, or adjust the forbidden patterns themselves.

These forbidden patterns matched content:

s.b. workaround(s)

\bwork[- ]arounds?\b
If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
Flow.Launcher/Helper/WindowsInteropHelper.cs (4)

171-179: Add parameter validation.

The method should validate the input parameter to prevent potential null reference exceptions.

Apply this diff to add parameter validation:

 public static void HideFromAltTab(Window window)
 {
+    if (window == null)
+    {
+        throw new ArgumentNullException(nameof(window));
+    }
+
     var exStyle = GetCurrentWindowStyle(window);

     // Add TOOLWINDOW style, remove APPWINDOW style
     var newExStyle = ((uint)exStyle | (uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) & ~(uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;

     SetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
 }

185-193: Add parameter validation.

The method should validate the input parameter to prevent potential null reference exceptions.

Apply this diff to add parameter validation:

 public static void ShowInAltTab(Window window)
 {
+    if (window == null)
+    {
+        throw new ArgumentNullException(nameof(window));
+    }
+
     var exStyle = GetCurrentWindowStyle(window);

     // Remove the TOOLWINDOW style and add the APPWINDOW style.
     var newExStyle = ((uint)exStyle & ~(uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) | (uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;

     SetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
 }

200-208: Add parameter validation.

The method should validate the input parameter to prevent potential null reference exceptions.

Apply this diff to add parameter validation:

 private static int GetCurrentWindowStyle(Window window)
 {
+    if (window == null)
+    {
+        throw new ArgumentNullException(nameof(window));
+    }
+
     var style = PInvoke.GetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
     if (style == 0 && Marshal.GetLastPInvokeError() != 0)
     {
         throw new Win32Exception(Marshal.GetLastPInvokeError());
     }
     return style;
 }

152-210: Enhance documentation and add unit tests.

The implementation is solid, but consider the following improvements:

  1. Document potential exceptions in the XML comments.
  2. Add unit tests to verify the behavior of these methods.

Would you like me to:

  1. Generate enhanced XML documentation with exception details?
  2. Create unit tests for these methods?
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 829dbaa and dc07e76.

📒 Files selected for processing (1)
  • Flow.Launcher/Helper/WindowsInteropHelper.cs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm
  • GitHub Check: gitStream.cm
🔇 Additional comments (1)
Flow.Launcher/Helper/WindowsInteropHelper.cs (1)

154-165: LGTM! Robust error handling implementation.

The method follows Win32 API best practices by clearing any existing error before making the call and properly checking both the return value and the last error code.

@VictoriousRaptor VictoriousRaptor merged commit 0893134 into Flow-Launcher:dev Feb 15, 2025
7 checks passed
@jjw24 jjw24 added bug Something isn't working and removed 5 min review labels Feb 23, 2025
@jjw24 jjw24 added this to the 1.20.0 milestone Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Win11上使用alt+tab时遇到的一些问题
4 participants