Skip to content

Commit 2979ce0

Browse files
committed
refactoring
code cleanup
1 parent 518729f commit 2979ce0

26 files changed

+1987
-1948
lines changed

SmartImage 3/Shell/UI.Styles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// ReSharper disable InconsistentNaming
55

66
namespace SmartImage.Shell;
7-
7+
// todo: possible overkill with caching
88
internal static partial class UI
99
{
1010
#region Attributes

SmartImage 3/Shell/UI.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static partial class UI
1212
{
1313
internal static bool QueueProgress(CancellationTokenSource cts, ProgressBar pbr, Action<object>? f = null)
1414
{
15-
return ThreadPool.QueueUserWorkItem((state) =>
15+
return ThreadPool.QueueUserWorkItem(state =>
1616
{
1717
while (state is CancellationToken { IsCancellationRequested: false }) {
1818
pbr.Pulse();
@@ -25,7 +25,7 @@ internal static bool QueueProgress(CancellationTokenSource cts, ProgressBar pbr,
2525

2626
internal static Button CreateLinkButton(Dialog d, string text, string? url = null, Action? urlAction = null)
2727
{
28-
var b = new Button()
28+
var b = new Button
2929
{
3030
Text = text,
3131
AutoSize = true,
@@ -47,16 +47,16 @@ internal static void SetLabelStatus(Label lbl, bool? b)
4747

4848
switch (b) {
4949
case null:
50-
lbl.Text = UI.NA;
51-
lbl.ColorScheme = UI.Cs_NA;
50+
lbl.Text = NA;
51+
lbl.ColorScheme = Cs_NA;
5252
break;
5353
case true:
54-
lbl.Text = UI.OK;
55-
lbl.ColorScheme = UI.Cs_Ok;
54+
lbl.Text = OK;
55+
lbl.ColorScheme = Cs_Ok;
5656
break;
5757
case false:
58-
lbl.Text = UI.Err;
59-
lbl.ColorScheme = UI.Cs_Err;
58+
lbl.Text = Err;
59+
lbl.ColorScheme = Cs_Err;
6060
break;
6161
}
6262
}

SmartImage 3/ShellMain.Dialog.cs

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
// Read Stanton SmartImage ShellMain.Dialog.cs
2+
// 2023-01-13 @ 11:28 PM
3+
4+
using System.Data;
5+
using Kantan.Console;
6+
using Novus.OS;
7+
using NStack;
8+
using SmartImage.App;
9+
using SmartImage.Lib;
10+
using SmartImage.Lib.Engines;
11+
using SmartImage.Shell;
12+
using Terminal.Gui;
13+
14+
namespace SmartImage;
15+
16+
public sealed partial class ShellMain
17+
{
18+
private void AboutDialog()
19+
{
20+
var d = new Dialog()
21+
{
22+
Text = $"{R2.Name} {Integration.Version} by {R2.Author}\n" +
23+
$"Current directory: {Integration.CurrentAppFolder}",
24+
25+
Title = R2.Name,
26+
AutoSize = true,
27+
Width = UI.Dim_30_Pct,
28+
Height = UI.Dim_30_Pct,
29+
};
30+
31+
var b1 = UI.CreateLinkButton(d, "Repo", R2.Repo_Url);
32+
var b2 = UI.CreateLinkButton(d, "Wiki", R2.Wiki_Url);
33+
var b3 = UI.CreateLinkButton(d, "Ok", null, () => Application.RequestStop());
34+
35+
d.AddButton(b1);
36+
d.AddButton(b2);
37+
d.AddButton(b3);
38+
39+
Application.Run(d);
40+
}
41+
42+
/// <summary>
43+
/// <see cref="Btn_Config"/>
44+
/// </summary>
45+
private void ConfigDialog()
46+
{
47+
var dlCfg = new Dialog("Configuration")
48+
{
49+
Text = ustring.Empty,
50+
AutoSize = false,
51+
Width = UI.Dim_80_Pct,
52+
Height = UI.Dim_80_Pct,
53+
};
54+
55+
var btnRefresh = new Button("Refresh") { };
56+
var btnSave = new Button("Save") { };
57+
var btnOk = new Button("Ok") { };
58+
59+
const int WIDTH = 15;
60+
const int HEIGHT = 17;
61+
62+
/*============================================================================*\
63+
Engines
64+
\*============================================================================*/
65+
66+
Label lbSearchEngines = new(R1.S_SearchEngines)
67+
{
68+
X = 0,
69+
Y = 0,
70+
AutoSize = true,
71+
ColorScheme = UI.Cs_Lbl1
72+
};
73+
74+
ListView lvSearchEngines = new(UI.EngineOptions)
75+
{
76+
AllowsMultipleSelection = true,
77+
AllowsMarking = true,
78+
AutoSize = true,
79+
Width = WIDTH,
80+
Height = HEIGHT,
81+
82+
Y = Pos.Bottom(lbSearchEngines)
83+
};
84+
85+
Label lbPriorityEngines = new(R1.S_PriorityEngines)
86+
{
87+
X = Pos.Right(lbSearchEngines) + 1,
88+
Y = 0,
89+
AutoSize = true,
90+
ColorScheme = UI.Cs_Lbl1
91+
};
92+
93+
ListView lvPriorityEngines = new(UI.EngineOptions)
94+
{
95+
AllowsMultipleSelection = true,
96+
AllowsMarking = true,
97+
AutoSize = true,
98+
Width = WIDTH,
99+
Height = HEIGHT,
100+
101+
Y = Pos.Bottom(lbPriorityEngines),
102+
X = Pos.Right(lvSearchEngines) + 1
103+
};
104+
105+
var cfgInfo = new FileInfo(SearchConfig.Configuration.FilePath);
106+
107+
Label lbConfig = new($"Config")
108+
{
109+
X = Pos.Right(lbPriorityEngines) + 1,
110+
Y = 0,
111+
AutoSize = true,
112+
ColorScheme = UI.Cs_Lbl1
113+
// Height = 10,
114+
};
115+
116+
lbConfig.Clicked += () =>
117+
{
118+
FileSystem.ExploreFile(cfgInfo.FullName);
119+
};
120+
121+
DataTable dtConfig = Config.ToTable();
122+
123+
var tvConfig = new TableView(dtConfig)
124+
{
125+
AutoSize = true,
126+
Y = Pos.Bottom(lbConfig),
127+
X = Pos.Right(lvPriorityEngines) + 1,
128+
Width = Dim.Fill(WIDTH),
129+
Height = 9,
130+
};
131+
132+
void ReloadDialog()
133+
{
134+
tvConfig.Table = Config.ToTable();
135+
tvConfig.SetNeedsDisplay();
136+
dlCfg.SetNeedsDisplay();
137+
}
138+
139+
lvSearchEngines.OpenSelectedItem += args1 =>
140+
{
141+
SearchEngineOptions e = Config.SearchEngines;
142+
UI.OnEngineSelected(args1, ref e, lvSearchEngines);
143+
Config.SearchEngines = e;
144+
ReloadDialog();
145+
};
146+
147+
lvPriorityEngines.OpenSelectedItem += args1 =>
148+
{
149+
SearchEngineOptions e = Config.PriorityEngines;
150+
UI.OnEngineSelected(args1, ref e, lvPriorityEngines);
151+
Config.PriorityEngines = e;
152+
ReloadDialog();
153+
};
154+
155+
lvSearchEngines.FromEnum(Config.SearchEngines);
156+
lvPriorityEngines.FromEnum(Config.PriorityEngines);
157+
158+
/*============================================================================*\
159+
Checkboxes
160+
\*============================================================================*/
161+
162+
CheckBox cbContextMenu = new(R2.Int_ContextMenu)
163+
{
164+
X = Pos.X(tvConfig),
165+
Y = Pos.Bottom(tvConfig) + 1,
166+
Width = WIDTH,
167+
Height = 1,
168+
ColorScheme = UI.Cs_Btn3
169+
};
170+
171+
cbContextMenu.Toggled += b =>
172+
{
173+
Integration.HandleContextMenu(!b);
174+
};
175+
176+
Label lbHelp = new($"{UI.Line} Arrow keys or mouse :: select option\n" +
177+
$"{UI.Line} Space bar or click :: toggle mark option\n" +
178+
$"{UI.Line} Enter :: save option")
179+
{
180+
AutoSize = true,
181+
182+
X = 0,
183+
Y = Pos.Bottom(lvSearchEngines) + 1
184+
};
185+
186+
CheckBox cbOnTop = new(R1.S_OnTop)
187+
{
188+
X = Pos.Right(cbContextMenu) + 1,
189+
Y = Pos.Y(cbContextMenu),
190+
AutoSize = true,
191+
ColorScheme = UI.Cs_Btn3,
192+
Height = 1,
193+
};
194+
195+
cbOnTop.Toggled += b =>
196+
{
197+
Integration.KeepOnTop(!b);
198+
Config.OnTop = Integration.IsOnTop;
199+
ReloadDialog();
200+
};
201+
202+
CheckBox cbAutoSearch = new(R1.S_AutoSearch)
203+
{
204+
X = Pos.Right(cbOnTop) + 1,
205+
Y = Pos.Y(cbOnTop),
206+
// Width = WIDTH,
207+
Height = 1,
208+
AutoSize = true,
209+
ColorScheme = UI.Cs_Btn3
210+
211+
};
212+
213+
cbAutoSearch.Toggled += b =>
214+
{
215+
m_autoSearch = b;
216+
ReloadDialog();
217+
};
218+
219+
cbContextMenu.Checked = Integration.IsContextMenuAdded;
220+
cbOnTop.Checked = Config.OnTop;
221+
222+
/*============================================================================*\
223+
Eh username/password
224+
\*============================================================================*/
225+
226+
Label lbEhUsername = new(R1.S_EhUsername)
227+
{
228+
X = Pos.X(cbContextMenu),
229+
Y = Pos.Bottom(cbContextMenu) + 1,
230+
CanFocus = false,
231+
ColorScheme = UI.Cs_Lbl1
232+
};
233+
234+
TextField tfEhUsername = new()
235+
{
236+
X = Pos.Right(lbEhUsername) + 1,
237+
Y = Pos.Y(lbEhUsername),
238+
Width = WIDTH * 2,
239+
Height = 1,
240+
};
241+
242+
tfEhUsername.TextChanging += args =>
243+
{
244+
Config.EhUsername = args.NewText.ToString();
245+
ReloadDialog();
246+
};
247+
248+
Label lbEhPassword = new(R1.S_EhPassword)
249+
{
250+
X = Pos.X(lbEhUsername),
251+
Y = Pos.Bottom(lbEhUsername),
252+
CanFocus = false,
253+
ColorScheme = UI.Cs_Lbl1
254+
};
255+
256+
TextField tfEhPassword = new()
257+
{
258+
X = Pos.Right(lbEhPassword) + 1,
259+
Y = Pos.Y(lbEhPassword),
260+
Width = WIDTH * 2,
261+
Height = 1,
262+
};
263+
264+
tfEhPassword.TextChanging += args =>
265+
{
266+
Config.EhPassword = args.NewText.ToString();
267+
ReloadDialog();
268+
};
269+
270+
/*============================================================================*\
271+
Register
272+
\*============================================================================*/
273+
274+
btnRefresh.Clicked += ReloadDialog;
275+
276+
btnOk.Clicked += () =>
277+
{
278+
Application.RequestStop();
279+
};
280+
281+
btnSave.Clicked += () =>
282+
{
283+
Config.Save();
284+
ReloadDialog();
285+
};
286+
287+
dlCfg.Add(tvConfig, lvSearchEngines, lvPriorityEngines,
288+
cbContextMenu, cbOnTop, lbConfig, lbSearchEngines, lbPriorityEngines,
289+
lbHelp, cbAutoSearch, lbEhUsername, tfEhUsername, lbEhPassword, tfEhPassword);
290+
291+
dlCfg.AddButton(btnRefresh);
292+
dlCfg.AddButton(btnOk);
293+
dlCfg.AddButton(btnSave);
294+
295+
Application.Run(dlCfg);
296+
297+
Tf_Input.SetFocus();
298+
Tf_Input.EnsureFocus();
299+
}
300+
}

0 commit comments

Comments
 (0)