Skip to content

Commit

Permalink
Merge pull request #31 from cybozu/fix-preprocessor
Browse files Browse the repository at this point in the history
Optimized preprocessors
  • Loading branch information
elmetal authored Oct 23, 2024
2 parents 19ed437 + b85a63e commit d645c64
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
18 changes: 13 additions & 5 deletions Examples/Examples/ContentViewState.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import WebKit

#if canImport(UIKit)
private typealias OSEnvironment = UIApplication
#elseif canImport(AppKit)
private typealias OSEnvironment = NSWorkspace
private extension OSEnvironment {
@MainActor func open(_ url: URL) async -> Bool {
let syncOpen: (URL) -> Bool = self.open
return syncOpen(url)
}
}
#endif

@MainActor
final class ContentViewState: NSObject, ObservableObject {
enum WebDialog {
Expand Down Expand Up @@ -147,11 +159,7 @@ extension ContentViewState: WKNavigationDelegate {
}

// Open the URL in an external app.
#if os(iOS)
let resultOpenURL = await UIApplication.shared.open(requestedURL)
#elseif os(macOS)
let resultOpenURL = NSWorkspace.shared.open(requestedURL)
#endif
let resultOpenURL = await OSEnvironment.shared.open(requestedURL)
guard !resultOpenURL else {
return (.cancel, preferences)
}
Expand Down
18 changes: 12 additions & 6 deletions Sources/WebUI/EnhancedWKWebView.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import WebKit

/// This is a workaround for the absence of refresh control in macOS.
#if os(iOS) || os(visionOS)
#if canImport(UIKit)
typealias RefreshControl = UIRefreshControl
#elseif os(macOS)
#else
struct RefreshControl {
enum ControlEvent {
case valueChanged
}
func addTarget(_ target: Any?, action: Selector, for controlEvents: ControlEvent) {}
func endRefreshing() {}
}
private extension WKWebView {
struct ScrollView {
var bounces = false
var refreshControl: RefreshControl? = .init()
}
var scrollView: ScrollView {
get { .init() }
set {}
}
}
#endif

class EnhancedWKWebView: WKWebView {
Expand All @@ -26,21 +36,17 @@ class EnhancedWKWebView: WKWebView {

var allowsScrollViewBounces = true {
willSet {
#if os(iOS)
self.scrollView.bounces = newValue
#endif
}
}

var isRefreshable = false {
willSet {
#if os(iOS) || os(visionOS)
if newValue {
self.scrollView.refreshControl = refreshControl
} else {
self.scrollView.refreshControl = nil
}
#endif
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/WebUI/Remakeable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if os(iOS) || os(visionOS)
#if canImport(UIKit)
import UIKit
typealias OSView = UIView
#elseif os(macOS)
#elseif canImport(AppKit)
import AppKit
typealias OSView = NSView
#endif
Expand Down
8 changes: 4 additions & 4 deletions Sources/WebUI/WebView+Extension.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import SwiftUI

#if os(iOS) || os(visionOS)
#if canImport(UIKit)
private typealias ViewRepresentable = UIViewRepresentable
#elseif os(macOS)
#elseif canImport(AppKit)
private typealias ViewRepresentable = NSViewRepresentable
#endif

Expand Down Expand Up @@ -35,15 +35,15 @@ extension WebView: View {
parent.applyModifiers(to: view.wrappedValue)
}

#if os(iOS) || os(visionOS)
#if canImport(UIKit)
func makeUIView(context: Context) -> Remakeable<EnhancedWKWebView> {
makeView()
}

func updateUIView(_ view: Remakeable<EnhancedWKWebView>, context: Context) {
updateView(view)
}
#elseif os(macOS)
#elseif canImport(AppKit)
func makeNSView(context: Context) -> Remakeable<EnhancedWKWebView> {
makeView()
}
Expand Down

0 comments on commit d645c64

Please sign in to comment.