1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "VibrancyManager.h"
9 #import <objc/message.h>
11 #include "nsChildView.h"
13 using namespace mozilla;
15 @interface MOZVibrantView : NSVisualEffectView {
18 - (instancetype)initWithFrame:(NSRect)aRect
19 vibrancyType:(VibrancyType)aVibrancyType;
22 @interface MOZVibrantLeafView : MOZVibrantView
25 static NSVisualEffectState VisualEffectStateForVibrancyType(
28 case VibrancyType::TOOLTIP:
29 case VibrancyType::MENU:
30 // Tooltip and menu windows are never "key", so we need to tell the
31 // vibrancy effect to look active regardless of window state.
32 return NSVisualEffectStateActive;
34 return NSVisualEffectStateFollowsWindowActiveState;
38 static NSVisualEffectMaterial VisualEffectMaterialForVibrancyType(
39 VibrancyType aType, BOOL* aOutIsEmphasized) {
41 case VibrancyType::TOOLTIP:
42 return (NSVisualEffectMaterial)NSVisualEffectMaterialToolTip;
43 case VibrancyType::MENU:
44 return NSVisualEffectMaterialMenu;
48 @implementation MOZVibrantView
50 - (instancetype)initWithFrame:(NSRect)aRect vibrancyType:(VibrancyType)aType {
51 self = [super initWithFrame:aRect];
54 self.appearance = nil;
55 self.state = VisualEffectStateForVibrancyType(mType);
57 BOOL isEmphasized = NO;
58 self.material = VisualEffectMaterialForVibrancyType(mType, &isEmphasized);
59 self.emphasized = isEmphasized;
64 // Don't override allowsVibrancy here, because this view may have subviews, and
65 // returning YES from allowsVibrancy forces on foreground vibrancy for all
66 // descendant views, which can have unintended effects.
70 @implementation MOZVibrantLeafView
72 - (NSView*)hitTest:(NSPoint)aPoint {
73 // This view must be transparent to mouse events.
77 // MOZVibrantLeafView does not have subviews, so we can return YES here without
78 // having unintended effects on other contents of the window.
79 - (BOOL)allowsVibrancy {
85 bool VibrancyManager::UpdateVibrantRegion(
86 VibrancyType aType, const LayoutDeviceIntRegion& aRegion) {
87 if (aRegion.IsEmpty()) {
88 return mVibrantRegions.Remove(uint32_t(aType));
90 auto& vr = *mVibrantRegions.GetOrInsertNew(uint32_t(aType));
91 return vr.UpdateRegion(aRegion, mCoordinateConverter, mContainerView, ^() {
92 return this->CreateEffectView(aType);
96 LayoutDeviceIntRegion VibrancyManager::GetUnionOfVibrantRegions() const {
97 LayoutDeviceIntRegion result;
98 for (const auto& region : mVibrantRegions.Values()) {
99 result.OrWith(region->Region());
104 /* static */ NSView* VibrancyManager::CreateEffectView(VibrancyType aType,
106 return aIsContainer ? [[MOZVibrantView alloc] initWithFrame:NSZeroRect
108 : [[MOZVibrantLeafView alloc] initWithFrame:NSZeroRect