Skip to content

Commit

Permalink
Re-create UIs whenever tools are added / updates
Browse files Browse the repository at this point in the history
  • Loading branch information
xezno committed Nov 4, 2022
1 parent 7e3c259 commit 27a1c3f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 25 deletions.
2 changes: 2 additions & 0 deletions code/AddToolDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ private async Task DownloadTool()
var configPath = System.IO.Path.Combine( folder, ".addon" );
Utility.Projects.TryAddFromFile( configPath );
}

ToolsManager.Instance.Refresh();
}

private void PaintAddonItem( VirtualWidget v )
Expand Down
77 changes: 57 additions & 20 deletions code/ToolInfoPage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Sandbox;
using System.IO;
using System.Threading.Tasks;

namespace Tools;

Expand All @@ -23,6 +24,13 @@ public ToolInfoPage( LocalProject project, Widget parent = null, bool isDarkWind
Layout.Spacing = 8;
Layout.Margin = 24;

UpdateFromProject( project );
}

private void UpdateFromProject( LocalProject project )
{
DestroyChildren();

Project = project;
Manifest = project.GetManifest();

Expand Down Expand Up @@ -52,13 +60,22 @@ private void AddManifestWidgets()

Layout.AddSpacingCell( 8f );

Layout.Add( new Label.Body( $"{Manifest.Description}" ) );
Layout.Add( new Label( $"{Manifest.Description}" ) { WordWrap = true } );

// Installed release info
Layout.Add( new Subheading( $"{Manifest.ReleaseName}" ) );
Layout.Add( new Label.Body( $"{Manifest.ReleaseDescription}" ) );
{
var scroll = new ScrollArea( this );
var canvas = new Widget( this );
canvas.SetLayout( LayoutMode.TopToBottom );

Layout.AddStretchCell();
canvas.Layout.Add( new Subheading( $"{Manifest.ReleaseName}" ) );
canvas.Layout.Add( new Label( $"{Manifest.ReleaseDescription}" ) { WordWrap = true } );
canvas.Layout.AddStretchCell();

scroll.Canvas = canvas;

Layout.Add( scroll );
}

// Update info (if available)
if ( Manifest.CheckUpdateAvailable() )
Expand All @@ -68,12 +85,21 @@ private void AddManifestWidgets()
group.Layout.Margin = 10;
Layout.Add( group );

group.Layout.Add( new Heading( "Update Available" ) );
LatestReleaseName = group.Layout.Add( new Subheading( $"Loading..." ) );
LatestReleaseBody = group.Layout.Add( new Label( $"Loading..." ) );
{
group.Layout.Add( new Heading( "Update Available" ) );

group.Layout.AddSpacingCell( 8f );
var scroll = new ScrollArea( this );
var canvas = new Widget( this );
canvas.SetLayout( LayoutMode.TopToBottom );

LatestReleaseName = canvas.Layout.Add( new Subheading( $"Loading..." ) );
LatestReleaseBody = canvas.Layout.Add( new Label( $"Loading..." ) );

scroll.Canvas = canvas;
group.Layout.Add( scroll );
}

group.Layout.AddSpacingCell( 8f );
group.Layout.Add( new Button( "Download Update", "download" ) { Clicked = DownloadUpdate } );
}
}
Expand All @@ -83,22 +109,33 @@ private void AddManifestWidgets()
/// </summary>
private void DownloadUpdate()
{
GithubApi.FetchLatestRelease( $"{Manifest.Repo}" ).ContinueWith( async t =>
{
var release = t.Result ?? default;
var release = GithubApi.FetchLatestRelease( $"{Manifest.Repo}" ).Result;

var folder = Project.GetRootPath();
var folder = Project.GetRootPath();

Log.Trace( folder );
Log.Trace( folder );

await GitUtils.Git( $"reset --hard HEAD" );
await GitUtils.Git( $"pull" );
await GitUtils.Git( $"checkout \"{release.TagName}\" --force", folder );
using var progress = Progress.Start( $"Updating {Project.Config.Title}" );

// Update Manifest
Manifest.SetRelease( release );
Manifest.WriteToFolder( folder );
} );
Progress.Update( "Removing local changes", 5, 100 );
_ = GitUtils.Git( $"reset --hard HEAD" );
_ = Task.Delay( 50 );


Progress.Update( "Pulling remote changes", 25, 100 );
_ = GitUtils.Git( $"pull" );
_ = Task.Delay( 50 );

Progress.Update( "Checking out release...", 90, 100 );
_ = GitUtils.Git( $"checkout \"{release.TagName}\" --force", folder );
_ = Task.Delay( 50 );

// Update Manifest
Manifest.SetRelease( release );
Manifest.WriteToFolder( folder );

// Refresh UI
UpdateFromProject( Project );
}

/// <summary>
Expand Down
16 changes: 12 additions & 4 deletions code/ToolsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
[Tool( "Tools Manager", "hardware", "Manages your tools." )]
public class ToolsManager : BaseWindow
{
public static ToolsManager Instance { get; set; }

public ToolsManager()
{
Instance = this;

Size = new Vector2( 600, 400 );
MinimumSize = Size;
WindowTitle = "Tools Manager";
Expand All @@ -16,6 +20,7 @@ public ToolsManager()

WriteDummyManifest();

SetLayout( LayoutMode.LeftToRight );
CreateUI();
Show();
}
Expand Down Expand Up @@ -46,12 +51,15 @@ private void WriteDummyManifest()
manifest.WriteToFile( manifestPath );
}

public void CreateUI()
public void Refresh()
{
SetLayout( LayoutMode.LeftToRight );
var layout = Layout;
DestroyChildren();
CreateUI();
}

var toolsList = layout.Add( new NavigationView( this ) );
public void CreateUI()
{
var toolsList = Layout.Add( new NavigationView( this ) );

foreach ( var project in Utility.Projects.GetAll() )
{
Expand Down
2 changes: 1 addition & 1 deletion code/Utils/GitUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static async Task Git( string command, string workingDir = null )

ProcessStartInfo info = new( "git", command );
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.CreateNoWindow = true;

if ( workingDir != null )
info.WorkingDirectory = workingDir;
Expand Down

0 comments on commit 27a1c3f

Please sign in to comment.