Browse Source

implement transparency api on osx.

Dan Walmsley 5 years ago
parent
commit
0c3f2aa856

+ 1 - 0
native/Avalonia.Native/inc/avalonia-native.h

@@ -258,6 +258,7 @@ AVNCOM(IAvnWindowBase, 02) : IUnknown
     virtual HRESULT ObtainNSViewHandleRetained(void** retOut) = 0;
     virtual HRESULT BeginDragAndDropOperation(AvnDragDropEffects effects, AvnPoint point,
                                               IAvnClipboard* clipboard, IAvnDndResultCallback* cb, void* sourceHandle) = 0;
+    virtual HRESULT SetBlurEnabled (bool enable) = 0;
 };
 
 AVNCOM(IAvnPopup, 03) : virtual IAvnWindowBase

+ 3 - 0
native/Avalonia.Native/src/OSX/window.h

@@ -3,6 +3,9 @@
 
 class WindowBaseImpl;
 
+@interface AutoFitContentVisualEffectView : NSVisualEffectView
+@end
+
 @interface AvnView : NSView<NSTextInputClient, NSDraggingDestination>
 -(AvnView* _Nonnull) initWithParent: (WindowBaseImpl* _Nonnull) parent;
 -(NSEvent* _Nonnull) lastMouseDownEvent;

+ 29 - 0
native/Avalonia.Native/src/OSX/window.mm

@@ -20,6 +20,7 @@ public:
         View = NULL;
         Window = NULL;
     }
+    NSVisualEffectView* VisualEffect;
     AvnView* View;
     AvnWindow* Window;
     ComPtr<IAvnWindowBaseEvents> BaseEvents;
@@ -47,6 +48,12 @@ public:
         
         [Window setStyleMask:NSWindowStyleMaskBorderless];
         [Window setBackingType:NSBackingStoreBuffered];
+        
+        VisualEffect = [AutoFitContentVisualEffectView new];
+        [VisualEffect setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
+        [VisualEffect setMaterial:NSVisualEffectMaterialLight];
+        [VisualEffect setAutoresizesSubviews:true];
+        
         [Window setContentView: View];
     }
     
@@ -383,6 +390,18 @@ public:
         return *ppv == nil ? E_FAIL : S_OK;
     }
     
+    virtual HRESULT SetBlurEnabled (bool enable) override
+    {
+        [Window setContentView: enable ? VisualEffect : View];
+        
+        if(enable)
+        {
+            [VisualEffect addSubview:View];
+        }
+        
+        return S_OK;
+    }
+    
     virtual HRESULT BeginDragAndDropOperation(AvnDragDropEffects effects, AvnPoint point,
                                               IAvnClipboard* clipboard, IAvnDndResultCallback* cb,
                                               void* sourceHandle) override
@@ -911,6 +930,16 @@ protected:
 
 NSArray* AllLoopModes = [NSArray arrayWithObjects: NSDefaultRunLoopMode, NSEventTrackingRunLoopMode, NSModalPanelRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode, nil];
 
+@implementation AutoFitContentVisualEffectView
+-(void)setFrameSize:(NSSize)newSize
+{
+    [super setFrameSize:newSize];
+    if([[self subviews] count] == 0)
+        return;
+    [[self subviews][0] setFrameSize: newSize];
+}
+@end
+
 @implementation AvnView
 {
     ComPtr<WindowBaseImpl> _parent;

+ 16 - 1
src/Avalonia.Native/WindowImplBase.cs

@@ -391,7 +391,22 @@ namespace Avalonia.Native
             _native.BeginDragAndDropOperation(effects, point, clipboard, callback, sourceHandle);
         }
 
-        public WindowTransparencyLevel TransparencyLevel { get => WindowTransparencyLevel.None; set { } }
+        private WindowTransparencyLevel _transparencyLevel;
+
+        public WindowTransparencyLevel TransparencyLevel 
+        {
+            get => _transparencyLevel;
+            set
+            {
+                if(_transparencyLevel != value)
+                {
+                    _transparencyLevel = value;
+
+                    _native.SetBlurEnabled(_transparencyLevel >= WindowTransparencyLevel.Blur);
+                    TransparencyLevelChanged?.Invoke(_transparencyLevel);
+                }
+            } 
+        }
 
         public IPlatformHandle Handle { get; private set; }
     }