Skip to content

Commit

Permalink
added DefaultButton no for script execution cancellation
Browse files Browse the repository at this point in the history
improved formatting
Progress is now only reported at the beginning of the execution of a script -> this fixed various issues with cancellation and logging
  • Loading branch information
5cover committed Jul 18, 2022
1 parent 6800689 commit 7fdcf92
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 22 deletions.
1 change: 0 additions & 1 deletion BusinessLogic/ScriptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ await Task.Run(() =>
{
ReportProgress();
scripts[scriptIndex].Execute(keepRunningOrKill, _cts);
ReportProgress();
void ReportProgress() => ((IProgress<ScriptExecutionProgressChangedEventArgs>)_progress).Report(new(scriptIndex));
}
Expand Down
13 changes: 10 additions & 3 deletions BusinessLogic/Scripts/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,34 @@ private Host(string executable)
public void ExecuteCode(string code, string scriptName, TimeSpan timeout, HungScriptCallback keepRunningOrKill, CancellationTokenSource? cancellationToken)
{
FileInfo tmpScriptFile = CreateTempFile(code, SupportedExtensions.First());
bool hostDisposed = false;

using Process host = ExecuteHost(tmpScriptFile);

host.Disposed += (_, _) => hostDisposed = true;
// If the operation is cancelled, kill the host process. Then, WaitForExit() will take care of it.
var registration = cancellationToken?.Token.Register(Kill);

while (true)
{
if (host.WaitForExit(Convert.ToInt32(timeout.TotalMilliseconds)))
{
registration?.Unregister();
break;
}

if (keepRunningOrKill(scriptName)) continue;
Kill();
break;
}
registration?.Unregister();
tmpScriptFile.Delete();

void Kill() => host.Kill(true);
void Kill()
{
if (!hostDisposed)
{
host.Kill(true);
}
}
}

private static FileInfo CreateTempFile(string text, string extension)
Expand Down
19 changes: 19 additions & 0 deletions Presentation/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,26 @@ private static void ShowUnhandledExceptionDialog(Exception e)
private void ApplicationExit(object? sender, ExitEventArgs? e)
{
Scripts.Save();
AppInfo.Settings.Save();


Happenings.Exit.SetAsHappening();
Logs.Exiting.Log();
}

private void ApplicationStartup(object? sender, StartupEventArgs? e)
{
if (e?.Args.Any() ?? false)
{
StartConsole(e.Args);
}
else
{
StartGui();
}
}

private static void StartGui()
{
AppInfo.ReadAppFileRetryOrFail = (ex, verb, info) =>
{
Expand All @@ -82,4 +96,9 @@ private void ApplicationStartup(object? sender, StartupEventArgs? e)
Logs.Started.Log();
new MainWindow().Show();
}

private static void StartConsole(IEnumerable<string> args)
{
//Environment.ExitCode = new CommandLineInterpreter(args).Execute();
}
}
5 changes: 1 addition & 4 deletions Presentation/Dialogs/DialogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ public static Dialog MakeInvalidScriptDataDialog(Exception e, string path, param
EnableHyperlinks = true,
ExpandedInformation = e.ToString()
};
dialog.HyperlinkClicked += (_, _) =>
{
Helpers.Open(path);
};
dialog.HyperlinkClicked += (_, _) => Helpers.Open(path);
return dialog;
}

Expand Down
5 changes: 3 additions & 2 deletions Presentation/ScriptExecution/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ProgressDialog(IReadOnlyList<Script> scripts) : base(Button.Stop)
MinimizeBox = true;
ExpandedInformation = Resources.UI.ProgressDialog.ExpandedInformation.FormatWith(null, null);
ExpandedByDefault = AppInfo.Settings.DetailsDuringExecution;
ProgressBarMaximum = scripts.Count - 1;
ProgressBarMaximum = scripts.Count;
ProgressBarStyle = ProgressBarStyle.ProgressBar;
Content = Resources.UI.ProgressDialog.Content;
VerificationText = Resources.UI.ProgressDialog.VerificationText;
Expand All @@ -32,7 +32,8 @@ public ProgressDialog(IReadOnlyList<Script> scripts) : base(Button.Stop)
SetConfirmation(Button.Stop, () => new Dialog(Button.Yes, Button.No)
{
MainIcon = TaskDialogIcon.Warning,
Content = Resources.UI.Dialogs.ConfirmAbortOperationContent
Content = Resources.UI.Dialogs.ConfirmAbortOperationContent,
DefaultButton = Button.No
}.ShowDialog() == Button.Yes);

