Skip to content

Commit 19232c0

Browse files
committed
...
1 parent cd215ae commit 19232c0

File tree

8 files changed

+77
-69
lines changed

8 files changed

+77
-69
lines changed

SmartImage 3/Mode/Shell/Assets/UI.Styles.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,16 @@ internal static partial class UI
243243

244244
static UI() { }
245245

246-
internal static ColorScheme Make(Attribute norm, Attribute focus = default, Attribute disabled = default)
246+
internal static ColorScheme Make(Attribute norm, Attribute? focus = null, Attribute? disabled = null)
247247
{
248-
if (EqualityComparer<Attribute>.Default.Equals(focus, default)) {
249-
focus = Attribute.Get();
250-
251-
}
252-
253-
if (EqualityComparer<Attribute>.Default.Equals(disabled, default)) {
254-
disabled = Attribute.Get();
255-
256-
}
257-
248+
focus??= Attribute.Get();
249+
disabled ??= Attribute.Get();
250+
258251
return new ColorScheme()
259252
{
260253
Normal = norm,
261-
Focus = focus,
262-
Disabled = disabled
254+
Focus = focus.Value,
255+
Disabled = disabled.Value
263256
}.NormalizeHot();
264257
}
265258

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

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,42 @@ public sealed partial class ShellMode
3939
private const int COL_STATUS = 1;
4040
private const int COL_METADATA = 12;
4141

42+
private static readonly Color[] ColorValues = Enum.GetValues<Color>();
43+
4244
private static readonly ConcurrentDictionary<object, string> Downloaded = new();
4345

44-
private static readonly Dictionary<BaseSearchEngine, ColorScheme> Colors = new()
45-
{ };
46+
private static ConcurrentDictionary<BaseSearchEngine, ColorScheme> EngineColors = new();
47+
48+
private static ConcurrentDictionary<int, ColorScheme> IndexColors = new();
49+
50+
private static ColorScheme GetColor(BaseSearchEngine baseSearchEngine)
51+
{
52+
if (EngineColors.TryGetValue(baseSearchEngine, out var cs)) {
53+
return cs;
54+
55+
}
56+
else {
57+
var cc = ColorValues[
58+
Array.IndexOf(UI.EngineOptions, baseSearchEngine.EngineOption) % UI.EngineOptions.Length];
59+
60+
cs = new ColorScheme()
61+
{
62+
Normal = Attribute.Make(cc, Color.Black),
63+
Focus = Attribute.Make(Color.White, cc),
64+
65+
};
66+
cs = cs.NormalizeHot();
67+
EngineColors.TryAdd(baseSearchEngine, cs);
68+
69+
}
70+
71+
return cs;
72+
}
4673

4774
private async Task<bool> Input_TextChanging(TextChangingEventArgs tc)
4875
{
4976

50-
var text = tc.NewText.ToString().TrimStart('\"');
77+
var text = tc.NewText?.ToString()?.TrimStart('\"');
5178

5279
// Debug.WriteLine($"testing {text}", nameof(Input_TextChanging));
5380

@@ -246,7 +273,7 @@ private static void Clear_Clicked()
246273
// Btn_Run.Enabled = false;
247274
Tf_Input.SetFocus();
248275
Btn_Delete.Enabled = false;
249-
276+
IndexColors.Clear();
250277
}
251278

252279
private void Cancel_Clicked()
@@ -365,7 +392,10 @@ private void ResultTable_CellActivated(TableView.CellActivatedEventArgs args)
365392

