-
-
Save a9/51fe78b88110e6b63a1441d70a700b6c to your computer and use it in GitHub Desktop.
Append Blur to a NSView, NSButton, NSTextField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// appendBlur.swift | |
// | |
// Created by Wesley de Groot on 09-07-16. | |
// Copyright © 2016 Wesley de Groot. All rights reserved. | |
// | |
import Foundation | |
enum blur: String { | |
case light = "light" | |
case dark = "dark" | |
case xlight = "xlight" | |
} | |
#if os(OSX) | |
import Cocoa | |
/** | |
Append Blur to a window | |
- Parameter view: The NSButton, NSView, NSTextField, ... | |
- Parameter color: The Color (light/dark) | |
- Parameter alwaysBlur: true (default), false=not when in background | |
- Parameter hideTitle: false (default), true=hide title | |
*/ | |
func appendBlur(view: AnyObject, _ color: blur? = blur.xlight, _ alwaysBlur: Bool? = true, _ hideTitle: Bool? = false) -> Void { | |
if (view is NSButton || | |
view is NSTextField || | |
view is NSView) { | |
var Mcolor = "" | |
if (NSUserDefaults.standardUserDefaults().objectForKey("blur") as! String == "(null)") { | |
if (color == blur.xlight) { | |
Mcolor = "light" | |
} | |
} else { | |
if (color == blur.xlight) { | |
Mcolor = NSUserDefaults.standardUserDefaults().objectForKey("blur") as! String | |
if (Mcolor == "light") { | |
Mcolor = "dark" | |
} else { | |
Mcolor = "light" | |
} | |
} | |
} | |
if (color == blur.light) { | |
Mcolor = "light" | |
} | |
if (color == blur.dark) { | |
Mcolor = "dark" | |
} | |
NSUserDefaults.standardUserDefaults().setObject(Mcolor, forKey: "blur") | |
NSUserDefaults.standardUserDefaults().synchronize() | |
if view is NSTextField || view is NSButton { | |
if (Mcolor == "dark") { | |
Mcolor = "light" | |
} else { | |
Mcolor = "dark" | |
} | |
} | |
let blurryView = NSVisualEffectView(frame: NSRect(x: 0, | |
y: 0, | |
width: view.frame.size.width, | |
height: view.frame.size.height)) | |
// this is default value but is here for clarity | |
blurryView.blendingMode = NSVisualEffectBlendingMode.BehindWindow | |
if (Mcolor == "light") { | |
blurryView.material = NSVisualEffectMaterial.Light | |
} else { | |
blurryView.material = NSVisualEffectMaterial.Dark | |
} | |
if ((alwaysBlur) != nil) { | |
blurryView.state = NSVisualEffectState.Active | |
} else { | |
blurryView.state = NSVisualEffectState.FollowsWindowActiveState | |
} | |
if view is NSView { | |
view.addSubview(blurryView, positioned: NSWindowOrderingMode.Below, relativeTo: (view as! NSView)) | |
} | |
if view is NSTextField { | |
view.addSubview(blurryView, positioned: NSWindowOrderingMode.Below, relativeTo: (view as! NSTextField)) | |
} | |
if view is NSButton { | |
view.addSubview(blurryView, positioned: NSWindowOrderingMode.Below, relativeTo: (view as! NSButton)) | |
} | |
} else { | |
print("At this point i only support NSButton, NSTextField, NSView") | |
print("Please report a issue on:") | |
print("https://gist.github.com/wdg/b5dfcb8b3cac0faa6907719f3992d696") | |
} | |
} | |
#else | |
/** | |
Append Blur to a window (*NOT SUPPORTED ON THIS PLATFORM!*) | |
- Parameter view: The NS* | |
- Parameter color: The Color (light/dark) | |
- Parameter alwaysBlur: true (default), false=not when in background | |
- Parameter hideTitle: false (default), true=hide title | |
*/ | |
func appendBlur(view: AnyObject, _ color: blur? = blur.xlight, _ alwaysBlur: Bool? = true, _ hideTitle: Bool? = false) -> Void { | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment