Skip to content

Commit ff39d3a

Browse files
committed
avolina text editor setup done
1 parent 6b1ae63 commit ff39d3a

5 files changed

Lines changed: 156 additions & 25 deletions

File tree

CutCode/BindableAvalonEditor.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace CutCode
10+
{
11+
public class BindableAvalonEditor : ICSharpCode.AvalonEdit.TextEditor, INotifyPropertyChanged
12+
{
13+
/// <summary>
14+
/// A bindable Text property
15+
/// </summary>
16+
public new string Text
17+
{
18+
get
19+
{
20+
return (string)GetValue(TextProperty);
21+
}
22+
set
23+
{
24+
SetValue(TextProperty, value);
25+
RaisePropertyChanged("Text");
26+
}
27+
}
28+
29+
/// <summary>
30+
/// The bindable text property dependency property
31+
/// </summary>
32+
public static readonly DependencyProperty TextProperty =
33+
DependencyProperty.Register(
34+
"Text",
35+
typeof(string),
36+
typeof(BindableAvalonEditor),
37+
new FrameworkPropertyMetadata
38+
{
39+
DefaultValue = default(string),
40+
BindsTwoWayByDefault = true,
41+
PropertyChangedCallback = OnDependencyPropertyChanged
42+
}
43+
);
44+
45+
protected static void OnDependencyPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
46+
{
47+
var target = (BindableAvalonEditor)obj;
48+
49+
if (target.Document != null)
50+
{
51+
var caretOffset = target.CaretOffset;
52+
var newValue = args.NewValue;
53+
54+
if (newValue == null)
55+
{
56+
newValue = "";
57+
}
58+
59+
target.Document.Text = (string)newValue;
60+
target.CaretOffset = Math.Min(caretOffset, newValue.ToString().Length);
61+
}
62+
}
63+
64+
protected override void OnTextChanged(EventArgs e)
65+
{
66+
if (this.Document != null)
67+
{
68+
Text = this.Document.Text;
69+
}
70+
71+
base.OnTextChanged(e);
72+
}
73+
74+
/// <summary>
75+
/// Raises a property changed event
76+
/// </summary>
77+
/// <param name="property">The name of the property that updates</param>
78+
public void RaisePropertyChanged(string property)
79+
{
80+
if (PropertyChanged != null)
81+
{
82+
PropertyChanged(this, new PropertyChangedEventArgs(property));
83+
}
84+
}
85+
86+
public event PropertyChangedEventHandler PropertyChanged;
87+
}
88+
}

CutCode/CutCode.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<OutputType>WinExe</OutputType>
55
<TargetFramework>net5.0-windows</TargetFramework>
66
<UseWPF>true</UseWPF>
7+
<UseWindowsForms>true</UseWindowsForms>
78
</PropertyGroup>
89

910
<ItemGroup>
@@ -36,6 +37,7 @@
3637
</ItemGroup>
3738

3839
<ItemGroup>
40+
<PackageReference Include="AvalonEdit" Version="6.1.2.30" />
3941
<PackageReference Include="Stylet" Version="1.3.6" />
4042
</ItemGroup>
4143

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using ICSharpCode.AvalonEdit.Highlighting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Data;
9+
10+
11+
12+
namespace CutCode
13+
{
14+
public class HighlightingDefinitionConverter : IValueConverter
15+
{
16+
private static readonly HighlightingDefinitionTypeConverter Converter = new HighlightingDefinitionTypeConverter();
17+
18+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
19+
{
20+
return Converter.ConvertFrom(value);
21+
}
22+
23+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
24+
{
25+
return Converter.ConvertToString(value);
26+
}
27+
}
28+
}

CutCode/ViewModels/AddViewModel.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
5+
using System.Diagnostics;
56
using System.Linq;
67
using System.Text;
78
using System.Threading.Tasks;
@@ -23,6 +24,7 @@ public AddViewModel(IThemeService _themeService)
2324
"Javascript", "Kotlin", "Php", "C", "Ruby", "Rust","Sql", "Swift"
2425
};
2526
leftText = "";
27+
CurrentLang = AllLangs[0];
2628
SetAppearance();
2729
}
2830
private void ThemeChanged(Object sender, EventArgs e)
@@ -32,9 +34,10 @@ private void ThemeChanged(Object sender, EventArgs e)
3234

3335
private void SetAppearance()
3436
{
37+
// 40444B
3538
textBoxBackground = themeService.IsLightTheme ? ColorCon.Convert("#DADBDC") : ColorCon.Convert("#2A2E33");
3639
textBoxForeground = themeService.IsLightTheme ? ColorCon.Convert("#000000") : ColorCon.Convert("#FFFFFF");
37-
richtextBoxBackground = themeService.IsLightTheme ? ColorCon.Convert("#E3E5E8") : ColorCon.Convert("#202225");
40+
richtextBoxBackground = themeService.IsLightTheme ? ColorCon.Convert("#E3E5E8") : ColorCon.Convert("#40444B");
3841
btnHoverColor = themeService.IsLightTheme ? ColorCon.Convert("#D0D1D2") : ColorCon.Convert("#373737");
3942
}
4043

