Skip to content

Commit

Permalink
Fix resizing issue,add more materials
Browse files Browse the repository at this point in the history
  • Loading branch information
0x61726b committed Oct 28, 2016
1 parent 57bc466 commit 06cfaf1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 25 deletions.
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion binding.gyp
Expand Up @@ -42,7 +42,7 @@
},
"xcode_settings": {
"OTHER_CFLAGS": [
"-x objective-c++"
"-x objective-c++ -stdlib=libc++"
]
},
"variables":{
Expand Down
1 change: 1 addition & 0 deletions src/Common.h
Expand Up @@ -26,6 +26,7 @@
#include <nan.h>

#define V8Value v8::Local<v8::Value>
#define V8Array v8::Local<v8::Array>

#if defined(_MSC_VER)
#define PLATFORM_WIN32
Expand Down
7 changes: 4 additions & 3 deletions src/Vibrancy.cc
Expand Up @@ -24,6 +24,7 @@
//----------------------------------------------------------------------------
namespace Vibrancy
{
static VibrancyHelper vibHelper_;
Vibrancy::Vibrancy()
{
}
Expand All @@ -36,6 +37,7 @@ namespace Vibrancy
{
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(SetVibrancy);
v8::Local<v8::ObjectTemplate> inst = tpl->InstanceTemplate();
//tpl->SetClassName(Nan::New("SetVibrancy").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Nan::Set(target,Nan::New("SetVibrancy").ToLocalChecked(),Nan::GetFunction(tpl).ToLocalChecked());
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/Vibrancy.h
Expand Up @@ -38,6 +38,8 @@ namespace Vibrancy
~Vibrancy();

static NAN_METHOD(SetVibrancy);

VibrancyHelper helper_;
};

}
Expand Down
1 change: 1 addition & 0 deletions src/VibrancyHelper.h
Expand Up @@ -45,6 +45,7 @@ namespace Vibrancy
int Height;
int X;
int Y;
int Material;
};

ViewOptions GetOptions(v8::Local<v8::Array> options);
Expand Down
86 changes: 67 additions & 19 deletions src/vibrancy_mac.cc
Expand Up @@ -23,9 +23,44 @@

#include "VibrancyHelper.h"

#import <CoreServices/CoreServices.h>
#include <map>
#include <utility>

// 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<int,std::string> 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<v8::Array> options)
{
NSView* view = *reinterpret_cast<NSView**>(windowHandleBuffer);
Expand All @@ -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)
{
Expand All @@ -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];
}
}

Expand All @@ -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;
}
Expand All @@ -72,20 +114,26 @@ namespace Vibrancy
{
NSView* view = *reinterpret_cast<NSView**>(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<v8::Array> options)
{
ViewOptions viewOptions;
VibrancyHelper::ViewOptions VibrancyHelper::GetOptions(v8::Local<v8::Array> options)
{
VibrancyHelper::ViewOptions viewOptions;
viewOptions.ResizeMask = 2; // auto width,height resize
viewOptions.Width = 0;
viewOptions.Height = 0;
Expand All @@ -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<v8::Array>::Cast(options->Get(v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), "Position")));// { x,y }
V8Array vSize = v8::Local<v8::Array>::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"));
Expand Down Expand Up @@ -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();
Expand All @@ -136,7 +184,7 @@ namespace Vibrancy

if(!vAutoResizeMask->IsNull() && vAutoResizeMask->IsInt32())
{
resizeMask = vAutoResizeMask->Int32Value();
viewOptions.ResizeMask = vAutoResizeMask->Int32Value();
}
}
return viewOptions;
Expand All @@ -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)];
Expand All @@ -177,7 +225,7 @@ namespace Vibrancy
{
NSView* view = *reinterpret_cast<NSView**>(buffer);

ViewOptions viewOptions = GetViewOptions(options);
ViewOptions viewOptions = GetOptions(options);

if(viewOptions.ViewId == -1)
return false;
Expand Down
8 changes: 7 additions & 1 deletion test/main.js
Expand Up @@ -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();
Expand Down

0 comments on commit 06cfaf1

Please sign in to comment.