Skip to content

Commit

Permalink
Decomission macOS (5)
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Sep 3, 2024
1 parent 3be0473 commit e92b905
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,4 @@ private func makePreviewViewModel() -> RichTextViewModel {
}
#endif

private struct TextViewSearchContextKey: EnvironmentKey {
static var defaultValue: RichTextViewModel.SearchContext?
}

extension EnvironmentValues {
var textViewSearchContext: RichTextViewModel.SearchContext? {
get { self[TextViewSearchContextKey.self] }
set { self[TextViewSearchContextKey.self] = newValue }
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
//
// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean).

#if os(iOS) || os(macOS) || os(visionOS)

import SwiftUI
import Pulse
import Combine

#if os(iOS) || os(visionOS)

final class RichTextViewModel: ObservableObject {
// Search
@Published var searchOptions: StringSearchOptions = .default
Expand Down Expand Up @@ -65,14 +65,10 @@ final class RichTextViewModel: ObservableObject {
didUpdateMatches(matches, string: textStorage)
if context.matchIndex < matches.count {
DispatchQueue.main.async {
#if os(iOS) || os(visionOS)
self.textView?.layoutManager.allowsNonContiguousLayout = false // Remove this workaround
UIView.performWithoutAnimation {
self.updateMatchIndex(context.matchIndex)
}
#else
self.updateMatchIndex(context.matchIndex)
#endif
}
}
}
Expand Down Expand Up @@ -181,26 +177,16 @@ final class RichTextViewModel: ObservableObject {
highlight(range: matches[previousMatch].range)
}
highlight(range: matches[selectedMatchIndex].range, isFocused: true)

#if os(macOS)
// A workaround for macOS where it doesn't always redraw itself correctly
textView?.setNeedsDisplay(textView?.bounds ?? .zero)
#endif
}

private func clearMatches() {
for match in matches {
let range = match.range
#if os(macOS)
textStorage.addAttribute(.foregroundColor, value: match.originalForegroundColor, range: range)
textStorage.removeAttribute(.underlineStyle, range: range)
#else
textStorage.addAttribute(.foregroundColor, value: match.originalForegroundColor, range: range)
textStorage.removeAttribute(.backgroundColor, range: range)
if let backgroundColor = match.originalBackgroundColor {
textStorage.addAttribute(.backgroundColor, value: backgroundColor, range: range)
}
#endif
}
}

Expand All @@ -221,9 +207,19 @@ private func search(searchTerm: String, in string: NSString, options: StringSear

#endif

extension RichTextViewModel {
struct SearchContext {
let searchTerm: ConsoleSearchTerm
let matchIndex: Int
private struct TextViewSearchContextKey: EnvironmentKey {
static var defaultValue: TextViewSearchContext?
}

extension EnvironmentValues {
var textViewSearchContext: TextViewSearchContext? {
get { self[TextViewSearchContextKey.self] }
set { self[TextViewSearchContextKey.self] = newValue }
}
}

struct TextViewSearchContext {
let searchTerm: ConsoleSearchTerm
let matchIndex: Int
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
//
// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean).

#if os(iOS) || os(macOS) || os(visionOS)
#if os(iOS) || os(visionOS)

import SwiftUI
import Combine

#if os(iOS) || os(visionOS)

struct WrappedTextView: UIViewRepresentable {
let viewModel: RichTextViewModel

Expand Down Expand Up @@ -59,81 +57,22 @@ struct WrappedTextView: UIViewRepresentable {
}
}

#elseif os(macOS)

struct WrappedTextView: NSViewRepresentable {
let viewModel: RichTextViewModel

@ObservedObject private var settings = UserSettings.shared

final class Coordinator: NSObject, NSTextViewDelegate {
var onLinkTapped: ((URL) -> Bool)?
var cancellables: [AnyCancellable] = []

func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool {
guard let url = link as? URL else {
return false
}
if let onLinkTapped = onLinkTapped, onLinkTapped(url) {
return true
}
return false
}
}

func makeNSView(context: Context) -> NSScrollView {
let scrollView = UXTextView.scrollableTextView()
let textView = scrollView.documentView as! UXTextView

scrollView.hasVerticalScroller = true
scrollView.autohidesScrollers = true

configureTextView(textView)
textView.delegate = context.coordinator

textView.attributedText = viewModel.originalText

viewModel.textView = textView

return scrollView
}

func updateNSView(_ scrollView: NSScrollView, context: Context) {
let textView = scrollView.documentView as! NSTextView
textView.isAutomaticLinkDetectionEnabled = settings.isLinkDetectionEnabled && viewModel.isLinkDetectionEnabled
}

func makeCoordinator() -> Coordinator {
let coordinator = Coordinator()
coordinator.onLinkTapped = viewModel.onLinkTapped
return coordinator
}
}
#endif

private func configureTextView(_ textView: UXTextView) {
textView.isSelectable = true
textView.isEditable = false
textView.linkTextAttributes = [
.underlineStyle: 1
]
textView.backgroundColor = .clear

#if os(iOS) || os(visionOS)
textView.alwaysBounceVertical = true
textView.autocorrectionType = .no
textView.autocapitalizationType = .none
textView.adjustsFontForContentSizeCategory = true
textView.textContainerInset = UIEdgeInsets(top: 8, left: 10, bottom: 8, right: 10)
#endif

#if os(iOS)
textView.keyboardDismissMode = .interactive
#endif

#if os(macOS)
textView.isAutomaticSpellingCorrectionEnabled = false
textView.textContainerInset = NSSize(width: 10, height: 10)
#endif
}

private func parseTooltip(_ url: URL) -> (title: String?, message: String)? {
Expand Down
11 changes: 11 additions & 0 deletions Sources/PulseUI/Features/Inspector/NetworkDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ final class NetworkDetailsViewModel {
self.makeString = closure
}
}

#if os(macOS)
final class RichTextViewModel: ObservableObject {
let isEmpty = true

init(string: String) {}
init(string: NSAttributedString) {}
init(string: NSAttributedString, contentType: NetworkLogger.ContentType?) {}

}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ final class ConsoleSearchOccurrence: Identifiable, Equatable, Hashable {
var line: Int { match.lineNumber }
var range: NSRange { NSRange(match.range, in: match.line) }
lazy var preview = ConsoleSearchOccurrence.makePreview(for: match, attributes: previewAttibutes)
let searchContext: RichTextViewModel.SearchContext
let searchContext: TextViewSearchContext

init(scope: ConsoleSearchScope,
match: ConsoleSearchMatch,
searchContext: RichTextViewModel.SearchContext) {
searchContext: TextViewSearchContext) {
self.scope = scope
self.match = match
self.searchContext = searchContext
Expand Down

0 comments on commit e92b905

Please sign in to comment.