366393
private ColorScheme? ResultTable_RowColor(TableView.RowColorGetterArgs r)
367394
{
368-
// var eng=args.Table.Rows[args.RowIndex]["Engine"];
395+
// var ar = r.Table.Rows[r.RowIndex];
396+
return IndexColors[r.RowIndex];
397+
398+
/*// var eng=args.Table.Rows[args.RowIndex]["Engine"];
369399
370400
ColorScheme? cs = null;
371401
@@ -377,44 +407,24 @@ private void ResultTable_CellActivated(TableView.CellActivatedEventArgs args)
377407
goto ret;
378408
}
379409
380-
var eng2 = Client.Engines.FirstOrDefault(f => eng.ToString().Contains(f.Name));
410+
// var eng2 = Client.Engines.FirstOrDefault(f => eng.ToString().Contains(f.Name));
381411
382-
if (eng2 == null) {
383-
goto ret;
384-
}
385-
386-
if (!Colors.ContainsKey(eng2)) {
387-
var colors = Enum.GetValues<Color>();
412+
BaseSearchEngine? eng2 = null;
388413
389-
var cc = colors[Array.IndexOf(UI.EngineOptions, eng2.EngineOption) % UI.EngineOptions.Length];
390-
391-
Color cc2;
392-
393-
switch (cc) {
394-
case Color.Cyan:
395-
cc2 = Color.BrightCyan;
396-
break;
397-
default:
398-
cc2 = cc;
399-
break;
414+
foreach (BaseSearchEngine csx in Client.Engines) {
415+
if (eng.ToString().Contains(csx.Name)) {
416+
eng2 = csx;
417+
break;
400418
}
401-
402-
cs = new ColorScheme()
403-
{
404-
Normal = Attribute.Make(cc, Color.Black),
405-
Focus = Attribute.Make(cc2, Color.DarkGray),
406-
407-
};
408-
cs = cs.NormalizeHot();
409-
410-
Colors.Add(eng2, cs);
411419
}
412-
else {
413-
cs = Colors[eng2];
420+
421+
if (eng2 == null) {
422+
goto ret;
414423
}
415424
425+
cs = Colors[eng2];
416426
ret:
417-
return cs;
427+
return cs;*/
418428
}
419429

420430
private async void ResultTable_KeyPress(View.KeyEventEventArgs eventArgs)
@@ -540,9 +550,10 @@ private async void ResultTable_KeyPress(View.KeyEventEventArgs eventArgs)
540550
case Key.X:
541551
//TODO: WIP
542552
break;
553+
default:
554+
eventArgs.Handled = false;
555+
break;
543556
}
544-
545-
// eventArgs.Handled = false;
546557
}
547558

548559
#if ALT

SmartImage 3/Mode/Shell/ShellMode.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,13 @@ private void OnResult(object o, SearchResult result)
572572

573573
Application.MainLoop.Invoke(() =>
574574
{
575+
int st = Dt_Results.Rows.Count;
576+
577+
IndexColors[st] = GetColor(result.Engine);
578+
575579
Dt_Results.Rows.Add($"{result.Engine.Name} (Raw)", string.Empty,
576580
result.RawUrl, 0, null, null, $"{result.Status}",
577581
null, null, null, null, null, null);
578-
579582
// Message[result.RawUrl] = "?";
580583

581584
for (int i = 0; i < result.Results.Count; i++) {
@@ -598,6 +601,10 @@ private void OnResult(object o, SearchResult result)
598601
sri.Similarity = null;
599602
}
600603

604+
st = Dt_Results.Rows.Count;
605+
606+
IndexColors[st] = GetColor(result.Engine);
607+
601608
Dt_Results.Rows.Add($"{result.Engine.Name} #{i + 1}", "",
602609
sri.Url, sri.Score, sri.Similarity, sri.Artist, sri.Description, sri.Source,
603610
sri.Title, sri.Site, sri.Width, sri.Height, meta);

SmartImage.Lib 3/Engines/Impl/Search/Ascii2DEngine.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ protected override async ValueTask<Url> GetRawUrlAsync(SearchQuery query)
6666
public override void Dispose() { }
6767

6868
protected override async Task<IDocument> GetDocumentAsync(object sender, SearchQuery query,
69-
CancellationToken? token = null)
69+
CancellationToken token = default)
7070
{
71-
token ??= CancellationToken.None;
7271

7372
var parser = new HtmlParser();
7473

@@ -94,11 +93,11 @@ protected override async Task<IDocument> GetDocumentAsync(object sender, SearchQ
9493
s.ExceptionHandled = true;
9594
9695
})*/
97-
.GetAsync();
96+
.GetAsync(token);
9897

9998
var str = await res.GetStringAsync();
10099

101-
var document = await parser.ParseDocumentAsync(str, token.Value);
100+
var document = await parser.ParseDocumentAsync(str, token);
102101

103102
return document;
104103

SmartImage.Lib 3/Engines/Impl/Search/EHentaiEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public async Task<bool> LoginAsync()
191191
#endregion
192192

193193
protected override async Task<IDocument> GetDocumentAsync(object origin, SearchQuery query,
194-
CancellationToken? token = null)
194+
CancellationToken token = default)
195195
{
196196
const string name = "a.jpg";
197197

@@ -253,17 +253,17 @@ protected override async Task<IDocument> GetDocumentAsync(object origin, SearchQ
253253
},
254254
};
255255

