Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Refactor layout and fix macOS resize issue
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Jan 7, 2021
1 parent 3e51554 commit 8281bdb
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 293 deletions.
14 changes: 9 additions & 5 deletions Sources/AttributedText/AttributedText.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#if canImport(SwiftUI) && !os(watchOS)
#if canImport(SwiftUI) && !os(watchOS) && !targetEnvironment(macCatalyst)

import SwiftUI

@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)
public struct AttributedText: View {
@StateObject private var store = AttributedTextStore()
@StateObject private var store = TextViewStore()

private let attributedText: NSAttributedString

Expand All @@ -14,13 +14,17 @@

public var body: some View {
GeometryReader { proxy in
AttributedTextViewWrapper(attributedText: attributedText, store: store)
TextViewWrapper(attributedText: attributedText, store: store)
.preference(key: ContainerSizePreference.self, value: proxy.size)
}
.onPreferenceChange(ContainerSizePreference.self) { value in
store.onContainerSizeChange(value)
}
.frame(height: store.height)
.frame(
idealWidth: store.intrinsicContentSize?.width,
idealHeight: store.intrinsicContentSize?.height
)
.fixedSize(horizontal: false, vertical: true)
}
}

Expand All @@ -29,7 +33,7 @@
static var defaultValue: CGSize?

static func reduce(value: inout CGSize?, nextValue: () -> CGSize?) {
value = value ?? nextValue()
value = nextValue()
}
}

Expand Down
27 changes: 0 additions & 27 deletions Sources/AttributedText/AttributedTextStore.swift

This file was deleted.

104 changes: 0 additions & 104 deletions Sources/AttributedText/AttributedTextView+AppKit.swift

This file was deleted.

101 changes: 0 additions & 101 deletions Sources/AttributedText/AttributedTextView+UIKit.swift

This file was deleted.

27 changes: 0 additions & 27 deletions Sources/AttributedText/AttributedTextViewWrapper+AppKit.swift

This file was deleted.

27 changes: 0 additions & 27 deletions Sources/AttributedText/AttributedTextViewWrapper+UIKit.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if canImport(SwiftUI) && !os(watchOS)
#if canImport(SwiftUI) && !os(watchOS) && !targetEnvironment(macCatalyst)

import SwiftUI

Expand Down
25 changes: 25 additions & 0 deletions Sources/AttributedText/TextViewStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if canImport(SwiftUI) && !os(watchOS) && !targetEnvironment(macCatalyst)

import SwiftUI

@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)
final class TextViewStore: ObservableObject {
@Published var intrinsicContentSize: CGSize?

weak var view: TextViewWrapper.View?

func onContainerSizeChange(_ containerSize: CGSize?) {
guard let containerSize = containerSize,
let view = self.view else { return }

view.maxWidth = containerSize.width
}

func didInvalidateIntrinsicContentSize() {
guard let view = self.view else { return }

intrinsicContentSize = view.intrinsicContentSize
}
}

#endif
Loading

0 comments on commit 8281bdb

Please sign in to comment.