fix some long-standing window and document management bugs

This commit is contained in:
Michael Becker 2020-05-12 00:29:29 -04:00
parent 7ba9469aa1
commit 0f8117af0a
No known key found for this signature in database
GPG Key ID: 506F54899E2BFED7
2 changed files with 53 additions and 63 deletions

View File

@ -208,28 +208,13 @@ namespace UniversalEditor.UserInterface
void Application_Activated(object sender, ApplicationActivatedEventArgs e)
{
Document[] docs = new Document[e.CommandLine.FileNames.Count];
if (e.CommandLine.FileNames.Count > 0)
{
for (int i = 0; i < e.CommandLine.FileNames.Count; i++)
{
if (String.IsNullOrEmpty(e.CommandLine.FileNames[i]))
{
Console.WriteLine("ue: warning: ignoring empty file name passed to command line");
continue;
}
docs[i] = new Document(new FileAccessor(e.CommandLine.FileNames[i]));
}
}
if (LastWindow != null)
{
LastWindow.OpenFile(docs);
LastWindow.OpenFile(e.CommandLine.FileNames.ToArray());
}
else
{
OpenWindow(docs);
OpenWindow(e.CommandLine.FileNames.ToArray());
}
List<string> commandsToExecute = (Application.CommandLine.Options.GetValueOrDefault<List<string>>("command", null));
@ -287,22 +272,6 @@ namespace UniversalEditor.UserInterface
// Glue.Common.Methods.SendApplicationEvent(new Glue.ApplicationEventEventArgs(Glue.Common.Constants.EventNames.ApplicationStop));
}
/// <summary>
/// Opens a new window, optionally loading the specified documents.
/// </summary>
/// <param name="documents">The document model(s) of the document(s) to load.</param>
/// <returns>An <see cref="IHostApplicationWindow"/> representing the window that was created.</returns>
protected IHostApplicationWindow OpenWindowInternal (params Document[] documents)
{
MainWindow mw = new MainWindow ();
LastWindow = mw;
if (documents.Length > 0)
{
mw.OpenFile(documents);
}
mw.Show ();
return mw;
}
public void ShowAboutDialog (DataFormatReference dfr)
{
if (dfr == null)
@ -752,13 +721,12 @@ namespace UniversalEditor.UserInterface
}
}
private IHostApplicationWindowCollection mvarWindows = new IHostApplicationWindowCollection();
public IHostApplicationWindowCollection Windows { get { return mvarWindows; } }
public IHostApplicationWindowCollection Windows { get; } = new IHostApplicationWindowCollection();
public void CloseAllWindows()
{
List<IHostApplicationWindow> windowsToClose = new List<IHostApplicationWindow>();
foreach (IHostApplicationWindow window in mvarWindows)
foreach (IHostApplicationWindow window in Windows)
{
windowsToClose.Add(window);
}
@ -830,7 +798,6 @@ namespace UniversalEditor.UserInterface
{
MainWindow mw = new MainWindow();
mw.Show();
// OpenWindow(new Document[0]);
}
/// <summary>
/// Opens a new window, optionally loading the specified documents.
@ -838,24 +805,10 @@ namespace UniversalEditor.UserInterface
/// <param name="fileNames">The file name(s) of the document(s) to load.</param>
public void OpenWindow(params string[] fileNames)
{
List<Document> loadedDocuments = new List<Document>();
for (int i = 0; i < fileNames.Length; i++)
{
AccessorReference[] accs = UniversalEditor.Common.Reflection.GetAvailableAccessors(fileNames[i]);
if (accs.Length > 0)
{
Accessor fa = accs[0].Create();
fa.OriginalUri = new Uri(fileNames[i]);
Document document = new Document(fa);
loadedDocuments.Add(document);
}
else
{
Console.Error.WriteLine("ue: accessor not found for path " + fileNames[i] + "; assuming local file");
loadedDocuments.Add(new Document(new FileAccessor(fileNames[i])));
}
}
OpenWindow(loadedDocuments.ToArray());
MainWindow mw = new MainWindow();
mw.Show();
mw.OpenFile(fileNames);
}
/// <summary>
/// Opens a new window, optionally loading the specified documents.
@ -863,12 +816,10 @@ namespace UniversalEditor.UserInterface
/// <param name="documents">The document model(s) of the document(s) to load.</param>
public void OpenWindow(params Document[] documents)
{
IHostApplicationWindow window = OpenWindowInternal(documents);
window.WindowClosed += delegate(object sender, EventArgs e)
{
mvarWindows.Remove(window);
};
mvarWindows.Add(window);
MainWindow mw = new MainWindow();
mw.Show();
mw.OpenFile(documents);
}
// UniversalDataStorage.Editor.WindowsForms.Program

View File

@ -28,6 +28,7 @@ using UniversalEditor.UserInterface.Pages;
using UniversalEditor.ObjectModels.Binary;
using UniversalEditor.DataFormats.Binary;
using System.Collections.Generic;
using System.Text;
namespace UniversalEditor.UserInterface
{
@ -704,7 +705,11 @@ namespace UniversalEditor.UserInterface
protected override void OnClosed(EventArgs e)
{
MBS.Framework.UserInterface.Application.Stop();
Engine.CurrentEngine.Windows.Remove(this);
if (Engine.CurrentEngine.Windows.Count <= 0)
{
MBS.Framework.UserInterface.Application.Stop();
}
}
protected override void OnCreated(EventArgs e)
{
@ -712,9 +717,18 @@ namespace UniversalEditor.UserInterface
{
new DragDropTarget("text/uri-list", DragDropTargetFlags.SameApplication | DragDropTargetFlags.OtherApplication, 0x1)
}, DragDropEffect.Copy, MouseButtons.Primary | MouseButtons.Secondary, KeyboardModifierKey.None);
Engine.CurrentEngine.Windows.Add(this);
Engine.CurrentEngine.LastWindow = this;
}
#region IHostApplicationWindow implementation
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
Engine.CurrentEngine.LastWindow = this;
}
#region IHostApplicationWindow implementation
public void OpenFile()
{
/*
@ -745,6 +759,8 @@ namespace UniversalEditor.UserInterface
public void OpenFile(params string[] fileNames)
{
Document[] documents = new Document[fileNames.Length];
List<string> failedFiles = new List<string>();
for (int i = 0; i < documents.Length; i++)
{
AccessorReference[] accs = UniversalEditor.Common.Reflection.GetAvailableAccessors(fileNames[i]);
@ -758,6 +774,29 @@ namespace UniversalEditor.UserInterface
FileAccessor fa = new FileAccessor(fileNames[i], true, false, true);
documents[i] = new Document(fa);
}
else
{
failedFiles.Add(fileNames[i]);
}
}
if (failedFiles.Count > 0)
{
StringBuilder sb = new StringBuilder();
if (failedFiles.Count == 1)
{
sb.Append(String.Format("The file '{0}' could not be found.", failedFiles[0]));
}
else
{
sb.AppendLine("The following files could not be found:");
sb.AppendLine();
for (int i = 0; i < failedFiles.Count; i++)
{
sb.AppendLine(failedFiles[i]);
}
}
MessageDialog.ShowDialog(sb.ToString(), "Error", MessageDialogButtons.OK, MessageDialogIcon.Error);
}
OpenFile(documents);
}