Skip to content

Commit 58745c5

Browse files
committed
Work on UI; work on thread safety
1 parent 362e148 commit 58745c5

9 files changed

Lines changed: 109 additions & 100 deletions

File tree

SmartImage.Lib/Engines/Results/SearchResultItem.cs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace SmartImage.Lib.Engines.Results;
1919

2020
// todo: refactor to not inherit from UniImageUrl and instead contain a UniImageUrl
2121

22-
public class SearchResultItem : IComparable<SearchResultItem>, IComparable, IEquatable<SearchResultItem>, IResultItem, IUrl
22+
public class SearchResultItem : UniImageUrl, IComparable<SearchResultItem>, IComparable, IEquatable<SearchResultItem>
2323
{
2424

2525
#region
@@ -42,9 +42,11 @@ public class SearchResultItem : IComparable<SearchResultItem>, IComparable, IEqu
4242

4343
#endregion
4444

45+
/*
4546
[MN]
4647
[JPN("url")]
4748
public Url Url { get; protected set; }
49+
*/
4850

4951
/// <summary>
5052
/// Title/caption of this result
@@ -102,15 +104,6 @@ public class SearchResultItem : IComparable<SearchResultItem>, IComparable, IEqu
102104
/// </summary>
103105
public DateTime? Time { get; internal set; }
104106

105-
public double? Similarity { get; internal set; }
106-
107-
[MNNW(true, nameof(Similarity))]
108-
public bool HasSimilarity => Similarity.HasValue;
109-
110-
public ulong? Hash { get; internal set; }
111-
112-
[MNNW(true, nameof(Hash))]
113-
public bool HasHash => Hash.HasValue;
114107

115108
/// <summary>
116109
/// Additional metadata.
@@ -134,9 +127,9 @@ public double Score
134127

135128
var s = 0d;
136129

137-
/*if (HasImage) {
130+
if (HasImage) {
138131
s++;
139-
}*/
132+
}
140133

141134
if (HasHash) {
142135
s++;
@@ -217,7 +210,7 @@ internal SearchResultItem(SearchResult r, bool isRaw = false) : base(null)
217210

218211
#region
219212

220-
/*public List<SearchResultItem> ScannedItems { get; }
213+
public List<SearchResultItem> ScannedItems { get; }
221214

222215
[MNNW(true, nameof(ScannedItems))]
223216
public bool HasScannedItems => ScannedItems is { Count: > 0 };
@@ -249,7 +242,7 @@ public override async ValueTask<bool> AllocImageAsync(CancellationToken ct = def
249242
else { }
250243

251244
return HasImage;
252-
}*/
245+
}
253246

254247
[MNNW(true, nameof(Thumbnail))]
255248
public async ValueTask<bool> LoadThumbnailAsync(CancellationToken ct = default)
@@ -437,20 +430,4 @@ public int CompareTo(object obj)
437430

438431
#endregion
439432

440-
public event PropertyChangedEventHandler PropertyChanged;
441-
442-
protected virtual void OnPropertyChanged([CMN] string propertyName = null)
443-
{
444-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
445-
}
446-
447-
protected virtual bool SetField<T>(ref T field, T value, [CMN] string propertyName = null)
448-
{
449-
if (EqualityComparer<T>.Default.Equals(field, value))
450-
return false;
451-
452-
field = value;
453-
OnPropertyChanged(propertyName);
454-
return true;
455-
}
456433
}

