Exemplo n.º 1
0
        //--------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="ManageLayoutsViewModel"/> class.
        /// </summary>
        /// <param name="layoutExtension">The <see cref="LayoutExtension"/>.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="layoutExtension"/> is <see langword="null"/>.
        /// </exception>
        public ManageLayoutsViewModel(LayoutExtension layoutExtension)
        {
            if (layoutExtension == null)
                throw new ArgumentNullException(nameof(layoutExtension));

            DisplayName = "Manage Layouts";

            _layoutExtension = layoutExtension;

            // Initial selection in the list box is the active window layout.
            _selectedLayout = _layoutExtension.ActiveLayout;

            RenameCommand = new DelegateCommand(Rename, CanRename);
            DeleteCommand = new DelegateCommand(Delete, CanDelete);
            CloseCommand = new DelegateCommand(Close);
        }
        internal void InitializeLayouts()
        {
            Logger.Info("Loading window layouts.");

            Layouts.Clear();

            try
            {
                // Load factory presets.
                string applicationFolder = EditorHelper.GetUserSettingsFolder(ConfigurationUserLevel.None);
                string layoutsFolder     = Path.Combine(applicationFolder, LayoutsFolder, PresetsFolder);
                Layouts.AddRange(Directory.EnumerateFiles(layoutsFolder, LayoutPattern)
                                 .Select(Path.GetFileNameWithoutExtension)
                                 .Select(name => new WindowLayout(name, true)));
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to load factory presets.");
            }

            try
            {
                // Load user presets.
                string applicationFolder = EditorHelper.GetUserSettingsFolder(ConfigurationUserLevel.PerUserRoamingAndLocal);
                string layoutsFolder     = Path.Combine(applicationFolder, LayoutsFolder, PresetsFolder);
                Layouts.AddRange(Directory.EnumerateFiles(layoutsFolder, LayoutPattern)
                                 .Select(Path.GetFileNameWithoutExtension)
                                 .Select(name => new WindowLayout(name, false)));
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to load user presets.");
            }

            // Add a dummy entry, if no presets are available.
            if (Layouts.Count == 0)
            {
                Layouts.Add(new WindowLayout(DefaultLayout, true));
            }

            // The window layout will be loaded in OnEditorActivated().
            ActiveLayout = null;
        }
Exemplo n.º 3
0
        internal void Delete(WindowLayout layout)
        {
            Debug.Assert(layout != null);
            Debug.Assert(layout != ActiveLayout, "Cannot delete active window layout.");

            Logger.Info("Deleting window layout \"{0}\".", layout.Name);

            Layouts.Remove(layout);
            UpdateWindowLayoutItem();

            try
            {
                DeleteUserSession(layout);
                DeleteUserPreset(layout);
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to delete window layout \"{0}\".", layout.Name);
            }
        }
        internal void Delete(WindowLayout layout)
        {
            Debug.Assert(layout != null);
            Debug.Assert(layout != ActiveLayout, "Cannot delete active window layout.");

            Logger.Info("Deleting window layout \"{0}\".", layout.Name);

            Layouts.Remove(layout);
            UpdateWindowLayoutItem();

            try
            {
                DeleteUserSession(layout);
                DeleteUserPreset(layout);
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to delete window layout \"{0}\".", layout.Name);
            }
        }
        private void ResetWindowLayout(WindowLayout layout)
        {
            Logger.Info("Resetting window layout \"{0}\".", layout.Name);

            // Delete session in memory.
            layout.SerializedLayout = null;
            layout.IsDirty          = false;

            try
            {
                // Delete session on disk.
                DeleteUserSession(layout);

                // Restore original preset.
                LoadPreset(layout);
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, $"Failed to restore preset \"{0}\".", layout.Name);
            }
        }
