@@ -7,7 +7,7 @@ namespace UnityShell
7
7
[ Serializable ]
8
8
public class AutocompleteBox
9
9
{
10
- static class Styles
10
+ private static class Styles
11
11
{
12
12
public const float resultHeight = 20f ;
13
13
public const float resultsBorderWidth = 2f ;
@@ -36,7 +36,7 @@ static Styles()
36
36
}
37
37
}
38
38
39
- const int HintToNextCompletionHeight = 7 ;
39
+ private const int HintToNextCompletionHeight = 7 ;
40
40
41
41
public Action < string > onConfirm ;
42
42
public int maxResults = 10 ;
@@ -45,17 +45,17 @@ static Styles()
45
45
public string [ ] results = new string [ 0 ] ;
46
46
47
47
[ SerializeField ]
48
- Vector2 scrollPos ;
48
+ private Vector2 scrollPos ;
49
49
50
50
[ SerializeField ]
51
- int selectedIndex = - 1 ;
51
+ private int selectedIndex = - 1 ;
52
52
53
53
[ SerializeField ]
54
- int visualIndex = - 1 ;
54
+ private int visualIndex = - 1 ;
55
55
56
- bool showResults ;
56
+ private bool showResults ;
57
57
58
- string searchString ;
58
+ private string searchString ;
59
59
60
60
public void Clear ( )
61
61
{
@@ -65,7 +65,10 @@ public void Clear()
65
65
66
66
public void OnGUI ( string result , Rect rect )
67
67
{
68
- if ( results == null ) results = new string [ 0 ] ;
68
+ if ( results == null )
69
+ {
70
+ results = new string [ 0 ] ;
71
+ }
69
72
70
73
if ( result != searchString )
71
74
{
@@ -80,7 +83,10 @@ public void OnGUI(string result, Rect rect)
80
83
81
84
public void HandleEvents ( )
82
85
{
83
- if ( results . Length == 0 ) return ;
86
+ if ( results . Length == 0 )
87
+ {
88
+ return ;
89
+ }
84
90
85
91
var current = Event . current ;
86
92
@@ -100,20 +106,29 @@ public void HandleEvents()
100
106
current . Use ( ) ;
101
107
selectedIndex ++ ;
102
108
}
103
- else if ( current . keyCode == KeyCode . Return && selectedIndex >= 0 )
109
+ else if ( current . keyCode == KeyCode . Return && selectedIndex >= 0 )
104
110
{
105
111
current . Use ( ) ;
106
112
OnConfirm ( results [ selectedIndex ] ) ;
107
113
}
108
114
109
- if ( selectedIndex >= results . Length ) selectedIndex = 0 ;
110
- else if ( selectedIndex < 0 ) selectedIndex = results . Length - 1 ;
115
+ if ( selectedIndex >= results . Length )
116
+ {
117
+ selectedIndex = 0 ;
118
+ }
119
+ else if ( selectedIndex < 0 )
120
+ {
121
+ selectedIndex = results . Length - 1 ;
122
+ }
111
123
}
112
124
}
113
125
114
- void DoResults ( Rect drawRect )
126
+ private void DoResults ( Rect drawRect )
115
127
{
116
- if ( results . Length <= 0 || ! showResults ) return ;
128
+ if ( results . Length <= 0 || ! showResults )
129
+ {
130
+ return ;
131
+ }
117
132
118
133
var current = Event . current ;
119
134
drawRect . height = Styles . resultHeight * Mathf . Min ( maxResults , results . Length ) ;
@@ -123,7 +138,7 @@ void DoResults(Rect drawRect)
123
138
drawRect . height += Styles . resultsBorderWidth ;
124
139
125
140
var backgroundRect = drawRect ;
126
- if ( results . Length > maxResults )
141
+ if ( results . Length > maxResults )
127
142
{
128
143
backgroundRect . height += HintToNextCompletionHeight + Styles . resultsBorderWidth ;
129
144
}
@@ -154,21 +169,21 @@ void DoResults(Rect drawRect)
154
169
elementRect . x = Styles . resultsBorderWidth ;
155
170
elementRect . y = 0 ;
156
171
157
- if ( results . Length > maxResults )
172
+ if ( results . Length > maxResults )
158
173
{
159
174
elementRect . y = - visualIndex * Styles . resultHeight ;
160
175
161
176
var maxPos = GetTotalResultsShown ( clipRect ) * Styles . resultHeight - HintToNextCompletionHeight ;
162
177
163
- if ( - elementRect . y > maxPos )
178
+ if ( - elementRect . y > maxPos )
164
179
{
165
180
elementRect . y = - maxPos ;
166
181
}
167
182
}
168
183
169
184
for ( var i = 0 ; i < results . Length ; i ++ )
170
185
{
171
- if ( current . type == EventType . Repaint )
186
+ if ( current . type == EventType . Repaint )
172
187
{
173
188
var style = i % 2 == 0 ? Styles . entryOdd : Styles . entryEven ;
174
189
@@ -181,30 +196,30 @@ void DoResults(Rect drawRect)
181
196
elementRect . y += Styles . resultHeight ;
182
197
}
183
198
184
- if ( results . Length > maxResults )
199
+ if ( results . Length > maxResults )
185
200
{
186
201
DrawScroll ( clipRect ) ;
187
202
}
188
203
}
189
204
GUI . EndClip ( ) ;
190
205
}
191
206
192
- void DrawScroll ( Rect clipRect )
207
+ private void DrawScroll ( Rect clipRect )
193
208
{
194
209
var scrollRect = clipRect ;
195
210
scrollRect . x += scrollRect . width - 30 ;
196
211
scrollRect . y = 0 ;
197
212
198
213
var resultsShown = GetTotalResultsShown ( clipRect ) ;
199
214
200
- scrollRect . height = ( ( float ) maxResults / resultsShown * clipRect . height ) ;
215
+ scrollRect . height = ( ( float ) maxResults / resultsShown * clipRect . height ) ;
201
216
202
217
scrollRect . y = ( ( float ) visualIndex / ( resultsShown ) ) * ( clipRect . height - scrollRect . height ) ;
203
218
204
219
GUI . Box ( scrollRect , GUIContent . none , Styles . sliderStyle ) ;
205
220
}
206
221
207
- int GetTotalResultsShown ( Rect clipRect )
222
+ private int GetTotalResultsShown ( Rect clipRect )
208
223
{
209
224
// Actual scrolling is a bit less as there's also the view itself in which is not scrolled,
210
225
// when moving down initially, for example.
@@ -213,7 +228,7 @@ int GetTotalResultsShown(Rect clipRect)
213
228
return resultsShown ;
214
229
}
215
230
216
- void UpdateVisualIndex ( Rect clipRect )
231
+ private void UpdateVisualIndex ( Rect clipRect )
217
232
{
218
233
var ySelectedPos = selectedIndex * Styles . resultHeight ;
219
234
var yVisualPos = visualIndex * Styles . resultHeight ;
@@ -226,27 +241,30 @@ void UpdateVisualIndex(Rect clipRect)
226
241
var diffMax = ySelectedPos - ( yVisualPos + max ) + Styles . resultHeight ;
227
242
var diffMin = ( yVisualPos + min ) - ySelectedPos ;
228
243
229
- if ( diffMax > 0 )
244
+ if ( diffMax > 0 )
230
245
{
231
- visualIndex += Mathf . CeilToInt ( diffMax / Styles . resultHeight ) ;
246
+ visualIndex += Mathf . CeilToInt ( diffMax / Styles . resultHeight ) ;
232
247
}
233
- else if ( diffMin > 0 )
248
+ else if ( diffMin > 0 )
234
249
{
235
- visualIndex -= Mathf . CeilToInt ( diffMin / Styles . resultHeight ) ;
250
+ visualIndex -= Mathf . CeilToInt ( diffMin / Styles . resultHeight ) ;
236
251
}
237
252
}
238
253
239
- void OnConfirm ( string result )
254
+ private void OnConfirm ( string result )
240
255
{
241
- if ( onConfirm != null ) onConfirm ( result ) ;
256
+ if ( onConfirm != null )
257
+ {
258
+ onConfirm ( result ) ;
259
+ }
242
260
RepaintFocusedWindow ( ) ;
243
261
showResults = false ;
244
262
searchString = result ;
245
263
}
246
264
247
- static void RepaintFocusedWindow ( )
265
+ private static void RepaintFocusedWindow ( )
248
266
{
249
- if ( EditorWindow . focusedWindow != null )
267
+ if ( EditorWindow . focusedWindow != null )
250
268
{
251
269
EditorWindow . focusedWindow . Repaint ( ) ;
252
270
}
0 commit comments