@@ -9,15 +9,17 @@ import SwiftUI
99
1010struct NotchNotesView : View {
1111 @StateObject var notesManager = NotesManager ( )
12+ @State private var searchText : String = " "
1213
1314 var body : some View {
1415 GeometryReader { geo in
1516 HStack ( spacing: 0 ) {
16- // editor panel
1717 VStack {
1818 if notesManager. notes. indices. contains ( notesManager. selectedNoteIndex) {
1919 ZStack ( alignment: . topLeading) {
20- if notesManager. notes [ notesManager. selectedNoteIndex] . content. isEmpty {
20+ let currentNote = notesManager. notes [ notesManager. selectedNoteIndex]
21+
22+ if currentNote. content. isEmpty {
2123 Text ( " Enter your note... " )
2224 . foregroundColor ( . gray)
2325 . padding ( . leading, 8 )
@@ -27,24 +29,57 @@ struct NotchNotesView: View {
2729 . allowsHitTesting ( false )
2830 }
2931
30- TextEditor (
31- text: Binding (
32- get: {
33- guard notesManager. selectedNoteIndex < notesManager. notes. count else { return " " }
34- return notesManager. notes [ notesManager. selectedNoteIndex] . content
35- } ,
36- set: { newValue in
37- guard notesManager. selectedNoteIndex < notesManager. notes. count else { return }
38- notesManager. save ( note: newValue, at: notesManager. selectedNoteIndex)
39- }
32+ ZStack ( alignment: . bottomTrailing) {
33+ TextEditor (
34+ text: Binding (
35+ get: {
36+ guard notesManager. selectedNoteIndex < notesManager. notes. count else { return " " }
37+ return notesManager. notes [ notesManager. selectedNoteIndex] . content
38+ } ,
39+ set: { newValue in
40+ guard notesManager. selectedNoteIndex < notesManager. notes. count else { return }
41+ notesManager. save ( note: newValue, at: notesManager. selectedNoteIndex)
42+ }
43+ )
44+ )
45+ . fontWeight ( . thin)
46+ . font (
47+ notesManager. notes. indices. contains ( notesManager. selectedNoteIndex) &&
48+ notesManager. notes [ notesManager. selectedNoteIndex] . isMonospaced
49+ ? . system( . caption, design: . monospaced)
50+ : . caption
4051 )
41- )
42- . fontWeight ( . ultraLight)
43- . fontWidth ( . expanded)
44- . textEditorStyle ( . plain)
45- . transition ( . opacity. combined ( with: . blurReplace) )
46- . id ( notesManager. selectedNoteIndex)
47- . padding ( 4 )
52+ . animation (
53+ . easeInOut( duration: 0.2 ) ,
54+ value: notesManager. notes [ notesManager. selectedNoteIndex] . isMonospaced
55+ )
56+ . textEditorStyle ( . plain)
57+ . transition ( . opacity. combined ( with: . blurReplace) )
58+ . id ( notesManager. selectedNoteIndex)
59+ . padding ( 4 )
60+
61+ Button {
62+ notesManager. toggleMonospaced ( at: notesManager. selectedNoteIndex)
63+ } label: {
64+ Image ( systemName: " textformat " )
65+ . padding ( 5 )
66+ . background (
67+ ZStack {
68+ Rectangle ( )
69+ . fill ( . ultraThinMaterial)
70+ if notesManager. notes. indices. contains ( notesManager. selectedNoteIndex) &&
71+ notesManager. notes [ notesManager. selectedNoteIndex] . isMonospaced
72+ {
73+ Rectangle ( )
74+ . fill ( Color . white. opacity ( 0.15 ) )
75+ }
76+ }
77+ )
78+ . clipShape ( RoundedRectangle ( cornerRadius: 8 ) )
79+ . padding ( 3 )
80+ }
81+ . buttonStyle ( . plain)
82+ }
4883 }
4984 . background (
5085 RoundedRectangle ( cornerRadius: 10 )
@@ -55,30 +90,40 @@ struct NotchNotesView: View {
5590 . animation ( . easeInOut, value: notesManager. selectedNoteIndex)
5691 . frame ( width: ( geo. size. width / 3 ) * 2 )
5792
58- // sidebar
5993 VStack {
6094 ScrollView ( . vertical) {
61- Button {
62- notesManager. addNote ( )
63- notesManager. selectedNoteIndex = notesManager. notes. count - 1
64- } label: {
65- Image ( systemName: " plus " )
95+ HStack {
96+ TextField ( " Search " , text: $searchText)
97+ . textFieldStyle ( . plain)
6698 . frame ( maxWidth: . infinity)
99+ . padding ( 3 )
100+ . padding ( . leading, 3 )
67101 . frame ( height: 25 )
68- . padding ( . vertical, 1 )
69102 . background ( Color . white. opacity ( 0.1 ) )
70103 . clipShape ( RoundedRectangle ( cornerRadius: 8 ) )
71104 . contentShape ( RoundedRectangle ( cornerRadius: 8 ) )
105+
106+ Button {
107+ notesManager. addNote ( )
108+ } label: {
109+ Image ( systemName: " plus " )
110+ . frame ( width: 25 , height: 25 )
111+ . padding ( . bottom, 1 )
112+ . background ( Color . white. opacity ( 0.1 ) )
113+ . clipShape ( RoundedRectangle ( cornerRadius: 8 ) )
114+ . contentShape ( RoundedRectangle ( cornerRadius: 8 ) )
115+ }
116+ . buttonStyle ( . plain)
72117 }
73- . buttonStyle ( . plain)
74118
75119 LazyVStack ( spacing: 4 ) {
76- ForEach ( Array ( notesManager. notes. enumerated ( ) ) , id: \. element. id) {
77- index,
78- note in
120+ let displayed = Array ( notesManager. notes. enumerated ( ) )
121+ . filter { searchText. isEmpty || $0. element. content. localizedCaseInsensitiveContains ( searchText) }
122+
123+ ForEach ( displayed, id: \. element. id) { originalIndex, note in
79124 HStack {
80125 Button {
81- notesManager. selectedNoteIndex = index
126+ notesManager. selectedNoteIndex = originalIndex
82127 } label: {
83128 HStack {
84129 if !note. content. isEmpty {
@@ -96,7 +141,7 @@ struct NotchNotesView: View {
96141 . animation ( . easeInOut, value: note. content)
97142 . frame ( maxWidth: . infinity, alignment: . center)
98143 . background (
99- notesManager. selectedNoteIndex == index
144+ notesManager. selectedNoteIndex == originalIndex
100145 ? Color . accentColor. opacity ( 0.4 )
101146 : Color . white. opacity ( 0.1 )
102147 )
@@ -106,7 +151,7 @@ struct NotchNotesView: View {
106151 . buttonStyle ( . plain)
107152
108153 Button {
109- notesManager. removeNote ( at: index )
154+ notesManager. removeNote ( at: originalIndex )
110155 } label: {
111156 Label ( " Delete " , systemImage: " trash " )
112157 . foregroundStyle ( . white. opacity ( 0.7 ) )
@@ -131,12 +176,9 @@ struct NotchNotesView: View {
131176 . frame ( width: geo. size. width / 3 )
132177 . clipShape ( RoundedRectangle ( cornerRadius: 8 ) )
133178 }
179+ . preferredColorScheme ( . dark)
134180 . frame ( height: geo. size. height)
135181 }
136182 . transition ( . opacity. combined ( with: . blurReplace) )
137183 }
138184}
139-
140- #Preview {
141- NotchNotesView ( )
142- }
0 commit comments