Exemplo n.º 6
0
        private void OnEditorActivated(object sender, ActivationEventArgs eventArgs)
        {
            if (eventArgs.Opened)
            {
                Editor.Activated -= OnEditorActivated;

                // Load EditorWindow state.
                LoadEditorWindowState();

                // Check if a layout was requested using the --layout command line parameter.
                var          layoutName = GetLayoutNameFromCommandLine();
                WindowLayout layout     = null;
                if (layoutName != null)
                {
                    layout = Layouts.FirstOrDefault(l => l.Name == layoutName);
                    if (layout == null)
                    {
                        Logger.Error(Invariant($"The layout \"{layoutName}\" was not found. This layout was requested using a command line parameter."));
                    }
                }

                if (layout == null)
                {
                    // Load previous window layout.
                    layout = Layouts.FirstOrDefault(l => l.Name == Settings.Default.ActiveWindowLayout)
                             ?? Layouts.FirstOrDefault();
                }
                SwitchLayout(layout);

                if (Editor.Window != null)
                {
                    // Save EditorWindow state and window layout before window is closed.
                    Editor.Window.Closing += (s, e) =>
                    {
                        SaveEditorWindowState();
                        SaveLayouts();
                    };
                }
            }
        }
Exemplo n.º 7
0
        private void LoadUserSession(WindowLayout layout)
        {
            Debug.Assert(layout != null);

            // Load window layout from "%LOCALAPPDATA%\Layouts\*.xml".
            string userSettingsFolder = EditorHelper.GetUserSettingsFolder(ConfigurationUserLevel.PerUserRoamingAndLocal);
            string fileName = Path.Combine(userSettingsFolder, LayoutsFolder, $"{layout.Name}.xml");
            layout.SerializedLayout = XDocument.Load(fileName).Root;
            Editor.LoadLayout(layout.SerializedLayout);
            layout.IsDirty = false;
        }
Exemplo n.º 8
0
        private void LoadPreset(WindowLayout layout)
        {
            Debug.Assert(layout != null);

            string layoutFolder;
            if (layout.IsFactoryPreset)
            {
                // Load window layout from "<EXECUTABLE_FOLDER>\Layouts\Presets\*.xml".
                string applicationFolder = EditorHelper.GetUserSettingsFolder(ConfigurationUserLevel.None);
                layoutFolder = Path.Combine(applicationFolder, LayoutsFolder, PresetsFolder);
            }
            else
            {
                // Load window layout from "%LOCALAPPDATA%\Layouts\Presets\*.xml".
                string userSettingsFolder = EditorHelper.GetUserSettingsFolder(ConfigurationUserLevel.PerUserRoamingAndLocal);
                layoutFolder = Path.Combine(userSettingsFolder, LayoutsFolder, PresetsFolder);
            }

            string fileName = Path.Combine(layoutFolder, $"{layout.Name}.xml");
            layout.SerializedLayout = XDocument.Load(fileName).Root;
            Editor.LoadLayout(layout.SerializedLayout);
            layout.IsDirty = false;
        }
Exemplo n.º 9
0
        private static void SaveUserSession(WindowLayout layout)
        {
            Debug.Assert(layout != null);
            Debug.Assert(layout.SerializedLayout != null);

            // Save window layout to "%LOCALAPPDATA%\Layouts\*.xml".
            string userSettingsFolder = EditorHelper.GetUserSettingsFolder(ConfigurationUserLevel.PerUserRoamingAndLocal);
            string layoutFolder = Path.Combine(userSettingsFolder, LayoutsFolder);
            string fileName = Path.Combine(layoutFolder, $"{layout.Name}.xml");

            if (!Directory.Exists(layoutFolder))
                Directory.CreateDirectory(layoutFolder);

            new XDocument(layout.SerializedLayout).Save(fileName);
        }
Exemplo n.º 10
0
        private static void DeleteUserSession(WindowLayout layout)
        {
            Debug.Assert(layout != null);

            // Delete window layout from "%LOCALAPPDATA%\Layouts\*.xml".
            string userSettingsFolder = EditorHelper.GetUserSettingsFolder(ConfigurationUserLevel.PerUserRoamingAndLocal);
            string fileName = Path.Combine(userSettingsFolder, LayoutsFolder, $"{layout.Name}.xml");
            File.Delete(fileName);
        }
