So I have an NSVisualEffectView
hooked up with a button and some icons. The material
of this view is originally equal to NSVisualEffectMaterialDark
in other words, Vibrant Dark.
The following code I wrote is supposed to do the following:
1) Detect if the view's material is NSVisualEffectMaterialDark
or NSVisualEffectMaterialLight
2) Change the BOOL isDark
to YES/NO respectively
3) Change the view's appearance
, in other words the material
, from Dark to Light / Light to Dark based on the current view.
The problem is that when I run the app and click the button, NSVisualEffectView
's colour changes from a saturated dark to a less one, and not Light as it is supposed to.
What can I do to fix this problem and prevent it from happening?
Note: the NSVisualEffectView
's name is sideBar
, the button's name is lightButton
and isDark
is originally set to YES
.
Here is my code:
-(IBAction)toggleLighting:(id)sender{
if (self.sideBar.material == NSVisualEffectMaterialDark){
_lightButton.title = (@"Dark Mode");
[_lightButton setImage:[NSImage imageNamed:@"Dark Mode Icon"]];
[_lightButton setAlternateImage:[NSImage imageNamed:@"Dark Mode Icon (Alt)"]];
isDark = YES;
NSLog(@"Changed to Light Theme");
} else if (self.sideBar.material == NSVisualEffectMaterialLight){
_lightButton.title = (@"Light Mode");
[_lightButton setImage:[NSImage imageNamed:@"Light Mode Icon"]];
[_lightButton setAlternateImage:[NSImage imageNamed:@"Light Mode Icon (Alt)"]];
isDark = NO;
NSLog(@"Changed to Dark Theme");
}
if (isDark==YES){
_sideBar.material = NSVisualEffectMaterialLight;
isDark = NO;
} else if (isDark==NO) {
_sideBar.material = NSVisualEffectMaterialDark;
isDark = YES;
}
}
It turns out that the code I wrote is a little problematic, as expected of course.
The NSVisualEffectView
has none of the above material it checks for, NSVisualEffectMaterialDark
nor NSVisualEffectMaterialLight
, therefore both conditions are false. It seems that it chooses to set the material to a different type of dark so that the materials will finally match and be able to compare them properly.
By stating either material (NSVisualEffectMaterialDark
nor NSVisualEffectMaterialLight
), in - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
I was able to fix the issue where I had to click twice for the button to actually work.
EDIT:
It turns out there are other materials I wasn't aware of and they happened to be the desired ones.
These are NSVisualEffectMaterialMediumLight
and NSVisualEffectMaterialUltraDark
.