Skip to content

Commit

Permalink
add confirmation prompt for url schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMulhima committed Jun 29, 2023
1 parent 8e3476f commit 88ba12e
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Scarab/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@
<StreamGeometry x:Key="share_android_regular">M17,2.49823144 C18.9339734,2.49823144 20.5017686,4.06602662 20.5017686,6 C20.5017686,7.93397338 18.9339734,9.50176856 17,9.50176856 C15.9700352,9.50176856 15.0439275,9.0571034 14.4031791,8.34927521 L9.84455071,10.9536986 C9.94786118,11.284101 10.0035371,11.6355486 10.0035371,12 C10.0035371,12.3644514 9.94786118,12.715899 9.84455071,13.0463014 L14.4022538,15.6517469 C15.0430358,14.9433244 15.9695393,14.4982314 17,14.4982314 C18.9339734,14.4982314 20.5017686,16.0660262 20.5017686,17.9999996 C20.5017686,19.9339729 18.9339734,21.5017681 17,21.5017681 C15.0660266,21.5017681 13.4982314,19.9339729 13.4982314,17.9999996 C13.4982314,17.6355481 13.5539074,17.2841005 13.6572179,16.9536982 L9.09951477,14.3482526 C8.45873279,15.0566751 7.53222925,15.5017686 6.50176856,15.5017686 C4.56779519,15.5017686 3,13.9339734 3,12 C3,10.0660266 4.56779519,8.49823144 6.50176856,8.49823144 C7.53173336,8.49823144 8.45784104,8.9428966 9.09858948,9.65072479 L13.6572179,7.04630139 C13.5539074,6.71589903 13.4982314,6.36445142 13.4982314,6 C13.4982314,4.06602662 15.0660266,2.49823144 17,2.49823144 Z M17,15.998231 C15.8944537,15.998231 14.9982314,16.8944533 14.9982314,17.9999996 C14.9982314,19.1055458 15.8944537,20.0017681 17,20.0017681 C18.1055463,20.0017681 19.0017686,19.1055458 19.0017686,17.9999996 C19.0017686,16.8944533 18.1055463,15.998231 17,15.998231 Z M6.50176856,9.99823144 C5.39622231,9.99823144 4.5,10.8944537 4.5,12 C4.5,13.1055463 5.39622231,14.0017686 6.50176856,14.0017686 C7.60731481,14.0017686 8.50353713,13.1055463 8.50353713,12 C8.50353713,10.8944537 7.60731481,9.99823144 6.50176856,9.99823144 Z M17,3.99823144 C15.8944537,3.99823144 14.9982314,4.89445375 14.9982314,6 C14.9982314,7.10554625 15.8944537,8.00176856 17,8.00176856 C18.1055463,8.00176856 19.0017686,7.10554625 19.0017686,6 C19.0017686,4.89445375 18.1055463,3.99823144 17,3.99823144 Z</StreamGeometry>
<StreamGeometry x:Key="folder_regular">M17.0606622,9 C17.8933043,9 18.7000032,9.27703406 19.3552116,9.78392956 L19.5300545,9.92783739 L22.116207,12.1907209 C22.306094,12.356872 22.5408581,12.4608817 22.7890575,12.4909364 L22.9393378,12.5 L40.25,12.5 C42.2542592,12.5 43.8912737,14.0723611 43.994802,16.0508414 L44,16.25 L44,35.25 C44,37.2542592 42.4276389,38.8912737 40.4491586,38.994802 L40.25,39 L7.75,39 C5.74574083,39 4.10872626,37.4276389 4.00519801,35.4491586 L4,35.25 L4,12.75 C4,10.7457408 5.57236105,9.10872626 7.55084143,9.00519801 L7.75,9 L17.0606622,9 Z M22.8474156,14.9988741 L20.7205012,17.6147223 C20.0558881,18.4327077 19.0802671,18.9305178 18.0350306,18.993257 L17.8100737,19 L6.5,18.999 L6.5,35.25 C6.5,35.8972087 6.99187466,36.4295339 7.62219476,36.4935464 L7.75,36.5 L40.25,36.5 C40.8972087,36.5 41.4295339,36.0081253 41.4935464,35.3778052 L41.5,35.25 L41.5,16.25 C41.5,15.6027913 41.0081253,15.0704661 40.3778052,15.0064536 L40.25,15 L22.8474156,14.9988741 Z M17.0606622,11.5 L7.75,11.5 C7.10279131,11.5 6.5704661,11.9918747 6.50645361,12.6221948 L6.5,12.75 L6.5,16.499 L17.8100737,16.5 C18.1394331,16.5 18.4534488,16.3701335 18.6858203,16.1419575 L18.7802162,16.0382408 L20.415,14.025 L17.883793,11.8092791 C17.693906,11.643128 17.4591419,11.5391183 17.2109425,11.5090636 L17.0606622,11.5 Z</StreamGeometry>
<StreamGeometry x:Key="bug_regular">M10.5 2.75134C10.5 2.33713 10.1642 2.00134 9.75 2.00134C9.33579 2.00134 9 2.33713 9 2.75134V3.50345C9 4.13591 9.19571 4.72266 9.52988 5.20644C8.13703 5.68546 7.11506 6.96179 7.00909 8.49031H6.76245L6.75315 8.49029C5.51052 8.48515 4.50733 7.47364 4.51247 6.23101L4.51861 4.74541C4.52032 4.3312 4.18593 3.99403 3.77172 3.99232C3.35751 3.9906 3.02034 4.325 3.01863 4.73921L3.01248 6.22481C3.00392 8.29586 4.6759 9.98171 6.74695 9.99027L6.76245 9.99031H7V11.5H2.75C2.33579 11.5 2 11.8358 2 12.25C2 12.6642 2.33579 13 2.75 13H7V14.9923H6.76245L6.74695 14.9923C4.6759 15.0009 3.00392 16.6867 3.01248 18.7578L3.01863 20.2434C3.02034 20.6576 3.35751 20.992 3.77172 20.9903C4.18593 20.9886 4.52032 20.6514 4.51861 20.2372L4.51247 18.7516C4.50733 17.509 5.51052 16.4975 6.75315 16.4923L6.76245 16.4923H7.22633C7.86093 18.5251 9.75816 20.0005 12 20.0005C14.2418 20.0005 16.1391 18.5251 16.7737 16.4923H17.2375L17.2468 16.4923C18.4895 16.4975 19.4927 17.509 19.4875 18.7516L19.4814 20.2372C19.4797 20.6514 19.8141 20.9886 20.2283 20.9903C20.6425 20.992 20.9797 20.6576 20.9814 20.2434L20.9875 18.7578C20.9961 16.6867 19.3241 15.0009 17.253 14.9923L17.2375 14.9923H17V13H21.2514C21.6656 13 22.0014 12.6642 22.0014 12.25C22.0014 11.8358 21.6656 11.5 21.2514 11.5H17V9.99031H17.2375L17.253 9.99027C19.3241 9.98171 20.9961 8.29586 20.9875 6.2248L20.9814 4.73921C20.9797 4.325 20.6425 3.9906 20.2283 3.99232C19.8141 3.99403 19.4797 4.3312 19.4814 4.74541L19.4875 6.23101C19.4927 7.47364 18.4895 8.48515 17.2468 8.49029L17.2375 8.49031H16.9909C16.8849 6.96179 15.863 5.68546 14.4701 5.20644C14.8043 4.72267 15 4.13592 15 3.50345V2.75134C15 2.33713 14.6642 2.00134 14.25 2.00134C13.8358 2.00134 13.5 2.33713 13.5 2.75134V3.50345C13.5 4.33188 12.8284 5.00345 12 5.00345C11.1716 5.00345 10.5 4.33188 10.5 3.50345V2.75134ZM8.5 8.75348C8.5 7.51084 9.50736 6.50348 10.75 6.50348H13.25C14.4926 6.50348 15.5 7.51084 15.5 8.75348V15.0005C15.5 16.9335 13.933 18.5005 12 18.5005C10.067 18.5005 8.5 16.9335 8.5 15.0005V8.75348Z</StreamGeometry>
<StreamGeometry x:Key="checkmark_circle_regular">M24,4 C35.045695,4 44,12.954305 44,24 C44,35.045695 35.045695,44 24,44 C12.954305,44 4,35.045695 4,24 C4,12.954305 12.954305,4 24,4 Z M24,6.5 C14.3350169,6.5 6.5,14.3350169 6.5,24 C6.5,33.6649831 14.3350169,41.5 24,41.5 C33.6649831,41.5 41.5,33.6649831 41.5,24 C41.5,14.3350169 33.6649831,6.5 24,6.5 Z M32.6338835,17.6161165 C33.1220388,18.1042719 33.1220388,18.8957281 32.6338835,19.3838835 L21.6338835,30.3838835 C21.1457281,30.8720388 20.3542719,30.8720388 19.8661165,30.3838835 L15.3661165,25.8838835 C14.8779612,25.3957281 14.8779612,24.6042719 15.3661165,24.1161165 C15.8542719,23.6279612 16.6457281,23.6279612 17.1338835,24.1161165 L20.75,27.732233 L30.8661165,17.6161165 C31.3542719,17.1279612 32.1457281,17.1279612 32.6338835,17.6161165 Z</StreamGeometry>
<StreamGeometry x:Key="dismiss_circle_regular">M24,4 C35.045695,4 44,12.954305 44,24 C44,35.045695 35.045695,44 24,44 C12.954305,44 4,35.045695 4,24 C4,12.954305 12.954305,4 24,4 Z M24,6.5 C14.3350169,6.5 6.5,14.3350169 6.5,24 C6.5,33.6649831 14.3350169,41.5 24,41.5 C33.6649831,41.5 41.5,33.6649831 41.5,24 C41.5,14.3350169 33.6649831,6.5 24,6.5 Z M17.7823881,16.0249942 L17.8838835,16.1161165 L24,22.233 L30.1161165,16.1161165 C30.5717282,15.6605048 31.2915486,15.6301307 31.7823881,16.0249942 L31.8838835,16.1161165 C32.3394952,16.5717282 32.3698693,17.2915486 31.9750058,17.7823881 L31.8838835,17.8838835 L25.767,24 L31.8838835,30.1161165 C32.3394952,30.5717282 32.3698693,31.2915486 31.9750058,31.7823881 L31.8838835,31.8838835 C31.4282718,32.3394952 30.7084514,32.3698693 30.2176119,31.9750058 L30.1161165,31.8838835 L24,25.767 L17.8838835,31.8838835 C17.4282718,32.3394952 16.7084514,32.3698693 16.2176119,31.9750058 L16.1161165,31.8838835 C15.6605048,31.4282718 15.6301307,30.7084514 16.0249942,30.2176119 L16.1161165,30.1161165 L22.233,24 L16.1161165,17.8838835 C15.6605048,17.4282718 15.6301307,16.7084514 16.0249942,16.2176119 L16.1161165,16.1161165 C16.5717282,15.6605048 17.2915486,15.6301307 17.7823881,16.0249942 Z</StreamGeometry>
</Application.Resources>
</Application>
4 changes: 2 additions & 2 deletions Scarab/Scarab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
<PackageReference Include="Deadpikle.AvaloniaProgressRing" Version="0.10.0-preview20230601001" />
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0-eap3" />
<PackageReference Include="Markdown.Avalonia" Version="11.0.0-d1" />
<PackageReference Include="MessageBox.Avalonia" Version="2.3.1-rc1" />
<PackageReference Include="MessageBox.Avalonia" Version="2.3.1-rc1-1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-preview.5.23280.8" />
<PackageReference Include="Microsoft.Toolkit.HighPerformance" Version="7.1.2" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
<PackageReference Include="NetSparkleUpdater.SparkleUpdater" Version="2.3.0-preview20230605001" />
<PackageReference Include="NetSparkleUpdater.SparkleUpdater" Version="2.3.0-preview20230611002" />
<PackageReference Include="NetSparkleUpdater.UI.Avalonia" Version="3.0.0-preview20230330001" />
<PackageReference Include="PropertyChanged.SourceGenerator" Version="1.0.8">
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions Scarab/Services/Interfaces/IUrlSchemeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Scarab.Interfaces;
public interface IUrlSchemeHandler
{
public void SetCommand(string arg);
public void SetCommand(UrlSchemeCommands arg);
public Task ShowConfirmation(MessageBoxStandardParams param);
public Task ShowConfirmation(string title, string message, Icon icon = Icon.Success);
public Dictionary<string, string?> ParseDownloadCommand(string data);
Expand Down
5 changes: 5 additions & 0 deletions Scarab/Services/UrlSchemeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public UrlSchemeHandler(bool handled = false)
{UrlSchemeCommands.removeGlobalSettings, s => Data = s},
};
}

