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

MacOS handles window opacity better #7928

Closed
wants to merge 1 commit into from

Conversation

sfsam
Copy link

@sfsam sfsam commented Apr 23, 2024

On MacOS, when the window is configured to be opaque, draw an opaque title bar and proper window border.
This is achieved by giving the window the standard background color when it is opaque.

The image below shows the current translucent drawing for an opaque window versus this pull request's opaque drawing.
alacritty

Window has opaque title bar and real border when
config.window_opacity == 1.
Copy link
Member

@chrisduerr chrisduerr left a comment

Choose a reason for hiding this comment

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

If you're looking for this functionality, it should be added to Winit.

@chrisduerr chrisduerr closed this Apr 23, 2024
@kchibisov
Copy link
Member

IIRC we rejected such thing upstream because it was too specific and you can do it very easy downstream. it's also a bit opinionated since you can change the color pretty much arbitrary, so you need to define coloring APIs, etc.

Maybe in the future it'll be a bit easier to manipulate things like that because the macOS stuff will be exposed directly, but for now I don't mind such change. Though, I'm not sure whether it's how the rest of the apps behave by default, since macOS tends to blend background color into decorations and what not.

@chrisduerr
Copy link
Member

it's also a bit opinionated since you can change the color pretty much arbitrary, so you need to define coloring APIs, etc.

Is it not a standard thing to have transparent for transparent windows? I don't know how it actually works. If there's no such standard then I'd prefer just always keeping Alacritty's decorations opaque.

@chrisduerr chrisduerr reopened this Apr 23, 2024
@sfsam
Copy link
Author

sfsam commented Apr 23, 2024

FWIW, I didn't intend for this PR to be opinionated. It is applying MacOS's windowBackgroundColor only if the window is opaque. This makes MacOS draw the window with proper borders and opaque title bar. You can still set Alacritty's opacity config option to get transparency. In fact, if you set opacity to 0.9999, this PR will draw much like current Alacritty. That said, maybe this functionality belongs in winit.

MacOS windowBackgroundColor isn't a specific color. It is computed by the OS depending on a number of factors (dark mode, accessibility, etc).
https://developer.apple.com/documentation/appkit/nscolor/1528630-windowbackgroundcolor?language=objc

Here are some apps on a black background with Dark Mode on to illustrate how the windows are drawn:

pr

@kchibisov
Copy link
Member

Is it not a standard thing to have transparent for transparent windows? I don't know how it actually works. If there's no such standard then I'd prefer just always keeping Alacritty's decorations opaque.

You can set arbitrary color for decorations. If winit will do so it could conflict with users doing so as well, since you can set a color with alpha iirc. Maybe winit should do that by default, but it's hard to understand what the default is...

We could add it here and then try to upstream in a sane way, but I know that e.g. neovide uses this in a different manner with a custom color, etc...

@nixpulvis
Copy link
Contributor

I might be misreading the effect of this PR, but it seems to be mostly setting things to a reasonable default for when there’s no transparency or custom colors desired.

Seems like a great addition to winit without all that much macOS bullshit. Why would other applications like Alacritty not want this is the way I’d think about it.

Just my two cents.

@kchibisov
Copy link
Member

Seems like a great addition to winit without all that much macOS bullshit. Why would other applications like Alacritty not want this is the way I’d think about it.

The point is that other applications sometimes change this color on their own, so winit can't do that implicitly by default, it must be explicit, and making it explicit is a bit cumbersome because defining colors, etc sounds a bit out of place for winit.

We could re-iterate it upstream, ofc, but I'm not sure in the current state things will be added upstream.

@madsmtm
Copy link

madsmtm commented Apr 26, 2024

I think the missing border is due to a call to set_has_shadow, which is done by Alacritty when the window's opacity is turned down.

Regarding transparency, maybe the problem is rather that Winit sets the background color to NSColor::clearColor() when initializing with transparency? If we didn't do that, then the window title is opaque (like on most other applications, and which generally seems to be the default) regardless of whether the window contents is opaque.

