|
| 1 | +/* |
| 2 | + * __ __ _ __ __ _ _ |
| 3 | + * | \/ | __ _ (_) _ _ \ \ / / (_) _ _ __| | ___ __ __ __ |
| 4 | + * | |\/| | / _` | | | | ' \ \ \/\/ / | | | ' \ / _` | / _ \ \ V V / |
| 5 | + * |_| |_| \__,_| |_| |_||_| \_/\_/ |_| |_||_| \__,_| \___/ \_/\_/ |
| 6 | + * |
| 7 | + * MIT License * MF366 |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | + |
| 12 | +// System |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.IO; |
| 16 | +using System.Linq; |
| 17 | +using System.Reflection; |
| 18 | +using System.Text; |
| 19 | +using System.Threading.Tasks; |
| 20 | + |
| 21 | +// Avalonia |
| 22 | +using Avalonia.Controls; |
| 23 | +using Avalonia.Input; |
| 24 | +using Avalonia.Platform.Storage; |
| 25 | +using Avalonia.Win32.Interop.Automation; |
| 26 | + |
| 27 | + |
| 28 | +// Message Box |
| 29 | +using MsBox.Avalonia; |
| 30 | +using MsBox.Avalonia.Dto; |
| 31 | +using MsBox.Avalonia.Models; |
| 32 | + |
| 33 | +// Internal |
| 34 | +using WriterSharp.Browser; |
| 35 | + |
| 36 | + |
| 37 | +namespace WriterSharp |
| 38 | +{ |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Handles everything related to the main window. |
| 42 | + /// </summary> |
| 43 | + public partial class MainWindow : Window |
| 44 | + { |
| 45 | + |
| 46 | + Assembly currentAssembly = Assembly.GetExecutingAssembly(); |
| 47 | + string? currentFile = null; |
| 48 | + bool hasBeenModified = false; |
| 49 | + readonly string appName; |
| 50 | + readonly string appVersion; |
| 51 | + readonly string[] appVersionValues; |
| 52 | + const string REPOSITORY_URL = "https://github.com/MF366-Coding/WriterSharp"; |
| 53 | + const string WEBSITE_URL = "https://mf366-coding.github.io/writersharp.html"; |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// The WriterSharp encoding to use. Defaults to UTF-8. |
| 57 | + /// </summary> |
| 58 | + Encoding encoding = Encoding.UTF8; |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Custom message box parameters for errors. |
| 62 | + /// </summary> |
| 63 | + MessageBoxCustomParams errorMessageParams = new() |
| 64 | + { |
| 65 | + |
| 66 | + ButtonDefinitions = new List<ButtonDefinition> |
| 67 | + { |
| 68 | + |
| 69 | + new() { Name = "Ok", IsDefault = true } |
| 70 | + |
| 71 | + }, |
| 72 | + Icon = MsBox.Avalonia.Enums.Icon.Error, |
| 73 | + WindowStartupLocation = WindowStartupLocation.CenterOwner, |
| 74 | + CanResize = false, |
| 75 | + MaxWidth = 500, |
| 76 | + MaxHeight = 800, |
| 77 | + SizeToContent = SizeToContent.WidthAndHeight, |
| 78 | + ShowInCenter = true, |
| 79 | + Topmost = true |
| 80 | + |
| 81 | + }; |
| 82 | + |
| 83 | + public MainWindow() |
| 84 | + { |
| 85 | + |
| 86 | + appName = currentAssembly.GetCustomAttribute<AssemblyTitleAttribute>()!.Title; |
| 87 | + appVersion = currentAssembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!.InformationalVersion; |
| 88 | + appVersionValues = appVersion.Split('.'); |
| 89 | + InitializeComponent(); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + private async Task<string> ShowError(string title, string contents) |
| 94 | + { |
| 95 | + |
| 96 | + errorMessageParams.ContentTitle = title; |
| 97 | + errorMessageParams.ContentMessage = contents; |
| 98 | + |
| 99 | + var messageBox = MessageBoxManager.GetMessageBoxCustom(errorMessageParams); |
| 100 | + return await messageBox.ShowAsync(); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// |
| 106 | + /// </summary> |
| 107 | + /// <param name="sender"></param> |
| 108 | + /// <param name="e"></param> |
| 109 | + private void OnContentModification(object? sender, TextChangedEventArgs e) |
| 110 | + { |
| 111 | + |
| 112 | + if (!hasBeenModified) |
| 113 | + { |
| 114 | + |
| 115 | + hasBeenModified = true; |
| 116 | + ((TextBox)sender!).TextChanged -= OnContentModification; |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + private async void OnClickOpen(object? sender, Avalonia.Interactivity.RoutedEventArgs e) |
| 124 | + { |
| 125 | + |
| 126 | + List<FilePickerFileType> allowedFiletypes = [FilePickerFileTypes.TextPlain]; |
| 127 | + |
| 128 | + // Start async operation to open the dialog. |
| 129 | + var filepath = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions |
| 130 | + { |
| 131 | + |
| 132 | + Title = "Open...", |
| 133 | + AllowMultiple = false, |
| 134 | + FileTypeFilter = [FilePickerFileTypes.All] |
| 135 | + |
| 136 | + }); |
| 137 | + |
| 138 | + if (filepath.Count >= 1) |
| 139 | + { |
| 140 | + |
| 141 | + await using var stream = await filepath[0].OpenReadAsync(); |
| 142 | + string? fileContents; |
| 143 | + |
| 144 | + try |
| 145 | + { |
| 146 | + |
| 147 | + using var streamReader = new StreamReader(stream, true); |
| 148 | + fileContents = await streamReader.ReadToEndAsync(); |
| 149 | + fileContents.ReplaceLineEndings(); |
| 150 | + |
| 151 | + } |
| 152 | + catch (Exception) |
| 153 | + { |
| 154 | + |
| 155 | + await ShowError("Failed to open file", "Are you sure the file you selected is a plain text file?"); |
| 156 | + return; |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + var textBox = this.FindControl<TextBox>("MainTextBox"); |
| 161 | + textBox!.Text = fileContents; |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + private async void OnClickRepository(object? sender, Avalonia.Interactivity.RoutedEventArgs e) |
| 168 | + { |
| 169 | + |
| 170 | + await BrowserService.OpenURLAsync(REPOSITORY_URL); |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + private async void OnClickWebsite(object? sender, Avalonia.Interactivity.RoutedEventArgs e) |
| 175 | + { |
| 176 | + |
| 177 | + await BrowserService.OpenURLAsync(WEBSITE_URL); |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + private void OnClickNewFromScratch(object? sender, Avalonia.Interactivity.RoutedEventArgs e) |
| 182 | + { |
| 183 | + // TODO |
| 184 | + } |
| 185 | + |
| 186 | + private void OnClickExit(object? sender = null, Avalonia.Interactivity.RoutedEventArgs? e = null) |
| 187 | + { |
| 188 | + |
| 189 | + // TODO |
| 190 | + if (!hasBeenModified) |
| 191 | + { |
| 192 | + |
| 193 | + Close(); |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | + private void OnClickExitWithoutSaving(object? sender = null, Avalonia.Interactivity.RoutedEventArgs? e = null) |
| 200 | + { |
| 201 | + |
| 202 | + Close(); |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments