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

Themed code editor #335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
58 changes: 55 additions & 3 deletions src/Gemini.Modules.CodeEditor/Controls/CodeEditor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Windows;
using System.Windows.Media;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Search;
Expand All @@ -6,17 +7,58 @@ namespace Gemini.Modules.CodeEditor.Controls
{
public class CodeEditor : TextEditor
{
#region LineNumber

/// <summary>
/// The <see cref="LineNumber"/> property.
/// </summary>
public static DependencyProperty LineNumberProperty = DependencyProperty.Register(
"LineNumber",
typeof(int),
typeof(CodeEditor),
new PropertyMetadata(default(int)));

public int LineNumber
{
get { return (int)GetValue(LineNumberProperty); }
set { SetValue(LineNumberProperty, value); }
}

#endregion LineNumber

#region ColumnPosition

/// <summary>
/// The <see cref="ColumnPosition"/> property.
/// </summary>
public static DependencyProperty ColumnPositionProperty = DependencyProperty.Register(
"ColumnPosition",
typeof(int),
typeof(CodeEditor),
new PropertyMetadata(default(int)));

public int ColumnPosition
{
get { return (int)GetValue(ColumnPositionProperty); }
set { SetValue(ColumnPositionProperty, value); }
}

#endregion ColumnPosition

public CodeEditor()
{
ApplySettings();
Loaded += (s, e) => SearchPanel.Install(this);
DocumentChanged += (s, e) => SearchPanel.Install(this);

// Caret related
LineNumber = 1;
ColumnPosition = 1;
TextArea.Caret.PositionChanged += OnCaretPositionChanged;
}

public void ApplySettings()
{
FontFamily = new FontFamily("Consolas");
FontSize = 12;
FontSize = 14;
ShowLineNumbers = Properties.Settings.Default.ShowLineNumbers;
WordWrap = Properties.Settings.Default.WordWrap;

Expand All @@ -37,5 +79,15 @@ public void ApplySettings()
Options.ShowTabs = Properties.Settings.Default.ShowTabs;
}
}

#region Event Handlers

private void OnCaretPositionChanged(object sender, System.EventArgs e)
{
LineNumber = TextArea.Caret.Line;
ColumnPosition = TextArea.Caret.Column;
}

#endregion Event Handlers
}
}
Binary file modified src/Gemini.Modules.CodeEditor/Resources/Icons/ShowEndOfLine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/Gemini.Modules.CodeEditor/Resources/Icons/ShowLineNumbers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/Gemini.Modules.CodeEditor/Resources/Icons/ShowSpaces.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/Gemini.Modules.CodeEditor/Resources/Icons/ShowTabs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/Gemini.Modules.CodeEditor/Resources/Icons/WordWrap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions src/Gemini.Modules.CodeEditor/Resources/Resources.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
xmlns:editing="clr-namespace:ICSharpCode.AvalonEdit.Editing;assembly=ICSharpCode.AvalonEdit"
xmlns:rendering="clr-namespace:ICSharpCode.AvalonEdit.Rendering;assembly=ICSharpCode.AvalonEdit"
xmlns:controls="clr-namespace:Gemini.Modules.CodeEditor.Controls">

<Style TargetType="{x:Type controls:CodeEditor}">
<!-- AvalonEdit does not support RTL, so ensure we use LTR by default -->
<Setter Property="Foreground" Value="{DynamicResource Editor.Foreground}" />
<Setter Property="Background" Value="{DynamicResource Editor.Background}" />
<Setter Property="LineNumbersForeground" Value="{DynamicResource Editor.LineNumbersForeground}" />
<Setter Property="FlowDirection" Value="LeftToRight"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:CodeEditor}">
<ControlTemplate.Resources>
<Style TargetType="{x:Type rendering:TextView}">
<Setter Property="CurrentLineBackground" Value="{DynamicResource Editor.CurrentLineBackground}" />
<Setter Property="CurrentLineBorder">
<Setter.Value>
<Pen Brush="{DynamicResource Editor.CurrentLineBorder}"
Thickness="1"/>
</Setter.Value>
</Setter>
<Setter Property="NonPrintableCharacterBrush" Value="{DynamicResource Editor.NonPrintableCharacter}" />
<Setter Property="LinkTextForegroundBrush" Value="{DynamicResource Editor.LinkTextForeground}" />
<Setter Property="LinkTextBackgroundBrush" Value="{DynamicResource Editor.LinkTextBackground}" />
</Style>
<Style TargetType="{x:Type editing:TextArea}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="SelectionBrush" Value="{DynamicResource Editor.SelectionBrush}"/>
<Setter Property="SelectionBorder">
<Setter.Value>
<Pen Brush="{DynamicResource Editor.SelectionBorder}"
Thickness="1"/>
</Setter.Value>
</Setter>
<Setter Property="SelectionCornerRadius" Value="0" />
<Setter Property="SelectionForeground" Value="{DynamicResource Editor.SelectionForeground}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type avalonEdit:TextArea}">
<DockPanel Focusable="False">
<ItemsControl DockPanel.Dock="Left"
Focusable="False"
ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LeftMargins}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ContentPresenter
Panel.ZIndex="-1"
Focusable="False"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TextView}"/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ControlTemplate.Resources>
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer
Name="PART_ScrollViewer"
Focusable="False"
CanContentScroll="True"
VerticalScrollBarVisibility="{TemplateBinding VerticalScrollBarVisibility}"
HorizontalScrollBarVisibility="{TemplateBinding HorizontalScrollBarVisibility}"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TextArea}"
VerticalContentAlignment="Top"
HorizontalContentAlignment="Left"
Padding="{TemplateBinding Padding}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="WordWrap"
Value="True">
<Setter TargetName="PART_ScrollViewer"
Property="HorizontalScrollBarVisibility"
Value="Disabled" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


