Skip to content

Commit

Permalink
Improve "add tool" dialog by auto-appending folder name & showing mor…
Browse files Browse the repository at this point in the history
…e repo info
  • Loading branch information
xezno committed Nov 4, 2022
1 parent 2c7202b commit 5407eb5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
59 changes: 48 additions & 11 deletions code/AddToolDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Sandbox;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Tools;
Expand Down Expand Up @@ -30,21 +32,13 @@ public void CreateUI()
Layout.Spacing = 0;
Layout.Margin = 0;


// Filtering
{
var filter = Layout.Add( LayoutMode.TopToBottom );
filter.Margin = new( 20, 20, 20, 0 );

filter.Add( new LineEdit( "" ) { PlaceholderText = "Search GitHub..." } );
}

// body
{
Layout.AddSpacingCell( 8 );
RepoList = Layout.Add( new ListView(), 1 );
RepoList.ItemPaint = PaintAddonItem;
RepoList.ItemSize = new Vector2( 0, 38 );
RepoList.ItemSelected = OnRepoSelected;
Layout.AddSpacingCell( 8 );

GithubApi.FetchSearch( "topic:sbox-tool" ).ContinueWith( t =>
Expand Down Expand Up @@ -158,10 +152,53 @@ private void PaintAddonItem( VirtualWidget v )

Paint.SetFont( "Poppins", 10, 450 );
Paint.SetPen( fg );
Paint.DrawText( textRect, repo.Name, TextFlag.LeftTop );
Paint.DrawText( textRect, repo.FullName, TextFlag.LeftTop );

Paint.SetDefaultFont();
Paint.SetPen( fg.WithAlpha( 0.6f ) );
Paint.DrawText( textRect, repo.HtmlUrl, TextFlag.LeftBottom );
Paint.DrawText( textRect, repo.Description, TextFlag.LeftBottom );
}

Repository SelectedRepo;

private void OnRepoSelected( object o )
{
if ( o is not Repository repo )
return;

SelectedRepo = repo;
TrySetFolder( repo.Name );
}

void TrySetFolder( string subFolder )
{
try
{
var dir = new DirectoryInfo( Location.Text );

while ( true )
{
// If the folder exists and isn't an addon folder
// .. then assume this is a nice place to create an addon folder
if ( dir.Exists && dir.GetFiles( ".addon" ).Count() == 0 )
{
Location.Text = Path.Combine( dir.FullName, subFolder ).NormalizeFilename( false );
return;
}

// If we can't find anything at all for whatever reason, default to this
if ( dir.Parent == null )
{
Location.Text = Path.Combine( EditorPreferences.AddonLocation, subFolder ).NormalizeFilename( false );
return;
}

dir = dir.Parent;
}
}
catch ( System.Exception e )
{
Log.Warning( e, e.Message );
}
}
}
2 changes: 1 addition & 1 deletion tm-manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"repo":"xezno/tools-manager","description":"Manages your tools.","release_version":"0","release_name":"None","release_description":"Invalid release","auto_update":true}
{"repo":"xezno/sbox-tools-manager","description":"Manages your tools.","release_version":null,"release_name":null,"release_description":null,"auto_update":true}

0 comments on commit 5407eb5

Please sign in to comment.