Skip to content

Commit

Permalink
Merge pull request #408 from kaltinril/fix_file_handle_on_new_save_pr…
Browse files Browse the repository at this point in the history
…oject

Add enabled for FileWatchManager and FileWatchLogic.  Add call to HandleProjectLoaded inside SaveProject
  • Loading branch information
vchelaru authored Dec 24, 2024
2 parents fe25e6a + 9a8b907 commit 265b640
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 80 deletions.
4 changes: 2 additions & 2 deletions Gum/Gum.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@
<DependentUpon>DeleteOptionsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Logic\CopyPasteLogic.cs" />
<Compile Include="Logic\FileWatch\FileWatchLogic.cs" />
<Compile Include="Plugins\InternalPlugins\FileWatchPlugin\FileWatchLogic.cs" />
<Compile Include="Logic\ReorderLogic.cs" />
<Compile Include="Logic\VariableSaveLogic.cs" />
<Compile Include="Managers\DeleteLogic.cs" />
<Compile Include="Managers\FileChangeReactionLogic.cs" />
<Compile Include="Logic\FileWatch\FileWatchManager.cs" />
<Compile Include="Plugins\InternalPlugins\FileWatchPlugin\FileWatchManager.cs" />
<Compile Include="Managers\GraphicalUiElementManager.cs" />
<Compile Include="Managers\HotkeyManager.cs" />
<Compile Include="Managers\LocalizationManager.cs" />
Expand Down
10 changes: 6 additions & 4 deletions Gum/Managers/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public void Initialize()

public void CreateNewProject()
{
FileWatchLogic.Self.HandleProjectUnloaded();

mGumProjectSave = new GumProjectSave();
ObjectFinder.Self.GumProjectSave = mGumProjectSave;

Expand Down Expand Up @@ -232,8 +230,6 @@ public void LoadProject(FilePath fileName)

GraphicalUiElement.CanvasWidth = mGumProjectSave.DefaultCanvasWidth;
GraphicalUiElement.CanvasHeight = mGumProjectSave.DefaultCanvasHeight;

FileWatchLogic.Self.HandleProjectLoaded();
}
else
{
Expand Down Expand Up @@ -667,6 +663,8 @@ internal void SaveProject(bool forceSaveContainedElements = false)
PluginManager.Self.BeforeElementSave(standardElementSave);
}
}

// todo - this should go through the plugin...
FileWatchLogic.Self.IgnoreNextChangeOn(GumProjectSave.FullFileName);

GumCommands.Self.TryMultipleTimes(() => GumProjectSave.Save(GumProjectSave.FullFileName, saveContainedElements));
Expand Down Expand Up @@ -800,6 +798,10 @@ public bool AskUserForProjectNameIfNecessary(out bool isProjectNew)
if(result == DialogResult.OK)
{
GumProjectSave.FullFileName = openFileDialog.FileName;
var filePath = new FilePath(openFileDialog.FileName);
PluginManager.Self.ProjectLocationSet(filePath);
WpfDataUi.Controls.FileSelectionDisplay.FolderRelativeTo = filePath.GetDirectoryContainingThis().FullPath;

shouldSave = true;
isProjectNew = true;
}
Expand Down
70 changes: 25 additions & 45 deletions Gum/Plugins/BaseClasses/PluginBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public abstract class PluginBase : IPlugin
public event Action<GumProjectSave> ProjectLoad;
public event Action<GumProjectSave> AfterProjectSave;
public event Action<GumProjectSave> BeforeProjectSave;
public event Action<FilePath> ProjectLocationSet;

public event Action<ElementSave> BeforeElementSave;
public event Action<ElementSave> AfterElementSave;
public event Action GuidesChanged;
Expand Down Expand Up @@ -315,66 +317,44 @@ public void RemoveControl(System.Windows.Controls.UserControl control)

#region Event calling

public void CallProjectLoad(GumProjectSave newlyLoadedProject)
{
if (ProjectLoad != null)
{
ProjectLoad(newlyLoadedProject);
}
}
public void CallProjectLoad(GumProjectSave newlyLoadedProject) =>
ProjectLoad?.Invoke(newlyLoadedProject);

public void CallProjectSave(GumProjectSave savedProject)
{
if (AfterProjectSave != null)
{
AfterProjectSave(savedProject);
}
}
public void CallProjectSave(GumProjectSave savedProject) =>
AfterProjectSave?.Invoke(savedProject);

public void CallGuidesChanged()
{
public void CallProjectLocationSet(FilePath filePath) =>
ProjectLocationSet?.Invoke(filePath);

public void CallGuidesChanged() =>
GuidesChanged?.Invoke();
}