SmartImage.Lib/Engines/WebSearchEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ protected WebSearchEngine(Url url) : base(url) { }
1919
[MURV]
2020
protected override async Task<IDocument> GetSourceAsync(SearchResult sr, SearchQuery query, CancellationToken token = default)
2121
{
22-
2322
var parser = new HtmlParser();
2423

2524
using var res = await Client.Request(sr.RawUrl)

SmartImage.Lib/Images/Uni/IResultItem.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,4 @@ public interface IResultItem : IDisposable, ISimilarity, IHashable, INotifyPrope
1515

1616

1717

18-
}
19-
20-
public interface IUni : IImage, IResultItem
21-
{
22-
23-
byte[] Bytes { get; }
24-
25-
[MNNW(true, nameof(Bytes))]
26-
bool HasBytes { get; }
27-
28-
Stream GetStream();
29-
30-
/// <summary>
31-
/// Allocates <see cref="Bytes"/>
32-
/// </summary>
33-
[MNNW(true, nameof(Bytes))]
34-
ValueTask<bool> AllocSourceAsync(CancellationToken ct = default);
35-
36-
/// <summary>
37-
/// Allocates <see cref="IImage.Image"/> from <see cref="Bytes"/>
38-
/// </summary>
39-
[MNNW(true, nameof(Image))]
40-
ValueTask<bool> AllocImageAsync(CancellationToken ct = default);
41-
42-
/// <returns><see cref="AllocSourceAsync"/>, <see cref="AllocImageAsync"/></returns>
43-
async ValueTask<(bool AllocSourceOk, bool AllocImageOk)> AllocAll(CancellationToken ct)
44-
{
45-
bool allocOk = await AllocSourceAsync(ct);
46-
bool allocImgOk = false;
47-
48-
if (allocOk) {
49-
allocImgOk = await AllocImageAsync(ct);
50-
}
51-
52-
return (allocOk, allocImgOk);
53-
}
5418
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Author: Deci | Project: SmartImage.Lib | Name: IUniImage.cs
2+
// Date: 2026/02/08 @ 02:02:10
3+
4+
using SmartImage.Lib.Model;
5+
6+
namespace SmartImage.Lib.Images.Uni;
7+
8+
public interface IUniImage : IImage, IResultItem
9+
{
10+
11+
byte[] Bytes { get; }
12+
13+
[MNNW(true, nameof(Bytes))]
14+
bool HasBytes { get; }
15+
16+
Stream GetStream();
17+
18+
/// <summary>
19+
/// Allocates <see cref="Bytes"/>
20+
/// </summary>
21+
[MNNW(true, nameof(Bytes))]
22+
ValueTask<bool> AllocSourceAsync(CancellationToken ct = default);
23+
24+
/// <summary>
25+
/// Allocates <see cref="IImage.Image"/> from <see cref="Bytes"/>
26+
/// </summary>
27+
[MNNW(true, nameof(Image))]
28+
ValueTask<bool> AllocImageAsync(CancellationToken ct = default);
29+
30+
/// <returns><see cref="AllocSourceAsync"/>, <see cref="AllocImageAsync"/></returns>
31+
ValueTask<(bool AllocSourceOk, bool AllocImageOk)> AllocAll(CancellationToken ct);
32+
}

SmartImage.Lib/Images/Uni/ScannedResultItem.cs

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

SmartImage.Lib/Images/Uni/UniImage.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public enum UniImageType
5050
/// <summary>
5151
/// <seealso cref="UniSource"/>
5252
/// </summary>
53-
public abstract class UniImage : IUni, IEquatable<UniImage>
53+
public abstract class UniImage : IUniImage, IEquatable<UniImage>
5454
{
5555

5656
protected static readonly ILogger s_logger;
@@ -215,9 +215,16 @@ public virtual async ValueTask<bool> AllocImageAsync(CancellationToken ct = defa
215215
}
216216

217217
/// <returns><see cref="AllocSourceAsync"/>, <see cref="AllocImageAsync"/></returns>
218-
public virtual ValueTask<(bool AllocSourceOk, bool AllocImageOk)> AllocAll(CancellationToken ct)
218+
public async ValueTask<(bool AllocSourceOk, bool AllocImageOk)> AllocAll(CancellationToken ct)
219219
{
220-
return ((IUni)this).AllocAll(ct);
220+
bool allocOk = await AllocSourceAsync(ct);
221+
bool allocImgOk = false;
222+
223+
if (allocOk) {
224+
allocImgOk = await AllocImageAsync(ct);
225+
}
226+
227+
return (allocOk, allocImgOk);
221228
}
222229

223230
/// <summary>
@@ -296,7 +303,7 @@ public bool TryDeleteFile()
296303
return !HasLocalFilePath;
297304
}
298305

299-
public virtual string Name { get; protected set;}
306+
public virtual string Name { get; protected set; }
300307

301308
[MURV]
302309
public virtual string WriteImageToFile([CBN] string fn = null)

SmartImage.Lib/SearchClient.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ public async Task<bool> RunSearchAsync(SearchQuery query, CancellationToken toke
122122
}
123123

124124
var tasks = GetSearchTasks(query, token);
125-
126125
var results = await Task.WhenAll(tasks);
127126

127+
128128
s_logger.LogTrace("Results: {Res}", results.Length);
129129

130130
CompleteSearchAsync();
@@ -233,12 +233,17 @@ public IEnumerable<Task<SearchResult>> GetSearchTasks(SearchQuery query, Cancell
233233

234234
return Engines.Select(e =>
235235
{
236-
var res = e.GetResultAsync(query, token: token).ContinueWith(c =>
236+
var res = e.GetResultAsync(query, token: token).ContinueWith((c,tk) =>
237237
{
238238
var sr = c.Result;
239+
240+
if (tk is CancellationToken {IsCancellationRequested: true} ctk) {
241+
return sr;
242+
}
243+
239244
ProcessResult(sr);
240245
return sr;
241-
}, TaskContinuationOptions.OnlyOnRanToCompletion);
246+
}, TaskContinuationOptions.OnlyOnRanToCompletion, token);
242247

243248
return res;
244249
});

SmartImage.Rdx/Commands/Search/SearchCommand.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public sealed partial class SearchCommand : CommonAsyncCommand<SearchCommandSett
6262
private readonly CancellationTokenSource m_cts;
6363

6464
private readonly CancellationTokenSource m_ctsRun;
65+
private readonly CancellationTokenSource m_ctsRunSearch;
6566

6667
/// <summary>
6768
/// Key: <see cref="SearchResult"/>
@@ -85,6 +86,7 @@ public SearchCommand()
8586
{
8687
m_cts = new CancellationTokenSource();
8788
m_ctsRun = new CancellationTokenSource();
89+
m_ctsRunSearch = new CancellationTokenSource();
8890

8991
// m_results = new();
9092
m_resultTables = new ConcurrentDictionary<SearchResult, SpcTable>();
@@ -169,13 +171,13 @@ public override async Task<int> ExecuteAsync(CommandContext context, SearchComma
169171
);
170172

171173
// AnsiConsole.Write(m_layout);
172-
174+
173175
try {
174176

175177
IRenderable elem = CommandSettings.Interactive ? m_layout : m_mainTable;
176178

177179
Task main = AnsiConsole.Live(elem)
178-
.StartAsync(c => RunSearchLiveAsync(c, m_ctsRun.Token));
180+
.StartAsync(c => RunSearchLiveAsync(c, m_ctsRunSearch.Token));
179181

180182
await main;
181183
}
@@ -221,10 +223,10 @@ private async Task RunSearchLiveAsync(LiveDisplayContext c, CancellationToken ct
221223
#if UNITTEST
222224
return;
223225
#endif
224-
226+
225227
var search = Client.RunSearchAsync(Query, token: ct);
226228

227-
while (await Client.ResultChannel.Reader.WaitToReadAsync(ct)) {
229+
while (!ct.IsCancellationRequested && await Client.ResultChannel.Reader.WaitToReadAsync(ct)) {
228230
var task = Client.ResultChannel.Reader.ReadAsync(ct);
229231

230232
var result = await task;
@@ -285,8 +287,7 @@ private async Task RunInteractiveAsync(CancellationToken ct = default)
285287

286288
clrWrite = true;
287289

288-
do
289-
{
290+
do {
290291
if (clrWrite) {
291292
AnsiConsole.Clear();
292293
AnsiConsole.Write(srTable);
@@ -555,10 +556,11 @@ private void OnCancelKeyPress(object sender, ConsoleCancelEventArgs args)
555556

556557
// AnsiConsole.Clear();
557558

558-
m_ctsRun.Cancel();
559-
m_cts.Cancel();
560-
m_cts.TryReset();
561-
m_ctsRun.TryReset();
559+
m_ctsRunSearch.Cancel();
560+
561+
// m_cts.Cancel();
562+
// m_cts.TryReset();
563+
// m_ctsRun.TryReset();
562564

563565
args.Cancel = true;
564566

@@ -584,6 +586,7 @@ public override void Dispose()
584586
m_resultTables.Clear();
585587
m_cts.Dispose();
586588
m_ctsRun.Dispose();
589+
m_ctsRunSearch.Dispose();
587590
m_previewCanvasCache.Dispose();
588591
Client.Dispose();
589592
Query.Dispose();

SmartImage.Rdx/Shell/ShellSelection.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,13 @@ public int Index2()
4646

4747
public static int GetIndex2(SearchResultItem sri)
4848
{
49-
var itemIdx = sri.Root.Results.IndexOf(sri);
5049
var isScannedItem = sri.IsChild;
50+
var itemIdx = sri.Root.Results.IndexOf(sri);
5151

5252
var scanIdx = isScannedItem ? sri.Parent.Root.Results.IndexOf(sri.Parent) : 0;
5353

5454
var rg = itemIdx + sri.Root.Results[..itemIdx].Sum(x => x.ScannedItems.Count);
5555

56-
5756
var sumIdx2 = rg + scanIdx + (isScannedItem ? ((scanIdx == 0) ? 1 : 0) : 1);
5857

5958
return sumIdx2;
@@ -96,6 +95,43 @@ public int Index()
9695
return root + scnIdx + t;
9796
}
9897

98+
public static int GetIndex(SearchResultItem sri)
99+
{
100+
int i = 0, j = 0;
101+
var scnIdx = 0;
102+
int root = 0;
103+
int t = 0;
104+
105+
if (sri.IsChild) {
106+
// scnIdx = Item.Parent.ScannedItems.IndexOf(Item);
107+
root = sri.Parent.Root.Results.IndexOf(sri.Parent);
108+
109+
scnIdx++;
110+
111+
}
112+
else {
113+
root = sri.Root.Results.IndexOf(sri);
114+
115+
}
116+
117+
for (int k = 0; k < root; k++) {
118+
119+
var result = sri.Parent.Root.Results[k];
120+
var scnItm = result.ScannedItems;
121+
var scnIdx2 = scnItm.IndexOf(sri);
122+
123+
if (scnIdx2 == -1) {
124+
t += scnItm.Count;
125+
}
126+
else {
127+
t += scnIdx2;
128+
}
129+
130+
}
131+
132+
return root + scnIdx + t;
133+
}
134+
99135
[CBN]
100136
private static ShellSelection Parse(string str, SearchResult sr)
101137
{

0 commit comments

Comments
 (0)