Skip to content

Commit 4600f9b

Browse files
committed
Added previews for find views
1 parent 4efb3e5 commit 4600f9b

File tree

5 files changed

+227
-0
lines changed

5 files changed

+227
-0
lines changed

Sources/CodeEditSourceEditor/Find/PanelView/FindControls.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,43 @@ struct FindControls: View {
7575
}
7676
}
7777
}
78+
79+
#Preview("Find Controls - Full") {
80+
FindControls(
81+
viewModel: {
82+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
83+
vm.findText = "example"
84+
vm.findMatches = [NSRange(location: 0, length: 7)]
85+
vm.currentFindMatchIndex = 0
86+
return vm
87+
}(),
88+
condensed: false
89+
)
90+
.padding()
91+
}
92+
93+
#Preview("Find Controls - Condensed") {
94+
FindControls(
95+
viewModel: {
96+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
97+
vm.findText = "example"
98+
vm.findMatches = [NSRange(location: 0, length: 7)]
99+
vm.currentFindMatchIndex = 0
100+
return vm
101+
}(),
102+
condensed: true
103+
)
104+
.padding()
105+
}
106+
107+
#Preview("Find Controls - No Matches") {
108+
FindControls(
109+
viewModel: {
110+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
111+
vm.findText = "example"
112+
return vm
113+
}(),
114+
condensed: false
115+
)
116+
.padding()
117+
}

Sources/CodeEditSourceEditor/Find/PanelView/FindPanelView.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import SwiftUI
99
import AppKit
1010
import CodeEditSymbols
11+
import CodeEditTextView
1112

1213
/// A SwiftUI view that provides a find and replace interface for the text editor.
1314
///
@@ -88,3 +89,54 @@ private struct FindModePickerWidthPreferenceKey: PreferenceKey {
8889
value = nextValue()
8990
}
9091
}
92+
93+
/// A mock target for previews that implements the FindPanelTarget protocol
94+
class MockFindPanelTarget: FindPanelTarget {
95+
var textView: TextView!
96+
var findPanelTargetView: NSView = NSView()
97+
var cursorPositions: [CursorPosition] = []
98+
99+
func setCursorPositions(_ positions: [CursorPosition], scrollToVisible: Bool) {}
100+
func updateCursorPosition() {}
101+
func findPanelWillShow(panelHeight: CGFloat) {}
102+
func findPanelWillHide(panelHeight: CGFloat) {}
103+
func findPanelModeDidChange(to mode: FindPanelMode) {}
104+
}
105+
106+
#Preview("Find Mode") {
107+
FindPanelView(viewModel: {
108+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
109+
vm.findText = "example"
110+
vm.findMatches = [NSRange(location: 0, length: 7)]
111+
vm.currentFindMatchIndex = 0
112+
return vm
113+
}())
114+
.frame(width: 400)
115+
.padding()
116+
}
117+
118+
#Preview("Replace Mode") {
119+
FindPanelView(viewModel: {
120+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
121+
vm.mode = .replace
122+
vm.findText = "example"
123+
vm.replaceText = "test"
124+
vm.findMatches = [NSRange(location: 0, length: 7)]
125+
vm.currentFindMatchIndex = 0
126+
return vm
127+
}())
128+
.frame(width: 400)
129+
.padding()
130+
}
131+
132+
#Preview("Condensed Layout") {
133+
FindPanelView(viewModel: {
134+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
135+
vm.findText = "example"
136+
vm.findMatches = [NSRange(location: 0, length: 7)]
137+
vm.currentFindMatchIndex = 0
138+
return vm
139+
}())
140+
.frame(width: 300)
141+
.padding()
142+
}