public void CallExport(ElementSave elementSave)
{
public void CallExport(ElementSave elementSave) =>
Export?.Invoke(elementSave);
}

public void CallDeleteOptionsWindowShow(DeleteOptionsWindow optionsWindow, object objectToDelete)
{
if (DeleteOptionsWindowShow != null)
{
DeleteOptionsWindowShow(optionsWindow, objectToDelete);
}
}

public void CallDeleteConfirm(DeleteOptionsWindow optionsWindow, object deletedObject)
{
DeleteConfirm?.Invoke(optionsWindow, deletedObject);
}
public void CallDeleteOptionsWindowShow(DeleteOptionsWindow optionsWindow, object objectToDelete) =>
DeleteOptionsWindowShow?.Invoke(optionsWindow, objectToDelete);

public void CallElementAdd(ElementSave element)
{
public void CallDeleteConfirm(DeleteOptionsWindow optionsWindow, object deletedObject) =>
DeleteConfirm?.Invoke(optionsWindow, deletedObject);

public void CallElementAdd(ElementSave element) =>
ElementAdd?.Invoke(element);
}

public void CallElementDelete(ElementSave element)
{

public void CallElementDelete(ElementSave element) =>
ElementDelete?.Invoke(element);
}


public void CallElementDuplicate(ElementSave oldElement, ElementSave newElement)
{
public void CallElementDuplicate(ElementSave oldElement, ElementSave newElement) =>
ElementDuplicate?.Invoke(oldElement, newElement);
}

public void CallElementRename(ElementSave elementSave, string oldName)
{
public void CallElementRename(ElementSave elementSave, string oldName) =>
ElementRename?.Invoke(elementSave, oldName);
}

public void CallStateRename(StateSave stateSave, string oldName) => StateRename?.Invoke(stateSave, oldName);
public void CallStateRename(StateSave stateSave, string oldName) =>
StateRename?.Invoke(stateSave, oldName);

