From fea08958853eee1fe865edac69cfe0e5f5c3d781 Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Wed, 3 Nov 2021 20:22:32 -0500 Subject: [PATCH] Improved document load flow --- .../IrisProjectViewModel.cs | 33 ++++++++++++ ZuneUIXTools/MainWindow.xaml | 7 +-- ZuneUIXTools/MainWindow.xaml.cs | 51 +++++++------------ 3 files changed, 53 insertions(+), 38 deletions(-) diff --git a/ZuneUIXTools.ViewModels/IrisProjectViewModel.cs b/ZuneUIXTools.ViewModels/IrisProjectViewModel.cs index cb1b446..ae433e0 100644 --- a/ZuneUIXTools.ViewModels/IrisProjectViewModel.cs +++ b/ZuneUIXTools.ViewModels/IrisProjectViewModel.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq; using System.Text; namespace ZuneUIXTools.ViewModels @@ -28,5 +29,37 @@ namespace ZuneUIXTools.ViewModels get => _selectedDocument; set => SetProperty(ref _selectedDocument, value); } + + public DocumentViewModelBase LoadDocument(string fileName, string fileFormat = null) + { + if (fileFormat == null) + fileFormat = System.IO.Path.GetExtension(fileName); + + DocumentViewModelBase doc = UIXDocuments.FirstOrDefault(oldDoc => fileName == oldDoc.FileName); + if (doc != null) + return doc; + + if (fileFormat.EndsWith(".uix")) + doc = new UIXDocumentViewModel(fileName); + else if (fileFormat.EndsWith(".uib")) + doc = new CompiledUIXDocumentViewModel(fileName); + else + throw new ArgumentException($"\"{fileName}\" is not a known format."); + + UIXDocuments.Add(doc); + return doc; + } + + public void UnloadDocument(string fileName) + { + DocumentViewModelBase doc = UIXDocuments.FirstOrDefault(oldDoc => fileName == oldDoc.FileName); + if (doc != null) + UIXDocuments.Remove(doc); + } + + public void UnloadDocument(DocumentViewModelBase doc) + { + UIXDocuments.Remove(doc); + } } } diff --git a/ZuneUIXTools/MainWindow.xaml b/ZuneUIXTools/MainWindow.xaml index 75ca4e9..335a2a4 100644 --- a/ZuneUIXTools/MainWindow.xaml +++ b/ZuneUIXTools/MainWindow.xaml @@ -34,11 +34,6 @@ - - - - - @@ -58,6 +53,8 @@ + diff --git a/ZuneUIXTools/MainWindow.xaml.cs b/ZuneUIXTools/MainWindow.xaml.cs index 880ed05..5e0cc3a 100644 --- a/ZuneUIXTools/MainWindow.xaml.cs +++ b/ZuneUIXTools/MainWindow.xaml.cs @@ -36,8 +36,7 @@ namespace ZuneUIXTools /// public partial class MainWindow : Window { - private const string SOURCE_FILE_FORMAT_FILTER = "Microsoft Iris UI (*.uix)|*.uix|XML files (*.xml)|*.xml|All Files|*.*"; - private const string COMPILED_FILE_FORMAT_FILTER = "Microsoft Iris Compiled UI (*.uib)|*.uib|All Files|*.*"; + private const string FILE_FORMAT_FILTER = "Microsoft Iris UI (*.uix)|*.uix|Microsoft Iris Compiled UI (*.uib)|*.uib|XML files (*.xml)|*.xml|All Files|*.*"; private readonly FontFamily CODE_FONT = new FontFamily("JetBrains Mono"); public IrisProjectViewModel IrisProject { get; } = new IrisProjectViewModel @@ -53,28 +52,15 @@ namespace ZuneUIXTools Loaded += MainWindow_Loaded; } - private void RemoveDocument(string fileName, bool removeFromProject = true, bool removeFromUI = true) + private void RemoveDocument(string fileName) { - if (removeFromProject) - IrisProject.UIXDocuments.Remove(IrisProject.UIXDocuments.First(doc => doc.FileName == fileName)); - - if (!removeFromUI) - return; FrameworkElement elem = GetDocumentUI(fileName); if (elem != null) DockingManager.Children.Remove(elem); } - private void AddDocument(string fileName, bool addToProject = true, bool addToUI = true) - => AddDocument(new UIXDocumentViewModel(fileName), addToProject, addToUI); - - private void AddDocument(DocumentViewModelBase doc, bool addToProject = true, bool addToUI = true) + private void AddDocument(DocumentViewModelBase doc) { - if (addToProject && !IrisProject.UIXDocuments.Any(oldDoc => doc.FileName == oldDoc.FileName)) - IrisProject.UIXDocuments.Add(doc); - - if (!addToUI) - return; // var editor = new TextEditor() @@ -169,12 +155,12 @@ namespace ZuneUIXTools private void UIXDocuments_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.OldItems != null) - foreach (UIXDocumentViewModel doc in e.OldItems) - RemoveDocument(doc.FileName, removeFromProject: false); + foreach (DocumentViewModelBase doc in e.OldItems) + RemoveDocument(doc.FileName); if (e.NewItems != null) - foreach (UIXDocumentViewModel doc in e.NewItems) - AddDocument(doc.FileName, addToProject: false); + foreach (DocumentViewModelBase doc in e.NewItems) + AddDocument(doc); } private void BuildAndRun() @@ -252,6 +238,7 @@ namespace ZuneUIXTools private void Decompile() { string compiledFile = IrisProject.SelectedDocument.FileName; + string uiRoot = (IrisProject.SelectedDocument as CompiledUIXDocumentViewModel)?.UIRoot; try { @@ -261,7 +248,7 @@ namespace ZuneUIXTools try { - Application.Window.RequestLoad("file://" + compiledFile);// + (string.IsNullOrEmpty(uiRoot) ? string.Empty : "#" + uiRoot)); + Application.Window.RequestLoad("file://" + compiledFile + (string.IsNullOrEmpty(uiRoot) ? string.Empty : "#" + uiRoot)); InterpreterContext.UseDecompile = true; Application.Run(); InterpreterContext.UseDecompile = false; @@ -324,12 +311,12 @@ namespace ZuneUIXTools { OpenFileDialog openFileDialog = new OpenFileDialog { - Filter = SOURCE_FILE_FORMAT_FILTER + Filter = FILE_FORMAT_FILTER }; if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; - AddDocument(openFileDialog.FileName, addToUI: false); + IrisProject.LoadDocument(openFileDialog.FileName); } private void Save_Click(object sender, RoutedEventArgs e) @@ -348,7 +335,7 @@ namespace ZuneUIXTools SaveFileDialog saveFileDialog = new SaveFileDialog { - Filter = SOURCE_FILE_FORMAT_FILTER, + Filter = FILE_FORMAT_FILTER, FileName = IrisProject.SelectedDocument.FileName }; if (saveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) @@ -356,21 +343,19 @@ namespace ZuneUIXTools var editor = GetDocumentUI(IrisProject.SelectedDocument.FileName) as TextEditor; editor.Save(saveFileDialog.FileName); - AddDocument(saveFileDialog.FileName); + IrisProject.LoadDocument(saveFileDialog.FileName); } private void Decompile_Click(object sender, RoutedEventArgs e) { - OpenFileDialog openFileDialog = new OpenFileDialog - { - Filter = COMPILED_FILE_FORMAT_FILTER - }; - if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) + if (!(IrisProject.SelectedDocument is CompiledUIXDocumentViewModel doc)) return; - AddDocument(openFileDialog.FileName, addToUI: false); + // Set UI root + doc.UIRoot = UIRootBox.Text; - IrisProject.SelectedDocument = new CompiledUIXDocumentViewModel(openFileDialog.FileName); + // Clear error list + ErrorPanel.Children.Clear(); var _decompThread = new Thread(new ThreadStart(Decompile)); _decompThread.SetApartmentState(ApartmentState.STA);