More XAML customizability, no more wrapping, auto scrolling, and DARK THEME

This commit is contained in:
David
2016-10-13 23:18:26 -04:00
parent 3e96ed3484
commit ba5d4adce6
8 changed files with 218 additions and 49 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
@@ -8,31 +9,48 @@ namespace Befunge.Editor.CharStyles
{
public class BefungeStyler : ITextStyler
{
private string _directionals = "<>^v?r";
public TextStyle DefaultStyle { get; set; } = new TextStyle();
public IEnumerable<Run> StyledString(string input)
{
var runs = new List<Run>();
public TextStyle OperatorStyle { get; set; } = new TextStyle(Colors.IndianRed, FontWeights.Bold);
public TextStyle StackOpStyle { get; set; } = new TextStyle(Colors.IndianRed, FontWeights.Bold);
public TextStyle CompareStyle { get; set; } = new TextStyle(Colors.CornflowerBlue, FontWeights.Bold);
public TextStyle IfStyle { get; set; } = new TextStyle(Colors.CornflowerBlue, FontWeights.Bold);
public TextStyle IoStyle { get; set; } = new TextStyle(Colors.SeaGreen, FontWeights.Bold);
public TextStyle IpStyle { get; set; } = new TextStyle(Colors.DarkGoldenrod, FontWeights.Bold);
public TextStyle LiteralStyle { get; set; } = new TextStyle(Colors.Black, FontWeights.Bold);
public TextStyle SourceModStyle { get; set; } = new TextStyle(Colors.SeaGreen, FontWeights.Bold);
public TextStyle TrampStyle { get; set; } = new TextStyle(Colors.Plum, FontWeights.Bold);
public TextStyle DirectionalStyle { get; set; } = new TextStyle(Colors.CornflowerBlue, FontWeights.Bold);
public TextStyle EndStyle { get; set; } = new TextStyle(Colors.OrangeRed, FontWeights.Bold);
foreach (var c in input)
private const string Operators = "!%*+-/";
private const string StackOps = "\\:$n";
private const string Compares = "`w";
private const string Ifs = "|_";
private const string Ios = ",.&~";
private const string Ips = "\"";
private const string Literals = "0123456789abcdef";
private const string SourceMods = "gp";
private const string Tramps = "#;";
private const string Directionals = "<>^v[]r?";
private const string Ends = "@";
private TextStyle GetStyle(char c)
{
if (_directionals.Contains(c))
runs.Add(new Run(c.ToString())
{
Foreground = new SolidColorBrush(Colors.CornflowerBlue),
FontWeight = FontWeights.Bold
});
else
{
runs.Add(new Run(c.ToString())
{
Foreground = new SolidColorBrush(Colors.Black),
FontWeight = FontWeights.Normal
});
}
if (Operators.Contains(c)) return OperatorStyle;
if (StackOps.Contains(c)) return StackOpStyle;
if (Compares.Contains(c)) return CompareStyle;
if (Ifs.Contains(c)) return IfStyle;
if (Ios.Contains(c)) return IoStyle;
if (Ips.Contains(c)) return IpStyle;
if (Literals.Contains(c)) return LiteralStyle;
if (SourceMods.Contains(c)) return SourceModStyle;
if (Tramps.Contains(c)) return TrampStyle;
if (Directionals.Contains(c)) return DirectionalStyle;
if (Ends.Contains(c)) return EndStyle;
return DefaultStyle;
}
return runs;
}
public IEnumerable<Run> StylizedString(string input) => input.Select(c => GetStyle(c).Stylize(c.ToString()));
}
}

View File

@@ -8,6 +8,6 @@ namespace Befunge.Editor.CharStyles
{
public interface ITextStyler
{
IEnumerable<Run> StyledString(string input);
IEnumerable<Run> StylizedString(string input);
}
}

View File

@@ -0,0 +1,96 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace Befunge.Editor.CharStyles
{
public class TextStyle
{
public Brush Foreground { get; set; }
public Brush Background { get; set; }
public FontWeight FontWeight { get; set; }
public FontStyle FontStyle { get; set; }
#region Constructors
public TextStyle()
{
}
#region Brushes
public TextStyle(Brush foreground)
{
Foreground = foreground;
}
public TextStyle(Brush foreground, FontWeight fontWeight)
{
Foreground = foreground;
FontWeight = fontWeight;
}
public TextStyle(Brush foreground, FontWeight fontWeight, FontStyle fontStyle)
{
Foreground = foreground;
FontWeight = fontWeight;
FontStyle = fontStyle;
}
public TextStyle(Brush foreground, FontWeight fontWeight, FontStyle fontStyle, Brush background)
{
Foreground = foreground;
Background = background;
FontWeight = fontWeight;
FontStyle = fontStyle;
}
#endregion
#region Colors
public TextStyle(Color foreground)
{
Foreground = new SolidColorBrush(foreground);
}
public TextStyle(Color foreground, FontWeight fontWeight)
{
Foreground = new SolidColorBrush(foreground);
FontWeight = fontWeight;
}
public TextStyle(Color foreground, FontWeight fontWeight, FontStyle fontStyle)
{
Foreground = new SolidColorBrush(foreground);
FontWeight = fontWeight;
FontStyle = fontStyle;
}
public TextStyle(Color foreground, FontWeight fontWeight, FontStyle fontStyle, Color background)
{
Foreground = new SolidColorBrush(foreground);
Background = new SolidColorBrush(background);
FontWeight = fontWeight;
FontStyle = fontStyle;
}
#endregion
#endregion
public Run Stylize(string text)
{
var run = new Run(text)
{
FontWeight = FontWeight,
FontStyle = FontStyle
};
if (Foreground != null) run.Foreground = Foreground;
if (Background != null) run.Background = Background;
return run;
}
}
}

