From 5407eb511a9aecbbd6e4f9ea24d9d8b914727644 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 4 Nov 2022 22:10:52 +0000 Subject: [PATCH] Improve "add tool" dialog by auto-appending folder name & showing more repo info --- code/AddToolDialog.cs | 59 +++++++++++++++++++++++++++++++++++-------- tm-manifest.json | 2 +- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/code/AddToolDialog.cs b/code/AddToolDialog.cs index 59504e3..192a7df 100644 --- a/code/AddToolDialog.cs +++ b/code/AddToolDialog.cs @@ -1,4 +1,6 @@ using Sandbox; +using System.IO; +using System.Linq; using System.Threading.Tasks; namespace Tools; @@ -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 => @@ -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 ); + } } } diff --git a/tm-manifest.json b/tm-manifest.json index 2ced777..1bc3192 100644 --- a/tm-manifest.json +++ b/tm-manifest.json @@ -1 +1 @@ -{"repo":"xezno/tools-manager","description":"Manages your tools.","release_version":"0","release_name":"None","release_description":"Invalid release","auto_update":true} \ No newline at end of file +{"repo":"xezno/sbox-tools-manager","description":"Manages your tools.","release_version":null,"release_name":null,"release_description":null,"auto_update":true} \ No newline at end of file