public void SetCommand(UrlSchemeCommands arg)
{
UrlSchemeCommand = arg;
}

public void SetCommand(string arg)
{
Expand Down
36 changes: 36 additions & 0 deletions Scarab/ViewModels/LoadingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,42 @@ private async Task UpdateLoadingText()
}
}

public async Task<bool> ShowUrlSchemePrompt(string prompt)
{
ShouldShowUrlSchemePrompt = true;
UrlSchemePromptText = prompt;

while (_shouldShowUrlSchemePrompt)
{
await Task.Delay(500);
}

return _accepted;
}

public void AcceptUrlSchemePrompt()
{
ShouldShowUrlSchemePrompt = false;
_accepted = true;
}

public void DeclineUrlSchemePrompt()
{
ShouldShowUrlSchemePrompt = false;
_accepted = false;
}

[Notify]
private string _loadingText = "";

[Notify]
private bool _shouldShowUrlSchemePrompt;

[Notify]
private string _urlSchemePromptText = "";

private bool _accepted = false;

public string PreUrlSchemePrompt => "Scarab has received the following command:";
public string PostUrlSchemePrompt => "Do you want to run this command?";
}
47 changes: 44 additions & 3 deletions Scarab/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ internal static bool _Debug

private static bool isFirstLoad { get; set; } = true;
public static MainWindowViewModel? Instance { get; private set; }

