Skip to content

Commit bfce15e

Browse files
authored
Feature/new script (#32)
* Convert script settings into a LiteDb (#31) * Rename Settings project * Quick fix and improvements in Script execution * Move Scripts file to db * Move scripts db to installation path Co-authored-by: Yeray <Yeray> * Fix start minimized not working * Fix style issues when playing with the form size * Add new On Startup script type * Tweaks * Rename publish exe file name --------- Co-authored-by: Yeray <Yeray>
1 parent f93e50b commit bfce15e

30 files changed

Lines changed: 531 additions & 370 deletions

Library/App/App.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<Nullable>enable</Nullable>
77
<UseWindowsForms>true</UseWindowsForms>
88
<ImplicitUsings>enable</ImplicitUsings>
9+
<AssemblyName>CommonScripts</AssemblyName>
910
</PropertyGroup>
1011

1112
<ItemGroup>
@@ -37,9 +38,9 @@
3738
<ItemGroup>
3839
<ProjectReference Include="..\Common\Common.csproj" />
3940
<ProjectReference Include="..\Contracts\Contracts.csproj" />
41+
<ProjectReference Include="..\Data\Data.csproj" />
4042
<ProjectReference Include="..\JobManager\JobManager.csproj" />
4143
<ProjectReference Include="..\Logging\Logging.csproj" />
42-
<ProjectReference Include="..\Settings\Data.csproj" />
4344
</ItemGroup>
4445

4546
<ItemGroup>

Library/App/Extension/RichTextBoxExtensions.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ public static void AppendText(this RichTextBox box, string text, Color color, bo
1414
}
1515
public static void AppendTextThreadSafe(this RichTextBox box, string text, Color color, bool addNewLine = false)
1616
{
17-
box.Invoke((MethodInvoker)delegate ()
17+
if (box.InvokeRequired)
18+
{
19+
box.Invoke((MethodInvoker)delegate ()
20+
{
21+
box.AppendText(text, color, addNewLine);
22+
});
23+
}
24+
else
1825
{
1926
box.AppendText(text, color, addNewLine);
20-
});
27+
}
2128
}
2229
public static void ColorLine(this RichTextBox box, int lineNumber, int lineLength, Color color)
2330
{
@@ -26,10 +33,17 @@ public static void ColorLine(this RichTextBox box, int lineNumber, int lineLengt
2633
}
2734
public static void ColorLineThreadSafe(this RichTextBox box, int lineNumber, int lineLength, Color color)
2835
{
29-
box.Invoke((MethodInvoker)delegate ()
36+
if (box.InvokeRequired)
37+
{
38+
box.Invoke((MethodInvoker)delegate ()
39+
{
40+
box.ColorLine(lineNumber, lineLength, color);
41+
});
42+
}
43+
else
3044
{
3145
box.ColorLine(lineNumber, lineLength, color);
32-
});
46+
}
3347
}
3448
}
3549
}

Library/App/Forms/Base/BaseForm.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ public partial class BaseForm : MaterialForm
99
private const int WS_MINIMIZEBOX = 0x20000;
1010
private const int CS_DBLCLKS = 0x8;
1111

12-
public BaseForm()
12+
protected BaseForm()
1313
{
14-
InitializeComponent();
15-
Utils.DropShadow.ApplyShadows(this);
1614
MaterialSkinManager.Instance.AddFormToManage(this);
1715
}
16+
protected override void OnLoad(EventArgs e)
17+
{
18+
base.OnLoad(e);
19+
ApplyFormStyle();
20+
}
21+
1822
protected override CreateParams CreateParams
1923
{
2024
get
@@ -29,6 +33,26 @@ protected override CreateParams CreateParams
2933
}
3034
}
3135

