Skip to content

Commit

Permalink
Minor UI Improviments
Browse files Browse the repository at this point in the history
- DialogBox OK/Yes react to Enter Button
- DialogBox No/Cancel react to Escape button
- Auto Resize Icon to fit a proportional space
- Allow open split pkgs from local json files.
  • Loading branch information
marcussacana committed Jul 2, 2022
1 parent bef0ec9 commit cb369d4
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 36 deletions.
2 changes: 1 addition & 1 deletion DirectPackageInstaller/DirectPackageInstaller/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define DEBUG_SINGLEVIEW
//#define DEBUG_SINGLEVIEW

using System;
using System.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ async Task Merge(HttpContext Context, NameValueCollection Query, string Url) {
Context.Response.StatusCode = Partial ? 206 : 200;
Context.Response.StatusDescription = Partial ? "Partial Content" : "OK";

Stream Origin = SplitHelper.OpenRemoteJSON(Url);
Stream Origin = Url.IsFilePath() ? SplitHelper.OpenLocalJSON(Url) : SplitHelper.OpenRemoteJSON(Url);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public static bool HasName(this ParamSfo This, string name)
return httpStream.Filename;
return null;
}

public static bool IsFilePath(this string Path)
{
return Path.Length > 2 && (Path[1] == ':' || Path[0] == '/' || Path.StartsWith("\\\\"));
}
public static string Substring(this string String, string Substring)
{
return String.Substring(String.IndexOf(Substring) + Substring.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public static Stream OpenRemoteJSON(string URL) {
return OpenJSON(JSON);
}
}

public static Stream OpenLocalJSON(string FilePath) {
using (Stream Reader = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
using (MemoryStream Buffer = new MemoryStream())
{
Reader.CopyTo(Buffer);
var JSON = Encoding.UTF8.GetString(Buffer.ToArray());
return OpenJSON(JSON);
}
}

private static Stream OpenJSON(string JSON)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public FileEntry(string FullPath, bool IsDirectory)

public bool IsPhone { get; set; }
public bool IsSDCard { get; set; }
public bool IsPKG => !IsDirectory && Name.EndsWith(".pkg", StringComparison.InvariantCultureIgnoreCase);

public bool IsPKG => !IsDirectory && (Name.EndsWith(".pkg", StringComparison.InvariantCultureIgnoreCase) ||
Name.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase));
public bool IsDirectory => FullPath.EndsWith("\\") || FullPath.EndsWith("/");

public string Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static string? MainExecutable

const string UpdateList = "Update.ini";

public static Version CurrentVersion = new Version("6.1.9");
public static Version CurrentVersion = new Version("6.1.10");

public static Version? LastVersion = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,20 @@ private static void DoSegmentedDownload(object sender, DoWorkEventArgs e)

try
{
This.SegmentedRead = new SegmentedStream(() => new FileHostStream(This.Url), null);
This.SegmentedRead = new SegmentedStream(() =>
{
if (This.Url.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase))
{
if (This.Url.IsFilePath())
{
return SplitHelper.OpenLocalJSON(This.Url);
}

return SplitHelper.OpenRemoteJSON(This.Url);
}

return new FileHostStream(This.Url);
}, null);
This.OpenRead = () => This.SegmentedRead;
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static async Task<bool> PushPackage(Settings Config, Source InputType, St
InputType &= ~(Source.Proxy | Source.Segmented | Source.DiskCache);

if (InputType.HasFlag(Source.JSON))
InputType &= ~(Source.Proxy | Source.Segmented);
InputType &= ~(Source.Proxy | Source.DiskCache | Source.URL | Source.File);

if (InputType.HasFlag(Source.File))
InputType &= ~(Source.Proxy | Source.Segmented | Source.DiskCache);
Expand Down Expand Up @@ -151,6 +151,7 @@ public static async Task<bool> PushPackage(Settings Config, Source InputType, St
URL = $"http://{Config.PCIP}:{ServerPort}/{(InputType.HasFlag(Source.SevenZip) ? "un7z" : "unrar")}/?id={ID}";
break;

case Source.JSON | Source.Segmented:
case Source.URL | Source.DiskCache:
case Source.URL | Source.Segmented:
var CacheTask = Downloader.CreateTask(URL);
Expand All @@ -170,9 +171,10 @@ public static async Task<bool> PushPackage(Settings Config, Source InputType, St
URL = $"http://{Config.PCIP}:{ServerPort}/proxy/?b64={Convert.ToBase64String(Encoding.UTF8.GetBytes(URL))}";
break;

case Source.URL | Source.JSON:
case Source.JSON:
URL = $"http://{Config.PCIP}:{ServerPort}/merge/?b64={Convert.ToBase64String(Encoding.UTF8.GetBytes(URL))}";
break;

case Source.File:
URL = $"http://{Config.PCIP}:{ServerPort}/file/?b64={Convert.ToBase64String(Encoding.UTF8.GetBytes(URL))}";
break;
Expand Down Expand Up @@ -461,7 +463,7 @@ private static async Task<bool> EnsureFreeSpace(Stream? PKGStream, DecompressorH
Message += $"\nOr clean more {AltMissingSpace.ToFileSize()} in your {AltStorageName}.";
}

if (InputType == (Source.URL | Source.Segmented))
if (InputType.HasFlag(Source.Segmented))
Message += "\nAlternatively, you can disable Segmented Download feature.";

if (!TempHelper.CacheIsEmpty() && Server is {Connections: 0})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Threading;
using LibOrbisPkg.PKG;
using SharpCompress.Archives;
using DirectPackageInstaller.Compression;
using DirectPackageInstaller.Host;
using DirectPackageInstaller.IO;
using DirectPackageInstaller.Others;
using DirectPackageInstaller.Tasks;
using DirectPackageInstaller.ViewModels;
using DynamicData.Kernel;
using LibOrbisPkg.PKG;
using SharpCompress.Archives;
using Image = Avalonia.Controls.Image;
using Path = System.IO.Path;
using Size = Avalonia.Size;
Expand Down Expand Up @@ -90,7 +88,10 @@ public MainView()
btnExit.IsVisible = App.IsAndroid;

CNL.OnLinksReceived = OnLinksReceived;

IconBox.PropertyChanged += IconBoxOnPropertyChanged;
}

public async Task OnShown(MainWindow? Parent)
{
if (Model == null)
Expand Down Expand Up @@ -277,7 +278,12 @@ private async void BtnLoadOnClick(object? sender, RoutedEventArgs e)
new()
{
Name = "ALL PKG Files",
Extensions = new List<string>() {"pkg", "PKG"},
Extensions = new List<string>() {"pkg", "PKG", "json", "JSON"},
},
new()
{
Name = "Split PKG Manifest",
Extensions = new List<string>() {"json", "JSON"}
},
new()
{
Expand Down Expand Up @@ -354,11 +360,18 @@ private async void BtnLoadOnClick(object? sender, RoutedEventArgs e)
{
if (SourcePackage.EndsWith(".json", StringComparison.InvariantCultureIgnoreCase))
{
PKGStream = SplitHelper.OpenRemoteJSON(SourcePackage);
InputType = Source.URL | Source.JSON;
if (SourcePackage.IsFilePath())
{
InputType = Source.File | Source.JSON;
PKGStream = SplitHelper.OpenLocalJSON(SourcePackage);
}
else
{
PKGStream = SplitHelper.OpenRemoteJSON(SourcePackage);
InputType = Source.URL | Source.JSON;
}
}
else if (SourcePackage.Length > 2 && (SourcePackage[1] == ':' || SourcePackage[0] == '/' ||
SourcePackage.StartsWith("\\\\")))
else if (SourcePackage.IsFilePath())
{
PKGStream = File.Open(SourcePackage, FileMode.Open, FileAccess.Read, FileShare.Read);
InputType = Source.File;
Expand Down Expand Up @@ -964,6 +977,15 @@ private void tbURLOnGotFocus(object? sender, GotFocusEventArgs e)
});
}

private void IconBoxOnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (e.Property.Name != "Bounds")
return;

InLandscape = null;
InvalidateMeasure();
}

bool? InLandscape = null;
protected override Size MeasureCore(Size availableSize)
{
Expand All @@ -978,13 +1000,15 @@ protected override Size MeasureCore(Size availableSize)
new RowDefinition()
};

var IconSize = IconBox.Bounds.Height;
var IconSize = PreviewGrid.Bounds.Height;
if (IconSize == 0)
IconSize = Double.PositiveInfinity;

var MaxWidth = availableSize.Width / 2;

PreviewGrid.ColumnDefinitions = new ColumnDefinitions()
{
availableSize.Width/2 > IconSize ? new ColumnDefinition(IconSize, GridUnitType.Pixel) : new ColumnDefinition(),
MaxWidth > IconSize ? new ColumnDefinition(IconSize, GridUnitType.Pixel) : new ColumnDefinition(),
new ColumnDefinition(App.IsAndroid ? 10f : 5f, GridUnitType.Pixel),
new ColumnDefinition()
};
Expand All @@ -1002,13 +1026,15 @@ protected override Size MeasureCore(Size availableSize)
{
InLandscape = false;

var IconSize = IconBox.Bounds.Width;
var IconSize = PreviewGrid.Bounds.Width;
if (IconSize == 0)
IconSize = Double.PositiveInfinity;

var MaxHeight = availableSize.Height / 2;

PreviewGrid.RowDefinitions = new RowDefinitions()
{
availableSize.Height/2 > IconSize ? new RowDefinition(IconSize, GridUnitType.Pixel) : new RowDefinition(),
MaxHeight > IconSize ? new RowDefinition(IconSize, GridUnitType.Pixel) : new RowDefinition(),
new RowDefinition(App.IsAndroid ? 10f : 5f, GridUnitType.Pixel),
new RowDefinition()
};
Expand All @@ -1027,6 +1053,16 @@ protected override Size MeasureCore(Size availableSize)
PkgInfoGrid[Grid.RowProperty] = 2;
}
}
else if (InLandscape != null)
{
var CurrentSize = InLandscape == true ? PreviewGrid.Bounds.Height : PreviewGrid.Bounds.Width;
var MaxSize = InLandscape == true ? availableSize.Width / 2 : availableSize.Height / 2;
if (CurrentSize > MaxSize)
{
InLandscape = null;
return MeasureCore(availableSize);
}
}

return base.MeasureCore(availableSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button Grid.Column="4" Name="ButtonA" />
<Button Grid.Column="3" Name="ButtonB" />
<Button Grid.Column="2" Name="ButtonC" />
<Button Grid.Column="1" Name="ButtonD" />
<Button Grid.Column="3" Name="ButtonB" />
<Button Grid.Column="2" Name="ButtonC" />
<Button Grid.Column="1" Name="ButtonD" />
</Grid>
</Grid>
<UserControl.Styles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using DirectPackageInstaller.ViewModels;
Expand Down Expand Up @@ -33,6 +34,53 @@ public MessageBoxView()
ButtonB.Click += BtnClicked;
ButtonC.Click += BtnClicked;
ButtonD.Click += BtnClicked;

KeyDown += OnKeyDown;

AttachedToVisualTree += (sender, args) =>
{
((UserControl?)sender)?.Focus();
};
}

private void OnKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
if (Model!.Buttons.HasFlag(MessageBoxButtons.OK))
{
e.Handled = true;
Model.Result = DialogResult.OK;
Close();
return;
}
if (Model!.Buttons.HasFlag(MessageBoxButtons.Yes))
{
e.Handled = true;
Model.Result = DialogResult.Yes;
Close();
return;
}
}

if (e.Key == Key.Escape)
{
if (Model!.Buttons.HasFlag(MessageBoxButtons.Cancel))
{
e.Handled = true;
Model.Result = DialogResult.Cancel;
Close();
return;
}

if (Model!.Buttons.HasFlag(MessageBoxButtons.No))
{
e.Handled = true;
Model.Result = DialogResult.No;
Close();
return;
}
}
}

private Window? Window;
Expand Down Expand Up @@ -152,17 +200,22 @@ private void BtnClicked(object? sender, RoutedEventArgs e)
return;
}

App.Callback(() =>
Close();
}

private void Close()
{
App.Callback(() =>
{
if (Window == null)
{
SingleView.ReturnView(this);
}
else
{
if (Window == null)
{
SingleView.ReturnView(this);
}
else
{
Window.Hide();
App.Callback(Window.Close);
}
});
Window.Hide();
App.Callback(Window.Close);
}
});
}
}
Loading

0 comments on commit cb369d4

Please sign in to comment.