Sources/CodeEditSourceEditor/Find/PanelView/FindSearchField.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,51 @@ struct FindSearchField: View {
106106
}
107107
}
108108
}
109+
110+
#Preview("Find Search Field - Full") {
111+
@FocusState var focus: FindPanelView.FindPanelFocus?
112+
FindSearchField(
113+
viewModel: {
114+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
115+
vm.findText = "example"
116+
vm.findMatches = [NSRange(location: 0, length: 7)]
117+
vm.currentFindMatchIndex = 0
118+
return vm
119+
}(),
120+
focus: $focus,
121+
findModePickerWidth: .constant(100),
122+
condensed: false
123+
)
124+
.frame(width: 300)
125+
.padding()
126+
}
127+
128+
#Preview("Find Search Field - Condensed") {
129+
@FocusState var focus: FindPanelView.FindPanelFocus?
130+
FindSearchField(
131+
viewModel: {
132+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
133+
vm.findText = "example"
134+
vm.findMatches = [NSRange(location: 0, length: 7)]
135+
vm.currentFindMatchIndex = 0
136+
return vm
137+
}(),
138+
focus: $focus,
139+
findModePickerWidth: .constant(100),
140+
condensed: true
141+
)
142+
.frame(width: 200)
143+
.padding()
144+
}
145+
146+
#Preview("Find Search Field - Empty") {
147+
@FocusState var focus: FindPanelView.FindPanelFocus?
148+
FindSearchField(
149+
viewModel: FindPanelViewModel(target: MockFindPanelTarget()),
150+
focus: $focus,
151+
findModePickerWidth: .constant(100),
152+
condensed: false
153+
)
154+
.frame(width: 300)
155+
.padding()
156+
}

Sources/CodeEditSourceEditor/Find/PanelView/ReplaceControls.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,46 @@ struct ReplaceControls: View {
7272
.fixedSize(horizontal: false, vertical: true)
7373
}
7474
}
75+
76+
#Preview("Replace Controls - Full") {
77+
ReplaceControls(
78+
viewModel: {
79+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
80+
vm.findText = "example"
81+
vm.replaceText = "replacement"
82+
vm.findMatches = [NSRange(location: 0, length: 7)]
83+
vm.currentFindMatchIndex = 0
84+
return vm
85+
}(),
86+
condensed: false
87+
)
88+
.padding()
89+
}
90+
91+
#Preview("Replace Controls - Condensed") {
92+
ReplaceControls(
93+
viewModel: {
94+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
95+
vm.findText = "example"
96+
vm.replaceText = "replacement"
97+
vm.findMatches = [NSRange(location: 0, length: 7)]
98+
vm.currentFindMatchIndex = 0
99+
return vm
100+
}(),
101+
condensed: true
102+
)
103+
.padding()
104+
}
105+
106+
#Preview("Replace Controls - No Matches") {
107+
ReplaceControls(
108+
viewModel: {
109+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
110+
vm.findText = "example"
111+
vm.replaceText = "replacement"
112+
return vm
113+
}(),
114+
condensed: false
115+
)
116+
.padding()
117+
}

Sources/CodeEditSourceEditor/Find/PanelView/ReplaceSearchField.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,47 @@ struct ReplaceSearchField: View {
5353
.focused($focus, equals: .replace)
5454
}
5555
}
56+
57+
#Preview("Replace Search Field - Full") {
58+
@FocusState var focus: FindPanelView.FindPanelFocus?
59+
ReplaceSearchField(
60+
viewModel: {
61+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
62+
vm.replaceText = "replacement"
63+
return vm
64+
}(),
65+
focus: $focus,
66+
findModePickerWidth: .constant(100),
67+
condensed: false
68+
)
69+
.frame(width: 300)
70+
.padding()
71+
}
72+
73+
#Preview("Replace Search Field - Condensed") {
74+
@FocusState var focus: FindPanelView.FindPanelFocus?
75+
ReplaceSearchField(
76+
viewModel: {
77+
let vm = FindPanelViewModel(target: MockFindPanelTarget())
78+
vm.replaceText = "replacement"
79+
return vm
80+
}(),
81+
focus: $focus,
82+
findModePickerWidth: .constant(100),
83+
condensed: true
84+
)
85+
.frame(width: 200)
86+
.padding()
87+
}
88+
89+
#Preview("Replace Search Field - Empty") {
90+
@FocusState var focus: FindPanelView.FindPanelFocus?
91+
ReplaceSearchField(
92+
viewModel: FindPanelViewModel(target: MockFindPanelTarget()),
93+
focus: $focus,
94+
findModePickerWidth: .constant(100),
95+
condensed: false
96+
)
97+
.frame(width: 300)
98+
.padding()
99+
}

0 commit comments

Comments
 (0)