256-
var res = await m_client.SendAsync(req);
256+
var res = await m_client.SendAsync(req, token);
257257

258-
var content = await res.Content.ReadAsStringAsync();
258+
var content = await res.Content.ReadAsStringAsync(token);
259259

260260
if (content.Contains("Please wait a bit longer between each file search.")) {
261261
Debug.WriteLine($"cooldown", Name);
262262
return null;
263263
}
264264

265265
var parser = new HtmlParser();
266-
return await parser.ParseDocumentAsync(content).ConfigureAwait(false);
266+
return await parser.ParseDocumentAsync(content, token).ConfigureAwait(false);
267267
}
268268

269269
private Task<IFlurlResponse> GetSessionAsync()

SmartImage.Lib 3/Engines/Impl/Search/IqdbEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static SearchResultItem ParseResult(IHtmlCollection<IElement> tr, Search
111111
return result;
112112
}
113113

114-
private async Task<IDocument> GetDocumentAsync(SearchQuery query)
114+
private async Task<IDocument> GetDocumentAsync(SearchQuery query, CancellationToken ct)
115115
{
116116
const int MAX_FILE_SIZE = 0x800000;
117117

@@ -127,12 +127,12 @@ private async Task<IDocument> GetDocumentAsync(SearchQuery query)
127127
}
128128

129129
return;
130-
});
130+
}, ct);
131131

132132
var s = await response.GetStringAsync();
133133

134134
var parser = new HtmlParser();
135-
return await parser.ParseDocumentAsync(s).ConfigureAwait(false);
135+
return await parser.ParseDocumentAsync(s, ct).ConfigureAwait(false);
136136

137137
}
138138
catch (Exception e) {
@@ -153,7 +153,7 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
153153
goto ret;
154154
}
155155

156-
var doc = await GetDocumentAsync(query);
156+
var doc = await GetDocumentAsync(query, token);
157157

158158
if (doc == null || doc.Body == null) {
159159
sr.ErrorMessage = $"Could not retrieve data";

SmartImage.Lib 3/Engines/WebSearchEngine.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ public override async Task<SearchResult> GetResultAsync(SearchQuery query, Cance
5656

5757
[ItemCanBeNull]
5858
protected virtual async Task<IDocument> GetDocumentAsync(object sender, SearchQuery query,
59-
CancellationToken? token = null)
59+
CancellationToken token = default)
6060
{
61-
token ??= CancellationToken.None;
6261

6362
var parser = new HtmlParser();
6463

@@ -77,11 +76,11 @@ protected virtual async Task<IDocument> GetDocumentAsync(object sender, SearchQu
7776
{
7877
s.ExceptionHandled = true;
7978
})*/
80-
.GetAsync(cancellationToken: token.Value);
79+
.GetAsync(cancellationToken: token);
8180

8281
var str = await res.GetStringAsync();
8382

84-
var document = await parser.ParseDocumentAsync(str, token.Value);
83+
var document = await parser.ParseDocumentAsync(str, token);
8584

8685
return document;
8786

SmartImage.Lib 3/Utilities/NetHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public static Action<FlurlHttpSettings> Configure()
3939
return r =>
4040
{
4141
r.OnError = SearchClient.Client.Settings.OnError;
42-
4342
};
4443
}
4544
}

0 commit comments

Comments
 (0)