Skip to content

Commit 00eb797

Browse files
committed
Tidy up the code (refactoring)
1 parent c166df3 commit 00eb797

4 files changed

Lines changed: 55 additions & 45 deletions

File tree

Sources/ResizingTextView/ResizingTextView.swift

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright © 2022 Manabu Nakazawa. All rights reserved.
22

3+
import Combine
34
import Foundation
45
import SwiftUI
5-
import Combine
66

77
public struct ResizingTextView: View, Equatable {
88
@Binding var text: String
@@ -28,6 +28,7 @@ public struct ResizingTextView: View, Equatable {
2828
public static var defaultLabelColor: NSColor {
2929
NSColor.labelColor
3030
}
31+
3132
#elseif os(iOS)
3233
public static var defaultLabelColor: UIColor {
3334
UIColor.label
@@ -86,7 +87,8 @@ public struct ResizingTextView: View, Equatable {
8687
.frame(
8788
maxWidth: hasGreedyWidth ? .infinity : nil,
8889
maxHeight: (isEditable && isScrollable) ? .infinity : nil,
89-
alignment: .leading)
90+
alignment: .leading
91+
)
9092
.opacity(0)
9193
.layoutPriority(1)
9294
}
@@ -124,7 +126,8 @@ public struct ResizingTextView: View, Equatable {
124126
.roundedFilledBorder(
125127
isEditable ? Color(UXColor.separatorColor) : .clear,
126128
width: isEditable ? 1 : 0,
127-
cornerRadius: isEditable ? 10 : 0)
129+
cornerRadius: isEditable ? 10 : 0
130+
)
128131
.overlay(RoundedRectangle(cornerRadius: 10)
129132
.stroke(Color.accentColor.opacity(0.5), lineWidth: 4)
130133
.opacity(isFocused && isEditable ? 1 : 0).scaleEffect(isFocused && isEditable ? 1 : 1.03))
@@ -140,7 +143,8 @@ public struct ResizingTextView: View, Equatable {
140143
canHaveNewLineCharacters: canHaveNewLineCharacters,
141144
foregroundColor: Color(foregroundColor),
142145
autocapitalizationType: autocapitalizationType,
143-
textContainerInset: textContainerInset)
146+
textContainerInset: textContainerInset
147+
)
144148
if let placeholder {
145149
Text(placeholder)
146150
.font(Font(font))
@@ -156,16 +160,16 @@ public struct ResizingTextView: View, Equatable {
156160

157161
public static func == (lhs: ResizingTextView, rhs: ResizingTextView) -> Bool {
158162
var result = lhs.text == rhs.text
159-
&& lhs.placeholder == rhs.placeholder
160-
&& lhs.isEditable == rhs.isEditable
161-
&& lhs.isScrollable == rhs.isScrollable
162-
&& lhs.isSelectable == rhs.isSelectable
163-
&& lhs.lineLimit == rhs.lineLimit
164-
&& lhs.font == rhs.font
165-
&& lhs.canHaveNewLineCharacters == rhs.canHaveNewLineCharacters
166-
&& lhs.foregroundColor == rhs.foregroundColor
167-
&& lhs.hasGreedyWidth == rhs.hasGreedyWidth
168-
&& lhs.isFocused == rhs.isFocused
163+
&& lhs.placeholder == rhs.placeholder
164+
&& lhs.isEditable == rhs.isEditable
165+
&& lhs.isScrollable == rhs.isScrollable
166+
&& lhs.isSelectable == rhs.isSelectable
167+
&& lhs.lineLimit == rhs.lineLimit
168+
&& lhs.font == rhs.font
169+
&& lhs.canHaveNewLineCharacters == rhs.canHaveNewLineCharacters
170+
&& lhs.foregroundColor == rhs.foregroundColor
171+
&& lhs.hasGreedyWidth == rhs.hasGreedyWidth
172+
&& lhs.isFocused == rhs.isFocused
169173
#if os(macOS)
170174
result = result && lhs.focusesNextKeyViewByTabKey == rhs.focusesNextKeyViewByTabKey
171175
#elseif os(iOS)

Sources/ResizingTextView/TextView (AppKit).swift

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ struct TextView: NSViewRepresentable {
2020
var onInsertNewline: (() -> Bool)?
2121
var textContainerInset: CGSize
2222

23-
init(_ text: Binding<String>,
24-
placeholder: String?,
25-
isEditable: Bool,
26-
isScrollable: Bool,
27-
isSelectable: Bool,
28-
lineLimit: Int,
29-
font: NSFont,
30-
canHaveNewLineCharacters: Bool,
31-
focusesNextKeyViewByTabKey: Bool,
32-
foregroundColor: Color?,
33-
onFocusChanged: ((Bool) -> Void)?,
34-
onInsertNewline: (() -> Bool)?,
35-
textContainerInset: CGSize?
23+
init(
24+
_ text: Binding<String>,
25+
placeholder: String?,
26+
isEditable: Bool,
27+
isScrollable: Bool,
28+
isSelectable: Bool,
29+
lineLimit: Int,
30+
font: NSFont,
31+
canHaveNewLineCharacters: Bool,
32+
focusesNextKeyViewByTabKey: Bool,
33+
foregroundColor: Color?,
34+
onFocusChanged: ((Bool) -> Void)?,
35+
onInsertNewline: (() -> Bool)?,
36+
textContainerInset: CGSize?
3637
) {
3738
self._text = text
3839
self.placeholder = placeholder
@@ -104,8 +105,9 @@ struct TextView: NSViewRepresentable {
104105
string: placeholder,
105106
attributes: [
106107
.foregroundColor: NSColor.placeholderTextColor,
107-
.font: font
108-
])
108+
.font: font,
109+
]
110+
)
109111
} else {
110112
textView.placeholderAttributedString = nil
111113
}
@@ -176,7 +178,7 @@ struct TextView: NSViewRepresentable {
176178
}
177179

178180
func textView(_ textView: NSTextView, shouldChangeTextIn affectedCharRange: NSRange, replacementString: String?) -> Bool {
179-
if (replacementString == "\n") && !parent.canHaveNewLineCharacters {
181+
if replacementString == "\n", !parent.canHaveNewLineCharacters {
180182
return false
181183
}
182184
return true
@@ -207,6 +209,7 @@ class TextEnclosingScrollView: NSScrollView {
207209
super.init(frame: .zero)
208210
}
209211

212+
@available(*, unavailable)
210213
required init?(coder: NSCoder) {
211214
fatalError("init(coder:) has not been implemented")
212215
}

Sources/ResizingTextView/TextView (UIKit).swift

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright © 2022 Manabu Nakazawa. All rights reserved.
22

33
#if os(iOS)
4-
import UIKit
54
import SwiftUI
5+
import UIKit
66

77
struct TextView: UIViewRepresentable {
88
static let defaultForegroundColor = Color(UIColor.label)
@@ -19,16 +19,18 @@ struct TextView: UIViewRepresentable {
1919
private var autocapitalizationType: UITextAutocapitalizationType
2020
private var textContainerInset: UIEdgeInsets
2121

22-
init(_ text: Binding<String>,
23-
isEditable: Bool,
24-
isScrollable: Bool,
25-
isSelectable: Bool,
26-
lineLimit: Int,
27-
font: UIFont,
28-
canHaveNewLineCharacters: Bool,
29-
foregroundColor: Color,
30-
autocapitalizationType: UITextAutocapitalizationType,
31-
textContainerInset: UIEdgeInsets?) {
22+
init(
23+
_ text: Binding<String>,
24+
isEditable: Bool,
25+
isScrollable: Bool,
26+
isSelectable: Bool,
27+
lineLimit: Int,
28+
font: UIFont,
29+
canHaveNewLineCharacters: Bool,
30+
foregroundColor: Color,
31+
autocapitalizationType: UITextAutocapitalizationType,
32+
textContainerInset: UIEdgeInsets?
33+
) {
3234
self._text = text
3335
self.isEditable = isEditable
3436
self.isScrollable = isScrollable
@@ -98,7 +100,7 @@ struct TextView: UIViewRepresentable {
98100
needsInvalidateIntrinsicContentSize = true
99101
}
100102

101-
if needsInvalidateIntrinsicContentSize && !isScrollable {
103+
if needsInvalidateIntrinsicContentSize, !isScrollable {
102104
view.invalidateIntrinsicContentSize()
103105
}
104106
}
@@ -146,6 +148,7 @@ class CustomTextView: UITextView {
146148
isScrollEnabled = true
147149
}
148150

151+
@available(*, unavailable)
149152
required init?(coder: NSCoder) {
150153
fatalError("init(coder:) has not been implemented")
151154
}
@@ -160,8 +163,8 @@ class CustomTextView: UITextView {
160163

161164
override open var intrinsicContentSize: CGSize {
162165
return hasDynamicHeight
163-
? contentSize
164-
: super.intrinsicContentSize
166+
? contentSize
167+
: super.intrinsicContentSize
165168
}
166169
}
167170
#endif

Sources/ResizingTextView/View+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import SwiftUI
44

55
extension View {
6-
func roundedFilledBorder<S>(_ content: S, width: CGFloat, cornerRadius: CGFloat) -> some View where S: ShapeStyle {
6+
func roundedFilledBorder(_ content: some ShapeStyle, width: CGFloat, cornerRadius: CGFloat) -> some View {
77
let roundedRect = RoundedRectangle(cornerRadius: cornerRadius)
88
return clipShape(roundedRect)
99
.overlay(roundedRect.strokeBorder(content, lineWidth: width))

0 commit comments

Comments
 (0)