</ResourceDictionary>
55 changes: 55 additions & 0 deletions src/Gemini.Modules.CodeEditor/ViewModels/CodeEditorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -8,6 +9,7 @@
using Gemini.Framework.Threading;
using Gemini.Modules.CodeEditor.Commands;
using Gemini.Modules.CodeEditor.Views;
using Gemini.Modules.StatusBar;

namespace Gemini.Modules.CodeEditor.ViewModels
{
Expand All @@ -27,12 +29,50 @@ public class CodeEditorViewModel : PersistedDocument,
private string _originalText;
private ICodeEditorView _view;

private IStatusBar _statusBar;

private int _lineNumber = 0;
[DisplayName("Line Number")]
public int LineNumber
{
get { return _lineNumber; }
set
{
if (_lineNumber != value)
{
_lineNumber = value;
NotifyOfPropertyChange(() => LineNumber);
UpdateStatusBar();
}
}
}

private int _columnPosition = 0;
[DisplayName("Column Position")]
public int ColumnPosition
{
get { return _columnPosition; }
set
{
if (_columnPosition != value)
{
_columnPosition = value;
NotifyOfPropertyChange(() => ColumnPosition);
UpdateStatusBar();
}
}
}

[ImportingConstructor]
public CodeEditorViewModel(LanguageDefinitionManager languageDefinitionManager)
{
_languageDefinitionManager = languageDefinitionManager;

_statusBar = IoC.Get<IStatusBar>();
}

#region Override methods

public override bool ShouldReopenOnStart
{
get { return true; }
Expand Down Expand Up @@ -88,6 +128,8 @@ protected override Task DoSave(string filePath)
return TaskUtility.Completed;
}

#endregion Override methods

private void ApplyOriginalText()
{
if (_view == null)
Expand Down Expand Up @@ -122,6 +164,19 @@ public void ApplySettings()
_view?.ApplySettings();
}

#region StatusBar

private void UpdateStatusBar()
{
if (_statusBar != null && _statusBar.Items.Count >= 3)
{
_statusBar.Items[1].Message = string.Format("Ln {0}", LineNumber);
_statusBar.Items[2].Message = string.Format("Col {0}", ColumnPosition);
}
}

#endregion StatusBar

#region CommandHandlers
void ICommandHandler<WordWrapCommandDefinition>.Update(Command command)
{
Expand Down
11 changes: 9 additions & 2 deletions src/Gemini.Modules.CodeEditor/Views/CodeEditorView.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<UserControl x:Class="Gemini.Modules.CodeEditor.Views.CodeEditorView"
<UserControl x:Class="Gemini.Modules.CodeEditor.Views.CodeEditorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Gemini.Modules.CodeEditor.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">

<UserControl.Resources>
<ResourceDictionary Source="../Resources/Resources.xaml"/>
</UserControl.Resources>

<Grid>
<local:CodeEditor x:Name="CodeEditor" />
<local:CodeEditor x:Name="CodeEditor"
LineNumber="{Binding LineNumber, Mode=OneWayToSource}"
ColumnPosition="{Binding ColumnPosition, Mode=OneWayToSource}"/>
</Grid>
</UserControl>
15 changes: 14 additions & 1 deletion src/Gemini/Themes/VS2013/BlueTheme.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Controls/Merged.xaml" />
Expand Down Expand Up @@ -170,6 +170,19 @@
<SolidColorBrush x:Key="TextBox.Selection.InactiveBackground" Color="#ADD6FF"/>
<SolidColorBrush x:Key="TextBox.Caret" Color="#212121"/>

<!--Text Editor - AvalonEdit-->
<SolidColorBrush x:Key="Editor.Foreground" Color="#FF000000" />
<SolidColorBrush x:Key="Editor.Background" Color="#FFFFFF" />
<SolidColorBrush x:Key="Editor.LineNumbersForeground" Color="#ff929292" />
<SolidColorBrush x:Key="Editor.CurrentLineBackground" Color="#FFFFFF" />
<SolidColorBrush x:Key="Editor.CurrentLineBorder" Color="#EAEAF2" />
<SolidColorBrush x:Key="Editor.NonPrintableCharacter" Color="#FFD3D3D3" />
<SolidColorBrush x:Key="Editor.LinkTextForeground" Color="#FF007ACC" />
<SolidColorBrush x:Key="Editor.LinkTextBackground" Color="Transparent" />
<SolidColorBrush x:Key="Editor.SelectionBrush" Color="#ADD6FF" Opacity="0.7"/>
<SolidColorBrush x:Key="Editor.SelectionBorder" Color="#50ffffff" />
<SolidColorBrush x:Key="Editor.SelectionForeground" Color="#FF000000" />

<!-- Slider -->
<SolidColorBrush x:Key="SliderThumb.MouseOver.Background" Color="#E0D2AA"/>
<SolidColorBrush x:Key="SliderThumb.MouseOver.Border" Color="#E2CD93"/>
Expand Down
15 changes: 14 additions & 1 deletion src/Gemini/Themes/VS2013/DarkTheme.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Controls/Merged.xaml" />
Expand Down Expand Up @@ -170,6 +170,19 @@
<SolidColorBrush x:Key="TextBox.Selection.InactiveBackground" Color="#335C87"/>
<SolidColorBrush x:Key="TextBox.Caret" Color="#F1F1F1"/>

<!--Text Editor - AvalonEdit-->
<SolidColorBrush x:Key="Editor.Foreground" Color="#FFF1F1F1" />
<SolidColorBrush x:Key="Editor.Background" Color="#333337" />
<SolidColorBrush x:Key="Editor.LineNumbersForeground" Color="#FF929292" />
<SolidColorBrush x:Key="Editor.CurrentLineBackground" Color="#333337" />
<SolidColorBrush x:Key="Editor.CurrentLineBorder" Color="#464646" />
<SolidColorBrush x:Key="Editor.NonPrintableCharacter" Color="#2FFFFFFF" />
<SolidColorBrush x:Key="Editor.LinkTextForeground" Color="#FFE5C365" />
<SolidColorBrush x:Key="Editor.LinkTextBackground" Color="Transparent" />
<SolidColorBrush x:Key="Editor.SelectionBrush" Color="#335C87" Opacity="0.7"/>
<SolidColorBrush x:Key="Editor.SelectionBorder" Color="#50ffffff" />
<SolidColorBrush x:Key="Editor.SelectionForeground" Color="#FFF1F1F1" />

<!-- Slider -->
<SolidColorBrush x:Key="SliderThumb.MouseOver.Background" Color="#404044"/>
<SolidColorBrush x:Key="SliderThumb.MouseOver.Border" Color="#0099FF"/>
Expand Down
15 changes: 14 additions & 1 deletion src/Gemini/Themes/VS2013/LightTheme.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Controls/Merged.xaml" />
Expand Down Expand Up @@ -170,6 +170,19 @@
<SolidColorBrush x:Key="TextBox.Selection.InactiveBackground" Color="#ADD6FF"/>
<SolidColorBrush x:Key="TextBox.Caret" Color="#212121"/>

<!--Text Editor - AvalonEdit-->
<SolidColorBrush x:Key="Editor.Foreground" Color="#FF1E1E1E" />
<SolidColorBrush x:Key="Editor.Background" Color="#FFFFFF" />
<SolidColorBrush x:Key="Editor.LineNumbersForeground" Color="#FF929292" />
<SolidColorBrush x:Key="Editor.CurrentLineBackground" Color="#FFFFFF" />
<SolidColorBrush x:Key="Editor.CurrentLineBorder" Color="#EAEAF2" />
<SolidColorBrush x:Key="Editor.NonPrintableCharacter" Color="#FFD3D3D3" />
<SolidColorBrush x:Key="Editor.LinkTextForeground" Color="#FF007ACC" />
<SolidColorBrush x:Key="Editor.LinkTextBackground" Color="Transparent" />
<SolidColorBrush x:Key="Editor.SelectionBrush" Color="#ADD6FF" Opacity="0.7"/>
<SolidColorBrush x:Key="Editor.SelectionBorder" Color="#50ffffff" />
<SolidColorBrush x:Key="Editor.SelectionForeground" Color="#FF1E1E1E" />

<!-- Slider -->
<SolidColorBrush x:Key="SliderThumb.MouseOver.Background" Color="#DCECFC"/>
<SolidColorBrush x:Key="SliderThumb.MouseOver.Border" Color="#7Eb4EA"/>
Expand Down