Skip to content

Commit 7dc07f2

Browse files
committed
Clipboard functionality improvements; added option to disable it; misc improvements
1 parent cca53dc commit 7dc07f2

File tree

7 files changed

+142
-50
lines changed

7 files changed

+142
-50
lines changed

SmartImage 3/App/Integration.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ public static void KeepOnTop(bool add)
227227

228228
public static bool ReadClipboardImage(out byte[] i)
229229
{
230-
const uint png = (uint) ClipboardFormat.PNG;
230+
const uint png = (uint) ClipboardFormat.PNG;
231+
231232
// var sb = new StringBuilder(2048);
232233
// var l=Native.GetClipboardFormatName(png, sb, sb.Length);
233234
// Debug.WriteLine($"{sb} {l}");
@@ -244,6 +245,11 @@ public static bool ReadClipboardImage(out byte[] i)
244245
public static bool ReadClipboard(out string str)
245246
{
246247
Clipboard.Open();
248+
249+
/*
250+
* 1 File
251+
*/
252+
247253
var data = Clipboard.GetData((uint) ClipboardFormat.FileNameW);
248254

249255
if (data is IntPtr { } p && p == IntPtr.Zero) {
@@ -253,17 +259,35 @@ public static bool ReadClipboard(out string str)
253259
str = (string) data;
254260
}
255261

262+
if (!string.IsNullOrWhiteSpace(str)) goto cl;
263+
264+
/*
265+
* 3 Text
266+
*/
267+
256268
if (!SearchQuery.IsValidSourceType(str)) {
257-
var o = Clipboard.GetData((uint) ClipboardFormat.CF_TEXT);
269+
var o = Clipboard.GetData((uint) ClipboardFormat.CF_UNICODETEXT);
258270

259271
if ((data is IntPtr { } p2 && p2 == IntPtr.Zero) || o is IntPtr data2 && data2 == IntPtr.Zero) {
260272
str = null;
261273
}
262274
else {
263275
str = (string) o;
264276
}
277+
278+
if (o is nint n && n != nint.Zero) {
279+
// str = (string) o;
280+
281+
str = Marshal.PtrToStringUni(n);
282+
}
283+
284+
if (!string.IsNullOrWhiteSpace(str)) goto cl;
265285
}
266286

287+
/*
288+
* 3 Screenshot
289+
*/
290+
267291
if (ReadClipboardImage(out var ms)) {
268292
//todo: delete on exit
269293
var s = Path.Combine(Path.GetTempPath(), $"clipboard_{ms.Length}.png");
@@ -276,8 +300,9 @@ public static bool ReadClipboard(out string str)
276300
Debug.WriteLine($"read png from clipboard {s}");
277301
}
278302

303+
cl:
279304
Clipboard.Close();
280-
// Debug.WriteLine($"Clipboard data: {str}");
305+
Debug.WriteLine($"Clipboard data: {str}");
281306

282307
var b = SearchQuery.IsValidSourceType(str);
283308
return b;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ void ReloadDialog()
270270

271271
CheckBox cbCb = new(R1.S_Clipboard)
272272
{
273-
X = Pos.Right(cbOpenRaw) + 1,
274-
Y = Pos.Y(cbOpenRaw),
273+
X = Pos.X(cbContextMenu) ,
274+
Y = Pos.Bottom(cbContextMenu)+1,
275275
// Width = WIDTH,
276276
Height = 1,
277277
AutoSize = true,
@@ -290,8 +290,8 @@ Eh username/password
290290

291291
Label lbEhUsername = new(R1.S_EhUsername)
292292
{
293-
X = Pos.X(cbContextMenu),
294-
Y = Pos.Bottom(cbContextMenu) + 1,
293+
X = Pos.X(cbCb),
294+
Y = Pos.Bottom(cbCb) + 1,
295295
CanFocus = false,
296296
ColorScheme = UI.Cs_Lbl1
297297
};

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace SmartImage.Mode.Shell;
2121

2222
public sealed partial class ShellMode
2323
{
24-
2524
/// <summary>
2625
/// <see cref="Tv_Results"/>
2726
/// </summary>
@@ -48,12 +47,14 @@ private async void Input_TextChanging(TextChangingEventArgs tc)
4847
{
4948
var text = tc.NewText;
5049

51-
// Debug.WriteLine($"testing {text}", nameof(Input_TextChanging));
50+
Debug.WriteLine($"testing {text}", nameof(Input_TextChanging));
51+
52+
Application.MainLoop.Invoke(() => Task.Delay(TimeSpan.FromSeconds(1)));
5253

5354
if (SearchQuery.IsValidSourceType(text.ToString())) {
5455
var ok = await SetQuery(text);
5556
Btn_Run.Enabled = ok;
56-
57+
Debug.WriteLine($"{nameof(Input_TextChanging)} :: ok");
5758
}
5859
}
5960

@@ -73,6 +74,8 @@ private void Restart_Clicked(bool force = false)
7374
Dt_Results.Clear();
7475
Tv_Results.Visible = false;
7576

77+
// m_clipboard.RemoveRange(1, m_clipboard.Count - 1);
78+
7679
m_clipboard.Clear();
7780
m_results.Clear();
7881

SmartImage 3/Mode/Shell/ShellMode.cs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,17 @@ static ShellMode() { }
234234

235235
#endregion
236236

237+
#region Static
238+
239+
private static readonly TimeSpan TimeoutTimeSpan = TimeSpan.FromSeconds(1.5);
240+
241+
[SupportedOSPlatform(Compat.OS)]
242+
private static readonly SoundPlayer Player = new(R2.hint);
243+
244+
private static readonly ILogger Logger = LogUtil.Factory.CreateLogger(nameof(ShellMode));
245+
246+
#endregion
247+
237248
#region Fields/properties
238249

239250
private object m_cbCallbackTok;
@@ -247,6 +258,7 @@ static ShellMode() { }
247258
private readonly ConcurrentBag<SearchResult> m_results;
248259

249260
private CancellationTokenSource m_token;
261+
private CancellationTokenSource m_tokenu;
250262

251263
private bool m_useclipboard;
252264

@@ -271,17 +283,6 @@ public bool UseClipboard
271283

272284
private readonly ConcurrentQueue<ustring> m_queue;
273285

274-
#region Static
275-
276-
private static readonly TimeSpan TimeoutTimeSpan = TimeSpan.FromSeconds(1.5);
277-
278-
[SupportedOSPlatform(Compat.OS)]
279-
private static readonly SoundPlayer Player = new(R2.hint);
280-
281-
private static readonly ILogger Logger = LogUtil.Factory.CreateLogger(nameof(ShellMode));
282-
283-
#endregion
284-
285286
#region
286287

287288
public SearchQuery Query { get; internal set; }
@@ -308,6 +309,7 @@ public ShellMode(string[] args)
308309
{
309310
Args = args;
310311
m_token = new();
312+
m_tokenu = new();
311313
Query = SearchQuery.Null;
312314
Client = new SearchClient(new SearchConfig());
313315
IsReady = new ManualResetEvent(false);
@@ -633,17 +635,27 @@ private async Task<bool> SetQuery(ustring text)
633635
if (sq is { } && sq != SearchQuery.Null) {
634636
try {
635637

638+
/*Btn_Cancel.Enabled = true;
639+
640+
Btn_Cancel.Clicked += () =>
641+
{
642+
m_tokenu.Cancel();
643+
644+
};*/
645+
636646
using CancellationTokenSource cts = new();
637647
Lbl_Status2.Text = $"Uploading...";
638648

639649
UI.QueueProgress(cts, Pbr_Status);
640650

641-
var u = await sq.UploadAsync();
651+
var u = await sq.UploadAsync(ct: m_tokenu.Token);
642652

643653
cts.Cancel();
644654

645-
Lbl_QueryUpload.Text = u.ToString();
646-
Lbl_Status2.Text = ustring.Empty;
655+
Lbl_QueryUpload.Text = u.ToString();
656+
Lbl_Status2.Text = ustring.Empty;
657+
658+
// Btn_Cancel.Clicked += Cancel_Clicked;
647659

648660
}
649661
catch (Exception e) {
@@ -707,7 +719,7 @@ private async Task<bool> SetQuery(ustring text)
707719

708720
private bool ClipboardCallback(MainLoop c)
709721
{
710-
Debug.WriteLine($"executing timeout {nameof(ClipboardCallback)} {c} {UseClipboard} {c.EventsPending(false)}");
722+
Debug.WriteLine($"executing timeout {nameof(ClipboardCallback)} {c} {UseClipboard}");
711723

712724
try {
713725
/*
@@ -721,8 +733,10 @@ private bool ClipboardCallback(MainLoop c)
721733
var s = Tf_Input.Text.ToString();
722734
s = s.CleanString();
723735

724-
if (!SearchQuery.IsValidSourceType(s)
725-
&& Integration.ReadClipboard(out var str)
736+
var rc = Integration.ReadClipboard(out var str);
737+
738+
if (/*!SearchQuery.IsValidSourceType(s)
739+
&&*/ rc
726740
&& !m_clipboard.Contains(str)
727741
/*&& (m_prevSeq != sequenceNumber)*/) {
728742
/*bool vl = SearchQuery.IsValidSourceType(str);
@@ -735,7 +749,7 @@ private bool ClipboardCallback(MainLoop c)
735749
}
736750
737751
Debug.WriteLine($"{str}");*/
738-
752+
739753
SetInputText(str);
740754
// Lbl_InputOk.Text = UI.Clp;
741755
Lbl_InputInfo.Text = R2.Inf_Clipboard;
@@ -758,16 +772,17 @@ private bool ClipboardCallback(MainLoop c)
758772

759773
finally { }
760774

761-
Debug.WriteLine($"{UseClipboard}");
762-
// return true;
763-
return UseClipboard;
775+
// Debug.WriteLine($"{UseClipboard}");
776+
return true;
777+
// return UseClipboard;
764778
}
765779

766780
public void Dispose()
767781
{
768782
Client.Dispose();
769783
Query.Dispose();
770784
m_token.Dispose();
785+
m_tokenu.Dispose();
771786
m_queue.Clear();
772787
m_results.Clear();
773788
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Read S SmartImage.Lib IEndpoint.cs
2+
// 2023-05-12 @ 1:01 AM
3+
4+
using System.Collections.Concurrent;
5+
using Flurl.Http;
6+
7+
namespace SmartImage.Lib.Engines;
8+
9+
public interface IEndpoint
10+
{
11+
public Url EndpointUrl { get; }
12+
13+
[ICBN]
14+
public static async ValueTask<T[]> QueryAlive<T>(T[] rg, CancellationToken ct = default) where T : IEndpoint
15+
{
16+
var cb = new ConcurrentBag<T>();
17+
18+
await Parallel.ForEachAsync(rg, ct, async (b, c) =>
19+
{
20+
var u = ((Url) b.EndpointUrl).Root;
21+
var r = await u.HeadAsync(ct);
22+
23+
if (r.ResponseMessage.IsSuccessStatusCode) {
24+
cb.Add(b);
25+
}
26+
});
27+
28+
return cb.ToArray();
29+
}
30+
}

SmartImage.Lib 3/Engines/Impl/Upload/BaseUploadEngine.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
using Flurl.Http;
1+
using System.Diagnostics;
2+
using System.Net.NetworkInformation;
3+
using Flurl.Http;
24
using Novus.OS;
35
using Novus.Utilities;
6+
using SmartImage.Lib.Utilities;
47

58
namespace SmartImage.Lib.Engines.Impl.Upload;
69

7-
public abstract class BaseUploadEngine
10+
public abstract class BaseUploadEngine : IEndpoint
811
{
912
/// <summary>
1013
/// Max file size, in bytes
@@ -13,16 +16,16 @@ public abstract class BaseUploadEngine
1316

1417
public abstract string Name { get; }
1518

16-
protected string EndpointUrl { get; }
19+
public Url EndpointUrl { get; }
1720

1821
protected BaseUploadEngine(string s)
1922
{
2023
EndpointUrl = s;
2124
}
2225

23-
public static BaseUploadEngine Default { get; } = new LitterboxEngine();
26+
// public static BaseUploadEngine Default { get; } = new LitterboxEngine();
2427

25-
public abstract Task<Url> UploadFileAsync(string file);
28+
public abstract Task<Url> UploadFileAsync(string file, CancellationToken ct = default);
2629

2730
public long Size { get; set; }
2831

@@ -36,38 +39,46 @@ private protected bool IsFileSizeValid(string file)
3639

3740
protected void Verify(string file)
3841
{
39-
if (string.IsNullOrWhiteSpace(file))
40-
{
42+
if (string.IsNullOrWhiteSpace(file)) {
4143
throw new ArgumentNullException(nameof(file));
4244
}
4345

44-
if (!IsFileSizeValid(file))
45-
{
46+
if (!IsFileSizeValid(file)) {
4647
throw new ArgumentException($"File {file} is too large (max {MaxSize}) for {Name}");
4748
}
4849
}
4950

5051
public static readonly BaseUploadEngine[] All =
5152
ReflectionHelper.CreateAllInAssembly<BaseUploadEngine>(TypeProperties.Subclass).ToArray();
53+
54+
public static async ValueTask<BaseUploadEngine[]> UpdateAlive()
55+
{
56+
var a = await IEndpoint.QueryAlive(All);
57+
58+
Default = a?.FirstOrDefault();
59+
60+
return a;
61+
}
62+
63+
public static BaseUploadEngine Default { get; set; } = new CatboxEngine();
5264
}
5365

5466
public abstract class BaseCatboxEngine : BaseUploadEngine
5567
{
56-
57-
public override async Task<Url> UploadFileAsync(string file)
68+
public override async Task<Url> UploadFileAsync(string file, CancellationToken ct = default)
5869
{
5970
Verify(file);
6071

6172
using var response = await EndpointUrl
62-
.PostMultipartAsync(mp =>
63-
mp.AddFile("fileToUpload", file)
64-
.AddString("reqtype", "fileupload")
65-
.AddString("time", "1h")
66-
);
73+
.PostMultipartAsync(mp =>
74+
mp.AddFile("fileToUpload", file)
75+
.AddString("reqtype", "fileupload")
76+
.AddString("time", "1h")
77+
, ct);
6778

6879
var responseMessage = response.ResponseMessage;
6980

70-
var content = await responseMessage.Content.ReadAsStringAsync();
81+
var content = await responseMessage.Content.ReadAsStringAsync(ct);
7182

7283
/*if (!responseMessage.IsSuccessStatusCode) {
7384

0 commit comments

Comments
 (0)