public void CallStateAdd(StateSave stateSave) => StateAdd?.Invoke(stateSave);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class FileWatchLogic : Singleton<FileWatchLogic>
{
FileWatchManager fileWatchManager;

public bool Enabled { get { return fileWatchManager.Enabled; } }

public FileWatchLogic()
{
fileWatchManager = FileWatchManager.Self;
Expand All @@ -22,7 +24,14 @@ public void HandleProjectLoaded()

var directories = GetFileWatchRootDirectories();

fileWatchManager.EnableWithDirectories(directories);
if(ProjectManager.Self.GumProjectSave?.FullFileName != null)
{
fileWatchManager.EnableWithDirectories(directories);
}
else
{
fileWatchManager.Disable();
}
}

private static HashSet<FilePath> GetFileWatchRootDirectories()
Expand Down Expand Up @@ -85,24 +94,27 @@ void AddRange(List<string> files)

FilePath gumProjectFilePath = ProjectManager.Self.GumProjectSave.FullFileName;

char gumProjectDrive = gumProjectFilePath.Standardized[0];
if(gumProjectFilePath != null)
{
char gumProjectDrive = gumProjectFilePath.Standardized[0];
directories.Add(gumProjectFilePath.GetDirectoryContainingThis());

directories.Add(gumProjectFilePath.GetDirectoryContainingThis());
// why are we adding the deep ones, isn't it enough to add the roots?
// why are we adding the deep ones, isn't it enough to add the roots?

//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Screens/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Components/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Standards/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Behaviors/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "FontCache/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Screens/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Components/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Standards/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "Behaviors/");
//directories.Add(gumProjectFilePath.GetDirectoryContainingThis() + "FontCache/");

var gumProject = GumState.Self.ProjectState.GumProjectSave;
if (!string.IsNullOrEmpty(gumProject.LocalizationFile))
{
var localizationDirectory = new FilePath(
GumState.Self.ProjectState.ProjectDirectory + gumProject.LocalizationFile)
.GetDirectoryContainingThis();
directories.Add(localizationDirectory);
var gumProject = GumState.Self.ProjectState.GumProjectSave;
if (!string.IsNullOrEmpty(gumProject.LocalizationFile))
{
var localizationDirectory = new FilePath(
GumState.Self.ProjectState.ProjectDirectory + gumProject.LocalizationFile)
.GetDirectoryContainingThis();
directories.Add(localizationDirectory);
}
}

return directories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class FileWatchManager : Singleton<FileWatchManager>
List<FilePath> filesCurrentlyFlushing = new List<FilePath>();

FileSystemWatcher fileSystemWatcher;
public bool Enabled { get { return fileSystemWatcher.EnableRaisingEvents; } }

DateTime LastFileChange;

Expand Down
50 changes: 45 additions & 5 deletions Gum/Plugins/InternalPlugins/FileWatchPlugin/MainFileWatchPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
using Gum.Logic.FileWatch;
using Gum.DataTypes;
using Gum.Logic.FileWatch;
using Gum.Plugins.BaseClasses;
using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Timers;
using ToolsUtilities;

namespace Gum.Plugins.FileWatchPlugin;

[Export(typeof(PluginBase))]
public class MainFileWatchPlugin : InternalPlugin
{
#region Fields/Properties

System.Timers.Timer timer;

FileWatchViewModel viewModel;

PluginTab pluginTab;
System.Windows.Forms.ToolStripMenuItem showFileWatchMenuItem;

#endregion

public override void StartUp()
{
var control = new FileWatchControl();
Expand All @@ -37,6 +43,31 @@ public override void StartUp()
timer = new System.Timers.Timer(millisecondsTimerFrequency);
timer.Elapsed += HandleElapsed;
timer.Start();

AssignEvents();
}

private void AssignEvents()
{
this.ProjectLoad += HandleProjectLoad;
this.ProjectLocationSet += HandleProjectLocationSet;
}

private void HandleProjectLocationSet(FilePath path)
{
FileWatchLogic.Self.HandleProjectLoaded();
}

private void HandleProjectLoad(GumProjectSave save)
{
if(save.FullFileName == null)
{
FileWatchLogic.Self.HandleProjectUnloaded();
}
else
{
FileWatchLogic.Self.HandleProjectLoaded();
}
}

private void HandleTabShown()
Expand Down Expand Up @@ -74,12 +105,21 @@ private void HandleElapsed(object sender, ElapsedEventArgs e)
return;
}

var filePathsWatchingText = $"File paths watching ({fileWatchManager.CurrentFilePathsWatching?.Count ?? 0}):";
foreach(var item in fileWatchManager.CurrentFilePathsWatching)
string filePathsWatchingText = "";

if(fileWatchManager.Enabled)
{
filePathsWatchingText = $"File paths watching ({fileWatchManager.CurrentFilePathsWatching?.Count ?? 0}):";
foreach(var item in fileWatchManager.CurrentFilePathsWatching)
{
filePathsWatchingText += $"\n\t{item}";
}
viewModel.WatchFolderInformation = filePathsWatchingText;
}
else
{
filePathsWatchingText += $"\n\t{item}";
viewModel.WatchFolderInformation = "File watching is disabled";
}
viewModel.WatchFolderInformation = filePathsWatchingText;

viewModel.NumberOfFilesToFlush = fileWatchManager.ChangedFilesWaitingForFlush.Count.ToString();

Expand Down
17 changes: 9 additions & 8 deletions Gum/Plugins/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,11 +599,18 @@ internal void BeforeElementSave(ElementSave savedElement) =>
internal void AfterElementSave(ElementSave savedElement) =>
CallMethodOnPlugin(plugin => plugin.CallAfterElementSave(savedElement));



internal void BeforeProjectSave(GumProjectSave savedProject) =>
CallMethodOnPlugin(plugin => plugin.CallBeforeProjectSave(savedProject));

internal void ProjectLoad(GumProjectSave newlyLoadedProject) =>
CallMethodOnPlugin(plugin => plugin.CallProjectLoad(newlyLoadedProject));

internal void ProjectSave(GumProjectSave savedProject) =>
CallMethodOnPlugin(plugin => plugin.CallProjectSave(savedProject));

internal void ProjectLocationSet(FilePath filePath) =>
CallMethodOnPlugin(plugin => plugin.CallProjectLocationSet(filePath));

internal void Export(ElementSave elementToExport) =>
CallMethodOnPlugin(plugin => plugin.CallExport(elementToExport));

Expand Down Expand Up @@ -688,12 +695,6 @@ internal void AfterUndo() =>
internal void GuidesChanged() =>
CallMethodOnPlugin(plugin => plugin.CallGuidesChanged());

internal void ProjectLoad(GumProjectSave newlyLoadedProject) =>
CallMethodOnPlugin(plugin => plugin.CallProjectLoad(newlyLoadedProject));

internal void ProjectSave(GumProjectSave savedProject) =>
CallMethodOnPlugin(plugin => plugin.CallProjectSave(savedProject));

internal List<Attribute> GetAttributesFor(VariableSave variableSave)
{
var listToFill = new List<Attribute>();
Expand Down

0 comments on commit 265b640

Please sign in to comment.