diff --git a/README.md b/README.md index ea8f486..53bf07d 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,17 @@ There are several methods depending on what you want to do and what platform you * `state` Boolean * `windowBuffer` Buffer * `options` Object - * `Material` - Integer. The Material for `NSVisualEffectMaterial`.`0`: AppearanceBased,`1`: Light,`2`: Dark,`3`: Titlebar,`4`: Selection. + * `Material` - Integer. The Material for `NSVisualEffectMaterial`. + * `0` - NSVisualEffectMaterialAppearanceBased *10.10+* + * `1` - NSVisualEffectMaterialLight *10.10+* + * `2` - NSVisualEffectMaterialDark *10.10+* + * `3` - NSVisualEffectMaterialTitlebar *10.10+* + * `4` - NSVisualEffectMaterialSelection *10.11+* + * `5` - NSVisualEffectMaterialMenu *10.11+* + * `6` - NSVisualEffectMaterialPopover *10.11+* + * `7` - NSVisualEffectMaterialSidebar *10.11+* + * `8` - NSVisualEffectMaterialMediumLight *10.11+* + * `9` - NSVisualEffectMaterialUltraDark *10.11+* Enables or disables vibrancy for the **WHOLE** window. It will resize automatically. If you want something custom, see `AddView`. See [here](https://developer.apple.com/reference/appkit/nsvisualeffectmaterial?language=objc) for more info about `NSVisualEffectMaterial`. diff --git a/binding.gyp b/binding.gyp index a439452..00dca84 100644 --- a/binding.gyp +++ b/binding.gyp @@ -42,7 +42,7 @@ }, "xcode_settings": { "OTHER_CFLAGS": [ - "-x objective-c++" + "-x objective-c++ -stdlib=libc++" ] }, "variables":{ diff --git a/src/Common.h b/src/Common.h index d98240e..13a6112 100644 --- a/src/Common.h +++ b/src/Common.h @@ -26,6 +26,7 @@ #include #define V8Value v8::Local +#define V8Array v8::Local #if defined(_MSC_VER) #define PLATFORM_WIN32 diff --git a/src/Vibrancy.cc b/src/Vibrancy.cc index a570b58..946ad6b 100644 --- a/src/Vibrancy.cc +++ b/src/Vibrancy.cc @@ -24,6 +24,7 @@ //---------------------------------------------------------------------------- namespace Vibrancy { + static VibrancyHelper vibHelper_; Vibrancy::Vibrancy() { } @@ -36,6 +37,7 @@ namespace Vibrancy { v8::Local tpl = Nan::New(SetVibrancy); v8::Local inst = tpl->InstanceTemplate(); + //tpl->SetClassName(Nan::New("SetVibrancy").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); Nan::Set(target,Nan::New("SetVibrancy").ToLocalChecked(),Nan::GetFunction(tpl).ToLocalChecked()); @@ -56,11 +58,10 @@ namespace Vibrancy char* bufferData = node::Buffer::Data(handleBuffer); bool result = false; - VibrancyHelper helper; if(toggleState) - result = helper.EnableVibrancy((unsigned char*)bufferData,options); + result = vibHelper_.EnableVibrancy((unsigned char*)bufferData,options); else - result = helper.DisableVibrancy((unsigned char*)bufferData); + result = vibHelper_.DisableVibrancy((unsigned char*)bufferData); info.GetReturnValue().Set(result); diff --git a/src/Vibrancy.h b/src/Vibrancy.h index c76ca35..c9cdc40 100644 --- a/src/Vibrancy.h +++ b/src/Vibrancy.h @@ -38,6 +38,8 @@ namespace Vibrancy ~Vibrancy(); static NAN_METHOD(SetVibrancy); + + VibrancyHelper helper_; }; } diff --git a/src/VibrancyHelper.h b/src/VibrancyHelper.h index 572f7c5..944679e 100644 --- a/src/VibrancyHelper.h +++ b/src/VibrancyHelper.h @@ -45,6 +45,7 @@ namespace Vibrancy int Height; int X; int Y; + int Material; }; ViewOptions GetOptions(v8::Local options); diff --git a/src/vibrancy_mac.cc b/src/vibrancy_mac.cc index f232770..3c46266 100644 --- a/src/vibrancy_mac.cc +++ b/src/vibrancy_mac.cc @@ -23,9 +23,44 @@ #include "VibrancyHelper.h" +#import +#include +#include + +// NSVisualEffectMaterialAppearanceBased 10.10 - 0 +// NSVisualEffectMaterialLight 10.10 - 1 +// NSVisualEffectMaterialDark 10.10 - 2 +// NSVisualEffectMaterialTitlebar 10.10 - 3 +// +// NSVisualEffectMaterialSelection 10.11 - 4 +// NSVisualEffectMaterialMenu 10.11 - 5 +// NSVisualEffectMaterialPopover - 6 +// NSVisualEffectMaterialSidebar 10.11 - 7 +// NSVisualEffectMaterialMediumLight 10.11 - 8 +// NSVisualEffectMaterialUltraDark 10.11 - 9 + +static std::map materialIndexMap = +{ + std::make_pair(0,"NSVisualEffectMaterialAppearanceBased"), + std::make_pair(1,"NSVisualEffectMaterialLight"), + std::make_pair(2,"NSVisualEffectMaterialDark"), + std::make_pair(3,"NSVisualEffectMaterialTitlebar"), + std::make_pair(8,"NSVisualEffectMaterialMediumLight"), + std::make_pair(5,"NSVisualEffectMaterialMenu"), + std::make_pair(6,"NSVisualEffectMaterialPopover"), + std::make_pair(7,"NSVisualEffectMaterialSidebar"), + std::make_pair(9,"NSVisualEffectMaterialUltraDark"), + std::make_pair(4,"NSVisualEffectMaterialSelection") +}; + namespace Vibrancy { + bool IsHigherThanYosemite() + { + NSOperatingSystemVersion operatingSystemVersion = [[NSProcessInfo processInfo] operatingSystemVersion]; + return operatingSystemVersion.majorVersion == 10 && operatingSystemVersion.minorVersion > 10; + } bool VibrancyHelper::EnableVibrancy(unsigned char* windowHandleBuffer,v8::Local options) { NSView* view = *reinterpret_cast(windowHandleBuffer); @@ -34,9 +69,9 @@ namespace Vibrancy NSLog(@"%@",NSStringFromRect(rect)); //To verify the bounds - vibrantView = [[NSVisualEffectView alloc] initWithFrame:NSMakeRect(0, 0, rect.size.width, rect.size.height)]; - [vibrantView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; - [vibrantView setBlendingMode:NSVisualEffectBlendingModeBehindWindow]; + fullSizeVibrantView_ = [[NSVisualEffectView alloc] initWithFrame:NSMakeRect(0, 0, rect.size.width, rect.size.height)]; + [fullSizeVibrantView_ setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; + [fullSizeVibrantView_ setBlendingMode:NSVisualEffectBlendingModeBehindWindow]; if(options->Length() > 0) { @@ -46,9 +81,13 @@ namespace Vibrancy { int materialNumber = vMaterial->Int32Value(); - if(materialNumber >= 0 && materialNumber <= 14) // would crash if you give anything other than those specified here https://developer.apple.com/reference/appkit/nsvisualeffectmaterial?language=objc + if(materialNumber >= 0 && materialNumber <= 9) // would crash if you give anything other than those specified here https://developer.apple.com/reference/appkit/nsvisualeffectmaterial?language=objc { - [vibrantView setMaterial:(NSVisualEffectMaterial)materialNumber]; + if(materialNumber > 3 && !IsHigherThanYosemite()) + { + return false; + } + [fullSizeVibrantView_ setMaterial:(NSVisualEffectMaterial)materialNumber]; } } @@ -59,11 +98,14 @@ namespace Vibrancy { double alphaNumber = vAlpha->NumberValue(); //[vibrantView setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.1]]; - vibrantView.alphaValue = alphaNumber; + fullSizeVibrantView_.alphaValue = alphaNumber; } } - [view addSubview:vibrantView positioned:NSWindowBelow relativeTo:nil]; + //[view addSubview:fullSizeVibrantView_ positioned:NSWindowBelow relativeTo:nil]; + [view.window.contentView addSubview:fullSizeVibrantView_ positioned:NSWindowBelow relativeTo:nil]; + + views_.push_back(fullSizeVibrantView_); return true; } @@ -72,20 +114,26 @@ namespace Vibrancy { NSView* view = *reinterpret_cast(windowHandleBuffer); - if(views_.size()) + NSLog(@"Disabling Vibrancy - View count %i",views_.size()); + + if(views_.size() > 0) { + NSLog(@"Disabling Vibrancy - View count %i",views_.size()); + + NSView* contentView = (NSView*)view.window.contentView; for(int i=0; i < views_.size();++i) { - [[[view subviews] objectAtIndex:i] removeFromSuperview]; + NSView* viewToRemove = [[contentView subviews] objectAtIndex:i]; + NSLog(@"Removing subview %i %@",i,viewToRemove); + [viewToRemove removeFromSuperview]; } } - //[[[view subviews] objectAtIndex:0] removeFromSuperview]; return true; } - ViewOptions VibrancyHelper::GetOptions(v8::Local options) - { - ViewOptions viewOptions; + VibrancyHelper::ViewOptions VibrancyHelper::GetOptions(v8::Local options) + { + VibrancyHelper::ViewOptions viewOptions; viewOptions.ResizeMask = 2; // auto width,height resize viewOptions.Width = 0; viewOptions.Height = 0; @@ -96,8 +144,8 @@ namespace Vibrancy if(options->Length() > 0) { - V8Value vPosition = options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "Position"));// { x,y } - V8Value vSize = options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "Size")); // { width,height } + V8Array vPosition = v8::Local::Cast(options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "Position")));// { x,y } + V8Array vSize = v8::Local::Cast(options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "Size"))); // { width,height } V8Value vAutoResizeMask = options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "ResizeMask")); // //Integer width = 0,height = 1,both = 2,off = 3 V8Value vViewId = options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "ViewId")); V8Value vMaterial = options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "Material")); @@ -125,7 +173,7 @@ namespace Vibrancy { // Position V8Value vX = vPosition->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "x"));// Integer - V8Value vY = vPosition->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "y"));// IntegerFactory + V8Value vY = vPosition->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "y"));// Integer if(vX->IsInt32()) viewOptions.X = vX->Int32Value(); @@ -136,7 +184,7 @@ namespace Vibrancy if(!vAutoResizeMask->IsNull() && vAutoResizeMask->IsInt32()) { - resizeMask = vAutoResizeMask->Int32Value(); + viewOptions.ResizeMask = vAutoResizeMask->Int32Value(); } } return viewOptions; @@ -153,7 +201,7 @@ namespace Vibrancy NSRect rect = [[view window] frame]; - ViewOptions viewOptions = GetViewOptions(options); + ViewOptions viewOptions = GetOptions(options); NSVisualEffectView* vibrantView = [[NSVisualEffectView alloc] initWithFrame:NSMakeRect(viewOptions.X, viewOptions.Y, viewOptions.Width, viewOptions.Height)]; @@ -177,7 +225,7 @@ namespace Vibrancy { NSView* view = *reinterpret_cast(buffer); - ViewOptions viewOptions = GetViewOptions(options); + ViewOptions viewOptions = GetOptions(options); if(viewOptions.ViewId == -1) return false; diff --git a/test/main.js b/test/main.js index acc88a3..13b7a3e 100644 --- a/test/main.js +++ b/test/main.js @@ -23,7 +23,13 @@ app.on('ready', function () { console.log(vib); mainWindow.show(); - mainWindow.openDevTools(); + mainWindow.openDevTools({detach: true}); + + // function removeVibrancy() { + // electronVibrancy.SetVibrancy(false,nativeHandleBuffer); + // console.log("removed"); + // } + // setTimeout(removeVibrancy,3000); //Windows8.1 hack //mainWindow.minimize();