36+
protected override void OnResize(EventArgs e)
37+
{
38+
base.OnResize(e);
39+
if (this.WindowState == FormWindowState.Maximized)
40+
Invalidate();
41+
}
42+
43+
protected override void OnResizeEnd(EventArgs e)
44+
{
45+
base.OnResizeEnd(e);
46+
if (this.WindowState == FormWindowState.Normal)
47+
ApplyFormStyle();
48+
49+
}
50+
51+
protected void ApplyFormStyle()
52+
{
53+
Utils.DropShadow.ApplyShadows(this);
54+
}
55+
3256
public DialogResult ShowDialogCenter(IWin32Window form)
3357
{
3458
StartPosition = FormStartPosition.CenterParent;

Library/App/Forms/MainForm/MainForm.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using App.Utils;
44
using Contracts.Scripts.Base;
55
using Data;
6+
using Data.Service.Interfaces;
67
using JobManager.Service;
78
using MaterialSkin.Controls;
89

@@ -12,18 +13,21 @@ public partial class MainForm : BaseForm
1213
{
1314
private readonly TrayContextMenu _trayContextMenu;
1415
private readonly IWindowsRegistryService _windowsRegistryService;
16+
public bool RunOnStartup { get; set; }
1517

16-
public MainForm(IRunScriptService runScriptService, IWindowsRegistryService windowsRegistryService)
18+
public MainForm(IRunScriptService runScriptService, IWindowsRegistryService windowsRegistryService, IScriptsService scriptsService)
1719
{
1820
InitializeComponent();
1921
_windowsRegistryService = windowsRegistryService;
2022

21-
_trayContextMenu = new TrayContextMenu();
22-
ConfigureTrayContextMenu();
23+
var scripts = runTabControl.SetScriptsService(scriptsService);
2324
runTabControl.SetRunScriptService(runScriptService);
2425
runTabControl.ScriptEdited += RunTabControl_ScriptEdited;
2526
runTabControl.ScriptAdded += RunTabControl_ScriptAdded;
2627
runTabControl.ScriptRemoved += RunTabControl_ScriptRemoved;
28+
29+
_trayContextMenu = new TrayContextMenu();
30+
ConfigureTrayContextMenu(scripts);
2731
}
2832

2933
protected override void WndProc(ref Message m)
@@ -52,6 +56,10 @@ protected async override void OnLoad(EventArgs e)
5256
{
5357
base.OnLoad(e);
5458
await ShowRunAtStartupDialogIfNeededAsync();
59+
if (RunOnStartup)
60+
{
61+
runTabControl.RunScriptsOnStartupAsync();
62+
}
5563
}
5664
private void SettingsTab_Open(object sender, EventArgs e)
5765
{
@@ -97,13 +105,14 @@ private void ShowForm()
97105
bool currentTop = TopMost;
98106
TopMost = true;
99107
TopMost = currentTop;
108+
ApplyFormStyle();
100109
}
101-
private void ConfigureTrayContextMenu()
110+
private void ConfigureTrayContextMenu(List<ScriptAbs> scripts)
102111
{
103112
_trayContextMenu.CloseClicked += ContextMenu_Close_Click;
104113
_trayContextMenu.OpenClicked += ContextMenu_Open_Click;
105114
_trayContextMenu.ScriptStatusClicked += ContextMenu_StatusClick;
106-
_trayContextMenu.LoadScriptList(SettingsManager.Scripts);
115+
_trayContextMenu.LoadScriptList(scripts);
107116
this.appNotifyIcon.ContextMenuStrip = _trayContextMenu;
108117
}
109118
private void ChangeScriptStatus(ScriptAbs script)

Library/App/Forms/MainForm/Tabs/Run/RunTabControl.cs

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Contracts.Key;
44
using Contracts.Scripts;
55
using Contracts.Scripts.Base;
6-
using Data;
6+
using Data.Service.Interfaces;
77
using JobManager.Service;
88
using MaterialSkin.Controls;
99
using Serilog;
@@ -20,32 +20,58 @@ public partial class RunTabControl : UserControl
2020
private readonly ScriptListAdapter _scriptListAdapter;
2121
private readonly ListenKeysService _listenKeysService;
2222
private IRunScriptService? _runScriptService;
23+
private IScriptsService? _scriptsService;
24+
private List<ScriptAbs> _scripts;
2325
public RunTabControl()
2426
{
2527
InitializeComponent();
2628

29+
_scripts = new();
30+
2731
_scriptListAdapter = new ScriptListAdapter(pnlScripts);
2832
_scriptListAdapter.ShowMenu += ShowContextMenuScript;
2933
_scriptListAdapter.StatusClicked += ChangeScriptStatusClicked;
3034

3135
_listenKeysService = ListenKeysService.GetInstance();
3236
_listenKeysService.KeyUpClicked += ListenKeysService_KeyUpClicked;
3337
}
38+
39+
#region Public methods
3440
public void SetRunScriptService(IRunScriptService runScriptService)
3541
{
3642
_runScriptService = runScriptService;
3743
_runScriptService.OneOffScriptExecuted += OneOffJobWasExecuted;
3844
}
45+
public List<ScriptAbs> SetScriptsService(IScriptsService scriptsService)
46+
{
47+
_scriptsService = scriptsService;
48+
_scripts = _scriptsService.GetScripts();
49+
return _scripts;
50+
}
51+
public async void RefreshScriptStatusAsync(string scriptId)
52+
{
53+
var script = _scripts.FirstOrDefault(s => s.Id == scriptId)?.Clone();
54+
await ChangeScriptStatusAsync(script);
55+
}
56+
public async void RunScriptsOnStartupAsync()
57+
{
58+
var scripts = GetRunningScriptsByType<ScriptOnStartup>();
59+
foreach (var script in scripts)
60+
{
61+
await _runScriptService!.RunScriptAsync(script);
62+
}
63+
}
64+
#endregion
3965
#region Events
4066
private async void OnLoad(object sender, EventArgs e)
4167
{
4268
if (this.DesignMode) return;
43-
_scriptListAdapter.CreateWithList(SettingsManager.Scripts);
44-
await CheckIfScriptsNeedToRunOnLoad();
69+
_scriptListAdapter.CreateWithList(_scripts);
70+
await CheckIfScriptsNeedToRunOnLoadAsync();
4571
}
4672
private void OneOffJobWasExecuted(string scriptId)
4773
{
48-
var script = SettingsManager.FindScriptById(scriptId);
74+
var script = _scripts.FirstOrDefault(s => s.Id == scriptId)?.Clone();
4975
if (script != null)
5076
ChangeScriptStatusThreadSafe(script);
5177
}
@@ -74,7 +100,7 @@ private async void ContextMenuItemClicked(object sender, ToolStripItemClickedEve
74100
}
75101
else if (e.ClickedItem.Text == "Remove")
76102
{
77-
await ShowRemoveScriptDialogAsync(script);
103+
ShowRemoveScriptDialog(script);
78104
}
79105
}
80106
private void ListenKeysService_KeyUpClicked(KeyPressed keyPressed)
@@ -93,18 +119,18 @@ private void ListenKeysService_KeyUpClicked(KeyPressed keyPressed)
93119
#endregion
94120

95121
#region Private Methods
96-
private async Task CheckIfScriptsNeedToRunOnLoad()
122+
private async Task CheckIfScriptsNeedToRunOnLoadAsync()
97123
{
98124
foreach (var script in GetRunningScriptsByType<ScriptScheduled>())
99125
await _runScriptService!.RunScriptAsync(script);
100126

101127
if (IsListenKeyServiceNecessary())
102128
_listenKeysService.Run();
103129
}
104-
private static bool IsListenKeyServiceNecessary(string? executingScriptId = null)
130+
private bool IsListenKeyServiceNecessary(string? executingScriptId = null)
105131
=> GetRunningScriptsByType<ScriptListenKey>()?.Any(s => s.Id != executingScriptId) ?? false;
106-
private static IEnumerable<T> GetRunningScriptsByType<T>()
107-
where T : ScriptAbs => SettingsManager.Scripts.OfType<T>().Where(s => s.ScriptStatus == ScriptStatus.Running);
132+
private IEnumerable<T> GetRunningScriptsByType<T>()
133+
where T : ScriptAbs => _scripts.OfType<T>().Where(s => s.ScriptStatus == ScriptStatus.Running);
108134
private void ChangeScriptStatusThreadSafe(ScriptAbs script)
109135
=> this.Invoke((MethodInvoker) async delegate () { await ChangeScriptStatusAsync(script); });
110136
private async Task ShowEditScriptFormAsync(ScriptAbs script)
@@ -114,7 +140,7 @@ await ShowScriptForm(script, async (ScriptAbs? editedScript) => {
114140
return;
115141
Log.Debug("Editing Script {@ScriptName} ({@ScriptType})", editedScript.ScriptName, editedScript.ScriptType);
116142
bool rescheduleJob = ScriptAbs.HasScriptTypeChanged(script.ScriptType, editedScript.ScriptType) || ScriptAbs.HasScheduledTimeChanged(script, editedScript);
117-
await SettingsManager.EditScriptAsync(editedScript);
143+
UpdateScript(editedScript);
118144
if (editedScript.ScriptStatus == ScriptStatus.Running && rescheduleJob)
119145
{
120146
await _runScriptService!.StopScriptAsync(editedScript);
@@ -129,23 +155,24 @@ await ShowScriptForm(script, async (ScriptAbs? editedScript) => {
129155
}
130156
private async Task ShowAddScriptForm()
131157
{
132-
await ShowScriptForm(null, async (ScriptAbs? addedScript) => {
158+
await ShowScriptForm(null, (ScriptAbs? addedScript) => {
133159
if (addedScript == null)
134-
return;
160+
return Task.CompletedTask;
135161
Log.Debug("Adding Script {@ScriptName} ({@ScriptType})", addedScript.ScriptName, addedScript.ScriptType);
136162
addedScript.Id = Guid.NewGuid().ToString();
137-
await SettingsManager.AddScriptAsync(addedScript);
163+
AddScript(addedScript);
138164
_scriptListAdapter.AddItem(addedScript);
139165
ScriptAdded?.Invoke(addedScript);
166+
return Task.CompletedTask;
140167
});
141168
}
142-
private async Task ShowRemoveScriptDialogAsync(ScriptAbs script)
169+
private void ShowRemoveScriptDialog(ScriptAbs script)
143170
{
144171
var dialog = new MaterialDialog(this.ParentForm, "Remove", "Do you want to remove the script?", "Yes", true, "No");
145172
if (dialog.ShowDialog(this) == DialogResult.OK)
146173
{
147174
Log.Debug("Removing ScriptId {@ScriptId}", script.Id);
148-
await SettingsManager.RemoveScriptAsync(script.Id);
175+
DeleteScript(script.Id);
149176
_scriptListAdapter.RemoveItem(script.Id);
150177
ScriptRemoved?.Invoke(script);
151178
}
@@ -182,7 +209,7 @@ private async Task ChangeScriptStatusAsync(ScriptAbs? script)
182209
await _runScriptService!.StopScriptAsync(script);
183210
}
184211

185-
await SettingsManager.EditScriptAsync(script);
212+
UpdateScript(script);
186213

187214
_scriptListAdapter.RefreshScriptStatus(script.Id, script.ScriptStatus);
188215
ScriptEdited?.Invoke(script);
@@ -204,13 +231,26 @@ private static ScriptStatus GetNewScriptStatus(ScriptStatus oldStatus)
204231
}
205232
return newStatus;
206233
}
207-
#endregion
208-
209-
#region Public methods
210-
public async void RefreshScriptStatusAsync(string scriptId)
234+
private void AddScript(ScriptAbs addedScript)
211235
{
212-
var script = SettingsManager.FindScriptById(scriptId);
213-
await ChangeScriptStatusAsync(script);
236+
_scriptsService!.AddScript(addedScript);
237+
_scripts.Add(addedScript);
238+
}
239+
private void UpdateScript(ScriptAbs editedScript)
240+
{
241+
_scriptsService!.UpdateScript(editedScript);
242+
int index = _scripts.FindIndex(s => s.Id == editedScript.Id);
243+
if (index == -1)
244+
return;
245+
_scripts[index] = editedScript;
246+
}
247+
private void DeleteScript(string scriptId)
248+
{
249+
_scriptsService!.DeleteScript(scriptId);
250+
var scriptToDelete = _scripts.FirstOrDefault(s => s.Id == scriptId);
251+
if (scriptToDelete == null)
252+
return;
253+
_scripts.Remove(scriptToDelete);
214254
}
215255
#endregion
216256

0 commit comments

Comments
 (0)