@@ -96,6 +99,22 @@ public string leftText
9699
set => SetAndNotify(ref _leftText, value);
97100
}
98101

102+
private string _CurrentLang;
103+
public string CurrentLang
104+
{
105+
get => _CurrentLang;
106+
set
107+
{
108+
SetAndNotify(ref _CurrentLang, SyntaxLanguages[AllLangs.IndexOf(value)]);
109+
}
110+
}
111+
112+
private List<string> SyntaxLanguages = new List<string>()
113+
{
114+
"text", "Python", "C++", "C#", "CSS", "C#", "Java", "CSS", "Java",
115+
"Java", "Java", "C++", "C++", "C#", "C++","C++", "Java"
116+
};
117+
99118
public void DoneClicked()
100119
{
101120
if (string.IsNullOrEmpty(title))

CutCode/Views/AddView.xaml

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="clr-namespace:CutCode"
7+
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
78
d:DataContext="{d:DesignInstance local:AddViewModel}"
89
xmlns:s="https://github.com/canton7/Stylet"
910
mc:Ignorable="d">
1011

1112
<Grid.Resources>
12-
13+
<local:HighlightingDefinitionConverter x:Key="HighlightingDefinitionConverter"/>
1314
<Style x:Key="textBoxStyle" TargetType="TextBox">
1415
<Setter Property="BorderThickness" Value="0"/>
1516
<Setter Property="Foreground" Value="White"/>
@@ -70,7 +71,7 @@
7071
</Grid.RowDefinitions>
7172
<Label Grid.Row="0" Content="Language" FontFamily="{StaticResource poppins_regular}" Foreground="{Binding textBoxForeground}"/>
7273
<ComboBox Grid.Row="1" Style="{StaticResource comboBoxStyle}" Background="{Binding textBoxBackground}" Width="130" Margin="5,0,0,0"
73-
SelectedIndex="0"
74+
SelectedIndex="0" SelectedValue="{Binding CurrentLang}"
7475
BorderBrush="{Binding textBoxBackground}" ItemsSource="{Binding AllLangs}" Foreground="{Binding textBoxForeground}" FontFamily="{StaticResource poppins_regular}">
7576
<ComboBox.Resources>
7677
<Style TargetType="ComboBoxItem">
@@ -103,29 +104,22 @@
103104
</Grid>
104105
</Border>
105106

106-
<Border Grid.Row="2" BorderThickness="0" Background="{Binding textBoxBackground}" Margin="10,15,12,5" CornerRadius="8"
107+
<Border Grid.Row="2" BorderThickness="0" Background="{Binding richtextBoxBackground}" Margin="10,15,12,5" CornerRadius="8"
107108
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
108-
<ScrollViewer Style="{StaticResource ScrollViewerStyle}">
109-
<Grid>
110-
<local:CustomTextEditor x:Name="codeEditor" ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="5,9,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
111-
Background="{Binding textBoxBackground}" Foreground="{Binding textBoxForeground}" TextWrapping="Wrap" AcceptsReturn="True"
112-
Text="{Binding code}" FontFamily="{StaticResource firacode}" AcceptsTab="True" FontSize="14" BorderThickness="0"/>
113-
114-
<TextBlock Text="Start by typing your code here ..." FontSize="15" FontFamily="{StaticResource poppins_regular}"
115-
IsHitTestVisible="False" Foreground="#929292" Margin="10,7,0,0">
116-
<TextBlock.Style>
117-
<Style TargetType="{x:Type TextBlock}">
118-
<Setter Property="Visibility" Value="Collapsed"/>
119-
<Style.Triggers>
120-
<DataTrigger Binding="{Binding Text, ElementName=codeEditor}" Value="">
121-
<Setter Property="Visibility" Value="Visible"/>
122-
</DataTrigger>
123-
</Style.Triggers>
124-
</Style>
125-
</TextBlock.Style>
126-
</TextBlock>
127-
</Grid>
128-
</ScrollViewer>
109+
<local:BindableAvalonEditor
110+
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
111+
Background="{Binding richtextBoxBackground}"
112+
Foreground="{Binding textBoxForeground}"
113+
BorderThickness="0"
114+
Margin="5,9,0,0"
115+
VerticalAlignment="Stretch"
116+
HorizontalAlignment="Stretch"
117+
FontFamily="{StaticResource firacode}"
118+
FontSize="14"
119+
ShowLineNumbers="True"
120+
ScrollViewer.CanContentScroll="True"
121+
Text="{Binding code}"
122+
SyntaxHighlighting="{Binding CurrentLang, Converter={StaticResource HighlightingDefinitionConverter}}"/>
129123
</Border>
130124

131125
<Grid Grid.Row="3">

0 commit comments

Comments
 (0)