Skip to content

Commit a11f257

Browse files
committed
Watch project file for changes and reload project; indicate missing project files
1 parent cfa4d5b commit a11f257

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

MiniWebCompiler/ViewModels/Project.cs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ public class Project : ViewModelBase
2121

2222
public const string ProjectFileName = "miniwebcompiler.json";
2323

24-
private readonly bool isLoaded;
2524
private readonly DispatcherTimer updateTimeTimer;
25+
private readonly DelayedCall reloadDc;
26+
27+
private bool isLoading;
28+
private string projectFileName;
2629
private FileSystemWatcher watcher;
2730

2831
#endregion Private data
@@ -31,10 +34,10 @@ public class Project : ViewModelBase
3134

3235
public Project(string projectPath)
3336
{
37+
reloadDc = DelayedCall.Create(() => Reload(), 500);
38+
3439
ProjectPath = projectPath;
35-
Name = Path.GetFileName(projectPath);
3640
LoadFromFile();
37-
isLoaded = true;
3841

3942
updateTimeTimer = new DispatcherTimer();
4043
updateTimeTimer.Tick += UpdateTimeTimerOnTick;
@@ -60,12 +63,16 @@ private async void InitializeAsync()
6063

6164
private void OnNameChanged()
6265
{
63-
if (isLoaded)
66+
if (!isLoading)
6467
{
6568
SaveToFile();
6669
}
6770
}
6871

72+
public bool IsDeleted { get; set; }
73+
74+
public TextDecorationCollection TextDecorations => IsDeleted ? System.Windows.TextDecorations.Strikethrough : null;
75+
6976
public string ProjectPath { get; set; }
7077

7178
private void OnProjectPathChanged()
@@ -81,6 +88,7 @@ private void OnProjectPathChanged()
8188
watcher.Changed += Watcher_Changed;
8289
watcher.Created += Watcher_Changed;
8390
watcher.Renamed += Watcher_Changed;
91+
watcher.Deleted += Watcher_Changed;
8492
watcher.IncludeSubdirectories = true;
8593
watcher.EnableRaisingEvents = true;
8694
}
@@ -94,11 +102,24 @@ private void Watcher_Changed(object sender, FileSystemEventArgs args)
94102
}
95103
else
96104
{
97-
foreach (var file in Files)
105+
if (PathUtil.PathEquals(args.FullPath, projectFileName))
98106
{
99-
if (file.MatchesFile(args.FullPath))
107+
reloadDc.Reset();
108+
}
109+
else if (args.ChangeType == WatcherChangeTypes.Renamed &&
110+
args is RenamedEventArgs renamedArgs &&
111+
PathUtil.PathEquals(renamedArgs.OldFullPath, projectFileName))
112+
{
113+
reloadDc.Reset();
114+
}
115+
else
116+
{
117+
foreach (var file in Files)
100118
{
101-
file.ScheduleCompile();
119+
if (file.MatchesFile(args.FullPath))
120+
{
121+
file.ScheduleCompile();
122+
}
102123
}
103124
}
104125
}
@@ -108,7 +129,7 @@ private void Watcher_Changed(object sender, FileSystemEventArgs args)
108129

109130
private void OnKeepIntermediaryFilesChanged()
110131
{
111-
if (isLoaded)
132+
if (!isLoading)
112133
{
113134
SaveToFile();
114135
}
@@ -118,7 +139,7 @@ private void OnKeepIntermediaryFilesChanged()
118139

119140
private void OnKeepUnminifiedFilesChanged()
120141
{
121-
if (isLoaded)
142+
if (!isLoading)
122143
{
123144
SaveToFile();
124145
}
@@ -272,13 +293,16 @@ private void UpdateStatus()
272293

273294
private void LoadFromFile()
274295
{
275-
string projectFileName = Path.Combine(ProjectPath, ProjectFileName);
296+
projectFileName = Path.Combine(ProjectPath, ProjectFileName);
297+
isLoading = true;
276298
try
277299
{
300+
Name = Path.GetFileName(ProjectPath);
278301
KeepUnminifiedFiles = App.Settings.KeepUnminifiedFilesDefault;
279302

280303
if (File.Exists(projectFileName))
281304
{
305+
IsDeleted = false;
282306
Name = "";
283307
Files.Clear();
284308

@@ -357,11 +381,28 @@ private void LoadFromFile()
357381
}
358382
}
359383
}
384+
else
385+
{
386+
IsDeleted = true;
387+
KeepUnminifiedFiles = false;
388+
KeepIntermediaryFiles = false;
389+
Files.Clear();
390+
}
360391
}
361392
catch (Exception ex)
362393
{
363394
MessageBox.Show("The project file \"" + projectFileName + "\" could not be loaded. " + ex.Message, App.Name, MessageBoxButton.OK, MessageBoxImage.Error);
364395
}
396+
finally
397+
{
398+
isLoading = false;
399+
}
400+
}
401+
402+
private void Reload()
403+
{
404+
LoadFromFile();
405+
InitializeAsync();
365406
}
366407

367408
private void SaveToFile()

MiniWebCompiler/Views/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
</Style>
5858
</Ellipse.Style>
5959
</Ellipse>
60-
<TextBlock Grid.Column="1" Margin="6,0,0,0" Text="{Binding Name}"/>
60+
<TextBlock Grid.Column="1" Margin="6,0,0,0" Text="{Binding Name}" TextDecorations="{Binding TextDecorations}"/>
6161
</Grid>
6262
</DataTemplate>
6363
</ListBox.ItemTemplate>

0 commit comments

Comments
 (0)