[Notify]
private LoadingViewModel _loadingPage { get; set; }

[UsedImplicitly]
private ViewModelBase Content => Loading || SelectedTabIndex < 0 ? new LoadingViewModel() : Tabs[SelectedTabIndex].Item;
private ViewModelBase Content => Loading || SelectedTabIndex < 0 ? LoadingPage : Tabs[SelectedTabIndex].Item;
public IBrush BorderBrush => new SolidColorBrush(Color.FromRgb(0x28, 0x28, 0x28));
public Thickness BorderThickness => new(1);
public CornerRadius CornerRadius => new(3);
Expand All @@ -62,11 +65,13 @@ internal static bool _Debug

private async Task Impl()
{
LoadingPage = new LoadingViewModel();

Trace.WriteLine($"Opening Scarab Version: {Assembly.GetExecutingAssembly().GetName().Version}");

var urlSchemeHandler = new UrlSchemeHandler(handled: !isFirstLoad);

HandleURLSchemeCommand(urlSchemeHandler);
await HandleURLSchemeCommand(urlSchemeHandler);

Trace.WriteLine("Checking if up to date...");

Expand Down Expand Up @@ -247,13 +252,49 @@ await MessageBoxUtil.GetMessageBoxStandardWindow
Trace.WriteLine("Selected Tab 0");
}