View File

@@ -1,14 +1,19 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using Befunge.Editor.CharStyles;
using Befunge.Editor.Utils;
namespace Befunge.Editor.Controls
{
[ContentProperty("TextStyler")]
public class CodeTextBox : RichTextBox
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
@@ -47,11 +52,11 @@ namespace Befunge.Editor.Controls
Resources.Add(typeof(Paragraph), s);
FontFamily = new FontFamily("Consolas");
}
HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
protected override void OnTextChanged(TextChangedEventArgs e)
{
UpdateColors();
Loaded += (sender, e) => UpdateColors();
TextChanged += (sender, e) => UpdateColors();
}
public void UpdateColors()
@@ -84,6 +89,9 @@ namespace Befunge.Editor.Controls
p.AppendStylizedText(TextStyler, afterText);
_changingColors = false;
var m = (allText.Split('\n').Select(x => x.Length).Max() + 2)*Document.FontSize*1126/2048;
Document.PageWidth = m;
}
}
}

View File

@@ -65,6 +65,7 @@
</Compile>
<Compile Include="CharStyles\BefungeStyler.cs" />
<Compile Include="CharStyles\ITextStyler.cs" />
<Compile Include="CharStyles\TextStyle.cs" />
<Compile Include="Controls\CodeTextBox.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Utils\Extensions.cs" />

View File

@@ -4,19 +4,67 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:loc="clr-namespace:Befunge.Editor.Controls"
xmlns:local="clr-namespace:Befunge.Editor" x:Class="Befunge.Editor.MainWindow"
xmlns:local="clr-namespace:Befunge.Editor"
xmlns:charStyles="clr-namespace:Befunge.Editor.CharStyles"
x:Class="Befunge.Editor.MainWindow"
mc:Ignorable="d"
Title="Befunge Editor" Height="350" Width="525">
<Window.Resources>
<Brush x:Key="LightBg">#333333</Brush>
<Brush x:Key="LightFg">#eeeeff</Brush>
<Brush x:Key="DarkBg">#222222</Brush>
<Brush x:Key="DarkFg">#aaaabb</Brush>
<charStyles:TextStyle x:Key="CompIfDir" Foreground="CornflowerBlue" FontWeight="Bold" />
<charStyles:TextStyle x:Key="OpStackOp" Foreground="IndianRed" FontWeight="Bold" />
<charStyles:TextStyle x:Key="IoSource" Foreground="SeaGreen" FontWeight="Bold" />
<charStyles:TextStyle x:Key="Ip" Foreground="DarkGoldenrod" FontWeight="Bold" />
<charStyles:TextStyle x:Key="Literal" Foreground="{StaticResource DarkFg}" FontWeight="Bold" />
<charStyles:TextStyle x:Key="Tramp" Foreground="Plum" FontWeight="Bold" />
<charStyles:TextStyle x:Key="End" Foreground="OrangeRed" FontWeight="Bold" />
<charStyles:TextStyle x:Key="Default" Foreground="{StaticResource LightFg}" FontWeight="Normal" />
</Window.Resources>
<Window.Foreground>
<StaticResource ResourceKey="LightFg" />
</Window.Foreground>
<Window.Background>
<StaticResource ResourceKey="LightBg" />
</Window.Background>
<Window.DataContext>
<local:TextContext/>
<local:TextContext Text="&quot;!dlrow ,olleH&quot;>:#,_@" />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<loc:CodeTextBox x:Name="EditRegion" Margin="10" Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="0"/>
<TextBox x:Name="PlainTextBox" Margin="10" Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1"/>
<loc:CodeTextBox x:Name="EditRegion" Margin="10" Grid.Row="0" Background="{StaticResource DarkBg}"
Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<loc:CodeTextBox.TextStyler>
<charStyles:BefungeStyler
CompareStyle="{StaticResource CompIfDir}"
IfStyle="{StaticResource CompIfDir}"
DirectionalStyle="{StaticResource CompIfDir}"
OperatorStyle="{StaticResource OpStackOp}"
StackOpStyle="{StaticResource OpStackOp}"
IoStyle="{StaticResource IoSource}"
SourceModStyle="{StaticResource IoSource}"
IpStyle="{StaticResource Ip}"
LiteralStyle="{StaticResource Literal}"
TrampStyle="{StaticResource Tramp}"
EndStyle="{StaticResource End}"
DefaultStyle="{StaticResource Default}" />
</loc:CodeTextBox.TextStyler>
</loc:CodeTextBox>
<TextBox x:Name="PlainTextBox" Margin="10" Grid.Row="1" FontFamily="Consolas" AcceptsReturn="True"
Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Foreground="{StaticResource DarkFg}" Background="{StaticResource DarkBg}" />
</Grid>
</Window>

View File

@@ -34,9 +34,6 @@ namespace Befunge.Editor
public MainWindow()
{
InitializeComponent();
EditRegion.TextStyler = new BefungeStyler();
EditRegion.UpdateColors();
}
}
}

View File

@@ -1,3 +1,4 @@
using System.Windows.Controls;
using System.Windows.Documents;
using Befunge.Editor.CharStyles;
@@ -9,6 +10,6 @@ namespace Befunge.Editor.Utils
=> AppendStylizedText(p, styler, text.ToString());
public static void AppendStylizedText(this Paragraph p, ITextStyler styler, string text)
=> p.Inlines.AddRange(styler?.StyledString(text) ?? new[] {new Run(text)});
=> p.Inlines.AddRange(styler?.StylizedString(text) ?? new[] {new Run(text)});
}
}