Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / widget / cocoa / VibrancyManager.mm
bloba2bef29a19e25a099ae8f0670ad08323dca9852b
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"
8 #include "ViewRegion.h"
9 #include "nsRegion.h"
10 #include "ViewRegion.h"
12 #import <objc/message.h>
14 #include "nsChildView.h"
15 #include "mozilla/StaticPrefs_widget.h"
17 using namespace mozilla;
19 @interface MOZVibrantView : NSVisualEffectView {
20   VibrancyType mType;
22 - (instancetype)initWithFrame:(NSRect)aRect
23                  vibrancyType:(VibrancyType)aVibrancyType;
24 @end
26 static NSVisualEffectState VisualEffectStateForVibrancyType(
27     VibrancyType aType) {
28   switch (aType) {
29     case VibrancyType::Titlebar:
30       break;
31   }
32   return NSVisualEffectStateFollowsWindowActiveState;
35 static NSVisualEffectMaterial VisualEffectMaterialForVibrancyType(
36     VibrancyType aType) {
37   switch (aType) {
38     case VibrancyType::Titlebar:
39       return NSVisualEffectMaterialTitlebar;
40   }
43 static NSVisualEffectBlendingMode VisualEffectBlendingModeForVibrancyType(
44     VibrancyType aType) {
45   switch (aType) {
46     case VibrancyType::Titlebar:
47       return StaticPrefs::widget_macos_titlebar_blend_mode_behind_window()
48                  ? NSVisualEffectBlendingModeBehindWindow
49                  : NSVisualEffectBlendingModeWithinWindow;
50   }
53 @implementation MOZVibrantView
54 - (instancetype)initWithFrame:(NSRect)aRect vibrancyType:(VibrancyType)aType {
55   self = [super initWithFrame:aRect];
56   mType = aType;
58   self.appearance = nil;
59   self.state = VisualEffectStateForVibrancyType(mType);
60   self.material = VisualEffectMaterialForVibrancyType(mType);
61   self.blendingMode = VisualEffectBlendingModeForVibrancyType(mType);
62   self.emphasized = NO;
63   return self;
66 - (NSView*)hitTest:(NSPoint)aPoint {
67   // This view must be transparent to mouse events.
68   return nil;
70 @end
72 VibrancyManager::VibrancyManager(const nsChildView& aCoordinateConverter,
73                                  NSView* aContainerView)
74     : mCoordinateConverter(aCoordinateConverter),
75       mContainerView(aContainerView) {}
77 VibrancyManager::~VibrancyManager() = default;
79 bool VibrancyManager::UpdateVibrantRegion(
80     VibrancyType aType, const LayoutDeviceIntRegion& aRegion) {
81   auto& slot = mVibrantRegions[aType];
82   if (aRegion.IsEmpty()) {
83     bool hadRegion = !!slot;
84     slot = nullptr;
85     return hadRegion;
86   }
87   if (!slot) {
88     slot = MakeUnique<ViewRegion>();
89   }
90   return slot->UpdateRegion(aRegion, mCoordinateConverter, mContainerView, ^() {
91     return [[MOZVibrantView alloc] initWithFrame:NSZeroRect vibrancyType:aType];
92   });