private void HandleURLSchemeCommand(IUrlSchemeHandler urlSchemeHandler)
private async Task HandleURLSchemeCommand(IUrlSchemeHandler urlSchemeHandler)
{
var args = Environment.GetCommandLineArgs();
if (args.Length == 2) // only accept 2 args, the exe location and the uri
{
urlSchemeHandler.SetCommand(args[1]);
}

if (!urlSchemeHandler.Handled && urlSchemeHandler.UrlSchemeCommand != UrlSchemeCommands.none)
{
var prompt = urlSchemeHandler.UrlSchemeCommand switch
{
UrlSchemeCommands.download => $"Download the following mods: {GetListOfMods()}",
UrlSchemeCommands.reset => $"Reset Scarab's persistent settings",
UrlSchemeCommands.forceUpdateAll => $"Reinstall all mods which could help fix issues that happened because mods are not downloaded correctly.",
UrlSchemeCommands.customModLinks => $"Load a custom mod list from: {urlSchemeHandler.Data}",
UrlSchemeCommands.baseLink => $"Load Modlinks and APILinks from: {urlSchemeHandler.Data}",
UrlSchemeCommands.removeAllModsGlobalSettings => $"Reset all mods' global settings",
UrlSchemeCommands.removeGlobalSettings => $"Remove global settings for the following mods: {GetListOfMods()}",
_ => ""
};
bool accepted = await LoadingPage.ShowUrlSchemePrompt(prompt);

if (!accepted) urlSchemeHandler.SetCommand(UrlSchemeCommands.none);
}


string GetListOfMods()
{
var mods = urlSchemeHandler.ParseDownloadCommand(urlSchemeHandler.Data);
if (mods.Count == 0) return "None";
var list = "\n";
foreach (var (mod, url) in mods)
{
list += mod;
if (url is not null)
list += $" ({Resources.MVVM_NotInModlinks_Disclaimer}),\n";
else
list += ",\n";
}

return list;
}
}

