Skip to content

Commit e47f2fd

Browse files
committed
File picker working
1 parent 263459a commit e47f2fd

File tree

4 files changed

+179
-104
lines changed

4 files changed

+179
-104
lines changed

SmartImage 3/App/Integration.cs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ public static bool ReadClipboard(out string str)
316316
return b;
317317
}
318318

319-
public static string[] OpenFile(int f = 0)
319+
public static string[] OpenFile(OFN f = 0)
320320
{
321321
// Span<char> p1 = stackalloc char[1024];
322322
// Span<char> p2 = stackalloc char[512];
323323
unsafe {
324-
const int ss = 1024;
324+
const int ss = 4096;
325325

326326
var p1 = stackalloc sbyte[ss];
327327
var p2 = stackalloc sbyte[ss];
@@ -337,7 +337,7 @@ public static string[] OpenFile(int f = 0)
337337

338338
lpstrInitialDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures),
339339
lpstrTitle = "Pick an image",
340-
Flags = f
340+
Flags = (int) f
341341
};
342342

343343
// ofn.nMaxFile = ofn.lpstrFile.Length;
@@ -349,18 +349,38 @@ public static string[] OpenFile(int f = 0)
349349

350350
bool ok = Native.GetOpenFileName(ref ofn);
351351

352-
string[] files;
353-
var pd = Marshal.PtrToStringAuto((nint) p1);
352+
List<string> files = new List<string>();
354353

355-
if ((f & 0x00000200) == 0 /*!Directory.Exists(pd)&&File.Exists(pd)*/) {
356-
files = new[] { pd };
354+
if (!ok) {
357355
goto ret;
358356
}
359357

360-
var ofs = (ofn.nFileOffset * 2);
361-
var ptr1 = ((byte*) p1 + ofs);
362-
var _dir1 = Encoding.Unicode.GetString(ptr1, ofs);
363-
files = _dir1.Split('\0').Select(s => Path.Combine(pd, s)).ToArray();
358+
var pd = Marshal.PtrToStringAuto((nint) p1);
359+
360+
if (!(f.HasFlag(OFN.OFN_ALLOWMULTISELECT)) /*!Directory.Exists(pd)&&File.Exists(pd)*/) {
361+
files = new List<string>() { pd };
362+
goto ret;
363+
}
364+
365+
var ofs = (ofn.nFileOffset*2);
366+
var ptr1 = ((byte*) p1 + ofs);
367+
368+
while (true) {
369+
// var _dir1 = Encoding.Unicode.GetString(ptr1, ofs+1);
370+
371+
var _dir1 = Marshal.PtrToStringAuto((nint)ptr1);
372+
if (string.IsNullOrWhiteSpace(_dir1)) {
373+
break;
374+
}
375+
376+
var ff = _dir1.Split('\0').Select(s => Path.Combine(pd, s)).ToArray();
377+
files.AddRange(ff);
378+
ptr1 += (_dir1.Length*2) + 2;
379+
380+
if ((*ptr1 ==0 && ptr1[1] == 0)) {
381+
break;
382+
}
383+
}
364384

365385
// Debug.WriteLine($"{_dir1} {sz1}");
366386
/*rgz = ok ? new string(ofn.lpstrFile, 0, ) : null;
@@ -378,7 +398,7 @@ public static string[] OpenFile(int f = 0)
378398
379399
}*/
380400
ret:
381-
return files;
401+
return files.ToArray();
382402

383403
}
384404
}

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

Lines changed: 0 additions & 77 deletions
This file was deleted.

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

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Read S SmartImage ShellMode.Handlers.cs
22
// 2023-02-14 @ 12:13 AM
33

4+
using System.Collections;
5+
using System.Collections.Concurrent;
46
using System.Diagnostics;
57
using Kantan.Net.Utilities;
68
using Kantan.Text;
@@ -14,6 +16,7 @@
1416
using Terminal.Gui;
1517
using Clipboard = Novus.Win32.Clipboard;
1618
using Microsoft.VisualBasic.FileIO;
19+
using Novus.Win32.Structures.User32;
1720
using SmartImage.Lib.Utilities;
1821
using FileSystem = Novus.OS.FileSystem;
1922

@@ -120,14 +123,59 @@ private async void Run_Clicked()
120123

121124
private void Queue_Clicked()
122125
{
123-
if (IsQueryReady()) { }
126+
var d = new Dialog()
127+
{
128+
AutoSize = false,
129+
Width = Dim.Percent(60),
130+
Height = Dim.Percent(50),
131+
// Height = UI.Dim_80_Pct,
132+
};
133+
134+
var cpy = Queue.ToList();
135+
136+
var lv = new ListView(cpy)
137+
{
138+
Width = Dim.Fill(),
139+
Height = Dim.Fill()
140+
};
141+
142+
var btnRm = new Button("Remove")
143+
{ };
144+
145+
lv.KeyPress += args =>
146+
{
147+
if (args.KeyEvent.Key == Key.DeleteChar) {
148+
Debug.WriteLine($"{args}!!!");
149+
150+
}
151+
};
152+
153+
btnRm.Clicked += () =>
154+
{
155+
var cpy2 = lv.Source.ToList();
156+
if (cpy2.Count < lv.SelectedItem && lv.SelectedItem >= 0) {
157+
var i = (string) cpy2[lv.SelectedItem];
158+
Debug.WriteLine($"{i}");
159+
cpy.Remove(i);
160+
// Queue.Clear();
161+
Queue = new ConcurrentQueue<string>(cpy);
162+
lv.SetFocus();
163+
164+
}
165+
};
166+
167+
d.Add(lv);
168+
d.AddButton(btnRm);
169+
170+
Application.Run(d);
124171
}
125172

126173
private void Queue_Checked(bool b)
127174
{
128175
QueueMode = !b;
129176

130177
Btn_Queue.Enabled = QueueMode;
178+
Btn_Next.Enabled = QueueMode;
131179
}
132180

133181
/// <summary>
@@ -137,23 +185,24 @@ private void Browse_Clicked()
137185
{
138186
Integration.KeepOnTop(false);
139187

140-
int flags = 0x0;
141-
188+
OFN flags = 0x0;
189+
142190
if (QueueMode) {
143-
flags |= 0x00000200;
191+
flags |= OFN.OFN_ALLOWMULTISELECT;
144192
}
145193

146-
flags |= 0x00080000 | 0x00001000;
194+
flags |= OFN.OFN_EXPLORER | OFN.OFN_FILEMUSTEXIST;
147195

148196
var files = Integration.OpenFile(flags);
149-
197+
150198
if (QueueMode) {
151199
foreach (string fs in files) {
152200
Queue.Enqueue(fs);
153201
}
154202
}
155203

156-
var f = files[0]; //todo
204+
var f = files.FirstOrDefault(); //todo
205+
157206
if (!string.IsNullOrWhiteSpace(f)) {
158207
Tf_Input.DeleteAll();
159208
Debug.WriteLine($"Picked file: {f}", nameof(Browse_Clicked));
@@ -249,4 +298,16 @@ private void Delete_Clicked()
249298
}
250299

251300
}
301+
302+
private void Next_Clicked()
303+
{
304+
Restart_Clicked(true);
305+
var tryDequeue = Queue.TryDequeue(out var n);
306+
if (tryDequeue) {
307+
// SetQuery(n);
308+
309+
SetInputText(n);
310+
}
311+
}
312+
252313
}

0 commit comments

Comments
 (0)