Skip to content

Commit

Permalink
refactor open file button
Browse files Browse the repository at this point in the history
this will allow #130 to be implemented in the future
  • Loading branch information
PhantomGamers committed Jun 20, 2023
1 parent 988bcf0 commit c5f7b89
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 97 deletions.
121 changes: 121 additions & 0 deletions SFP_UI/Models/OpenFileModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System.Reactive;
using Avalonia.Controls;
using ReactiveUI;
using SFP.Models;
using SFP.Models.Injection.Config;
using SFP.Properties;

namespace SFP_UI.Models;

public class OpenFileModel
{
public static void PopulateOpenFileDropDownButton(object itemsCollection)
{

var sfpConfig = SfpConfig.GetConfig();
var targetCssFiles = new HashSet<string>();
var targetJsFiles = new HashSet<string>();
var skinDir = Steam.SkinDir;

foreach (var patch in sfpConfig.Patches)
{
// only add if not empty
if (!string.IsNullOrWhiteSpace(patch.TargetCss))
{
targetCssFiles.Add(patch.TargetCss);
}

if (!string.IsNullOrWhiteSpace(patch.TargetJs))
{
targetJsFiles.Add(patch.TargetJs);
}
}

switch (itemsCollection)
{
case IList<NativeMenuItemBase> nativeMenuItems:
{
nativeMenuItems.Clear();
nativeMenuItems.Add(new NativeMenuItem { Header = "steamui/skins/", Command = OpenDir, CommandParameter = Steam.SkinsDir });
nativeMenuItems.Add(new NativeMenuItem { Header = "Active Skin: " + Settings.Default.SelectedSkin + '/', Command = OpenDir, CommandParameter = Steam.SkinDir });
nativeMenuItems.Add(new NativeMenuItemSeparator());

foreach (var cssFile in targetCssFiles)
{
nativeMenuItems.Add(new NativeMenuItem { Header = Path.GetFileName(cssFile), Command = OpenFile, CommandParameter = Path.Join(skinDir, cssFile) });
}

nativeMenuItems.Add(new NativeMenuItemSeparator());

foreach (var jsFile in targetJsFiles)
{
nativeMenuItems.Add(new NativeMenuItem { Header = Path.GetFileName(jsFile), Command = OpenFile, CommandParameter = Path.Join(skinDir, jsFile) });
}
break;
}
case ItemCollection avaloniaItems:
{
avaloniaItems.Clear();
avaloniaItems.Add(new MenuItem { Header = "steamui/skins/", Command = OpenDir, CommandParameter = Steam.SkinsDir });
avaloniaItems.Add(new MenuItem { Header = "Active Skin: " + Settings.Default.SelectedSkin + '/', Command = OpenDir, CommandParameter = Steam.SkinDir });
avaloniaItems.Add(new Separator());

foreach (var cssFile in targetCssFiles)
{
avaloniaItems.Add(new MenuItem { Header = Path.GetFileName(cssFile), Command = OpenFile, CommandParameter = Path.Join(skinDir, cssFile) });
}

avaloniaItems.Add(new Separator());

foreach (var jsFile in targetJsFiles)
{
avaloniaItems.Add(new MenuItem { Header = Path.GetFileName(jsFile), Command = OpenFile, CommandParameter = Path.Join(skinDir, jsFile) });
}
break;
}
}
}

private static ReactiveCommand<string, Unit> OpenFile { get; } = ReactiveCommand.CreateFromTask<string>(OpenFileImpl);
private static ReactiveCommand<string, Unit> OpenDir { get; } = ReactiveCommand.Create<string>(OpenDirImpl);


private static async Task OpenPath(string path, bool isDirectory)
{
try
{
if (isDirectory)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
else
{
if (!File.Exists(path))
{
await File.Create(path).DisposeAsync();
}
}

Utils.OpenUrl(path);
}
catch (Exception e)
{
Log.Logger.Warn("Could not open " + path);
Log.Logger.Debug(e);
}
}

private static async Task OpenFileImpl(string relativeFilePath)
{
await OpenPath(relativeFilePath, false);
}

private static void OpenDirImpl(string relativeDirPath)
{
OpenPath(relativeDirPath, true).Wait();
}

}
100 changes: 3 additions & 97 deletions SFP_UI/Pages/MainPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SFP.Models;
using SFP.Models.Injection.Config;
using SFP.Properties;
using SFP_UI.Models;
using SFP_UI.ViewModels;

#endregion
Expand All @@ -18,102 +19,7 @@ public MainPage()
{
InitializeComponent();
DataContext = new MainPageViewModel();
OpenFileDropDownButton.Flyout!.Opened += (_, _) => PopulateOpenFileDropDownButton();
}


private ReactiveCommand<string, Unit> OpenFileCommand { get; } = ReactiveCommand.CreateFromTask<string>(OpenFile);
private ReactiveCommand<string, Unit> OpenDirCommand { get; } = ReactiveCommand.Create<string>(OpenDir);

private void PopulateOpenFileDropDownButton()
{
if (OpenFileDropDownButton.Flyout is not MenuFlyout flyout)
{
return;
}
var sfpConfig = SfpConfig.GetConfig();
var targetCssFiles = new HashSet<string>();
var targetJsFiles = new HashSet<string>();
var skinDir = Steam.SkinDir;
foreach (var patch in sfpConfig.Patches)
{
// only add if not empty
if (!string.IsNullOrWhiteSpace(patch.TargetCss))
{
targetCssFiles.Add(patch.TargetCss);
}

if (!string.IsNullOrWhiteSpace(patch.TargetJs))
{
targetJsFiles.Add(patch.TargetJs);
}
}

flyout.Items.Clear();
flyout.Items.Add(new MenuItem { Header = "steamui/skins/", Command = OpenDirCommand, CommandParameter = Steam.SkinsDir });
flyout.Items.Add(new MenuItem
{
Header = "Active Skin: " + Settings.Default.SelectedSkin + '/',
Command = OpenDirCommand,
CommandParameter = Steam.SkinDir
});
flyout.Items.Add(new Separator());
foreach (var cssFile in targetCssFiles)
{
flyout.Items.Add(new MenuItem
{
Header = Path.GetFileName(cssFile),
Command = OpenFileCommand,
CommandParameter = Path.Join(skinDir, cssFile)
});
}
flyout.Items.Add(new Separator());
foreach (var jsFile in targetJsFiles)
{
flyout.Items.Add(new MenuItem
{
Header = Path.GetFileName(jsFile),
Command = OpenFileCommand,
CommandParameter = Path.Join(skinDir, jsFile)
});
}
}

private static async Task OpenPath(string path, bool isDirectory)
{
try
{
if (isDirectory)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
else
{
if (!File.Exists(path))
{
await File.Create(path).DisposeAsync();
}
}

Utils.OpenUrl(path);
}
catch (Exception e)
{
Log.Logger.Warn("Could not open " + path);
Log.Logger.Debug(e);
}
}

private static async Task OpenFile(string relativeFilePath)
{
await OpenPath(relativeFilePath, false);
}

private static void OpenDir(string relativeDirPath)
{
OpenPath(relativeDirPath, true).Wait();
var flyout = (OpenFileDropDownButton.Flyout as MenuFlyout)!;
flyout.Opened += (_, _) => OpenFileModel.PopulateOpenFileDropDownButton(flyout.Items);
}
}

0 comments on commit c5f7b89

Please sign in to comment.