private void HandleResetUrlScheme(IUrlSchemeHandler urlSchemeHandler)
Expand Down
4 changes: 2 additions & 2 deletions Scarab/ViewModels/ModListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ await _urlSchemeHandler.ShowConfirmation(
switch (correspondingMod.State)
{
case NotInstalledState:
await OnInstall(correspondingMod);
await InternalModDownload(correspondingMod, correspondingMod.OnInstall);
break;
case InstalledState { Updated: false }:
case NotInModLinksState { ModlinksMod: true }:
await OnUpdate(correspondingMod);
await InternalModDownload(correspondingMod, correspondingMod.OnUpdate);
break;
case InstalledState { Enabled: false }:
await OnEnable(correspondingMod);
Expand Down
90 changes: 73 additions & 17 deletions Scarab/Views/Pages/LoadingView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,80 @@
xmlns:views="clr-namespace:Scarab.Views"
xmlns:viewModels="clr-namespace:Scarab.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Scarab.Views.Pages.LoadingView">
<Grid RowDefinitions="3*, *">
x:Class="Scarab.Views.Pages.LoadingView"
x:DataType="viewModels:LoadingViewModel"
x:CompileBindings="True">
<StackPanel>
<Grid
IsVisible="{Binding !ShouldShowUrlSchemePrompt}"
RowDefinitions="3*, *">

<progRing:ProgressRing
Grid.Row="0"
Width="150"
Height="150"
IsActive="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="Ivory"
Margin="10,20,0,0"/>
<progRing:ProgressRing
Grid.Row="0"
Width="150"
Height="150"
IsActive="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="Ivory"
Margin="10,20,0,0"/>

<TextBlock
Grid.Row="1"
Text="{Binding LoadingText}"
FontSize="26"
TextAlignment="Center"/>
<TextBlock
Grid.Row="1"
Text="{Binding LoadingText}"
FontSize="26"
TextAlignment="Center"/>

</Grid>
</Grid>

<Grid
IsVisible="{Binding ShouldShowUrlSchemePrompt}"
RowDefinitions="3*, *"
ColumnDefinitions="*, *">
<StackPanel
Grid.Row="0"
Grid.Column="0" Grid.ColumnSpan="2"
Margin="40 20 40 10">
<TextBlock
Text="{Binding PreUrlSchemePrompt}"
FontSize="22"
Margin="0 0 0 10"
TextWrapping="Wrap"/>
<ScrollViewer
MinHeight="100"
MaxHeight="200">
<TextBlock
Text="{Binding UrlSchemePromptText}"
FontSize="14"
TextWrapping="Wrap"/>
</ScrollViewer>
<TextBlock
Margin="0 10"
Text="{Binding PostUrlSchemePrompt}"
FontSize="22"
TextWrapping="Wrap"
TextAlignment="Center"/>
</StackPanel>
<Button
Grid.Row="1"
Grid.Column="0"
Background="Transparent"
HorizontalAlignment="Center"
Command="{Binding DeclineUrlSchemePrompt}">
<PathIcon
Data="{StaticResource dismiss_circle_regular}"
Height="50" Width="50"/>
</Button>
<Button
Grid.Row="1"
Grid.Column="1"
Background="Transparent"
HorizontalAlignment="Center"
Command="{Binding AcceptUrlSchemePrompt}">
<PathIcon
Data="{StaticResource checkmark_circle_regular}"
Height="50" Width="50"/>
</Button>
</Grid>
</StackPanel>
</views:View>
4 changes: 2 additions & 2 deletions Scarab/Views/Windows/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800" Height="500"
MinWidth="500"
MinHeight="375"
MinWidth="550"
MinHeight="400"
x:Class="Scarab.Views.Windows.MainWindow"
Icon="/Assets/ConstructionKnight.ico"
Title="Scarab+"
Expand Down

0 comments on commit 88ba12e

Please sign in to comment.