Skip to content

Commit d2cd43c

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

File tree

6 files changed

+50
-120
lines changed

6 files changed

+50
-120
lines changed

SmartImage 3/Mode/CliMode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void OnResult(object sender, SearchResult sr)
105105

106106
m_client.OnResult += OnResult;
107107

108-
var run = m_client.RunSearchAsync(m_query, m_cts.Token);
108+
var run = m_client.RunSearchAsync(m_query, token: m_cts.Token);
109109

110110
var sw = Stopwatch.StartNew();
111111

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

Lines changed: 11 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,32 @@ namespace SmartImage.Mode.Shell.Assets;
1818
// todo: possible overkill with caching
1919
internal static partial class UI
2020
{
21-
internal static readonly Attribute Atr_Green_Black = Attribute.Make(Color.Green, Color.Black);
22-
internal static readonly Attribute Atr_Green_White = Attribute.Make(Color.Green, Color.White);
23-
internal static readonly Attribute Atr_Green_Gray = Attribute.Make(Color.Green, Color.Gray);
24-
internal static readonly Attribute Atr_BrightGreen_White = Attribute.Make(Color.BrightGreen, Color.White);
25-
internal static readonly Attribute Atr_BrightGreen_Gray = Attribute.Make(Color.BrightGreen, Color.Gray);
26-
internal static readonly Attribute Atr_BrightRed_White = Attribute.Make(Color.BrightRed, Color.White);
27-
internal static readonly Attribute Atr_BrightRed_Gray = Attribute.Make(Color.BrightRed, Color.Gray);
28-
internal static readonly Attribute Atr_Brown_White = Attribute.Make(Color.Brown, Color.White);
29-
internal static readonly Attribute Atr_Red_Black = Attribute.Make(Color.Red, Color.Black);
30-
internal static readonly Attribute Atr_Red_White = Attribute.Make(Color.Red, Color.White);
31-
internal static readonly Attribute Atr_Red_DarkGray = Attribute.Make(Color.Red, Color.DarkGray);
32-
internal static readonly Attribute Atr_BrightYellow_Black = Attribute.Make(Color.BrightYellow, Color.Black);
33-
21+
internal static readonly Attribute Atr_Green_White = Attribute.Make(Color.Green, Color.White);
22+
internal static readonly Attribute Atr_Green_Gray = Attribute.Make(Color.Green, Color.Gray);
23+
internal static readonly Attribute Atr_BrightGreen_White = Attribute.Make(Color.BrightGreen, Color.White);
24+
internal static readonly Attribute Atr_BrightGreen_Gray = Attribute.Make(Color.BrightGreen, Color.Gray);
25+
internal static readonly Attribute Atr_BrightRed_White = Attribute.Make(Color.BrightRed, Color.White);
26+
internal static readonly Attribute Atr_BrightRed_Gray = Attribute.Make(Color.BrightRed, Color.Gray);
27+
internal static readonly Attribute Atr_Brown_White = Attribute.Make(Color.Brown, Color.White);
28+
internal static readonly Attribute Atr_Red_Black = Attribute.Make(Color.Red, Color.Black);
29+
internal static readonly Attribute Atr_Red_White = Attribute.Make(Color.Red, Color.White);
3430
internal static readonly Attribute Atr_White_Black = Attribute.Make(Color.White, Color.Black);
35-
internal static readonly Attribute Atr_White_Blue = Attribute.Make(Color.White, Color.Blue);
3631
internal static readonly Attribute Atr_White_Cyan = Attribute.Make(Color.White, Color.Cyan);
3732
internal static readonly Attribute Atr_White_DarkGray = Attribute.Make(Color.White, Color.DarkGray);
3833
internal static readonly Attribute Atr_White_BrightCyan = Attribute.Make(Color.White, Color.BrightCyan);
3934
internal static readonly Attribute Atr_BrightCyan_DarkGray = Attribute.Make(Color.BrightCyan, Color.DarkGray);
40-
internal static readonly Attribute Atr_BrightCyan_Gray = Attribute.Make(Color.BrightCyan, Color.Gray);
41-
internal static readonly Attribute Atr_Cyan_Gray = Attribute.Make(Color.Cyan, Color.Gray);
4235
internal static readonly Attribute Atr_Cyan_Black = Attribute.Make(Color.Cyan, Color.Black);
43-
internal static readonly Attribute Atr_Cyan_White = Attribute.Make(Color.Cyan, Color.White);
4436
internal static readonly Attribute Atr_Blue_White = Attribute.Make(Color.Blue, Color.White);
4537
internal static readonly Attribute Atr_Blue_Gray = Attribute.Make(Color.Blue, Color.Gray);
46-
internal static readonly Attribute Atr_BrightBlue_White = Attribute.Make(Color.BrightBlue, Color.White);
4738
internal static readonly Attribute Atr_BrightBlue_Gray = Attribute.Make(Color.BrightBlue, Color.Gray);
48-
internal static readonly Attribute Atr_BrightRed_Black = Attribute.Make(Color.BrightRed, Color.Black);
4939
internal static readonly Attribute Atr_BrightGreen_Black = Attribute.Make(Color.BrightGreen, Color.Black);
50-
internal static readonly Attribute Atr_Black_White = Attribute.Make(Color.Black, Color.White);
5140
internal static readonly Attribute Atr_Gray_Black = Attribute.Make(Color.Gray, Color.Black);
5241
internal static readonly Attribute Atr_DarkGray_Blue = Attribute.Make(Color.DarkGray, Color.Blue);
5342
internal static readonly Attribute Atr_DarkGray_Black = Attribute.Make(Color.DarkGray, Color.Black);
5443
private static readonly Attribute Atr_DarkGray_White = Attribute.Make(Color.DarkGray, Color.White);
5544
internal static readonly Attribute Atr_Black_DarkGray = Attribute.Make(Color.Black, Color.DarkGray);
5645
internal static readonly Attribute Atr_Black_Gray = Attribute.Make(Color.Black, Color.Gray);
5746
internal static readonly Attribute Atr_Brown_Gray = Attribute.Make(Color.Brown, Color.Gray);
58-
internal static readonly Attribute Atr_Brown_DarkGray = Attribute.Make(Color.Brown, Color.DarkGray);
59-
internal static readonly Attribute Atr_Yellow_DarkGray = Attribute.Make(Color.BrightYellow, Color.DarkGray);
60-
internal static readonly Attribute Atr_Yellow_White = Attribute.Make(Color.Brown, Color.White);
6147

6248
internal static readonly ColorScheme Cs_Err = Make(Atr_BrightRed_White, disabled: Atr_BrightRed_Gray);
6349
internal static readonly ColorScheme Cs_Ok = Make(Atr_BrightGreen_White, disabled: Atr_BrightGreen_Gray);
@@ -84,15 +70,6 @@ internal static partial class UI
8470
Focus = Atr_BrightBlue_Gray
8571
};
8672

