private void FileClose_Click(object sender, RoutedEventArgs e) { var userSettings = UserSettings.Create(); // If the file doesn't have a save location ask the user to save the file if (!string.IsNullOrWhiteSpace(CurrentTextEditor.Text) && !CurrentTextEditor.HasSaveLocation && Tabs.Items.Count == 2) { FileSave_Click(sender, e); userSettings.RemoveFilePaths(CurrentTextEditor.DocumentPath); userSettings.Save(); Tabs.Items[Tabs.SelectedIndex] = EmptyTab; Tabs.SelectedIndex = 0; return; } // If the file is empty and doesn't have a save location do nothing if (string.IsNullOrWhiteSpace(CurrentTextEditor.Text) && !CurrentTextEditor.HasSaveLocation && Tabs.Items.Count == 2) { return; } // If there are more than 2 tabs // Save the file if it has a save location var files = userSettings.Editors.ToList(); if (CurrentTextEditor.HasSaveLocation) { CurrentTextEditor.SaveFile(); } // If it doesn't have a save location but has some text ask the user to save the file else if (!string.IsNullOrWhiteSpace(CurrentTextEditor.Text)) { FileSave_Click(sender, e); } if (Tabs.SelectedIndex == 0) { // Remove the first tab/file to account for the negative index Tabs.Items.RemoveAt(0); files.RemoveAt(0); } else { // If it's not the first tab select the previous tab Tabs.SelectedIndex--; // // Remove the tab that was requested to be closed Tabs.Items.RemoveAt(Tabs.SelectedIndex + 1); files.RemoveAt(Tabs.SelectedIndex + 1); } userSettings.Editors = files.ToArray(); userSettings.Save(); }
private void Application_Startup(object sender, StartupEventArgs e) { if (e.Args.Length > 0) // Check if there are args { var userSettings = UserSettings.Create(); userSettings.AddFiles(e.Args); // Add all files to user settings userSettings.SelectedFileIndex = userSettings.Editors.Length - 1; // Change selected index to last added file userSettings.Save(); } }
private void Tabs_SelectionChanged(object sender, SelectionChangedEventArgs e) { // if + tab is selected add a new tab if (ReferenceEquals(Tabs.SelectedItem, TabAdd)) { var userSettings = UserSettings.Create(); Tabs.Items.Insert(Tabs.Items.Count - 1, EmptyTab); userSettings.AddFiles(""); userSettings.Save(); Tabs.SelectedIndex--; } }
public void UserSettings_Create() { var settings = UserSettings.Create(); Assert.IsNotNull(settings.ClientId); Assert.IsNotNull(settings.RootItem); Assert.IsNotNull(settings.CustomTheme); Assert.IsTrue(settings.ShowStatusColors); Assert.IsTrue(settings.ShowStatusIcons); Assert.IsTrue(settings.DebugMode); Assert.IsTrue(settings.ReportAnonymousUsage); }
public static void AddFiles_AddsCorrectValues() { var userSettings = UserSettings.Create(); userSettings.Editors = null; var paths = new[] { "test.txt", "test.txt", "test2.txt" }; userSettings.AddFiles(paths); var savedPaths = userSettings.Editors.Select(x => x.FilePath); Assert.That(savedPaths.Contains("test.txt") && savedPaths.Contains("test2.txt")); }
public MainWindow() { InitializeComponent(); var userSettings = UserSettings.Create(); // if there are files, load them if (userSettings.Editors.Length != 0) { foreach (var i in userSettings.Editors) { try { Tabs.Items.Insert(Tabs.Items.Count - 1, new TabItem { Content = new TextEditor(i.FilePath) { FileLanguage = i.HighlightingLanguage }, Header = new FileInfo(i.FilePath).Name }); } catch { } } // Select the tab that was previously selected Tabs.SelectedIndex = userSettings.SelectedFileIndex; } // else insert an empty tab else { Tabs.Items.Insert(0, EmptyTab); Tabs.SelectedIndex = 0; } // Changes the font according to settings ChangeFont(); // Restore previous window state Left = Properties.Settings.Default.LeftWindowPosition; Top = Properties.Settings.Default.TopWindowPosition; Width = Properties.Settings.Default.WindowWidth; Height = Properties.Settings.Default.WindowHeight; }
public static void Save_SavesCorrectValues() { var userSettings = UserSettings.Create(); userSettings.Editors = new[] { new EditorInfo(HighlightingLanguage.None, "path.txt") }; userSettings.TabSize = 4; userSettings.EditorFontFamily = "Consolas"; userSettings.EditorFontSize = 12; userSettings.SelectedFileIndex = 0; userSettings.Save(); var loadedSettings = UserSettings.Create(); foreach (var propertyInfo in typeof(UserSettings).GetProperties(BindingFlags.Public)) { Assert.That(propertyInfo.GetValue(userSettings) == propertyInfo.GetValue(loadedSettings)); } }
/// <summary> /// Saves the content of the current file to a new file /// </summary> private void FileSaveAs_Click(object sender, RoutedEventArgs e) { var userSettings = UserSettings.Create(); if (CurrentTextEditor.HasSaveLocation) { CurrentTextEditor.SaveFile(); } var saveDialog = new SaveFileDialog(); saveDialog.ShowDialog(); // If file path is empty return if (string.IsNullOrEmpty(saveDialog.FileName)) { return; } // Remove the current file path from settings userSettings.RemoveFilePaths(CurrentTextEditor.DocumentPath); // Create new file using (var sw = new StreamWriter(saveDialog.FileName, false)) { sw.Write(CurrentTextEditor.Text); } // Assign the new file path to the current text editor CurrentTextEditor.DocumentPath = saveDialog.FileName; CurrentTextEditor.SaveFile(); // Insert the file path at a specified index userSettings.AddFiles(Tabs.SelectedIndex, saveDialog.FileName); userSettings.Save(); ((TabItem)Tabs.SelectedItem).Header = CurrentTextEditor.FileName; }
/// <summary> /// Writes the text from MainTextBox when the window closes /// </summary> private void Window_Closing(object sender, CancelEventArgs e) { var userSettings = UserSettings.Create(); // loop through Text Editors and save contents for (var i = 0; i < Tabs.Items.Count - 1; i++) { try { // saves the text from the TextEditor ((Tabs.Items[i] as TabItem).Content as TextEditor).SaveFile(); } catch (InvalidSaveLocationException ex) { // select the tab without a save location and ask for a save location if (!string.IsNullOrEmpty(((Tabs.Items[i] as TabItem).Content as TextEditor).Text)) { Tabs.SelectedIndex = i; FileSave_Click(null, null); } } } // Save the index of a currently selected tab userSettings.SelectedFileIndex = Tabs.SelectedIndex; userSettings.RemoveInvalidFilePaths(); userSettings.Save(); // Save window state Properties.Settings.Default.LeftWindowPosition = Left; Properties.Settings.Default.TopWindowPosition = Top; Properties.Settings.Default.WindowWidth = Width; Properties.Settings.Default.WindowHeight = Height; Properties.Settings.Default.Save(); Application.Current.Shutdown(); }
/// <summary> /// Saves the current file /// </summary> private void FileSave_Click(object sender, RoutedEventArgs e) { var userSettings = UserSettings.Create(); if (CurrentTextEditor.HasSaveLocation) { CurrentTextEditor.SaveFile(); } else { var saveDialog = new SaveFileDialog(); saveDialog.ShowDialog(); if (string.IsNullOrEmpty(saveDialog.FileName)) { return; } CurrentTextEditor.DocumentPath = saveDialog.FileName; // sets the document path to that one in save file dialog var paths = userSettings.Editors.ToList(); paths.Insert(Tabs.SelectedIndex, new EditorInfo(HighlightingLanguage.None, CurrentTextEditor.DocumentPath)); userSettings.Editors = paths.ToArray(); ((TabItem)Tabs.Items[Tabs.SelectedIndex]).Header = CurrentTextEditor.FileName; try { CurrentTextEditor.SaveFile(); } catch { // ignored } userSettings.Save(); } }
/// <summary> /// Opens an existing file /// </summary> private void FileOpen_Click(object sender, RoutedEventArgs e) { var userSettings = UserSettings.Create(); var openDialog = new OpenFileDialog(); openDialog.ShowDialog(); if (string.IsNullOrEmpty(openDialog.FileName)) { return; } // writes the new file path to the constant Path.Document location userSettings.AddFiles(openDialog.FileName); // If the selected TextEditor is empty or contains only whitespace and doesn't have save location // Open a new one on the same spot if (string.IsNullOrWhiteSpace(CurrentTextEditor.Text) && !CurrentTextEditor.HasSaveLocation) { var item = Tabs.SelectedItem as TabItem; item.Content = new TextEditor(openDialog.FileName); item.Header = new FileInfo(openDialog.FileName).Name; } else { // else insert a new TextEditor on the end Tabs.Items.Insert(Tabs.Items.Count - 1, new TabItem { Content = new TextEditor(openDialog.FileName), Header = new FileInfo(openDialog.FileName).Name }); Tabs.SelectedIndex = Tabs.Items.Count - 2; } userSettings.Save(); }
/// <summary> /// Creates and opens the file /// </summary> private void FileNew_Click(object sender, RoutedEventArgs e) { var userSettings = UserSettings.Create(); var newDialog = new SaveFileDialog(); newDialog.ShowDialog(); if (!string.IsNullOrEmpty(newDialog.FileName)) { // Adds new file path userSettings.AddFiles(newDialog.FileName); Tabs.Items.Insert(Tabs.Items.Count - 1, new TabItem { Content = new TextEditor(newDialog.FileName), Header = new FileInfo(newDialog.FileName).Name }); userSettings.SelectedFileIndex = userSettings.Editors.Length - 1; Tabs.SelectedIndex = userSettings.Editors.Length - 1; } userSettings.Save(); }
public static void Create_Call_DoesntThrowExceptions() { Assert.DoesNotThrow(() => UserSettings.Create()); }