Bug 1880227 - Migrate Focus docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / widget / cocoa / VibrancyManager.mm
blobe9cfcc3be04d2000c46a0f68a0de104e4ddc48d7
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 {
16   VibrancyType mType;
18 - (instancetype)initWithFrame:(NSRect)aRect
19                  vibrancyType:(VibrancyType)aVibrancyType;
20 @end
22 @interface MOZVibrantLeafView : MOZVibrantView
23 @end
25 static NSVisualEffectState VisualEffectStateForVibrancyType(
26     VibrancyType aType) {
27   switch (aType) {
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;
33     default:
34       return NSVisualEffectStateFollowsWindowActiveState;
35   }
38 static NSVisualEffectMaterial VisualEffectMaterialForVibrancyType(
39     VibrancyType aType, BOOL* aOutIsEmphasized) {
40   switch (aType) {
41     case VibrancyType::TOOLTIP:
42       return (NSVisualEffectMaterial)NSVisualEffectMaterialToolTip;
43     case VibrancyType::MENU:
44       return NSVisualEffectMaterialMenu;
45   }
48 @implementation MOZVibrantView
50 - (instancetype)initWithFrame:(NSRect)aRect vibrancyType:(VibrancyType)aType {
51   self = [super initWithFrame:aRect];
52   mType = aType;
54   self.appearance = nil;
55   self.state = VisualEffectStateForVibrancyType(mType);
57   BOOL isEmphasized = NO;
58   self.material = VisualEffectMaterialForVibrancyType(mType, &isEmphasized);
59   self.emphasized = isEmphasized;
61   return self;
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.
68 @end
70 @implementation MOZVibrantLeafView
72 - (NSView*)hitTest:(NSPoint)aPoint {
73   // This view must be transparent to mouse events.
74   return nil;
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 {
80   return NO;
83 @end
85 bool VibrancyManager::UpdateVibrantRegion(
86     VibrancyType aType, const LayoutDeviceIntRegion& aRegion) {
87   if (aRegion.IsEmpty()) {
88     return mVibrantRegions.Remove(uint32_t(aType));
89   }
90   auto& vr = *mVibrantRegions.GetOrInsertNew(uint32_t(aType));
91   return vr.UpdateRegion(aRegion, mCoordinateConverter, mContainerView, ^() {
92     return this->CreateEffectView(aType);
93   });
96 LayoutDeviceIntRegion VibrancyManager::GetUnionOfVibrantRegions() const {
97   LayoutDeviceIntRegion result;
98   for (const auto& region : mVibrantRegions.Values()) {
99     result.OrWith(region->Region());
100   }
101   return result;
104 /* static */ NSView* VibrancyManager::CreateEffectView(VibrancyType aType,
105                                                        BOOL aIsContainer) {
106   return aIsContainer ? [[MOZVibrantView alloc] initWithFrame:NSZeroRect
107                                                  vibrancyType:aType]
108                       : [[MOZVibrantLeafView alloc] initWithFrame:NSZeroRect
109                                                      vibrancyType:aType];