Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Replace Editor With AvalonEdit #3

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/MimaSim/MimaSim/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:local="using:MimaSim"
x:Class="MimaSim.App"
RequestedThemeVariant="Light">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.DataTemplates>
<local:ViewLocator/>
Expand All @@ -18,6 +17,7 @@
<StyleInclude Source="avares://MimaSim/Styles/ButtonStyles.axaml" />

<FluentTheme />
<StyleInclude Source="avares://AvaloniaEdit/Themes/Fluent/AvaloniaEdit.xaml" />
</Application.Styles>

<Application.Resources>
Expand Down
60 changes: 60 additions & 0 deletions src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Avalonia;
using Avalonia.Xaml.Interactivity;
using AvaloniaEdit;

namespace MimaSim.Behaviors;

public class DocumentTextBindingBehavior : Behavior<TextEditor>
{
private TextEditor _textEditor;

public static readonly StyledProperty<string> TextProperty =
AvaloniaProperty.Register<DocumentTextBindingBehavior, string>(nameof(Text));

public string Text
{
get => GetValue(TextProperty);
set => SetValue(TextProperty, value);
}

protected override void OnAttached()
{
base.OnAttached();

if (AssociatedObject is TextEditor textEditor)
{
_textEditor = textEditor;
_textEditor.TextChanged += TextChanged;

Check warning on line 28 in src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void DocumentTextBindingBehavior.TextChanged(object sender, EventArgs eventArgs)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
this.GetObservable(TextProperty).Subscribe(TextPropertyChanged);
}
}

protected override void OnDetaching()
{
base.OnDetaching();

if (_textEditor != null)
{
_textEditor.TextChanged -= TextChanged;

Check warning on line 39 in src/MimaSim/MimaSim/Behaviors/DocumentTextBindingBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void DocumentTextBindingBehavior.TextChanged(object sender, EventArgs eventArgs)' doesn't match the target delegate 'EventHandler' (possibly because of nullability attributes).
}
}

private void TextChanged(object sender, EventArgs eventArgs)
{
if (_textEditor != null && _textEditor.Document != null)
{
Text = _textEditor.Document.Text;
}
}

private void TextPropertyChanged(string text)
{
if (_textEditor != null && _textEditor.Document != null && text != null)
{
var caretOffset = _textEditor.CaretOffset;
_textEditor.Document.Text = text;
//_textEditor.CaretOffset = caretOffset;
}
}
}
15 changes: 12 additions & 3 deletions src/MimaSim/MimaSim/Controls/ProgramEditorControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:c="clr-namespace:MimaSim.Controls;assembly=MimaSim" x:CompileBindings="False"
xmlns:c="clr-namespace:MimaSim.Controls;assembly=MimaSim"
xmlns:avaloniaEdit="https://github.com/avaloniaui/avaloniaedit"
xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity"
xmlns:behaviors="clr-namespace:MimaSim.Behaviors"
x:CompileBindings="False"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="MimaSim.Controls.ProgramEditorControl">
<Grid ColumnDefinitions="AUTO,*,AUTO" RowDefinitions="35,*,35">
<c:ExecutionBar Grid.Column="0" Grid.Row="0" Margin="5,3,0,0" />

<TextBox Text="{Binding Source, Mode=TwoWay}" TextWrapping="Wrap" AcceptsTab="True" AcceptsReturn="True" Watermark="Programmcode hier eingeben" MinWidth="150" MaxHeight="450" MinHeight="150" Grid.Row="1" Grid.ColumnSpan="3"
Grid.Column="0" Margin="0,5,0,5" />
<avaloniaEdit:TextEditor MinWidth="150" MaxHeight="450" MinHeight="150" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3"
FontFamily="Cascadia Code,Consolas,Menlo,Monospace" Margin="0,5,0,5"
ShowLineNumbers="True">
<i:Interaction.Behaviors>
<behaviors:DocumentTextBindingBehavior Text="{Binding Source, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
</avaloniaEdit:TextEditor>

<ComboBox Grid.Row="2" Grid.Column="0" MinWidth="110" HorizontalAlignment="Right" VerticalAlignment="Bottom" ItemsSource="{Binding LanguageNames}" SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}" />
<ComboBox Grid.Row="2" Grid.Column="1" MinWidth="110" HorizontalAlignment="Right" VerticalAlignment="Bottom" ItemsSource="{Binding SampleNames, Mode=TwoWay}" SelectedItem="{Binding SelectedSample, Mode=TwoWay}" />
Expand Down
2 changes: 2 additions & 0 deletions src/MimaSim/MimaSim/MimaSim.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.1.3" />
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.1.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.3" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.1.3" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.1.3" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.3" />
<PackageReference Include="Avalonia.Xaml.Interactions" Version="11.1.0.4" />
<PackageReference Include="ReactiveUI" Version="20.1.63" />
</ItemGroup>

Expand Down
Loading