Skip to content

Commit 64a857b

Browse files
committed
Handle files changing on disk whilst editor is focused.
When editor is not in focus, queue definition changes. Only reload on focused again.
1 parent 72a32db commit 64a857b

File tree

3 files changed

+88
-13
lines changed

3 files changed

+88
-13
lines changed

StructuredXmlEditor/Data/Document.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ public Document(string path, Workspace workspace)
101101
backupTimer.Start();
102102
}
103103

104+
//-----------------------------------------------------------------------
105+
public void PromptForReload()
106+
{
107+
if (NeedsReload)
108+
{
109+
NeedsReload = false;
110+
111+
string response = Message.Show("This document changed on disk, do you want to reload it? Clicking Yes will discard any local changes.", "Document Changed On Disk", "Yes", "No");
112+
113+
if (response == "Yes")
114+
{
115+
Close(true);
116+
Workspace.Open(Path);
117+
}
118+
}
119+
}
120+
104121
//-----------------------------------------------------------------------
105122
private void OpenInExplorer(string path)
106123
{

StructuredXmlEditor/Data/Workspace.cs

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ public class Workspace : NotifyPropertyChanged
2929
//-----------------------------------------------------------------------
3030
public static Workspace Instance;
3131

32+
//-----------------------------------------------------------------------
33+
public bool IsWorkspaceActive
34+
{
35+
get { return m_isWorkspaceActive; }
36+
set
37+
{
38+
m_isWorkspaceActive = value;
39+
40+
if (value)
41+
{
42+
Current?.PromptForReload();
43+
44+
if (NeedsLoadDefinitions)
45+
{
46+
NeedsLoadDefinitions = false;
47+
LoadDefinitions();
48+
}
49+
}
50+
}
51+
}
52+
private bool m_isWorkspaceActive;
53+
54+
//--------------------------------------------------------------------------
55+
private bool NeedsLoadDefinitions { get; set; }
56+
3257
//-----------------------------------------------------------------------
3358
public ObservableCollection<Document> Documents { get; set; } = new ObservableCollection<Document>();
3459
public Document Current
@@ -40,15 +65,7 @@ public Document Current
4065

4166
if (m_current != null && m_current.NeedsReload)
4267
{
43-
m_current.NeedsReload = false;
44-
45-
string response = Message.Show("This document changed on disk, do you want to reload it? Clicking Yes will discard any local changes.", "Document Changed On Disk", "Yes", "No");
46-
47-
if (response == "Yes")
48-
{
49-
m_current.Close(true);
50-
Current = Open(m_current.Path);
51-
}
68+
m_current.PromptForReload();
5269
}
5370

5471
RaisePropertyChangedEvent();
@@ -274,31 +291,51 @@ public void SetupFileChangeHandlers()
274291
{
275292
FileChanged += (path) =>
276293
{
277-
if (Path.GetExtension(path) == ".xmldef") LoadDefinitions();
294+
if (Path.GetExtension(path) == ".xmldef")
295+
{
296+
QueueLoadDefinitions();
297+
}
278298

279299
var open = Documents.FirstOrDefault(e => e.Path == path);
280300
if (open != null)
281301
{
282302
open.NeedsReload = true;
303+
if (IsWorkspaceActive)
304+
{
305+
open.PromptForReload();
306+
}
283307
}
284308

285309
ProjectViewTool.Instance.Add(path);
286310
};
287311

288312
FileCreated += (path) =>
289313
{
290-
if (Path.GetExtension(path) == ".xmldef") LoadDefinitions();
314+
if (Path.GetExtension(path) == ".xmldef")
315+
{
316+
QueueLoadDefinitions();
317+
}
318+
291319
ProjectViewTool.Instance.Add(path);
292320
};
293321

294322
FileDeleted += (path) =>
295323
{
296-
if (Path.GetExtension(path) == ".xmldef") LoadDefinitions();
324+
if (Path.GetExtension(path) == ".xmldef")
325+
{
326+
QueueLoadDefinitions();
327+
}
328+
297329
ProjectViewTool.Instance.Remove(path);
298330
};
299331

300332
FileRenamed += (oldPath, newPath) =>
301333
{
334+
if (Path.GetExtension(oldPath) == ".xmldef" || Path.GetExtension(newPath) == ".xmldef")
335+
{
336+
QueueLoadDefinitions();
337+
}
338+
302339
ProjectViewTool.Instance.Remove(oldPath);
303340
ProjectViewTool.Instance.Add(newPath);
304341
};
@@ -435,6 +472,19 @@ public string LoadProjectRoot(bool isFailure = true)
435472
return null;
436473
}
437474

475+
//-----------------------------------------------------------------------
476+
public void QueueLoadDefinitions()
477+
{
478+
if (IsWorkspaceActive)
479+
{
480+
LoadDefinitions();
481+
}
482+
else
483+
{
484+
NeedsLoadDefinitions = true;
485+
}
486+
}
487+
438488
//-----------------------------------------------------------------------
439489
public void LoadDefinitions()
440490
{

StructuredXmlEditor/MainWindow.xaml.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ public MainWindow()
6060
{
6161
if (Workspace != null)
6262
{
63-
Workspace.Current = Workspace.Current;
63+
Workspace.IsWorkspaceActive = true;
64+
}
65+
};
66+
67+
Deactivated += (e, args) =>
68+
{
69+
if (Workspace != null)
70+
{
71+
Workspace.IsWorkspaceActive = false;
6472
}
6573
};
6674
}

0 commit comments

Comments
 (0)