Skip to content

Commit 83b9865

Browse files
committed
Clean Up Boolean Statements in Views
1 parent 9161bef commit 83b9865

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

Sources/CodeEditSourceEditor/Find/PanelView/FindControls.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,42 @@ struct FindControls: View {
1111
@ObservedObject var viewModel: FindPanelViewModel
1212
var condensed: Bool
1313

14+
var imageOpacity: CGFloat {
15+
viewModel.matchesEmpty ? 0.33 : 1
16+
}
17+
18+
var dynamicPadding: CGFloat {
19+
condensed ? 0 : 5
20+
}
21+
1422
var body: some View {
1523
HStack(spacing: 4) {
1624
ControlGroup {
1725
Button {
1826
viewModel.moveToPreviousMatch()
1927
} label: {
2028
Image(systemName: "chevron.left")
21-
.opacity(viewModel.matchCount == 0 ? 0.33 : 1)
22-
.padding(.horizontal, condensed ? 0 : 5)
29+
.opacity(imageOpacity)
30+
.padding(.horizontal, dynamicPadding)
2331
}
2432
.help("Previous Match")
25-
.disabled(viewModel.matchCount == 0)
33+
.disabled(viewModel.matchesEmpty)
34+
2635
Divider()
2736
.overlay(Color(nsColor: .tertiaryLabelColor))
2837
Button {
2938
viewModel.moveToNextMatch()
3039
} label: {
3140
Image(systemName: "chevron.right")
32-
.opacity(viewModel.matchCount == 0 ? 0.33 : 1)
33-
.padding(.horizontal, condensed ? 0 : 5)
41+
.opacity(imageOpacity)
42+
.padding(.horizontal, dynamicPadding)
3443
}
3544
.help("Next Match")
36-
.disabled(viewModel.matchCount == 0)
45+
.disabled(viewModel.matchesEmpty)
3746
}
3847
.controlGroupStyle(PanelControlGroupStyle())
3948
.fixedSize()
49+
4050
Button {
4151
viewModel.dismiss?()
4252
} label: {
@@ -47,8 +57,8 @@ struct FindControls: View {
4757
Text("Done")
4858
}
4959
}
50-
.help(condensed ? "Done" : "")
51-
.padding(.horizontal, condensed ? 0 : 5)
60+
.help("Done")
61+
.padding(.horizontal, dynamicPadding)
5262
}
5363
.buttonStyle(PanelButtonStyle())
5464
}

Sources/CodeEditSourceEditor/Find/PanelView/FindPanelHostingView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import SwiftUI
99
import AppKit
1010
import Combine
1111

12-
// NSView wrapper for using SwiftUI view in AppKit
12+
/// A subclass of `NSHostingView` that hosts a `FindPanelView` in an AppKit context.
1313
final class FindPanelHostingView: NSHostingView<FindPanelView> {
1414
private weak var viewModel: FindPanelViewModel?
1515

Sources/CodeEditSourceEditor/Find/PanelView/FindSearchField.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ struct FindSearchField: View {
1313
@Binding var findModePickerWidth: CGFloat
1414
var condensed: Bool
1515

16+
private var helperText: String? {
17+
if viewModel.findText.isEmpty {
18+
nil
19+
} else if condensed {
20+
"\(viewModel.matchCount)"
21+
} else {
22+
"\(viewModel.matchCount) \(viewModel.matchCount == 1 ? "match" : "matches")"
23+
}
24+
}
25+
1626
var body: some View {
1727
PanelTextField(
1828
"Text",
@@ -74,11 +84,7 @@ struct FindSearchField: View {
7484
})
7585
.toggleStyle(.icon)
7686
},
77-
helperText: viewModel.findText.isEmpty
78-
? nil
79-
: condensed
80-
? "\(viewModel.matchCount)"
81-
: "\(viewModel.matchCount) \(viewModel.matchCount == 1 ? "match" : "matches")",
87+
helperText: helperText,
8288
clearable: true
8389
)
8490
.controlSize(.small)

Sources/CodeEditSourceEditor/Find/PanelView/ReplaceControls.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ struct ReplaceControls: View {
1111
@ObservedObject var viewModel: FindPanelViewModel
1212
var condensed: Bool
1313

14+
var shouldDisableSingle: Bool {
15+
!viewModel.isFocused || viewModel.findText.isEmpty || viewModel.matchesEmpty
16+
}
17+
18+
var shouldDisableAll: Bool {
19+
viewModel.findText.isEmpty || viewModel.matchesEmpty
20+
}
21+
1422
var body: some View {
1523
HStack(spacing: 4) {
1624
ControlGroup {
@@ -24,18 +32,10 @@ struct ReplaceControls: View {
2432
Text("Replace")
2533
}
2634
}
27-
.opacity(
28-
!viewModel.isFocused
29-
|| viewModel.findText.isEmpty
30-
|| viewModel.matchCount == 0 ? 0.33 : 1
31-
)
35+
.opacity(shouldDisableSingle ? 0.33 : 1)
3236
}
33-
.help(condensed ? "Replace" : "")
34-
.disabled(
35-
!viewModel.isFocused
36-
|| viewModel.findText.isEmpty
37-
|| viewModel.matchCount == 0
38-
)
37+
.help("Replace")
38+
.disabled(shouldDisableSingle)
3939
.frame(maxWidth: .infinity)
4040

4141
Divider().overlay(Color(nsColor: .tertiaryLabelColor))
@@ -50,10 +50,10 @@ struct ReplaceControls: View {
5050
Text("All")
5151
}
5252
}
53-
.opacity(viewModel.findText.isEmpty || viewModel.matchCount == 0 ? 0.33 : 1)
53+
.opacity(shouldDisableAll ? 0.33 : 1)
5454
}
55-
.help(condensed ? "Replace All" : "")
56-
.disabled(viewModel.findText.isEmpty || viewModel.matchCount == 0)
55+
.help("Replace All")
56+
.disabled(shouldDisableAll)
5757
.frame(maxWidth: .infinity)
5858
}
5959
.controlGroupStyle(PanelControlGroupStyle())

Sources/CodeEditSourceEditor/Find/ViewModel/FindPanelViewModel.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class FindPanelViewModel: ObservableObject {
4040
findMatches.count
4141
}
4242

43+
var matchesEmpty: Bool {
44+
matchCount == 0
45+
}
46+
4347
var isTargetFirstResponder: Bool {
4448
target?.findPanelTargetView.window?.firstResponder === target?.findPanelTargetView
4549
}

0 commit comments

Comments
 (0)