feat: add optional animation parameter to BrowserWindow.setVibrancy (#35987)

adds optional animation parameter to BrowserWindow.setVibrancy
This commit is contained in:
Gellert Hegyi 2024-11-12 18:03:30 +01:00 committed by GitHub
parent a6390b539c
commit 7a79d4c96e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 66 additions and 19 deletions

View file

@ -1344,22 +1344,44 @@ void NativeWindowMac::UpdateWindowOriginalFrame() {
original_frame_ = [window_ frame];
}
void NativeWindowMac::SetVibrancy(const std::string& type) {
NativeWindow::SetVibrancy(type);
void NativeWindowMac::SetVibrancy(const std::string& type, int duration) {
NativeWindow::SetVibrancy(type, duration);
NSVisualEffectView* vibrantView = [window_ vibrantView];
views::View* rootView = GetContentsView();
bool animate = duration > 0;
if (type.empty()) {
if (vibrant_native_view_host_ != nullptr) {
// Transfers ownership back to caller in the form of a unique_ptr which is
// subsequently deleted.
rootView->RemoveChildViewT(vibrant_native_view_host_);
vibrant_native_view_host_ = nullptr;
}
vibrancy_type_ = type;
if (vibrantView != nil) {
[window_ setVibrantView:nil];
auto cleanupHandler = ^{
if (vibrant_native_view_host_ != nullptr) {
// Transfers ownership back to caller in the form of a unique_ptr which
// is subsequently deleted.
rootView->RemoveChildViewT(vibrant_native_view_host_);
vibrant_native_view_host_ = nullptr;
}
if (vibrantView != nil) {
[window_ setVibrantView:nil];
}
};
if (animate) {
__weak ElectronNSWindowDelegate* weak_delegate = window_delegate_;
[NSAnimationContext
runAnimationGroup:^(NSAnimationContext* context) {
context.duration = duration / 1000.0f;
vibrantView.animator.alphaValue = 0.0;
}
completionHandler:^{
if (!weak_delegate)
return;
cleanupHandler();
}];
} else {
cleanupHandler();
}
return;
@ -1427,6 +1449,16 @@ void NativeWindowMac::SetVibrancy(const std::string& type) {
UpdateVibrancyRadii(IsFullscreen());
}
if (animate) {
[vibrantView setAlphaValue:0.0];
[NSAnimationContext
runAnimationGroup:^(NSAnimationContext* context) {
context.duration = duration / 1000.0f;
vibrantView.animator.alphaValue = 1.0;
}
completionHandler:nil];
}
[vibrantView setMaterial:vibrancyType];
}
}