Skip to content

Commit bd32e3a

Browse files
committed
we have a barely working proprtype
HELLZ YEAH!!!
1 parent 8dbada3 commit bd32e3a

File tree

3 files changed

+234
-6
lines changed

3 files changed

+234
-6
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
bin
33
obj
44

5+
# Config files
6+
.vs/*
7+
.vscode/* # I'm using Visual Studio but if I need VSCode...
8+
59
# Solution
610
*.sln
711

MainWindow.axaml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
Icon="avares://WriterSharp/Assets/TransparentIcon.ico"
88
Title="WriterSharp">
99
<Grid RowDefinitions="Auto, *, Auto" Name="MainGrid">
10-
<Grid Grid.Row="0">
10+
<Grid Grid.Row="0" Margin="6">
1111
<Menu>
12-
<MenuItem Header="_File">
12+
<MenuItem Header="_File" CornerRadius="10" Padding="5">
1313
<MenuItem Header="New text file"></MenuItem>
1414
<MenuItem Header="New from template"></MenuItem>
1515
<Separator></Separator>
@@ -19,11 +19,11 @@
1919
<MenuItem Header="Save"></MenuItem>
2020
<MenuItem Header="Save as"></MenuItem>
2121
<Separator></Separator>
22-
<MenuItem Header="Exit"></MenuItem>
22+
<MenuItem Header="Exit" Click="OnClickExit"></MenuItem>
2323
<Separator></Separator>
24-
<MenuItem Header="Exit without saving"></MenuItem>
24+
<MenuItem Header="Exit without saving" Click="OnClickExitWithoutSaving"></MenuItem>
2525
</MenuItem>
26-
<MenuItem Header="_About">
26+
<MenuItem Header="_About" CornerRadius="10" Padding="5">
2727
<MenuItem Header="What is WriterSharp?"></MenuItem>
2828
<MenuItem Header="License"></MenuItem>
2929
<Separator></Separator>
@@ -38,9 +38,25 @@
3838
<TextBox AcceptsReturn="True"
3939
AcceptsTab="True"
4040
TextWrapping="Wrap"
41-
Name="MainTextBox">
41+
Name="MainTextBox"
42+
Margin="5"
43+
TextChanged="OnContentModification">
4244
</TextBox>
4345
</Grid>
46+
<Grid Grid.Row="2" ColumnDefinitions="*, Auto, Auto, Auto" Margin="2">
47+
<Grid Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
48+
<TextBlock>Language (Managed by: Plugin)</TextBlock>
49+
</Grid>
50+
<Grid Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center">
51+
<TextBlock Padding="5">*</TextBlock>
52+
</Grid>
53+
<Grid Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center">
54+
<TextBlock Padding="5">Line Ending</TextBlock>
55+
</Grid>
56+
<Grid Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center">
57+
<TextBlock Padding="5">Encoding</TextBlock>
58+
</Grid>
59+
</Grid>
4460
</Grid>
4561
</Window>
4662

MainWindow.axaml.cs

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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

Comments
 (0)