Exemplo n.º 11
0
        internal void SwitchLayout(WindowLayout layout)
        {
            Debug.Assert(layout != null);

            Logger.Info("Switching to window layout \"{0}\".", layout.Name);

            try
            {
                // Save current window layout to memory.
                if (ActiveLayout != null)
                {
                    ActiveLayout.SerializedLayout = Editor.SaveLayout();
                    ActiveLayout.IsDirty = true;
                }
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to store active window layout to memory.");
            }

            ActiveLayout = layout;
            UpdateWindowLayoutItem();

            try
            {
                // Try to load window layout from memory.
                if (layout.SerializedLayout != null)
                {
                    Editor.LoadLayout(layout.SerializedLayout);
                    return;
                }
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to restore window layout from memory.");
            }

            try
            {
                // Try to load user session.
                LoadUserSession(layout);
                return;
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to restore window layout from session data.");
            }

            try
            {
                // Try to load factory/user preset.
                LoadPreset(layout);
                return;
            }
            catch (Exception exception)
            {
                Logger.Warn(exception, "Failed to restore window layout from preset.");
            }
        }
Exemplo n.º 12
0
        internal void RenameWindowLayout(WindowLayout layout)
        {
            Debug.Assert(layout != null);
            Debug.Assert(Layouts.Contains(layout));

            string oldLayoutName = layout.Name;

            Logger.Info("Renaming window layout \"{0}\".", oldLayoutName);

            ShowSaveLayoutDialog:
            var saveLayoutDialog = new SaveLayoutViewModel
            {
                DisplayName = "Rename Window Layout",
                LayoutName = oldLayoutName
            };

            string newLayoutName = null;
            WindowLayout existingLayout = null;
            var result = _windowService.ShowDialog(saveLayoutDialog);
            if (result.HasValue && result.Value && saveLayoutDialog.LayoutName != oldLayoutName)
            {
                Debug.Assert(!string.IsNullOrEmpty(saveLayoutDialog.LayoutName), "The layout name must not be null or empty.");
                Debug.Assert(saveLayoutDialog.LayoutName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1, "The layout name must not contain invalid characters.");

                newLayoutName = saveLayoutDialog.LayoutName;

                // Overwrite existing file?
                existingLayout = Layouts.FirstOrDefault(l => l.Name == newLayoutName);
                if (existingLayout != null)
                {
                    if (existingLayout.IsFactoryPreset)
                    {
                        MessageBox.Show(
                            $"\"{newLayoutName}\" is a factory preset. Factory presets cannot be overwritten.",
                            Editor.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation);

                        // Try again.
                        goto ShowSaveLayoutDialog;
                    }
                    else
                    {
                        var messageBoxResult = MessageBox.Show(
                            $"The layout \"{newLayoutName}\" already exists. Overwrite existing?",
                            Editor.ApplicationName, MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation);

                        if (messageBoxResult == MessageBoxResult.No)
                        {
                            // Try again.
                            goto ShowSaveLayoutDialog;
                        }

                        if (messageBoxResult == MessageBoxResult.Cancel)
                        {
                            // Abort.
                            newLayoutName = null;
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(newLayoutName))
            {
                try
                {
                    // Rename window layout.
                    RenameUserSession(layout.Name, newLayoutName);
                    RenameUserPreset(layout.Name, newLayoutName);

                    if (existingLayout != null)
                        Layouts.Remove(existingLayout);

                    layout.Name = newLayoutName;
                    UpdateWindowLayoutItem();
                }
                catch (Exception exception)
                {
                    Logger.Error(exception, "Could not rename window layout (old name: \"{0}\", new name: \"{1}\").", oldLayoutName, newLayoutName);

                    string message = $"Could not rename window layout.\n\n{exception.Message}";
                    MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        internal void RenameWindowLayout(WindowLayout layout)
        {
            Debug.Assert(layout != null);
            Debug.Assert(Layouts.Contains(layout));

            string oldLayoutName = layout.Name;

            Logger.Info("Renaming window layout \"{0}\".", oldLayoutName);

ShowSaveLayoutDialog:
            var saveLayoutDialog = new SaveLayoutViewModel
            {
                DisplayName = "Rename Window Layout",
                LayoutName  = oldLayoutName
            };

            string       newLayoutName  = null;
            WindowLayout existingLayout = null;
            var          result         = _windowService.ShowDialog(saveLayoutDialog);

            if (result.HasValue && result.Value && saveLayoutDialog.LayoutName != oldLayoutName)
            {
                Debug.Assert(!string.IsNullOrEmpty(saveLayoutDialog.LayoutName), "The layout name must not be null or empty.");
                Debug.Assert(saveLayoutDialog.LayoutName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1, "The layout name must not contain invalid characters.");

                newLayoutName = saveLayoutDialog.LayoutName;

                // Overwrite existing file?
                existingLayout = Layouts.FirstOrDefault(l => l.Name == newLayoutName);
                if (existingLayout != null)
                {
                    if (existingLayout.IsFactoryPreset)
                    {
                        MessageBox.Show(
                            $"\"{newLayoutName}\" is a factory preset. Factory presets cannot be overwritten.",
                            Editor.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation);

                        // Try again.
                        goto ShowSaveLayoutDialog;
                    }
                    else
                    {
                        var messageBoxResult = MessageBox.Show(
                            $"The layout \"{newLayoutName}\" already exists. Overwrite existing?",
                            Editor.ApplicationName, MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation);

                        if (messageBoxResult == MessageBoxResult.No)
                        {
                            // Try again.
                            goto ShowSaveLayoutDialog;
                        }

                        if (messageBoxResult == MessageBoxResult.Cancel)
                        {
                            // Abort.
                            newLayoutName = null;
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(newLayoutName))
            {
                try
                {
                    // Rename window layout.
                    RenameUserSession(layout.Name, newLayoutName);
                    RenameUserPreset(layout.Name, newLayoutName);

                    if (existingLayout != null)
                    {
                        Layouts.Remove(existingLayout);
                    }

                    layout.Name = newLayoutName;
                    UpdateWindowLayoutItem();
                }
                catch (Exception exception)
                {
                    Logger.Error(exception, "Could not rename window layout (old name: \"{0}\", new name: \"{1}\").", oldLayoutName, newLayoutName);

                    string message = $"Could not rename window layout.\n\n{exception.Message}";
                    MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
        private void SavePresetAs()
        {
            Logger.Info("Saving window layout as new preset.");

ShowSaveLayoutDialog:
            var saveLayoutDialog = new SaveLayoutViewModel
            {
                DisplayName = "Save Window Layout",
                LayoutName  = "New layout"
            };

            string       layoutName     = null;
            WindowLayout existingLayout = null;
            var          result         = _windowService.ShowDialog(saveLayoutDialog);

            if (result.HasValue && result.Value)
            {
                Debug.Assert(!string.IsNullOrEmpty(saveLayoutDialog.LayoutName), "The layout name must not be null or empty.");
                Debug.Assert(saveLayoutDialog.LayoutName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1, "The layout name must not contain invalid characters.");

                layoutName = saveLayoutDialog.LayoutName;

                // Overwrite existing window layout?
                existingLayout = Layouts.FirstOrDefault(l => string.Compare(l.Name, layoutName, StringComparison.OrdinalIgnoreCase) == 0);
                if (existingLayout != null)
                {
                    if (existingLayout.IsFactoryPreset)
                    {
                        MessageBox.Show(
                            $"\"{layoutName}\" is a factory preset. Factory presets cannot be overwritten.",
                            Editor.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Exclamation);

                        // Try again.
                        goto ShowSaveLayoutDialog;
                    }
                    else
                    {
                        var messageBoxResult = MessageBox.Show(
                            $"The layout \"{layoutName}\" already exists. Overwrite existing?",
                            Editor.ApplicationName, MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation);

                        if (messageBoxResult == MessageBoxResult.No)
                        {
                            // Try again.
                            goto ShowSaveLayoutDialog;
                        }

                        if (messageBoxResult == MessageBoxResult.Cancel)
                        {
                            // Abort.
                            layoutName = null;
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(layoutName))
            {
                try
                {
                    // Save window layout as new preset.
                    var serializedLayout = Editor.SaveLayout(true);
                    var layout           = new WindowLayout(layoutName, false)
                    {
                        SerializedLayout = serializedLayout
                    };
                    SaveUserPreset(layout);

                    if (existingLayout != null)
                    {
                        Layouts.Remove(existingLayout);
                    }

                    Layouts.Add(layout);
                    ActiveLayout = layout;
                    UpdateWindowLayoutItem();
                }
                catch (Exception exception)
                {
                    Logger.Error(exception, "Could not save window layout as new preset \"{0}\".", layoutName);

                    string message = $"Could not save window layout as new preset \"{layoutName}\".\n\n{exception.Message}";
                    MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
 private void UninitializeLayouts()
 {
     Layouts.Clear();
     ActiveLayout = null;
     UpdateWindowLayoutItem();
 }