87-
internal static readonly ColorScheme Cs_Btn2 = new()
88-
{
89-
Normal = Atr_BrightGreen_White,
90-
Disabled = Atr_DarkGray_White,
91-
HotNormal = Atr_BrightGreen_White,
92-
HotFocus = Atr_BrightGreen_Gray,
93-
Focus = Atr_BrightGreen_Gray
94-
};
95-
9673
internal static readonly ColorScheme Cs_Btn2x = new()
9774
{
9875
Normal = Atr_Green_White,
@@ -111,15 +88,6 @@ internal static partial class UI
11188
Focus = Atr_BrightBlue_Gray
11289
};
11390

114-
internal static readonly ColorScheme Cs_Btn4 = new()
115-
{
116-
Normal = Atr_Red_White,
117-
// Disabled = Atr_DarkGray_White,
118-
HotNormal = Atr_Red_White,
119-
HotFocus = Atr_BrightRed_White,
120-
Focus = Atr_BrightRed_White
121-
};
122-
12391
internal static readonly ColorScheme Cs_Elem2 = new()
12492
{
12593
Normal = Atr_White_Cyan,
@@ -135,12 +103,6 @@ internal static partial class UI
135103
HotFocus = Atr_Cyan_Black
136104
};
137105

138-
internal static readonly ColorScheme Cs_Title = new()
139-
{
140-
Normal = Atr_Red_Black,
141-
Focus = Atr_BrightRed_Black
142-
};
143-
144106
internal static readonly ColorScheme Cs_Lbl4 = new()
145107
{
146108
Normal = Atr_Red_Black,
@@ -149,15 +111,6 @@ internal static partial class UI
149111

150112
internal static readonly ColorScheme Cs_Win2 = Make(Atr_White_DarkGray, Atr_Blue_White);
151113

152-
internal static readonly ColorScheme Cs_ListView = new()
153-
{
154-
Disabled = Atr_Gray_Black,
155-
Normal = Atr_White_Black,
156-
HotNormal = Atr_White_Black,
157-
Focus = Atr_Green_Black,
158-
HotFocus = Atr_Green_Black
159-
};
160-
161114
internal static readonly ColorScheme Cs_ListView2 = new()
162115
{
163116
Normal = Atr_Black_Gray,
@@ -173,20 +126,6 @@ internal static partial class UI
173126
Focus = Atr_Cyan_Black,
174127
HotFocus = Atr_Cyan_Black,
175128
};
176-
internal static readonly ColorScheme Cs_Lbl1a = new()
177-
{
178-
Normal = Atr_Black_White,
179-
HotNormal = Atr_Black_White,
180-
Focus = Atr_Blue_Gray,
181-
HotFocus = Atr_Blue_Gray,
182-
};
183-
internal static readonly ColorScheme Cs_Lbl1x = new()
184-
{
185-
Normal = Atr_White_DarkGray,
186-
HotNormal = Atr_White_DarkGray,
187-
Focus = Atr_BrightYellow_Black,
188-
HotFocus = Atr_BrightYellow_Black,
189-
};
190129

191130
internal static readonly ColorScheme Cs_Lbl2 = new()
192131
{
@@ -195,20 +134,6 @@ internal static partial class UI
195134
Focus = Atr_Cyan_Black,
196135
HotFocus = Atr_Cyan_Black,
197136
};
198-
internal static readonly ColorScheme Cs_Lbl2x = new()
199-
{
200-
Normal = Atr_Cyan_Gray,
201-
HotNormal = Atr_Cyan_Gray,
202-
Focus = Atr_BrightCyan_DarkGray,
203-
HotFocus = Atr_BrightCyan_DarkGray,
204-
};
205-
internal static readonly ColorScheme Cs_Lbl3 = new()
206-
{
207-
Normal = Atr_BrightBlue_Gray,
208-
HotNormal = Atr_BrightBlue_Gray,
209-
Focus = Atr_BrightCyan_DarkGray,
210-
HotFocus = Atr_BrightCyan_DarkGray,
211-
};
212137

213138
internal static readonly Border Br_1 = new()
214139
{
@@ -220,16 +145,7 @@ internal static partial class UI
220145
Effect3D = true,
221146
};
222147

223-
internal static readonly ColorScheme Cs_Lbl1_Neutral = new ColorScheme()
224-
{
225-
Normal = Atr_BrightYellow_Black,
226-
HotNormal = Atr_BrightYellow_Black,
227-
Disabled = Cs_Lbl1.Disabled,
228-
Focus = Cs_Lbl1.Focus,
229-
HotFocus = Cs_Lbl1.HotFocus
230-
};
231-
232-
internal static readonly ColorScheme Cs_Lbl1_Success = new ColorScheme()
148+
internal static readonly ColorScheme Cs_Lbl1_Success = new()
233149
{
234150
Normal = Atr_BrightGreen_Black,
235151
HotNormal = Atr_BrightGreen_Black,
@@ -255,12 +171,5 @@ internal static ColorScheme Make(Attribute norm, Attribute? focus = null, Attrib
255171
Disabled = disabled.Value
256172
}.NormalizeHot();
257173
}
258-
259-
internal static void WithScheme(this View v, Action<View> f, ColorScheme cs)
260-
{
261-
var buf = v.ColorScheme;
262-
v.ColorScheme = cs;
263-
f(v);
264-
v.ColorScheme = buf;
265-
}
174+
266175
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ public sealed partial class ShellMode
4343

4444
private static readonly ConcurrentDictionary<object, string> Downloaded = new();
4545

46-
private static ConcurrentDictionary<BaseSearchEngine, ColorScheme> EngineColors = new();
46+
private static readonly ConcurrentDictionary<BaseSearchEngine, ColorScheme> EngineColors = new();
4747

48-
private static ConcurrentDictionary<int, ColorScheme> IndexColors = new();
48+
private static readonly ConcurrentDictionary<int, ColorScheme> IndexColors = new();
49+
50+
private static readonly ConcurrentDictionary<Color, ColorScheme> IndexColors2 = new();
4951

5052
private static ColorScheme GetColor(BaseSearchEngine baseSearchEngine)
5153
{
@@ -57,13 +59,16 @@ private static ColorScheme GetColor(BaseSearchEngine baseSearchEngine)
5759
var cc = ColorValues[
5860
Array.IndexOf(UI.EngineOptions, baseSearchEngine.EngineOption) % UI.EngineOptions.Length];
5961

60-
cs = new ColorScheme()
61-
{
62-
Normal = Attribute.Make(cc, Color.Black),
63-
Focus = Attribute.Make(Color.White, cc),
62+
if (!IndexColors2.TryGetValue(cc, out cs)) {
63+
cs = new ColorScheme()
64+
{
65+
Normal = Attribute.Make(cc, Color.Black),
66+
Focus = Attribute.Make(Color.White, cc),
67+
68+
}.NormalizeHot();
69+
IndexColors2.TryAdd(cc, cs);
70+
}
6471

65-
};
66-
cs = cs.NormalizeHot();
6772
EngineColors.TryAdd(baseSearchEngine, cs);
6873

6974
}

SmartImage 3/Mode/Shell/ShellMode.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Dynamic;
99
using System.Media;
1010
using System.Runtime.Versioning;
11+
using JetBrains.Annotations;
1112
using Kantan.Net.Utilities;
1213
using Kantan.Text;
1314
using Microsoft.Extensions.Logging;
@@ -274,7 +275,7 @@ static ShellMode() { }
274275
#endregion
275276

276277
#region Fields/properties
277-
278+
278279
private object m_cbCallbackTok;
279280

280281
private Func<bool>? m_runIdleTok;
@@ -660,7 +661,7 @@ private void OnCompleteWin(object sender, SearchResult[] results)
660661
IsReady.WaitOne();
661662

662663
// var results = await Client.RunSearchAsync(Query, m_token.Token);
663-
await Client.RunSearchAsync(Query, m_token.Token);
664+
await Client.RunSearchAsync(Query, token: m_token.Token);
664665

665666
Status = false;
666667

@@ -1078,10 +1079,20 @@ public void Dispose()
10781079

10791080
internal SearchResultItem? FindResultByUrl(Url v)
10801081
{
1081-
// todo: optimize
1082-
var sri = m_results.SelectMany(s => s.Results)
1083-
.FirstOrDefault(r => v.Equals(r.Url));
1082+
using var e = m_results.GetEnumerator();
1083+
1084+
while (e.MoveNext()) {
1085+
var c = e.Current;
10841086

1085-
return sri;
1087+
for (int i = 0; i < c.Results.Count; i++) {
1088+
var sr = c.Results[i];
1089+
1090+
if (v.Equals(sr.Url)) {
1091+
return sr;
1092+
}
1093+
}
1094+
}
1095+
1096+
return null;
10861097
}
10871098
}