RaiseTimerEvent = true;
Expand Down
2 changes: 1 addition & 1 deletion Presentation/ScriptExecution/ScriptExecutionWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ScriptExecutionWizard(IEnumerable<Script> scripts)
{
throw new ArgumentException(DevException.CollectionEmpty, nameof(scripts));
}
executor.ProgressChanged += (_, e) => Logs.ScriptExecuted.FormatWith(_scripts[e.ScriptIndex]);
executor.ProgressChanged += (_, e) => Logs.ScriptExecuting.FormatWith(_scripts[e.ScriptIndex]);
}

/// <summary>Executes the script(s) and displays a dialog tracking the progress.</summary>
Expand Down
9 changes: 4 additions & 5 deletions Presentation/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<Window
x:Class="Scover.WinClean.Presentation.Windows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:businessLogic="clr-namespace:Scover.WinClean.BusinessLogic"
xmlns:scripts="clr-namespace:Scover.WinClean.BusinessLogic.Scripts"
xmlns:controls="clr-namespace:Scover.WinClean.Presentation.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:scripts="clr-namespace:Scover.WinClean.BusinessLogic.Scripts"
xmlns:ui="clr-namespace:Scover.WinClean.Resources.UI"
Title="{x:Static businessLogic:AppInfo.Name}"
Width="{Binding MinWidth, RelativeSource={RelativeSource Self}}"
Height="{Binding MinHeight, RelativeSource={RelativeSource Self}}"
MinWidth="{Binding MinHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource GoldenRatioConverter}}"
MinHeight="369"
Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
Closed="Window_Closed"
mc:Ignorable="d">
<Window.Resources>
<Style TargetType="{x:Type ListBox}">
Expand Down
21 changes: 21 additions & 0 deletions Presentation/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public partial class MainWindow
public MainWindow()
{
InitializeComponent();
RestoreSizeAndLocation();
ResetTabs();
}

Expand Down Expand Up @@ -146,6 +147,24 @@ private void ResetTabs()
TabControlCategories.SelectedIndex = selectedIndex;
}

private void RestoreSizeAndLocation()
{
Top = AppInfo.Settings.Top;
Left = AppInfo.Settings.Left;
Width = AppInfo.Settings.Width;
Height = AppInfo.Settings.Height;
WindowState = AppInfo.Settings.IsMaximized ? WindowState.Maximized : WindowState.Normal;
}

private void SaveSizeAndLocation()
{
AppInfo.Settings.Top = Top;
AppInfo.Settings.Left = Left;
AppInfo.Settings.Width = Width;
AppInfo.Settings.Height = Height;
AppInfo.Settings.IsMaximized = WindowState == WindowState.Maximized;
}

private void ScriptEditorScriptChangedCategory(object sender, EventArgs e) => ResetTabs();

private void TabControlCategoriesSelectionChanged(object sender, SelectionChangedEventArgs e)
Expand All @@ -155,4 +174,6 @@ private void TabControlCategoriesSelectionChanged(object sender, SelectionChange
((ListBox)item.Content).SelectedItem = null;
}
}

private void Window_Closed(object sender, EventArgs e) => SaveSizeAndLocation();
}
60 changes: 60 additions & 0 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,20 @@
<Setting Name="ForbidScriptCodeEdit" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="Top" Type="System.Double" Scope="User">
<Value Profile="(Default)">NaN</Value>
</Setting>
<Setting Name="Left" Type="System.Double" Scope="User">
<Value Profile="(Default)">NaN</Value>
</Setting>
<Setting Name="Height" Type="System.Double" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="Width" Type="System.Double" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="IsMaximized" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
3 changes: 2 additions & 1 deletion Resources/DevException.resx
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@
</data>
<data name="CallbackNotSet" xml:space="preserve">
<value>Callback {0} not set.</value>
<comment>0 : string : callback name</comment>
<comment>0 : string : callback name@Invariant</comment>
</data>
<data name="ScriptsAreNotExecuting" xml:space="preserve">
<value>Scripts are not executing</value>
<comment>@Invariant</comment>
</data>
</root>
6 changes: 3 additions & 3 deletions Resources/Logs.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Resources/Logs.resx
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
<value>Script "{0}" deleted.</value>
<comment>0 : string : script filename@Invariant</comment>
</data>
<data name="ScriptExecuted" xml:space="preserve">
<value>Script "{0}" executed.</value>
<data name="ScriptExecuting" xml:space="preserve">
<value>Script "{0}" executing</value>
<comment>0 : string : script filename@Invariant</comment>
</data>
<data name="ScriptExecutionCanceled" xml:space="preserve">
Expand Down

0 comments on commit 7fdcf92

Please sign in to comment.