@@ -25,18 +25,28 @@ using namespace Menu;
25
25
public:
26
26
SDC& sdc;
27
27
// idx_t selIdx=0;//preserve selection context, because we preserve folder ctx too
28
- // we should use filename instead!
28
+ // we should use filename instead! idx is useful for delete operations thou...
29
+
30
+ File dir;
29
31
30
32
String folderName=" /" ;// set this to other folder when needed
31
33
String selectedFile=" " ;
32
34
// using menuNode::menuNode;//do not use default constructors as we wont allocate for data
33
- virtual ~SDMenuT () {}
35
+ virtual ~SDMenuT () {dir. close (); }
34
36
SDMenuT (SDC& sd,constText* title,const char * at,Menu::action act=doNothing,Menu::eventMask mask=noEvent)
35
37
:menuNode(title,0 ,NULL ,act,mask,
36
38
wrapStyle,(systemStyles)(_menuData|_canNav))
37
39
,sdc(sd)
38
40
,folderName(at)
39
- {}
41
+ // ,dir(sdc.open(at))
42
+ {
43
+ // Serial.println("open dir, construction");
44
+ // dir=sdc.open(at);
45
+ }
46
+
47
+ void begin () {
48
+ dir=sdc.open (folderName);
49
+ }
40
50
41
51
// this requires latest menu version to virtualize data tables
42
52
prompt& operator [](idx_t i) const override {return *(prompt*)this ;}// this will serve both as menu and as its own prompt
@@ -72,6 +82,9 @@ using namespace Menu;
72
82
// Serial.print("\nOpen folder...");
73
83
// open folder (reusing the menu)
74
84
folderName+=selFile;
85
+ dir.close ();
86
+ // Serial.println("reopen dir, context");
87
+ dir=sdc.open (folderName);
75
88
dirty=true ;// redraw menu
76
89
nav.sel =0 ;
77
90
} else {
@@ -91,6 +104,9 @@ using namespace Menu;
91
104
String fn=folderName.substring (at,folderName.length ()-1 );
92
105
folderName.remove (folderName.lastIndexOf (" /" ,folderName.length ()-2 )+1 );
93
106
// Serial.println(folderName);
107
+ dir.close ();
108
+ // Serial.println("reopen dir, back-nav");
109
+ dir=sdc.open (folderName);
94
110
dirty=true ;// redraw menu
95
111
nav.sel =entryIdx (fn);
96
112
}
@@ -101,25 +117,29 @@ using namespace Menu;
101
117
102
118
template <typename SDC>
103
119
idx_t SDMenuT<SDC>::count() {
104
- File dir=sdc.open (folderName.c_str ());
120
+ // Serial.print("count:");
121
+ // File dir=sdc.open(folderName.c_str());
122
+ dir.rewindDirectory ();
105
123
int cnt=0 ;
106
124
while (true ) {
107
125
File file=dir.openNextFile ();
108
126
if (!file) {
109
127
file.close ();
110
- dir.close ();
128
+ // dir.close();
111
129
break ;
112
130
}
113
131
file.close ();
114
132
cnt++;
115
133
}
116
- dir.close ();
134
+ // Serial.println(cnt);
135
+ // dir.close();
117
136
return cnt;
118
137
}
119
138
120
139
template <typename SDC>
121
140
idx_t SDMenuT<SDC>::entryIdx(String name) {
122
- File dir=sdc.open (folderName.c_str ());
141
+ // File dir=sdc.open(folderName.c_str());
142
+ dir.rewindDirectory ();
123
143
int cnt=0 ;
124
144
while (true ) {
125
145
File file=dir.openNextFile ();
@@ -129,19 +149,20 @@ using namespace Menu;
129
149
}
130
150
if (name==file.name ()) {
131
151
file.close ();
132
- dir.close ();
152
+ // dir.close();
133
153
return cnt;
134
154
}
135
155
file.close ();
136
156
cnt++;
137
157
}
138
- dir.close ();
158
+ // dir.close();
139
159
return 0 ;// stay at menu start if not found
140
160
}
141
161
142
162
template <typename SDC>
143
163
String SDMenuT<SDC>::entry(idx_t idx) {
144
- File dir=sdc.open (folderName.c_str ());
164
+ // File dir=sdc.open(folderName.c_str());
165
+ dir.rewindDirectory ();
145
166
idx_t cnt=0 ;
146
167
while (true ) {
147
168
File file=dir.openNextFile ();
@@ -152,25 +173,25 @@ using namespace Menu;
152
173
if (idx==cnt++) {
153
174
String n=String (file.name ())+(file.isDirectory ()?" /" :" " );
154
175
file.close ();
155
- dir.close ();
176
+ // dir.close();
156
177
return n;
157
178
}
158
179
file.close ();
159
180
}
160
- dir.close ();
181
+ // dir.close();
161
182
return " " ;
162
183
}
163
184
164
185
template <typename SDC>
165
186
Used SDMenuT<SDC>::printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t pn) {
166
- ((menuNodeShadow*)shadow )->sz =count ();
167
187
if (root.navFocus !=this ) {// show given title or filename if selected
168
188
return selectedFile==" " ?
169
189
menuNode::printTo (root,sel,out,idx,len,pn):
170
190
out.printRaw (selectedFile.c_str (),len);
171
- }
172
- else if (idx==- 1 ) // when menu open (show folder name)
191
+ } else if (idx==- 1 ) { // when menu open (show folder name)
192
+ ((menuNodeShadow*) shadow )-> sz = count ();
173
193
return out.printRaw (folderName.c_str (),len);
194
+ }
174
195
// drawing options
175
196
len-=out.printRaw (entry (idx).c_str (),len);
176
197
return len;
0 commit comments