/// <summary> /// Instantiation of MainWindowViewModel /// </summary> public MainWindowViewModel() { s_Instance = this; Model = new MainWindowModel(); AddAccent("BaseAccent", new Uri("pack://application:,,,/Macro Editor;component/Themes/BaseAccent.xaml")); AddTheme(new LightTheme()); AddTheme(new DarkTheme()); SetTheme(Properties.Settings.Default.Theme); SetAccent("BaseAccent"); DockManager = new DockManagerViewModel(Properties.Settings.Default.OpenDocuments); SettingsMenu = new SettingsMenuViewModel(); RuntimeItems = new ObservableCollection <ComboBoxItem>(); if (MacroUI.GetInstance().IsLoaded()) { Initialize(); } else { Events.SubscribeEvent("ApplicationLoaded", (Action)Initialize); } Events.SubscribeEvent("ActiveMacroChanged", (Action)ActiveMacroChanged); }
/// <summary> /// Identifies documents that are open from a serialized list /// </summary> /// <param name="docs"></param> /// <returns>List of DocumentViewModels</returns> private static List <DocumentViewModel> LoadVisibleDocuments(string[] paths) { List <DocumentViewModel> documents = new List <DocumentViewModel>(); if (paths == null) { return(documents); } foreach (string s in paths) { FileDeclaration d = MacroUI.GetInstance().GetDeclarationFromFullname(s); if (d != null) { DocumentModel model = DocumentModel.Create(d); if (model != null) { DocumentViewModel viewModel = DocumentViewModel.Create(model); documents.Add(viewModel); } } } return(documents); }
/// <summary> /// Closes the current document, prompts if unsaved /// </summary> public void Close() { if (!IsSaved) { MacroUI.GetInstance().DisplayYesNoCancelMessage(Title + " has unsaved changed. Do you want to save the changes?", "Confirmation", "Discard", new Action <MessageDialogResult>((result) => { if (result == MessageDialogResult.Affirmative) { SaveCommand.Execute(new Action(() => IsClosed = true)); } else if (result == MessageDialogResult.FirstAuxiliary) { IsClosed = true; } else { IsClosed = false; } })); } else { IsClosed = true; } }
/// <summary> /// Produces a collection of displable instances of assemblies /// </summary> private void LoadAssemblies() { LibraryItems.Clear(); foreach (AssemblyDeclaration ad in MacroUI.GetInstance().GetAssemblies()) { LibraryItems.Add(new DisplayableListViewItem(ad)); } OnPropertyChanged(nameof(LabelVisible)); }
/// <summary> /// Executes a macro, either directly or through the editor /// </summary> /// <param name="d">The file delcaration</param> /// <param name="async">Bool which indicates whether the execution should be asynchronous or not (synchronous)</param> public void ExecuteMacro(FileDeclaration d, bool async) { if (MainWindow.GetInstance().IsActive) { OpenMacro(d); MainWindowViewModel.GetInstance().ExecuteMacro(async); } else { MacroUI.GetInstance().TryExecuteFile(d, async); } }
/// <summary> /// Renames a macro and applies changes to UI /// </summary> /// <param name="d">The file declaration</param> /// <param name="newName">The new name of the macro</param> public void RenameMacro(FileDeclaration d, string newName) { MacroUI.GetInstance().RenameFile(d, newName); DocumentViewModel dvm = DockManager.GetDocument(d); if (dvm != null) { dvm.Title = d.Info.Name; dvm.ContentId = d.Info.FullName; } }
/// <summary> /// Renames a folder and applies changes to UI /// </summary> /// <param name="info">The current rfolder</param> /// <param name="newdir">The desired name for the new folder</param> public void RenameFolder(DirectoryInfo info, string newdir) { HashSet <FileDeclaration> affected = MacroUI.GetInstance().RenameFolder(info, newdir); foreach (FileDeclaration d in affected) { DocumentViewModel dvm = DockManager.GetDocument(d); if (dvm != null) { dvm.ToolTip = d.Info.Name; dvm.ContentId = d.Info.FullName; } } }
/// <summary> /// Instantiation of ExplorerViewModel /// </summary> public ExplorerViewModel() { Model = new ExplorerModel(); if (MacroUI.GetInstance().IsLoaded()) { Initialize(); } else { Events.SubscribeEvent("ApplicationLoaded", (Action)Initialize); } //Routing.EventManager.ApplicationLoadedEvent += Initialize; }
/// <summary> /// Populate drop down box with runtimes /// </summary> private void Initialize() { ObservableCollection <ComboBoxItem> items = new ObservableCollection <ComboBoxItem>(); foreach (string runtime in MacroUI.GetInstance().GetRuntimes()) { items.Add(new ComboBoxItem() { Content = runtime, ToolTip = runtime, Tag = runtime }); } RuntimeItems = items; ActiveMacroChanged(); }
/// <summary> /// Adds an assembly to the registry /// </summary> private void AddLibrary() { string path = Files.ImportAssembly(); if (string.IsNullOrEmpty(path)) { return; } string name = System.Reflection.Assembly.LoadFrom(path).FullName; AssemblyDeclaration ad = new AssemblyDeclaration(name, path, false); MacroUI.GetInstance().AddAssembly(ad); }
/// <summary> /// Create a contextmenu for the TreeView /// </summary> /// <returns>TreeView ContextMenu</returns> public ContextMenu CreateTreeViewContextMenu() { Style ContextMenuStyle = MainWindow.GetInstance().GetResource("MetroContextMenuStyle") as Style; Style MenuItemStyle = MainWindow.GetInstance().GetResource("MetroMenuItemStyle") as Style; ContextMenu cm = new ContextMenu(); cm.Resources.MergedDictionaries.Add(MainWindow.GetInstance().GetResources()); cm.Style = ContextMenuStyle; MenuItem mi_create = new MenuItem(); mi_create.Header = "Create Macro"; mi_create.Click += delegate(object sender, RoutedEventArgs args) { CreateMacro(null, "/", (d) => MacroUI.GetInstance().AddFile(d)); cm.IsOpen = false; }; mi_create.Style = MenuItemStyle as Style; cm.Items.Add(mi_create); MenuItem mi_folder = new MenuItem(); mi_folder.Header = "Create Folder"; mi_folder.Click += delegate(object sender, RoutedEventArgs args) { CreateFolder(null, "/"); cm.IsOpen = false; }; mi_folder.Style = MenuItemStyle as Style; cm.Items.Add(mi_folder); MenuItem mi_import = new MenuItem(); mi_import.Header = "Import Macro"; mi_import.Click += delegate(object sender, RoutedEventArgs args) { ImportMacro(); cm.IsOpen = false; }; mi_import.Style = MenuItemStyle as Style; cm.Items.Add(mi_import); return(cm); }
/// <summary> /// Uses recursive function to populate the tree view with macros /// </summary> private void Initialize() { HashSet <DataTreeViewItem> items = CreateTreeViewItemStructure(MacroUI.GetInstance().GetFileDeclarations()); foreach (DataTreeViewItem item in items) { DisplayableTreeViewItem tvi = CreateTreeViewItem(null, item); if (tvi != null) { ItemSource.Add(tvi); } } Sort(); CheckVisibility(); //Events.OnMacroCountChanged += CheckVisibility; Events.SubscribeEvent("OnMacroCountChanged", (Action)CheckVisibility); }
/// <summary> /// Deletes a folder and removes its displayable item /// </summary> /// <param name="item">The item to be deleted</param> /// <param name="path">The relativepath of the folder</param> /// <param name="name">The name of the folder</param> public async void DeleteFolder(DisplayableTreeViewItem item, string path, string name) { DirectoryInfo info = new DirectoryInfo(Files.FullPath(path, name)); bool result = await MacroUI.GetInstance().DeleteFolder(info); if (result) { if (item.Parent is DisplayableTreeViewItem) { Remove((item.Parent as DisplayableTreeViewItem), item); } else { Remove(null, item); } CloseItemMacro(item); } }
/// <summary> /// Change which runtime is selected /// </summary> private void ActiveMacroChanged() { FileDeclaration d = DockManager.GetActiveDocumentDeclaration(); if (d == null) { SelectedRuntime = RuntimeItems.FirstOrDefault <ComboBoxItem>(); return; } string runtime = MacroUI.GetInstance().GetDefaultRuntime(d); foreach (ComboBoxItem item in RuntimeItems) { if (string.Equals((string)item.Tag, runtime, StringComparison.OrdinalIgnoreCase)) { SelectedRuntime = item; return; } } }
/// <summary> /// Load avalondock layout, desearlize it and apply it /// </summary> private void LoadAvalonDockLayout() { if (MainWindow.GetInstance() == null) { return; } XmlLayoutSerializer serializer = new XmlLayoutSerializer(MainWindow.GetInstance().GetDockingManager()); serializer.LayoutSerializationCallback += (s, args) => { args.Content = args.Content; }; string layout = Properties.Settings.Default.AvalonLayout; if (String.IsNullOrEmpty(layout.Trim())) { return; } StringReader stringReader = new StringReader(layout); XmlReader xmlReader = XmlReader.Create(stringReader); serializer.Deserialize(xmlReader); xmlReader.Close(); stringReader.Close(); FileDeclaration d = MacroUI.GetInstance().GetDeclarationFromFullname(Properties.Settings.Default.ActiveDocument); if (d == null) { return; } DocumentViewModel dvm = DockManager.GetDocument(d); if (d != null) { ChangeActiveDocument(dvm); } }
/// <summary> /// Creates macro /// </summary> /// <param name="parent">The parent item to which it'll be added</param> /// <param name="root">The root of the item's relative future directory</param> /// <param name="OnReturn">The Action, and resulting file declaration of the created macro, to be fired when the task is completed</param> public void CreateMacro(DisplayableTreeViewItem parent, string root, Action <FileDeclaration> OnReturn) { if (m_IsCreating) { return; } m_IsCreating = true; DisplayableTreeViewItem item = new DisplayableTreeViewItem(); item.KeyUpEvent = delegate(object s, KeyEventArgs a) { if (a.Key == Key.Return) { Focus(); Keyboard.ClearFocus(); } else if (a.Key == Key.Escape) { m_IsCreating = false; item.IsInputting = false; } }; item.FocusLostEvent = delegate(object s, RoutedEventArgs a) { if (String.IsNullOrEmpty(item.Header) || (!item.IsInputting)) { Remove(parent, item); m_IsCreating = false; return; } if (!Path.HasExtension(item.Header)) { item.Header += MacroUI.GetInstance().GetDefaultFileExtension(); } item.Header = Regex.Replace(item.Header, "[^0-9a-zA-Z ._-]", ""); FileInfo info = new FileInfo(Files.FullPathMacro(Files.FullPath(root, item.Header))); FileDeclaration d = MainWindowViewModel.GetInstance().CreateMacro(info); Rename(parent, item); if (d == null) { Remove(parent, item); m_IsCreating = false; return; } item.Header = d.Name; item.Declaration = d; item.Root = root; item.Parent = parent; item.IsFolder = false; item.RightClickEvent = TreeViewItem_OnPreviewMouseRightButtonDown; item.SelectedEvent = delegate(object sender, RoutedEventArgs args) { SelectedItem = item; }; item.DoubleClickEvent = delegate(object sender, MouseButtonEventArgs args) { OpenMacro(d); args.Handled = true; }; item.IsInputting = false; m_IsCreating = false; OnReturn?.Invoke(d); OpenMacro(d); }; Add(parent, item); item.IsInputting = true; }
/// <summary> /// Removes an assembly from the registry /// </summary> private void RemoveLibrary() { MacroUI.GetInstance().RemoveAssembly(SelectedLibrary.declaration); }
/// <summary> /// Create a contextmenu for a tree view item folder /// </summary> /// <param name="item">The item for which the contextmenu is being made</param> /// <param name="name">The name of the folder</param> /// <param name="path">The relativepath of the folder</param> /// <returns>Folder TreeViewItem ContextMenu</returns> private ContextMenu CreateContextMenuFolder(DisplayableTreeViewItem item, string name, string path) { Style ContextMenuStyle = MainWindow.GetInstance().GetResource("MetroContextMenuStyle") as Style; Style MenuItemStyle = MainWindow.GetInstance().GetResource("MetroMenuItemStyle") as Style; Style SeparatorStyle = MainWindow.GetInstance().GetResource("MertoMenuSeparatorStyle") as Style; ContextMenu cm = new ContextMenu(); cm.Resources.MergedDictionaries.Add(MainWindow.GetInstance().GetResources()); cm.Style = ContextMenuStyle; MenuItem mi_create = new MenuItem(); mi_create.Header = "Create Macro"; mi_create.Click += delegate(object sender, RoutedEventArgs args) { item.IsExpanded = true; cm.IsOpen = false; CreateMacro(item, Files.FullPath(path, name), (d) => MacroUI.GetInstance().AddFile(d)); }; mi_create.Style = MenuItemStyle as Style; cm.Items.Add(mi_create); MenuItem mi_folder = new MenuItem(); mi_folder.Header = "Create Folder"; mi_folder.Click += delegate(object sender, RoutedEventArgs args) { item.IsExpanded = true; CreateFolder(item, path + "/" + name + "/"); cm.IsOpen = false; }; mi_folder.Style = MenuItemStyle as Style; cm.Items.Add(mi_folder); MenuItem mi_import = new MenuItem(); mi_import.Header = "Import Macro"; mi_import.Click += delegate(object sender, RoutedEventArgs args) { item.IsExpanded = true; DirectoryInfo info = new DirectoryInfo(Files.FullPath(path, name)); ImportMacro(item, info); cm.IsOpen = false; }; mi_import.Style = MenuItemStyle as Style; cm.Items.Add(mi_import); Separator sep1 = new Separator(); sep1.Style = SeparatorStyle; cm.Items.Add(sep1); MenuItem mi_del = new MenuItem(); mi_del.Header = "Delete"; mi_del.Click += delegate(object sender, RoutedEventArgs args) { DeleteFolder(item, path, name); args.Handled = true; cm.IsOpen = false; }; mi_del.Style = MenuItemStyle as Style; cm.Items.Add(mi_del); MenuItem mi_rename = new MenuItem(); mi_rename.Header = "Rename"; mi_rename.Click += delegate(object sender, RoutedEventArgs args) { args.Handled = true; cm.IsOpen = false; DisplayableTreeViewItem parentitem = item.Parent; string previousname = item.Header; item.KeyUpEvent = delegate(object s, KeyEventArgs a) { if (a.Key == Key.Return) { Focus(); Keyboard.ClearFocus(); } else if (a.Key == Key.Escape) { item.Header = previousname; Focus(); Keyboard.ClearFocus(); } }; item.FocusLostEvent = delegate(object s, RoutedEventArgs a) { if (item.Header == previousname) { item.IsInputting = false; return; } if (String.IsNullOrEmpty(item.Header)) { MacroUI.GetInstance().DisplayOkMessage("Please enter a valid name.", "Invalid Name"); item.IsInputting = true; return; } DirectoryInfo info = new DirectoryInfo(Files.FullPath(path, name)); MainWindowViewModel.GetInstance().RenameFolder(info, path + item.Header); Rename(parentitem, item); item.IsInputting = false; }; item.IsInputting = true; }; mi_rename.Style = MenuItemStyle as Style; cm.Items.Add(mi_rename); return(cm); }
/// <summary> /// Creates a contextmenu for a tree view item file /// </summary> /// <param name="item">The item for which the context menu is being made</param> /// <param name="name">The name of the macro</param> /// <param name="id">The id of the macro</param> /// <returns>File TreeViewItem ContextMenu</returns> private ContextMenu CreateContextMenuMacro(DisplayableTreeViewItem item, string name, FileDeclaration d) { Style ContextMenuStyle = MainWindow.GetInstance().GetResource("MetroContextMenuStyle") as Style; Style MenuItemStyle = MainWindow.GetInstance().GetResource("MetroMenuItemStyle") as Style; Style SeparatorStyle = MainWindow.GetInstance().GetResource("MertoMenuSeparatorStyle") as Style; ContextMenu cm = new ContextMenu(); cm.Resources.MergedDictionaries.Add(MainWindow.GetInstance().GetResources()); cm.Style = ContextMenuStyle; DisplayableTreeViewItem parentitem = item.Parent; if (d == null) { MacroUI.GetInstance().DisplayOkMessage("Could not get the macro (when attempting to create a context menu): " + name, "Macro Error"); return(null); } MenuItem mi_edit = new MenuItem(); mi_edit.Header = "Edit"; mi_edit.Click += delegate(object sender, RoutedEventArgs args) { OpenMacro(d); args.Handled = true; cm.IsOpen = false; }; mi_edit.Style = MenuItemStyle as Style; cm.Items.Add(mi_edit); MenuItem mi_execute = new MenuItem(); mi_execute.Header = "Synchronous Execute"; mi_execute.ToolTip = "Synchronous executions cannot be terminated."; mi_execute.Click += delegate(object sender, RoutedEventArgs args) { ExecuteMacro(d, false); args.Handled = true; cm.IsOpen = false; }; mi_execute.Style = MenuItemStyle as Style; cm.Items.Add(mi_execute); MenuItem mi_executea = new MenuItem(); mi_executea.Header = "Asynchronous Execute"; mi_executea.ToolTip = "Asynchronous executions can be terminated."; mi_executea.Click += delegate(object sender, RoutedEventArgs args) { ExecuteMacro(d, true); args.Handled = true; cm.IsOpen = false; }; mi_executea.Style = MenuItemStyle as Style; cm.Items.Add(mi_executea); Separator sep1 = new Separator(); sep1.Style = SeparatorStyle as Style; cm.Items.Add(sep1); MenuItem mi_export = new MenuItem(); mi_export.Header = "Export"; mi_export.Click += delegate(object sender, RoutedEventArgs args) { d.Export(); args.Handled = true; cm.IsOpen = false; }; mi_export.Style = MenuItemStyle as Style; cm.Items.Add(mi_export); MenuItem mi_del = new MenuItem(); mi_del.Header = "Delete"; mi_del.Click += delegate(object sender, RoutedEventArgs args) { DeleteMacro(item, d); args.Handled = true; cm.IsOpen = false; }; mi_del.Style = MenuItemStyle as Style; cm.Items.Add(mi_del); MenuItem mi_rename = new MenuItem(); mi_rename.Header = "Rename"; mi_rename.Click += delegate(object sender, RoutedEventArgs args) { args.Handled = true; cm.IsOpen = false; string previousname = item.Header; item.KeyUpEvent = delegate(object s, KeyEventArgs a) { if (a.Key == Key.Return) { Focus(); Keyboard.ClearFocus(); } else if (a.Key == Key.Escape) { item.Header = previousname; Focus(); Keyboard.ClearFocus(); } }; item.FocusLostEvent = delegate(object s, RoutedEventArgs a) { if (item.Header == previousname) { item.IsInputting = false; return; } if (String.IsNullOrEmpty(item.Header)) { MacroUI.GetInstance().DisplayOkMessage("Please enter a valid name.", "Invalid Name"); item.IsInputting = true; return; } if (!Path.HasExtension(item.Header)) { item.Header += MacroUI.GetInstance().GetDefaultFileExtension(); } MainWindowViewModel.GetInstance().RenameMacro(d, item.Header); Rename(parentitem, item); item.IsInputting = false; }; item.IsInputting = true; }; mi_rename.Style = MenuItemStyle as Style; cm.Items.Add(mi_rename); Separator sep2 = new Separator(); sep2.Style = SeparatorStyle; cm.Items.Add(sep2); return(cm); }
/// <summary> /// Create a new macro /// </summary> private void NewEvent() { DockManager.Explorer.CreateMacro((d) => MacroUI.GetInstance().AddFile(d)); }