Skip to content

Commit

Permalink
blur (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ay42 authored Sep 7, 2020
1 parent 3ef9e01 commit 0c67e95
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 76 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// swift-tools-version:5.2
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "VisualEffects",
platforms: [
.macOS(.v10_15)
.iOS(.v14), .macOS(.v11)
],
products: [
.library(name: "VisualEffects", targets: ["VisualEffects"])
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
![ci](https://github.com/swift-extensions/swiftui-visual-effects/workflows/ci/badge.svg)

## SwiftUI Visual Effects

### VisualEffectBlur
```swift
.visualEffect(material: .sidebar, blendingMode: .behindWindow)
.background(
VisualEffectBlur()
)
```

## Code Contributions
Feel free to contribute via fork/pull request to master branch. If you want to request a feature or report a bug please start a new issue.

## Coffee Contributions
If you find this project useful please consider becoming my GitHub sponsor.
81 changes: 81 additions & 0 deletions Sources/VisualEffects/Blur/VisualEffectBlur.iOS.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#if os(iOS)
import SwiftUI

public struct VisualEffectBlur<Content: View>: View {
var blurStyle: UIBlurEffect.Style
var vibrancyStyle: UIVibrancyEffectStyle?
var content: Content

public init(blurStyle: UIBlurEffect.Style = .systemMaterial, vibrancyStyle: UIVibrancyEffectStyle? = nil, @ViewBuilder content: () -> Content) {
self.blurStyle = blurStyle
self.vibrancyStyle = vibrancyStyle
self.content = content()
}

public var body: some View {
Representable(blurStyle: blurStyle, vibrancyStyle: vibrancyStyle, content: ZStack { content })
.accessibility(hidden: Content.self == EmptyView.self)
}
}

extension VisualEffectBlur {
struct Representable<Content: View>: UIViewRepresentable {
var blurStyle: UIBlurEffect.Style
var vibrancyStyle: UIVibrancyEffectStyle?
var content: Content

func makeUIView(context: Context) -> UIVisualEffectView {
context.coordinator.blurView
}

func updateUIView(_ view: UIVisualEffectView, context: Context) {
context.coordinator.update(content: content, blurStyle: blurStyle, vibrancyStyle: vibrancyStyle)
}

func makeCoordinator() -> Coordinator {
Coordinator(content: content)
}
}
}

// MARK: - Coordinator

extension VisualEffectBlur.Representable {
class Coordinator {
let blurView = UIVisualEffectView()
let vibrancyView = UIVisualEffectView()
let hostingController: UIHostingController<Content>

init(content: Content) {
hostingController = UIHostingController(rootView: content)
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
hostingController.view.backgroundColor = nil
blurView.contentView.addSubview(vibrancyView)
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
vibrancyView.contentView.addSubview(hostingController.view)
vibrancyView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

func update(content: Content, blurStyle: UIBlurEffect.Style, vibrancyStyle: UIVibrancyEffectStyle?) {
hostingController.rootView = content
let blurEffect = UIBlurEffect(style: blurStyle)
blurView.effect = blurEffect
if let vibrancyStyle = vibrancyStyle {
vibrancyView.effect = UIVibrancyEffect(blurEffect: blurEffect, style: vibrancyStyle)
} else {
vibrancyView.effect = nil
}
hostingController.view.setNeedsDisplay()
}
}
}

public extension VisualEffectBlur where Content == EmptyView {
init(blurStyle: UIBlurEffect.Style = .systemMaterial, vibrancyStyle: UIVibrancyEffectStyle? = nil) {
self.init(blurStyle: blurStyle, vibrancyStyle: vibrancyStyle) {
EmptyView()
}
}
}

#endif
51 changes: 51 additions & 0 deletions Sources/VisualEffects/Blur/VisualEffectBlur.macOS.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#if os(macOS)
import SwiftUI

public struct VisualEffectBlur: View {
private var material: NSVisualEffectView.Material
private let blendingMode: NSVisualEffectView.BlendingMode
private let isEmphasized: Bool

public init(material: NSVisualEffectView.Material = .headerView, blendingMode: NSVisualEffectView.BlendingMode = .withinWindow, isEmphasized: Bool = false) {
self.material = material
self.blendingMode = blendingMode
self.isEmphasized = isEmphasized
}

public var body: some View {
Representable(material: material, blendingMode: blendingMode, isEmphasized: isEmphasized)
.accessibility(hidden: true)
}
}

extension VisualEffectBlur {
struct Representable: NSViewRepresentable {
var material: NSVisualEffectView.Material
var blendingMode: NSVisualEffectView.BlendingMode
var isEmphasized: Bool

func makeNSView(context: Context) -> NSVisualEffectView {
context.coordinator.visualEffectView
}

func updateNSView(_ view: NSVisualEffectView, context: Context) {
context.coordinator.update(material: material, blendingMode: blendingMode, isEmphasized: isEmphasized)
}

func makeCoordinator() -> Coordinator {
Coordinator()
}
}

class Coordinator {
let visualEffectView = NSVisualEffectView()

func update(material: NSVisualEffectView.Material, blendingMode: NSVisualEffectView.BlendingMode, isEmphasized: Bool) {
visualEffectView.material = material
visualEffectView.blendingMode = blendingMode
visualEffectView.isEmphasized = isEmphasized
}
}
}

#endif
71 changes: 0 additions & 71 deletions Sources/VisualEffects/VisualEffects.swift

This file was deleted.

0 comments on commit 0c67e95

Please sign in to comment.