55using PaintPower . ProjectSystem ;
66using PaintPower . Dialogs ;
77using System ;
8- using System . Diagnostics ;
98using System . Threading . Tasks ;
109
1110namespace PaintPower . FileExplorer ;
@@ -14,7 +13,7 @@ public partial class ExplorerView : UserControl
1413{
1514 public ObservableCollection < ExplorerItem > Items { get ; } = new ( ) ;
1615
17- private string _currentDir ;
16+ private string _currentDir = "" ;
1817 private TempWorkspace _workspace ;
1918
2019 public ExplorerView ( )
@@ -26,26 +25,37 @@ public ExplorerView()
2625 public void Initialize ( TempWorkspace workspace )
2726 {
2827 _workspace = workspace ;
29- _currentDir = workspace . ItemsDir ;
28+ _currentDir = workspace . ActiveRoot ;
3029
3130 FileList . ItemsSource = Items ;
3231 Refresh ( ) ;
3332 }
3433
34+ // Called when switching between project root and sprite root
35+ public void SetRoot ( string newRoot )
36+ {
37+ _currentDir = newRoot ;
38+ Refresh ( ) ;
39+ }
40+
3541 private void Refresh ( )
3642 {
3743 Items . Clear ( ) ;
3844
39- Items . Clear ( ) ;
4045 if ( _workspace == null || ! Directory . Exists ( _currentDir ) )
4146 {
4247 PathLabel . Text = "(no project)" ;
4348 return ;
4449 }
4550
46- string relativePath = ( _currentDir . Replace ( _workspace . ItemsDir , "" ) . Replace ( "\\ " , "/" ) ) + "/" ;
51+ // Compute relative path inside ActiveRoot
52+ string relative = _currentDir . Replace ( _workspace . ActiveRoot , "" )
53+ . Replace ( "\\ " , "/" ) ;
54+
55+ if ( string . IsNullOrEmpty ( relative ) )
56+ relative = "/" ;
4757
48- PathLabel . Text = relativePath ;
58+ PathLabel . Text = relative ;
4959
5060 // Folders first
5161 foreach ( var dir in Directory . GetDirectories ( _currentDir ) )
@@ -75,32 +85,26 @@ private void Refresh()
7585 // -----------------------------
7686 private void OnGoRoot ( object ? sender , RoutedEventArgs e )
7787 {
78- _currentDir = _workspace . ItemsDir ;
88+ _currentDir = _workspace . ActiveRoot ;
7989 Refresh ( ) ;
8090 }
8191
8292 private void OnGoUp ( object ? sender , RoutedEventArgs e )
8393 {
84- if ( _currentDir == _workspace . ItemsDir )
94+ if ( _currentDir == _workspace . ActiveRoot )
8595 return ;
8696
8797 _currentDir = Directory . GetParent ( _currentDir ) ! . FullName ;
8898 Refresh ( ) ;
8999 }
90100
91- // Pseudocode plan:
92- // - The error is caused by passing 'this' (ExplorerView, a UserControl) to InputDialog.ShowAsync, which expects a Window.
93- // - To fix, get the parent Window of this control and pass it instead.
94- // - Use 'this.GetVisualRoot() as Window' or 'Window.GetWindow(this)' to get the parent window.
95- // - Apply this fix in both OnNewFile and OnNewFolder.
96-
101+ // -----------------------------
102+ // Create File / Folder
103+ // -----------------------------
97104 private async void OnNewFile ( object ? sender , RoutedEventArgs e )
98105 {
99- if ( string . IsNullOrEmpty ( _currentDir ) )
100- {
101- // Either initialize to workspace default or show error/disable UI earlier
102- _currentDir = _workspace ? . ItemsDir ?? throw new InvalidOperationException ( "Explorer not initialized." ) ;
103- }
106+ if ( _workspace == null )
107+ return ;
104108
105109 var dialog = new InputDialog ( "New File" , "Enter file name:" ) ;
106110 var window = this . VisualRoot as Window ;
@@ -110,11 +114,15 @@ private async void OnNewFile(object? sender, RoutedEventArgs e)
110114 return ;
111115
112116 string path = Path . Combine ( _currentDir , name ) ;
113- if ( ! Directory . Exists ( path ) && ! File . Exists ( path ) )
117+
118+ if ( ! File . Exists ( path ) && ! Directory . Exists ( path ) )
114119 File . WriteAllText ( path , "" ) ;
120+ else
121+ await ShowErrorPopup ( ) ;
115122
116123 MainWindow . App . SetProjectStatus ( "Save Project" ) ;
117124 MainWindow . App . saveNeeded = true ;
125+
118126 Refresh ( ) ;
119127 }
120128
@@ -130,27 +138,27 @@ private async void OnNewFolder(object? sender, RoutedEventArgs e)
130138 string path = Path . Combine ( _currentDir , name ) ;
131139
132140 if ( ! Directory . Exists ( path ) && ! File . Exists ( path ) )
133- {
134141 Directory . CreateDirectory ( path ) ;
135- }
136- else {
137- ShowErrorPopup ( ) ;
138- }
142+ else
143+ await ShowErrorPopup ( ) ;
139144
140145 MainWindow . App . SetProjectStatus ( "Save Project" ) ;
141146 MainWindow . App . saveNeeded = true ;
142147
143148 Refresh ( ) ;
144149 }
145150
146- private async Task ShowErrorPopup ( ) {
147- var dialog = new PopupWindowDialog ( "File/Folder Creation Error!" , "File or folder already exists in this directory!" , "Error" ) ;
151+ private async Task ShowErrorPopup ( )
152+ {
153+ var dialog = new PopupWindowDialog (
154+ "File/Folder Creation Error!" ,
155+ "File or folder already exists in this directory!" ,
156+ "Error"
157+ ) ;
158+
148159 var window = this . VisualRoot as Window ;
149- try
150- {
151- await dialog . ShowAsync ( window ) ;
152- }
153- catch ( Exception ex ) { }
160+ try { await dialog . ShowAsync ( window ) ; }
161+ catch { }
154162 }
155163
156164 // -----------------------------
@@ -175,8 +183,6 @@ private void OnItemDoubleTapped(object? sender, RoutedEventArgs e)
175183
176184 // Open file in editor
177185 if ( VisualRoot is MainWindow main )
178- {
179186 main . OpenFile ( item . FullPath ) ;
180- }
181187 }
182188}
0 commit comments