SmartImage.Lib 3/Engines/WebSearchEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Flurl.Http;
66
using JetBrains.Annotations;
77
using Kantan.Net.Utilities;
8+
using SmartImage.Lib.Model;
89
using SmartImage.Lib.Results;
910

1011
namespace SmartImage.Lib.Engines;

SmartImage.Lib 3/SearchClient.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ static SearchClient()
9494
/// Runs a search of <paramref name="query"/>.
9595
/// </summary>
9696
/// <param name="query">Search query</param>
97-
/// <param name="token">Cancellation token passed to <see cref="BaseSearchEngine.GetResultAsync"/></param>
9897
/// <param name="p"><see cref="IProgress{T}"/></param>
99-
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, CancellationToken token = default,
100-
[CBN] IProgress<int> p = null)
98+
/// <param name="token">Cancellation token passed to <see cref="BaseSearchEngine.GetResultAsync"/></param>
99+
public async Task<SearchResult[]> RunSearchAsync(SearchQuery query, [CBN] IProgress<int> p = null,
100+
CancellationToken token = default)
101101
{
102102
IsRunning = true;
103103

@@ -234,6 +234,8 @@ public BaseSearchEngine[] LoadEngines()
234234
[CBN]
235235
public BaseSearchEngine TryGetEngine(SearchEngineOptions o) => Engines.FirstOrDefault(e => e.EngineOption == o);
236236

237+
#region
238+
237239
public static ValueTask<IReadOnlyList<SearchResultItem>> Filter(IEnumerable<SearchResultItem> sri)
238240
{
239241
var sri2 = sri.AsParallel().DistinctBy(e => e.Url).ToList();
@@ -291,6 +293,8 @@ public static async Task<IReadOnlyList<UniSource>> GetDirectImagesAsync(IEnumera
291293
return di.AsReadOnly();
292294
}
293295

296+
#endregion
297+
294298
public void Dispose()
295299
{
296300
foreach (var engine in Engines) {

0 commit comments

Comments
 (0)