Skip to content

Commit bdbd5eb

Browse files
committed
OpenFileName improvements
1 parent 41854a3 commit bdbd5eb

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

SmartImage 3/App/Integration.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,26 +323,29 @@ public static string[] OpenFile(OFN flags = 0)
323323
unsafe {
324324
const int ss = 4096;
325325

326-
var p1 = stackalloc sbyte[ss];
327-
var p2 = stackalloc sbyte[ss];
326+
Memory<sbyte> p1 = new sbyte[ss];
327+
Memory<sbyte> p2 = new sbyte[ss];
328328

329-
var ofn = new OPENFILENAME()
329+
var p1p = p1.Pin();
330+
var p2p = p2.Pin();
331+
332+
var ofn = new OPENFILENAME
330333
{
331334
lStructSize = Marshal.SizeOf<OPENFILENAME>(),
332335
lpstrFilter = "All Files\0*.*\0\0",
333336
// lpstrFile = new string(p1),
334337
// lpstrFileTitle = new string(p2),
335-
lpstrFile = (nint) p1,
336-
lpstrFileTitle = new string(p2),
338+
lpstrFile = (nint) p1p.Pointer,
339+
lpstrFileTitle = (nint) p2p.Pointer,
337340

338341
lpstrInitialDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures),
339342
lpstrTitle = "Pick an image",
340-
Flags = (int) flags
343+
Flags = (int) flags,
344+
// ofn.nMaxFile = ofn.lpstrFile.Length;
345+
nMaxFile = ss,
346+
nMaxFileTitle = ss
341347
};
342348

343-
// ofn.nMaxFile = ofn.lpstrFile.Length;
344-
ofn.nMaxFile = ss;
345-
ofn.nMaxFileTitle = ss;
346349
// ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length;
347350

348351
bool ok = Native.GetOpenFileName(ref ofn);
@@ -353,15 +356,15 @@ public static string[] OpenFile(OFN flags = 0)
353356
goto ret;
354357
}
355358

356-
var pd = Marshal.PtrToStringAuto((nint) p1);
359+
var pd = Marshal.PtrToStringAuto((nint) p1p.Pointer);
357360

358361
if (!(flags.HasFlag(OFN.OFN_ALLOWMULTISELECT)) /*!Directory.Exists(pd)&&File.Exists(pd)*/) {
359362
files.Add(pd);
360363
goto ret;
361364
}
362365

363366
var ofs = (ofn.nFileOffset * 2);
364-
var ptr1 = ((byte*) p1 + ofs);
367+
var ptr1 = (((byte*) p1p.Pointer) + ofs);
365368

366369
while (true) {
367370

@@ -382,9 +385,10 @@ public static string[] OpenFile(OFN flags = 0)
382385
}
383386

384387
ret:
388+
p1p.Dispose();
389+
p2p.Dispose();
385390
return files.ToArray();
386391

387392
}
388393
}
389-
390394
}

SmartImage 3/Mode/Shell/ShellMode.Handlers.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ private async void Input_TextChanging(TextChangingEventArgs tc)
5454

5555
Application.MainLoop.Invoke(() => Task.Delay(TimeSpan.FromSeconds(1)));
5656

57-
if (SearchQuery.IsValidSourceType(text.ToString())) {
57+
var sourceType = SearchQuery.IsValidSourceType(text.ToString());
58+
59+
if (sourceType) {
5860
var ok = await SetQuery(text);
5961
Btn_Run.Enabled = ok;
6062
Debug.WriteLine($"{nameof(Input_TextChanging)} :: ok");
@@ -133,10 +135,18 @@ private void Queue_Clicked()
133135

134136
var cpy = Queue.ToList();
135137

138+
var tf = new TextField()
139+
{
140+
141+
Width = Dim.Fill(),
142+
Height = 3,
143+
};
144+
136145
var lv = new ListView(cpy)
137146
{
138147
Width = Dim.Fill(),
139-
Height = Dim.Fill()
148+
Height = Dim.Fill(),
149+
Y =Pos.Bottom(tf)
140150
};
141151

142152
var btnRm = new Button("Remove")
@@ -153,7 +163,7 @@ private void Queue_Clicked()
153163
btnRm.Clicked += () =>
154164
{
155165
var cpy2 = lv.Source.ToList();
156-
if (cpy2.Count < lv.SelectedItem && lv.SelectedItem >= 0) {
166+
if (lv.SelectedItem < cpy2.Count && lv.SelectedItem >= 0) {
157167
var i = (string) cpy2[lv.SelectedItem];
158168
Debug.WriteLine($"{i}");
159169
cpy.Remove(i);
@@ -164,8 +174,19 @@ private void Queue_Clicked()
164174
}
165175
};
166176

167-
d.Add(lv);
177+
var btnRmAll = new Button("Clear")
178+
{ };
179+
180+
btnRmAll.Clicked += () =>
181+
{
182+
lv.Source = new ListWrapper(Enumerable.Empty<string>().ToList());
183+
Queue.Clear();
184+
lv.SetFocus();
185+
};
186+
187+
d.Add(tf,lv);
168188
d.AddButton(btnRm);
189+
d.AddButton(btnRmAll);
169190

170191
Application.Run(d);
171192
}
@@ -191,9 +212,16 @@ private void Browse_Clicked()
191212
flags |= OFN.OFN_ALLOWMULTISELECT;
192213
}
193214

194-
flags |= OFN.OFN_EXPLORER | OFN.OFN_FILEMUSTEXIST;
215+
flags |= OFN.OFN_EXPLORER | OFN.OFN_FILEMUSTEXIST | OFN.OFN_LONGNAMES | OFN.OFN_PATHMUSTEXIST;
216+
string[]? files;
195217

196-
var files = Integration.OpenFile(flags);
218+
try {
219+
files = Integration.OpenFile(flags);
220+
}
221+
catch (Exception e) {
222+
Debug.WriteLine($"{e.Message}");
223+
return;
224+
}
197225

198226
if (QueueMode) {
199227
foreach (string fs in files) {

0 commit comments

Comments
 (0)