And then users can opt-in to a transparent window title bar using some other method, either the manual approach here, or using some API we provide in Winit?

@kchibisov
Copy link
Member

we call set_has_shadow(true) once you change the opacity to 1.0, so it should be basically as default? Or does the order matter here.

@sfsam
Copy link
Author

sfsam commented Apr 27, 2024

If you create a new Mac app in Xcode, it will automatically create an NSWindow for you with the backgroundColor property set to NSColor.windowBackgroundColor. This is the default behavior. This window draws the way this PR does. If you later explicitly change the window's backgroundColor to NSColor.clearColor, the window draws like current Alacritty.

@madsmtm
Copy link

madsmtm commented Apr 27, 2024

I've opened rust-windowing/winit#3657 to correct the behaviour in Winit - after that, the default background color is always +[NSColor windowBackgroundColor].

The rejected PR for setting the background color is rust-windowing/winit#2314, btw. Still not that against it.

@kchibisov
Copy link
Member

Is this patch still needed after this #7935 ?

@sfsam
Copy link
Author

sfsam commented May 5, 2024

After #7935, Alacritty still doesn't draw window borders. What's more, it also doesn't respect the config option[window] decorations_theme_variant = "Dark". Instead, it draws a white title bar.

I tried applying this PR to #7935, but it has a conflict: WindowBuilderExtMacOS was changed to WindowAttributesExtMacOS. If I make that change and apply the rest of this PR, it now has no effect. The title bar is still white and there are no borders.

@nixpulvis
Copy link
Contributor

IDK, it seems to still be drawing a dark title bar, but no border as desired. I didn't follow the PRs in winit, so I'm not sure what I expect at the moment, but I think agree with the intentions of this PR at least.

@kchibisov
Copy link
Member

It should draw border though.

@nixpulvis
Copy link
Contributor

@kchibisov seems like this PR is still functional. In fact it seems to improve the performance of the app switcher (Expose) since the window is now longer partially transparent. This was an outstanding issue for a while and may actually be a partial fix for #4474.

@nixpulvis
Copy link
Contributor

nixpulvis commented May 6, 2024

Here's a video showing the performance difference of Mission Control.

master: master

mac_transparency (this branch): branch

@nixpulvis
Copy link
Contributor

nixpulvis commented May 6, 2024

Also, here's the old/new behavior when dragging Alacritty windows between spaces.

master: master

mac_transparency (this branch): branch

@nixpulvis
Copy link
Contributor

@kchibisov leave me a day to recollect myself, I may have tested this wrong. Now when I compile master it seems the title bar is not transparent as intended. It's entirely possible I did something wrong this morning while I was testing a few different branches.

@kchibisov
Copy link
Member

In general, I'll re-look into this myself. Though, I'm limited to macOS 12.6 iirc.

@LeileiChui
Copy link

@kchibisov leave me a day to recollect myself, I may have tested this wrong. Now when I compile master it seems the title bar is not transparent as intended. It's entirely possible I did something wrong this morning while I was testing a few different branches.给我一天时间回忆一下,我可能测试错了。现在,当我编译 master 时,标题栏似乎不按预期透明。今天早上我在测试几个不同的分支时完全有可能做错了什么。

After

commit 48c088a50c977dfaf2a5a7052a5ddd39071e6063
Author: Kirill Chibisov <[email protected]>
Date:   2024-05-04 20:51:56

    Bump winit to 0.30.0

alacritty's transparency seems to be broken.

@gersonsosa
Copy link

alacritty's transparency seems to be broken.

I can confirm this, I noticed a few days ago, I don't see any issue in winit, not sure how to know whether this is an alacritty or winit issue.

@madsmtm
Copy link

madsmtm commented May 10, 2024

I've posted #7965 which extends upon this one with a few further fixes.

@chrisduerr
Copy link
Member

Closing in favor of #7965.

@chrisduerr chrisduerr closed this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants