Files
tubestation/widget/cocoa/VibrancyManager.mm
Emilio Cobos Álvarez 246a83fa36 Bug 1919165 - Get rid of nsChildView. r=mac-reviewers,mstange
In a follow-up I want to merge nsChildView.mm into nsCocoaWindow.mm, but
I thought I'd send this for feedback.

The most relevant changes:

 * Move members from nsChildView to nsCocoaWindow.
 * Manually position the ChildView* when custom titlebar is changed.
 * Deal with attaching directly to top level widget
   (mAttachedWidgetListener) like other platforms do.
 * On methods that are implemented in both nsChildView and
   nsCocoaWindow, look into which implementation we need (if one
   forwards to the other, pick the non-trivial one).
 * Some fix ups for code that lived in nsChildView that now needs to
   account for a client offset if there's a titlebar.

Differential Revision: https://phabricator.services.mozilla.com/D245604
2025-04-29 13:51:02 +00:00

134 lines
3.9 KiB
Plaintext

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "VibrancyManager.h"
#include "ViewRegion.h"
#include "nsRegion.h"
#include "ViewRegion.h"
#import <objc/message.h>
#include "nsCocoaWindow.h"
#include "mozilla/StaticPrefs_widget.h"
using namespace mozilla;
@interface MOZVibrantView : NSVisualEffectView {
VibrancyType mType;
}
- (instancetype)initWithFrame:(NSRect)aRect
vibrancyType:(VibrancyType)aVibrancyType;
- (void)prefChanged;
@end
static NSVisualEffectState VisualEffectStateForVibrancyType(
VibrancyType aType) {
switch (aType) {
case VibrancyType::Titlebar:
case VibrancyType::Sidebar:
break;
}
return NSVisualEffectStateFollowsWindowActiveState;
}
static NSVisualEffectMaterial VisualEffectMaterialForVibrancyType(
VibrancyType aType) {
switch (aType) {
case VibrancyType::Sidebar:
return NSVisualEffectMaterialSidebar;
case VibrancyType::Titlebar:
return NSVisualEffectMaterialTitlebar;
}
}
static NSVisualEffectBlendingMode VisualEffectBlendingModeForVibrancyType(
VibrancyType aType) {
switch (aType) {
case VibrancyType::Sidebar:
return StaticPrefs::widget_macos_sidebar_blend_mode_behind_window()
? NSVisualEffectBlendingModeBehindWindow
: NSVisualEffectBlendingModeWithinWindow;
case VibrancyType::Titlebar:
return StaticPrefs::widget_macos_titlebar_blend_mode_behind_window()
? NSVisualEffectBlendingModeBehindWindow
: NSVisualEffectBlendingModeWithinWindow;
}
}
@implementation MOZVibrantView
- (instancetype)initWithFrame:(NSRect)aRect vibrancyType:(VibrancyType)aType {
self = [super initWithFrame:aRect];
mType = aType;
self.appearance = nil;
self.state = VisualEffectStateForVibrancyType(mType);
self.material = VisualEffectMaterialForVibrancyType(mType);
self.blendingMode = VisualEffectBlendingModeForVibrancyType(mType);
self.emphasized = NO;
return self;
}
- (NSView*)hitTest:(NSPoint)aPoint {
// This view must be transparent to mouse events.
return nil;
}
- (void)prefChanged {
self.blendingMode = VisualEffectBlendingModeForVibrancyType(mType);
}
@end
static void PrefChanged(const char* aPref, void* aClosure) {
static_cast<VibrancyManager*>(aClosure)->PrefChanged();
}
static constexpr nsLiteralCString kObservedPrefs[] = {
"widget.macos.sidebar-blend-mode.behind-window"_ns,
"widget.macos.titlebar-blend-mode.behind-window"_ns,
};
VibrancyManager::VibrancyManager(const nsCocoaWindow& aCoordinateConverter,
NSView* aContainerView)
: mCoordinateConverter(aCoordinateConverter),
mContainerView(aContainerView) {
for (const auto& pref : kObservedPrefs) {
Preferences::RegisterCallback(::PrefChanged, pref, this);
}
}
VibrancyManager::~VibrancyManager() {
for (const auto& pref : kObservedPrefs) {
Preferences::UnregisterCallback(::PrefChanged, pref, this);
}
}
void VibrancyManager::PrefChanged() {
for (auto& region : mVibrantRegions) {
if (!region) {
continue;
}
for (NSView* view : region->Views()) {
[static_cast<MOZVibrantView*>(view) prefChanged];
}
}
}
bool VibrancyManager::UpdateVibrantRegion(
VibrancyType aType, const LayoutDeviceIntRegion& aRegion) {
auto& slot = mVibrantRegions[aType];
if (aRegion.IsEmpty()) {
bool hadRegion = !!slot;
slot = nullptr;
return hadRegion;
}
if (!slot) {
slot = MakeUnique<ViewRegion>();
}
return slot->UpdateRegion(aRegion, mCoordinateConverter, mContainerView, ^() {
return [[MOZVibrantView alloc] initWithFrame:NSZeroRect vibrancyType:aType];
});
}