Skip to content

Commit 00a402c

Browse files
committed
Fixed unwanted view update on selection change
1 parent 1ac7784 commit 00a402c

11 files changed

+590
-538
lines changed

Sources/RichEditorSwiftUI/Fonts/RichTextFont+ListPicker.swift

+32-32
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99

1010
extension RichTextFont {
1111

12-
/**
12+
/**
1313
This view uses a `List` to list a set of fonts of which
1414
one can be selected.
1515

@@ -28,47 +28,47 @@ extension RichTextFont {
2828
.richTextFontPickerConfig(...)
2929
```
3030
*/
31-
public struct ListPicker: View {
31+
public struct ListPicker: View {
3232

33-
/**
33+
/**
3434
Create a font list picker.
3535

3636
- Parameters:
3737
- selection: The selected font name.
3838
*/
39-
public init(
40-
selection: Binding<FontName>
41-
) {
42-
self._selection = selection
43-
}
39+
public init(
40+
context: RichEditorState
41+
) {
42+
self._selection = context.bindingForFontName()
43+
}
4444

45-
public typealias Config = RichTextFont.PickerConfig
46-
public typealias Font = Config.Font
47-
public typealias FontName = Config.FontName
45+
public typealias Config = RichTextFont.PickerConfig
46+
public typealias Font = Config.Font
47+
public typealias FontName = Config.FontName
4848

49-
@Binding
50-
private var selection: FontName
49+
@Binding
50+
private var selection: FontName
5151

52-
@Environment(\.richTextFontPickerConfig)
53-
private var config
52+
@Environment(\.richTextFontPickerConfig)
53+
private var config
5454

55-
public var body: some View {
56-
let font = Binding(
57-
get: { Font(fontName: selection) },
58-
set: { selection = $0.fontName }
59-
)
55+
public var body: some View {
56+
let font = Binding(
57+
get: { Font(fontName: selection) },
58+
set: { selection = $0.fontName }
59+
)
6060

61-
RichEditorSwiftUI.ListPicker(
62-
items: config.fontsToList(for: selection),
63-
selection: font,
64-
dismissAfterPick: config.dismissAfterPick
65-
) { font, isSelected in
66-
RichTextFont.PickerItem(
67-
font: font,
68-
fontSize: config.fontSize,
69-
isSelected: isSelected
70-
)
71-
}
72-
}
61+
RichEditorSwiftUI.ListPicker(
62+
items: config.fontsToList(for: selection),
63+
selection: font,
64+
dismissAfterPick: config.dismissAfterPick
65+
) { font, isSelected in
66+
RichTextFont.PickerItem(
67+
font: font,
68+
fontSize: config.fontSize,
69+
isSelected: isSelected
70+
)
71+
}
7372
}
73+
}
7474
}

Sources/RichEditorSwiftUI/Fonts/RichTextFont+Picker.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ extension RichTextFont {
4040
- selection: The selected font name.
4141
*/
4242
public init(
43-
selection: Binding<FontName>
43+
context: RichEditorState
4444
) {
45-
self._selection = selection
45+
self._selection = context.bindingForFontName()
4646
self.selectedFont = Config.Font.all.first
4747
}
4848

Sources/RichEditorSwiftUI/Fonts/RichTextFont+SizePicker.swift

+38-38
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99

1010
extension RichTextFont {
1111

12-
/**
12+
/**
1313
This picker can be used to pick a font size.
1414

1515
The view returns a plain SwiftUI `Picker` view that can
@@ -26,60 +26,60 @@ extension RichTextFont {
2626
.richTextFontSizePickerConfig(...)
2727
```
2828
*/
29-
public struct SizePicker: View {
29+
public struct SizePicker: View {
3030

31-
/**
31+
/**
3232
Create a font size picker.
3333

3434
- Parameters:
3535
- selection: The selected font size.
3636
*/
37-
public init(
38-
selection: Binding<CGFloat>
39-
) {
40-
self._selection = selection
41-
}
37+
public init(
38+
context: RichEditorState
39+
) {
40+
self._selection = context.bindingForFontSize()
41+
}
4242

43-
@Binding
44-
private var selection: CGFloat
43+
@Binding
44+
private var selection: CGFloat
4545

46-
@Environment(\.richTextFontSizePickerConfig)
47-
private var config
46+
@Environment(\.richTextFontSizePickerConfig)
47+
private var config
4848

49-
public var body: some View {
50-
SwiftUI.Picker("", selection: $selection) {
51-
ForEach(
52-
values(
53-
for: config.values,
54-
selection: selection
55-
), id: \.self
56-
) {
57-
text(for: $0)
58-
.tag($0)
59-
}
60-
}
49+
public var body: some View {
50+
SwiftUI.Picker("", selection: $selection) {
51+
ForEach(
52+
values(
53+
for: config.values,
54+
selection: selection
55+
), id: \.self
56+
) {
57+
text(for: $0)
58+
.tag($0)
6159
}
60+
}
6261
}
62+
}
6363
}
6464

6565
extension RichTextFont.SizePicker {
6666

67-
/// Get a list of values for a certain selection.
68-
public func values(
69-
for values: [CGFloat],
70-
selection: CGFloat
71-
) -> [CGFloat] {
72-
let values = values + [selection]
73-
return Array(Set(values)).sorted()
74-
}
67+
/// Get a list of values for a certain selection.
68+
public func values(
69+
for values: [CGFloat],
70+
selection: CGFloat
71+
) -> [CGFloat] {
72+
let values = values + [selection]
73+
return Array(Set(values)).sorted()
74+
}
7575
}
7676

7777
extension RichTextFont.SizePicker {
7878

79-
fileprivate func text(
80-
for fontSize: CGFloat
81-
) -> some View {
82-
Text("\(Int(fontSize))")
83-
.fixedSize(horizontal: true, vertical: false)
84-
}
79+
fileprivate func text(
80+
for fontSize: CGFloat
81+
) -> some View {
82+
Text("\(Int(fontSize))")
83+
.fixedSize(horizontal: true, vertical: false)
84+
}
8585
}

Sources/RichEditorSwiftUI/Fonts/RichTextFontSizePickerStack.swift

+74-77
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
//
77

88
#if os(iOS) || os(macOS) || os(visionOS)
9-
import SwiftUI
9+
import SwiftUI
1010

11-
extension RichTextFont {
11+
extension RichTextFont {
1212

13-
/**
13+
/**
1414
This view uses a ``RichTextFont/SizePicker`` and button
1515
steppers to increment and a decrement the font size.
1616

@@ -25,87 +25,84 @@
2525
.richTextFontSizePickerConfig(...)
2626
```
2727
*/
28-
public struct SizePickerStack: View {
28+
public struct SizePickerStack: View {
2929

30-
/**
30+
/**
3131
Create a rich text font size picker stack.
3232

3333
- Parameters:
3434
- context: The context to affect.
3535
*/
36-
public init(
37-
context: RichEditorState
38-
) {
39-
self._context = ObservedObject(wrappedValue: context)
40-
}
41-
42-
private let step = 1
43-
44-
@ObservedObject
45-
private var context: RichEditorState
46-
47-
public var body: some View {
48-
#if os(iOS) || os(visionOS)
49-
stack
50-
.fixedSize(horizontal: false, vertical: true)
51-
#else
52-
HStack(spacing: 3) {
53-
picker
54-
stepper
55-
}
56-
.overlay(macShortcutOverlay)
57-
#endif
58-
}
59-
}
36+
public init(
37+
context: RichEditorState
38+
) {
39+
self._context = ObservedObject(wrappedValue: context)
40+
}
41+
42+
private let step = 1
43+
44+
@ObservedObject
45+
private var context: RichEditorState
46+
47+
public var body: some View {
48+
#if os(iOS) || os(visionOS)
49+
stack
50+
.fixedSize(horizontal: false, vertical: true)
51+
#else
52+
HStack(spacing: 3) {
53+
picker
54+
stepper
55+
}
56+
.overlay(macShortcutOverlay)
57+
#endif
58+
}
6059
}
60+
}
6161

62-
extension RichTextFont.SizePickerStack {
63-
64-
fileprivate var macShortcutOverlay: some View {
65-
stack
66-
.opacity(0)
67-
.allowsHitTesting(false)
68-
}
69-
70-
fileprivate var stack: some View {
71-
HStack(spacing: 2) {
72-
stepButton(-step)
73-
picker
74-
stepButton(step)
75-
}
76-
}
77-
78-
fileprivate func stepButton(_ points: Int) -> some View {
79-
RichTextAction.Button(
80-
action: .stepFontSize(points: points),
81-
context: context,
82-
fillVertically: true
83-
)
84-
}
85-
86-
fileprivate var picker: some View {
87-
RichTextFont.SizePicker(
88-
selection: $context.fontSize
89-
)
90-
.onChangeBackPort(of: context.fontSize) { newValue in
91-
context.updateStyle(style: .size(Int(context.fontSize)))
92-
}
93-
}
94-
95-
fileprivate var stepper: some View {
96-
Stepper(
97-
"",
98-
onIncrement: increment,
99-
onDecrement: decrement
100-
)
101-
}
102-
103-
fileprivate func decrement() {
104-
context.fontSize -= CGFloat(step)
105-
}
106-
107-
fileprivate func increment() {
108-
context.fontSize += CGFloat(step)
109-
}
62+
extension RichTextFont.SizePickerStack {
63+
64+
fileprivate var macShortcutOverlay: some View {
65+
stack
66+
.opacity(0)
67+
.allowsHitTesting(false)
68+
}
69+
70+
fileprivate var stack: some View {
71+
HStack(spacing: 2) {
72+
stepButton(-step)
73+
picker
74+
stepButton(step)
75+
}
76+
}
77+
78+
fileprivate func stepButton(_ points: Int) -> some View {
79+
RichTextAction.Button(
80+
action: .stepFontSize(points: points),
81+
context: context,
82+
fillVertically: true
83+
)
84+
}
85+
86+
fileprivate var picker: some View {
87+
RichTextFont.SizePicker(
88+
context: context
89+
)
90+
}
91+
92+
fileprivate var stepper: some View {
93+
Stepper(
94+
"",
95+
onIncrement: increment,
96+
onDecrement: decrement
97+
)
98+
}
99+
100+
fileprivate func decrement() {
101+
context.fontSize -= CGFloat(step)
102+
}
103+
104+
fileprivate func increment() {
105+
context.fontSize += CGFloat(step)
110106
}
107+
}
111108
#endif

0 commit comments

Comments
 (0)