Search is not available for this dataset
id
stringlengths 1
8
| text
stringlengths 72
9.81M
| addition_count
int64 0
10k
| commit_subject
stringlengths 0
3.7k
| deletion_count
int64 0
8.43k
| file_extension
stringlengths 0
32
| lang
stringlengths 1
94
| license
stringclasses 10
values | repo_name
stringlengths 9
59
|
---|---|---|---|---|---|---|---|---|
10059650 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059651 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059652 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059653 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059654 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059655 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059656 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059657 | <NME> VisualLineLinkText.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Controls;
using System.Diagnostics;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Rendering
{
/// <summary>
/// VisualLineElement that represents a piece of text and is a clickable link.
/// </summary>
public class VisualLineLinkText : VisualLineText
{
static VisualLineLinkText()
{
OpenUriEvent.AddClassHandler<Window>(ExecuteOpenUriEventHandler);
}
/// <summary>
/// Routed event that should navigate to uri when the link is clicked.
/// </summary>
public static RoutedEvent<OpenUriRoutedEventArgs> OpenUriEvent { get; } = RoutedEvent.Register<VisualLineText,OpenUriRoutedEventArgs>(nameof(OpenUriEvent), RoutingStrategies.Bubble);
/// <summary>
/// Gets/Sets the URL that is navigated to when the link is clicked.
/// </summary>
public Uri NavigateUri { get; set; }
/// <summary>
/// Gets/Sets the window name where the URL will be opened.
/// </summary>
public string TargetName { get; set; }
/// <summary>
/// Gets/Sets whether the user needs to press Control to click the link.
/// The default value is true.
/// </summary>
public bool RequireControlModifierForClick { get; set; }
/// <summary>
/// Creates a visual line text element with the specified length.
/// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
/// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
/// </summary>
public VisualLineLinkText(VisualLine parentVisualLine, int length) : base(parentVisualLine, length)
{
RequireControlModifierForClick = true;
}
/// <inheritdoc/>
public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
{
this.TextRunProperties.SetForegroundBrush(context.TextView.LinkTextForegroundBrush);
this.TextRunProperties.SetBackgroundBrush(context.TextView.LinkTextBackgroundBrush);
if (context.TextView.LinkTextUnderline)
this.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
return base.CreateTextRun(startVisualColumn, context);
}
/// <summary>
{
if (LinkIsClickable(e.InputModifiers))
{
e.Handled = true;
//e.Cursor = Cursors.Hand;
}
}
}
/// <inheritdoc/>
protected internal override void OnQueryCursor(PointerEventArgs e)
{
if (LinkIsClickable(e.KeyModifiers))
{
if(e.Source is InputElement inputElement)
{
inputElement.Cursor = new Cursor(StandardCursorType.Hand);
}
e.Handled = true;
}
{
// ignore all kinds of errors during web browser start
}
}
e.Handled = true;
}
/// <inheritdoc/>
e.Source.RaiseEvent(eventArgs);
e.Handled = true;
}
}
/// <inheritdoc/>
protected override VisualLineText CreateInstance(int length)
{
return new VisualLineLinkText(ParentVisualLine, length)
{
NavigateUri = NavigateUri,
TargetName = TargetName,
RequireControlModifierForClick = RequireControlModifierForClick
};
}
private static void ExecuteOpenUriEventHandler(Window window, OpenUriRoutedEventArgs arg)
{
var url = arg.Uri.ToString();
try
{
Process.Start(new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
});
}
catch (Exception)
{
// Process.Start can throw several errors (not all of them documented),
// just ignore all of them.
}
}
}
public sealed class OpenUriRoutedEventArgs : RoutedEventArgs
{
public Uri Uri { get; }
public OpenUriRoutedEventArgs(Uri uri)
{
Uri = uri ?? throw new ArgumentNullException(nameof(uri));
}
}
}
<MSG> Merge branch 'master' into fix-scroll
<DFF> @@ -81,8 +81,11 @@ namespace AvaloniaEdit.Rendering
{
if (LinkIsClickable(e.InputModifiers))
{
+ if(e.Source is InputElement inputElement)
+ {
+ inputElement.Cursor = new Cursor(StandardCursorType.Hand);
+ }
e.Handled = true;
- //e.Cursor = Cursors.Hand;
}
}
@@ -102,8 +105,8 @@ namespace AvaloniaEdit.Rendering
{
// ignore all kinds of errors during web browser start
}
+ e.Handled = true;
}
- e.Handled = true;
}
/// <inheritdoc/>
| 5 | Merge branch 'master' into fix-scroll | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059658 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059659 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059660 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059661 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059662 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059663 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059664 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059665 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059666 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059667 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059668 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059669 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059670 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059671 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059672 | <NME> PixelSnapHelpers.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using Avalonia;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
internal static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
/// This method does not take transforms on visual into account.
/// </summary>
public static Size GetPixelSize(Visual visual)
{
if (visual == null)
throw new ArgumentNullException(nameof(visual));
// TODO-avedit
//PresentationSource source = PresentationSource.FromVisual(visual);
//if (source != null) {
// Matrix matrix = source.CompositionTarget.TransformFromDevice;
// return new Size(matrix.M11, matrix.M22);
//} else {
return new Size(1, 1);
//}
}
/// <summary>
/// Aligns <paramref name="value"/> on the next middle of a pixel.
/// </summary>
/// <param name="value">The value that should be aligned</param>
/// <param name="pixelSize">The size of one pixel</param>
public static double PixelAlign(double value, double pixelSize)
{
// 0 -> 0.5
// 0.1 -> 0.5
// 0.5 -> 0.5
// 0.9 -> 0.5
// 1 -> 1.5
return pixelSize * (Math.Round((value / pixelSize) + 0.5, MidpointRounding.AwayFromZero) - 0.5);
}
/// <summary>
/// Aligns the borders of rect on the middles of pixels.
/// </summary>
public static Rect PixelAlign(Rect rect, Size pixelSize)
{
var x = PixelAlign(rect.X, pixelSize.Width);
var y = PixelAlign(rect.Y, pixelSize.Height);
var width = Round(rect.Width, pixelSize.Width);
var height = Round(rect.Height, pixelSize.Height);
return new Rect(x, y, width, height);
}
/// <summary>
/// Rounds <paramref name="point"/> to whole number of pixels.
/// </summary>
public static Point Round(Point point, Size pixelSize)
{
return new Point(Round(point.X, pixelSize.Width), Round(point.Y, pixelSize.Height));
}
/// <summary>
/// Rounds val to whole number of pixels.
/// </summary>
public static Rect Round(Rect rect, Size pixelSize)
{
return new Rect(Round(rect.X, pixelSize.Width), Round(rect.Y, pixelSize.Height),
Round(rect.Width, pixelSize.Width), Round(rect.Height, pixelSize.Height));
}
/// <summary>
/// Rounds <paramref name="value"/> to a whole number of pixels.
/// </summary>
public static double Round(double value, double pixelSize)
{
return pixelSize * Math.Round(value / pixelSize, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds <paramref name="value"/> to an whole odd number of pixels.
/// </summary>
public static double RoundToOdd(double value, double pixelSize)
{
return Round(value - pixelSize, pixelSize * 2) + pixelSize;
}
}
}
<MSG> make pixel snap helpers public for people implementing own background renderers.
<DFF> @@ -24,7 +24,7 @@ namespace AvaloniaEdit.Utils
/// <summary>
/// Contains static helper methods for aligning stuff on a whole number of pixels.
/// </summary>
- internal static class PixelSnapHelpers
+ public static class PixelSnapHelpers
{
/// <summary>
/// Gets the pixel size on the screen containing visual.
| 1 | make pixel snap helpers public for people implementing own background renderers. | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059673 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059674 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059675 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059676 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059677 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059678 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059679 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059680 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059681 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059682 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059683 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059684 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059685 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059686 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059687 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextElement.GetFontSize(element);
if (foreground == null)
foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> Merge branch 'features/prep-for-custom-fonts' into as-mods
<DFF> @@ -43,7 +43,7 @@ namespace AvaloniaEdit.Utils
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
- public static FormattedText CreateFormattedText(Control element, string text, string typeface, double? emSize, IBrush foreground)
+ public static FormattedText CreateFormattedText(Control element, string text, Avalonia.Media.FontFamily typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
| 1 | Merge branch 'features/prep-for-custom-fonts' into as-mods | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059688 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059689 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059690 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059691 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059692 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059693 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059694 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059695 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059696 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059697 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059698 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059699 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059700 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059701 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059702 | <NME> TextMateColoringTransformer.cs
<BEF> using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
using Avalonia.Media.Immutable;
using Avalonia.Threading;
namespace AvaloniaEdit.TextMate
{
class TextEditorModel : AbstractLineList, IModelTokensChangedListener
{
private object _lock = new object();
private TextEditor _editor;
private TextDocument _document;
private int _lineCount;
public TextEditorModel(TextEditor editor)
{
_editor = editor;
_editor.DocumentChanged += EditorOnDocumentChanged;
EditorOnDocumentChanged(editor, EventArgs.Empty);
}
private void EditorOnDocumentChanged(object? sender, EventArgs e)
{
if (_document is { })
{
_document.Changing -= DocumentOnChanging;
_document.Changed -= DocumentOnChanged;
_document.LineCountChanged -= DocumentOnLineCountChanged;
}
_document = _editor.Document;
_lineCount = _document.LineCount;
_document.Changing += DocumentOnChanging;
_document.Changed += DocumentOnChanged;
_document.LineCountChanged += DocumentOnLineCountChanged;
for (int i = 0; i < _document.LineCount; i++)
{
AddLine(i);
}
}
private void DocumentOnLineCountChanged(object? sender, EventArgs e)
{
lock (_lock)
{
_lineCount = _document.LineCount;
}
}
private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
{
if (e.RemovedText is { })
{
var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
for (int i = endLine; i > startLine; i--)
{
RemoveLine(i);
}
}
}
private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
{
int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
if (e.InsertedText is { })
{
int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
for (int i = startLine; i < endLine; i++)
{
AddLine(i);
}
if (startLine == endLine)
{
UpdateLine(startLine);
}
}
else
{
UpdateLine(startLine);
}
InvalidateLine(startLine);
}
public override void UpdateLine(int lineIndex)
{
// No op
}
public override int GetNumberOfLines() => _lineCount;
public override string GetLineText(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.GetText(_document.Lines[lineIndex]);
}).GetAwaiter().GetResult();
}
public override int GetLineLength(int lineIndex)
{
return Dispatcher.UIThread.InvokeAsync(() =>
{
return _document.Lines[lineIndex].Length;
}).GetAwaiter().GetResult();
}
public override void Dispose()
{
// todo implement dispose.
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
Dispatcher.UIThread.Post(() =>
{
foreach (var range in e.ranges)
{
var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
_editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
}
});
}
}
public static class TextMate
{
public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
{
editor.InstallTheme(theme);
editor.InstallGrammar(grammar);
}
public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetGrammar(grammar);
}
public static void InstallTheme(this TextEditor editor, Theme theme)
{
var transformer = editor.GetOrCreateTransformer();
transformer.SetTheme(theme);
editor.InvalidateVisual();
}
private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
{
var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
if (transformer is null)
{
var editorModel = new TextEditorModel(editor);
var model = new TMModel(editorModel);
transformer = new TextMateColoringTransformer(model);
model.AddModelTokensChangedListener(editorModel);
editor.TextArea.TextView.LineTransformers.Add(transformer);
}
return transformer;
}
}
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
{
private Theme _theme;
private IGrammar _grammar;
private TMModel _model;
private TextDocument _document;
private TextView _textView;
private Action<Exception> _exceptionHandler;
private volatile bool _areVisualLinesValid = false;
private volatile int _firstVisibleLineIndex = -1;
private volatile int _lastVisibleLineIndex = -1;
private readonly Dictionary<int, IBrush> _brushes;
public TextMateColoringTransformer(
TextView textView,
Action<Exception> exceptionHandler)
: base(exceptionHandler)
{
_textView = textView;
_exceptionHandler = exceptionHandler;
_brushes = new Dictionary<int, IBrush>();
_textView.VisualLinesChanged += TextView_VisualLinesChanged;
}
public void SetModel(TextDocument document, TMModel model)
{
_areVisualLinesValid = false;
_document = document;
_model = model;
if (_grammar != null)
{
_model.SetGrammar(_grammar);
}
}
private void TextView_VisualLinesChanged(object sender, EventArgs e)
{
try
{
if (!_textView.VisualLinesValid || _textView.VisualLines.Count == 0)
return;
_areVisualLinesValid = true;
_firstVisibleLineIndex = _textView.VisualLines[0].FirstDocumentLine.LineNumber - 1;
_lastVisibleLineIndex = _textView.VisualLines[_textView.VisualLines.Count - 1].LastDocumentLine.LineNumber - 1;
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
}
public void SetTheme(Theme theme)
{
_theme = theme;
_brushes.Clear();
var map = _theme.GetColorMap();
foreach (var color in map)
{
var id = _theme.GetColorId(color);
_brushes[id] = new ImmutableSolidColorBrush(Color.Parse(NormalizeColor(color)));
}
}
public void SetGrammar(IGrammar grammar)
{
_grammar = grammar;
if (_model != null)
{
_model.SetGrammar(grammar);
}
}
IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;
_brushes.TryGetValue(colorId, out IBrush result);
return result;
}
protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
{
if (_model == null)
return;
int lineNumber = line.LineNumber;
var tokens = _model.GetLineTokens(lineNumber - 1);
if (tokens == null)
return;
var transformsInLine = ArrayPool<ForegroundTextTransformation>.Shared.Rent(tokens.Count);
try
{
GetLineTransformations(lineNumber, tokens, transformsInLine);
for (int i = 0; i < tokens.Count; i++)
{
if (transformsInLine[i] == null)
continue;
transformsInLine[i].Transform(this, line);
}
}
finally
{
ArrayPool<ForegroundTextTransformation>.Shared.Return(transformsInLine);
}
}
catch (Exception ex)
{
_exceptionHandler?.Invoke(ex);
}
}
private void GetLineTransformations(int lineNumber, List<TMToken> tokens, ForegroundTextTransformation[] transformations)
{
for (int i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var nextToken = (i + 1) < tokens.Count ? tokens[i + 1] : null;
var startIndex = token.StartIndex;
var endIndex = nextToken?.StartIndex ?? _model.GetLines().GetLineLength(lineNumber - 1);
if (startIndex >= endIndex || token.Scopes == null || token.Scopes.Count == 0)
{
transformations[i] = null;
continue;
}
var lineOffset = _document.GetLineByNumber(lineNumber).Offset;
int foreground = 0;
int background = 0;
int fontStyle = 0;
foreach (var themeRule in _theme.Match(token.Scopes))
{
if (foreground == 0 && themeRule.foreground > 0)
foreground = themeRule.foreground;
if (background == 0 && themeRule.background > 0)
background = themeRule.background;
if (fontStyle == 0 && themeRule.fontStyle > 0)
fontStyle = themeRule.fontStyle;
}
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();
transformations[i].ColorMap = this;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
transformations[i].ForegroundColor = foreground;
transformations[i].BackgroundColor = background;
transformations[i].FontStyle = fontStyle;
}
}
public void ModelTokensChanged(ModelTokensChangedEvent e)
{
if (e.Ranges == null)
return;
if (_model == null || _model.IsStopped)
return;
int firstChangedLineIndex = int.MaxValue;
int lastChangedLineIndex = -1;
foreach (var range in e.Ranges)
{
firstChangedLineIndex = Math.Min(range.FromLineNumber - 1, firstChangedLineIndex);
lastChangedLineIndex = Math.Max(range.ToLineNumber - 1, lastChangedLineIndex);
}
if (_areVisualLinesValid)
{
bool changedLinesAreNotVisible =
((firstChangedLineIndex < _firstVisibleLineIndex && lastChangedLineIndex < _firstVisibleLineIndex) ||
(firstChangedLineIndex > _lastVisibleLineIndex && lastChangedLineIndex > _lastVisibleLineIndex));
if (changedLinesAreNotVisible)
return;
}
Dispatcher.UIThread.Post(() =>
{
int firstLineIndexToRedraw = Math.Max(firstChangedLineIndex, _firstVisibleLineIndex);
int lastLineIndexToRedrawLine = Math.Min(lastChangedLineIndex, _lastVisibleLineIndex);
int totalLines = _document.Lines.Count - 1;
firstLineIndexToRedraw = Clamp(firstLineIndexToRedraw, 0, totalLines);
lastLineIndexToRedrawLine = Clamp(lastLineIndexToRedrawLine, 0, totalLines);
DocumentLine firstLineToRedraw = _document.Lines[firstLineIndexToRedraw];
DocumentLine lastLineToRedraw = _document.Lines[lastLineIndexToRedrawLine];
_textView.Redraw(
firstLineToRedraw.Offset,
(lastLineToRedraw.Offset + lastLineToRedraw.TotalLength) - firstLineToRedraw.Offset);
});
}
static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
static string NormalizeColor(string color)
{
if (color.Length == 9)
{
Span<char> normalizedColor = stackalloc char[] { '#', color[7], color[8], color[1], color[2], color[3], color[4], color[5], color[6] };
return normalizedColor.ToString();
}
return color;
}
}
}
<MSG> separate files
<DFF> @@ -1,8 +1,5 @@
-using System;
using System.Collections.Generic;
-using System.Linq;
using Avalonia.Media;
-using Avalonia.Threading;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using TextMateSharp.Grammars;
@@ -11,177 +8,6 @@ using TextMateSharp.Themes;
namespace AvaloniaEdit.TextMate
{
- class TextEditorModel : AbstractLineList, IModelTokensChangedListener
- {
- private object _lock = new object();
- private TextEditor _editor;
- private TextDocument _document;
- private int _lineCount;
-
- public TextEditorModel(TextEditor editor)
- {
- _editor = editor;
- _editor.DocumentChanged += EditorOnDocumentChanged;
-
- EditorOnDocumentChanged(editor, EventArgs.Empty);
- }
-
- private void EditorOnDocumentChanged(object? sender, EventArgs e)
- {
- if (_document is { })
- {
- _document.Changing -= DocumentOnChanging;
- _document.Changed -= DocumentOnChanged;
- _document.LineCountChanged -= DocumentOnLineCountChanged;
- }
-
- _document = _editor.Document;
- _lineCount = _document.LineCount;
-
- _document.Changing += DocumentOnChanging;
- _document.Changed += DocumentOnChanged;
- _document.LineCountChanged += DocumentOnLineCountChanged;
-
- for (int i = 0; i < _document.LineCount; i++)
- {
- AddLine(i);
- }
- }
-
- private void DocumentOnLineCountChanged(object? sender, EventArgs e)
- {
- lock (_lock)
- {
- _lineCount = _document.LineCount;
- }
- }
-
- private void DocumentOnChanging(object? sender, DocumentChangeEventArgs e)
- {
- if (e.RemovedText is { })
- {
- var startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
- var endLine = _document.GetLineByOffset(e.Offset + e.RemovalLength).LineNumber - 1;
-
- for (int i = endLine; i > startLine; i--)
- {
- RemoveLine(i);
- }
- }
- }
-
- private void DocumentOnChanged(object? sender, DocumentChangeEventArgs e)
- {
- int startLine = _document.GetLineByOffset(e.Offset).LineNumber - 1;
-
- if (e.InsertedText is { })
- {
- int endLine = _document.GetLineByOffset(e.Offset + e.InsertionLength).LineNumber - 1;
-
- for (int i = startLine; i < endLine; i++)
- {
- AddLine(i);
- }
-
- if (startLine == endLine)
- {
- UpdateLine(startLine);
- }
- }
- else
- {
- UpdateLine(startLine);
- }
-
- InvalidateLine(startLine);
- }
-
- public override void UpdateLine(int lineIndex)
- {
- // No op
- }
-
- public override int GetNumberOfLines() => _lineCount;
-
- public override string GetLineText(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.GetText(_document.Lines[lineIndex]);
- }).GetAwaiter().GetResult();
- }
-
- public override int GetLineLength(int lineIndex)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- return _document.Lines[lineIndex].Length;
- }).GetAwaiter().GetResult();
- }
-
- public override void Dispose()
- {
- // todo implement dispose.
- }
-
- public void ModelTokensChanged(ModelTokensChangedEvent e)
- {
- Dispatcher.UIThread.Post(() =>
- {
- foreach (var range in e.ranges)
- {
- var startLine = _editor.Document.GetLineByNumber(range.fromLineNumber);
- var endLine = _editor.Document.GetLineByNumber(range.toLineNumber);
-
- _editor.TextArea.TextView.Redraw(startLine.Offset, endLine.EndOffset - startLine.Offset);
- }
- });
- }
- }
-
- public static class TextMate
- {
- public static void InstallTextMate(this TextEditor editor, Theme theme, IGrammar grammar)
- {
- editor.InstallTheme(theme);
- editor.InstallGrammar(grammar);
- }
-
- public static void InstallGrammar(this TextEditor editor, IGrammar grammar)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetGrammar(grammar);
- }
-
- public static void InstallTheme(this TextEditor editor, Theme theme)
- {
- var transformer = editor.GetOrCreateTransformer();
-
- transformer.SetTheme(theme);
-
- editor.InvalidateVisual();
- }
-
- private static TextMateColoringTransformer GetOrCreateTransformer(this TextEditor editor)
- {
- var transformer = editor.TextArea.TextView.LineTransformers.OfType<TextMateColoringTransformer>().FirstOrDefault();
-
- if (transformer is null)
- {
- var editorModel = new TextEditorModel(editor);
- var model = new TMModel(editorModel);
-
- transformer = new TextMateColoringTransformer(model);
- model.AddModelTokensChangedListener(editorModel);
-
- editor.TextArea.TextView.LineTransformers.Add(transformer);
- }
-
- return transformer;
- }
- }
-
public class TextMateColoringTransformer : GenericLineTransformer
{
private Theme _theme;
| 0 | separate files | 174 | .cs | TextMate/TextMateColoringTransformer | mit | AvaloniaUI/AvaloniaEdit |
10059703 | <NME> README.md
<BEF> 
A lightweight modular layout engine for client and server. Currently powering the award winning FT Web App. FruitMachine was designed to construct nested view layouts from strictly modular components.
```js
// Define a module
var Apple = fruitmachine.define({
name: 'apple',
template: function(){ return 'hello' }
});
// Create a module
var apple = new Apple();
// Render it
apple.render();
apple.el.outerHTML;
//=> <div class="apple">hello</div>
```
## Installation
```
$ npm install fruitmachine
```
or
```
$ bower install fruitmachine
```
or
Download the [pre-built version][built] (~2k gzipped).
[built]: http://wzrd.in/standalone/fruitmachine@latest
## Examples
- [Article viewer](http://ftlabs.github.io/fruitmachine/examples/article-viewer/)
- [TODO](http://ftlabs.github.io/fruitmachine/examples/todo/)
## Documentation
- [Introduction](docs/introduction.md)
- [Getting started](docs/getting-started.md)
- [Defining modules](docs/defining-modules.md)
- [Slots](docs/slots.md)
- [View assembly](docs/layout-assembly.md)
- [Instantiation](docs/module-instantiation.md)
- [Templates](docs/templates.md)
- [Template markup](docs/template-markup.md)
- [Rendering](docs/rendering.md)
- [DOM injection](docs/injection.md)
- [The module element](docs/module-el.md)
- [Queries](docs/queries.md)
- [Helpers](docs/module-helpers.md)
- [Removing & destroying](docs/removing-and-destroying.md)
- [Extending](docs/extending-modules.md)
- [Server-side rendering](docs/server-side-rendering.md)
- [API](docs/api.md)
- [Events](docs/events.md)
## Tests
#### With PhantomJS
```
$ npm install
$ npm test
```
#### Without PhantomJS
```
$ node_modules/.bin/buster-static
```
...then visit http://localhost:8282/ in browser
## Author
- **Wilson Page** - [@wilsonpage](http://github.com/wilsonpage)
## Contributors
- **Wilson Page** - [@wilsonpage](http://github.com/wilsonpage)
- **Matt Andrews** - [@matthew-andrews](http://github.com/matthew-andrews)
## License
Copyright (c) 2018 The Financial Times Limited
Licensed under the MIT license.
## Credits and collaboration
FruitMachine is largely unmaintained/finished. All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request.
<MSG> Updated readme
<DFF> @@ -1,6 +1,8 @@

-A lightweight modular layout engine for client and server. Currently powering the award winning FT Web App. FruitMachine was designed to construct nested view layouts from strictly modular components.
+A lightweight modular layout engine for client and server. Currently powering the award winning FT Web App.
+
+FruitMachine was designed to construct nested view layouts from strictly modular components. We wanted it to be as light and unopinionated as possible so that it could be applied to almost any layout problem.
```js
// Define a module
| 3 | Updated readme | 1 | .md | md | mit | ftlabs/fruitmachine |
10059704 | <NME> fa.js
<BEF> ADDFILE
<MSG> Merge pull request #564 from mansoor-omrani/patch-1
i18n: Add Farsi locale
<DFF> @@ -0,0 +1,46 @@
+(function(jsGrid) {
+
+ jsGrid.locales.fa = {
+ grid: {
+ noDataContent: "پیدا نشد",
+ deleteConfirm: "آیا مطمئن هستید؟",
+ pagerFormat: "صفحه: {first} {prev} {pages} {next} {last} {pageIndex} از {pageCount}",
+ pagePrevText: "قبلی",
+ pageNextText: "بعدی",
+ pageFirstText: "ابتدا",
+ pageLastText: "انتها",
+ loadMessage: "لطفا منتظر باشید ...",
+ invalidMessage: "اطلاعات نامعتبر است!"
+ },
+
+ loadIndicator: {
+ message: "در حال بارگذاری ..."
+ },
+
+ fields: {
+ control: {
+ searchModeButtonTooltip: "تغییر به حالت جستجو",
+ insertModeButtonTooltip: "تغییر به حالت درج",
+ editButtonTooltip: "تغییر به حالت ویرایش",
+ deleteButtonTooltip: "حذف",
+ searchButtonTooltip: "جستجو",
+ clearFilterButtonTooltip: "پاک کردن فیلترها",
+ insertButtonTooltip: "درج",
+ updateButtonTooltip: "به روز رسانی",
+ cancelEditButtonTooltip: "لغو ویرایش"
+ }
+ },
+
+ validators: {
+ required: { message: "فیلد اجباری است" },
+ rangeLength: { message: "طول مقدار وارد شده از حداکثر تعریف شده بیشتر است" },
+ minLength: { message: "تعداد کاراکتر وارد شده خیلی کم است" },
+ maxLength: { message: "تعداد کاراکتر وارد شده خیلی زیاداست" },
+ pattern: { message: "مقدار وارد شده با الگوی تعریف شده مطابقت ندارد" },
+ range: { message: "مقدار وارد شده خارج از بازه معتبر است" },
+ min: { message: "مقدار وارد شده خیلی کوچک است" },
+ max: { message: "مقدار وارد شده خیلی بزرگ است" }
+ }
+ };
+
+}(jsGrid, jQuery));
| 46 | Merge pull request #564 from mansoor-omrani/patch-1 | 0 | .js | js | mit | tabalinas/jsgrid |
10059705 | <NME> fa.js
<BEF> ADDFILE
<MSG> Merge pull request #564 from mansoor-omrani/patch-1
i18n: Add Farsi locale
<DFF> @@ -0,0 +1,46 @@
+(function(jsGrid) {
+
+ jsGrid.locales.fa = {
+ grid: {
+ noDataContent: "پیدا نشد",
+ deleteConfirm: "آیا مطمئن هستید؟",
+ pagerFormat: "صفحه: {first} {prev} {pages} {next} {last} {pageIndex} از {pageCount}",
+ pagePrevText: "قبلی",
+ pageNextText: "بعدی",
+ pageFirstText: "ابتدا",
+ pageLastText: "انتها",
+ loadMessage: "لطفا منتظر باشید ...",
+ invalidMessage: "اطلاعات نامعتبر است!"
+ },
+
+ loadIndicator: {
+ message: "در حال بارگذاری ..."
+ },
+
+ fields: {
+ control: {
+ searchModeButtonTooltip: "تغییر به حالت جستجو",
+ insertModeButtonTooltip: "تغییر به حالت درج",
+ editButtonTooltip: "تغییر به حالت ویرایش",
+ deleteButtonTooltip: "حذف",
+ searchButtonTooltip: "جستجو",
+ clearFilterButtonTooltip: "پاک کردن فیلترها",
+ insertButtonTooltip: "درج",
+ updateButtonTooltip: "به روز رسانی",
+ cancelEditButtonTooltip: "لغو ویرایش"
+ }
+ },
+
+ validators: {
+ required: { message: "فیلد اجباری است" },
+ rangeLength: { message: "طول مقدار وارد شده از حداکثر تعریف شده بیشتر است" },
+ minLength: { message: "تعداد کاراکتر وارد شده خیلی کم است" },
+ maxLength: { message: "تعداد کاراکتر وارد شده خیلی زیاداست" },
+ pattern: { message: "مقدار وارد شده با الگوی تعریف شده مطابقت ندارد" },
+ range: { message: "مقدار وارد شده خارج از بازه معتبر است" },
+ min: { message: "مقدار وارد شده خیلی کوچک است" },
+ max: { message: "مقدار وارد شده خیلی بزرگ است" }
+ }
+ };
+
+}(jsGrid, jQuery));
| 46 | Merge pull request #564 from mansoor-omrani/patch-1 | 0 | .js | js | mit | tabalinas/jsgrid |
10059706 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059707 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059708 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059709 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059710 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059711 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059712 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059713 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059714 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059715 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059716 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059717 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059718 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059719 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059720 | <NME> CompletionWindowBase.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using AvaloniaEdit.Document;
using AvaloniaEdit.Editing;
using AvaloniaEdit.Rendering;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace AvaloniaEdit.CodeCompletion
{
/// <summary>
/// Base class for completion windows. Handles positioning the window at the caret.
/// </summary>
public class CompletionWindowBase : Popup, IStyleable
{
static CompletionWindowBase()
{
//BackgroundProperty.OverrideDefaultValue(typeof(CompletionWindowBase), Brushes.White);
}
Type IStyleable.StyleKey => typeof(PopupRoot);
/// <summary>
/// Gets the parent TextArea.
/// </summary>
public TextArea TextArea { get; }
private readonly Window _parentWindow;
private TextDocument _document;
/// <summary>
/// Gets/Sets the start of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int StartOffset { get; set; }
/// <summary>
/// Gets/Sets the end of the text range in which the completion window stays open.
/// This text portion is used to determine the text used to select an entry in the completion list by typing.
/// </summary>
public int EndOffset { get; set; }
/// <summary>
/// Gets whether the window was opened above the current line.
/// </summary>
protected bool IsUp { get; private set; }
/// <summary>
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
{
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
AttachEvents();
Initailize();
}
protected virtual void OnClosed()
{
DetachEvents();
}
private void Initailize()
{
if (_document != null && StartOffset != TextArea.Caret.Offset)
{
SetPosition(new TextViewPosition(_document.GetLocation(StartOffset)));
}
else
{
SetPosition(TextArea.Caret.Position);
}
}
public void Show()
{
Open();
Height = double.NaN;
MinHeight = 0;
UpdatePosition();
}
public void Hide()
{
Close();
OnClosed();
}
#region Event Handlers
private void AttachEvents()
{
((ISetLogicalParent)this).SetParent(TextArea.GetVisualRoot() as ILogical);
_document = TextArea.Document;
if (_document != null)
{
_document.Changing += TextArea_Document_Changing;
}
// LostKeyboardFocus seems to be more reliable than PreviewLostKeyboardFocus - see SD-1729
TextArea.LostFocus += TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged += TextViewScrollOffsetChanged;
TextArea.DocumentChanged += TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged += ParentWindow_LocationChanged;
_parentWindow.Deactivated += ParentWindow_Deactivated;
}
// close previous completion windows of same type
foreach (var x in TextArea.StackedInputHandlers.OfType<InputHandler>())
{
if (x.Window.GetType() == GetType())
TextArea.PopStackedInputHandler(x);
}
_myInputHandler = new InputHandler(this);
TextArea.PushStackedInputHandler(_myInputHandler);
}
/// <summary>
/// Detaches events from the text area.
/// </summary>
protected virtual void DetachEvents()
{
((ISetLogicalParent)this).SetParent(null);
if (_document != null)
{
_document.Changing -= TextArea_Document_Changing;
}
TextArea.LostFocus -= TextAreaLostFocus;
TextArea.TextView.ScrollOffsetChanged -= TextViewScrollOffsetChanged;
TextArea.DocumentChanged -= TextAreaDocumentChanged;
if (_parentWindow != null)
{
_parentWindow.PositionChanged -= ParentWindow_LocationChanged;
_parentWindow.Deactivated -= ParentWindow_Deactivated;
}
TextArea.PopStackedInputHandler(_myInputHandler);
}
#region InputHandler
private InputHandler _myInputHandler;
/// <summary>
/// A dummy input handler (that justs invokes the default input handler).
/// This is used to ensure the completion window closes when any other input handler
/// becomes active.
/// </summary>
private sealed class InputHandler : TextAreaStackedInputHandler
{
internal readonly CompletionWindowBase Window;
public InputHandler(CompletionWindowBase window)
: base(window.TextArea)
{
Debug.Assert(window != null);
Window = window;
}
public override void Detach()
{
base.Detach();
Window.Hide();
}
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyDownEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
public override void OnPreviewKeyUp(KeyEventArgs e)
{
if (e.Key == Key.DeadCharProcessed)
return;
e.Handled = RaiseEventPair(Window, null, KeyUpEvent,
new KeyEventArgs { Device = e.Device, Key = e.Key });
}
}
#endregion
private void TextViewScrollOffsetChanged(object sender, EventArgs e)
{
ILogicalScrollable textView = TextArea;
var visibleRect = new Rect(textView.Offset.X, textView.Offset.Y, textView.Viewport.Width, textView.Viewport.Height);
//close completion window when the user scrolls so far that the anchor position is leaving the visible area
if (visibleRect.Contains(_visualLocation) || visibleRect.Contains(_visualLocationTop))
{
UpdatePosition();
}
else
{
Hide();
}
}
private void TextAreaDocumentChanged(object sender, EventArgs e)
{
Hide();
}
private void TextAreaLostFocus(object sender, RoutedEventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
private void ParentWindow_Deactivated(object sender, EventArgs e)
{
Hide();
}
private void ParentWindow_LocationChanged(object sender, EventArgs e)
{
UpdatePosition();
}
/// <inheritdoc/>
private void OnDeactivated(object sender, EventArgs e)
{
Dispatcher.UIThread.Post(CloseIfFocusLost, DispatcherPriority.Background);
}
#endregion
/// <summary>
/// Raises a tunnel/bubble event pair for a control.
/// </summary>
/// <param name="target">The control for which the event should be raised.</param>
/// <param name="previewEvent">The tunneling event.</param>
/// <param name="event">The bubbling event.</param>
/// <param name="args">The event args to use.</param>
/// <returns>The <see cref="RoutedEventArgs.Handled"/> value of the event args.</returns>
[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
protected static bool RaiseEventPair(Control target, RoutedEvent previewEvent, RoutedEvent @event, RoutedEventArgs args)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (args == null)
throw new ArgumentNullException(nameof(args));
if (previewEvent != null)
{
args.RoutedEvent = previewEvent;
target.RaiseEvent(args);
}
args.RoutedEvent = @event ?? throw new ArgumentNullException(nameof(@event));
target.RaiseEvent(args);
return args.Handled;
}
// Special handler: handledEventsToo
private void OnMouseUp(object sender, PointerReleasedEventArgs e)
{
ActivateParentWindow();
}
/// <summary>
/// Activates the parent window.
/// </summary>
protected virtual void ActivateParentWindow()
{
_parentWindow?.Activate();
}
private void CloseIfFocusLost()
{
if (CloseOnFocusLost)
{
Debug.WriteLine("CloseIfFocusLost: this.IsFocues=" + IsFocused + " IsTextAreaFocused=" + IsTextAreaFocused);
if (!IsFocused && !IsTextAreaFocused)
{
Hide();
}
}
}
/// <summary>
/// Gets whether the completion window should automatically close when the text editor looses focus.
/// </summary>
protected virtual bool CloseOnFocusLost => true;
private bool IsTextAreaFocused
{
get
{
if (_parentWindow != null && !_parentWindow.IsActive)
return false;
return TextArea.IsFocused;
}
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape)
{
e.Handled = true;
Hide();
}
}
private Point _visualLocation;
private Point _visualLocationTop;
/// <summary>
/// Positions the completion window at the specified position.
/// </summary>
protected void SetPosition(TextViewPosition position)
{
var textView = TextArea.TextView;
_visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
_visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
UpdatePosition();
}
/// <summary>
/// Updates the position of the CompletionWindow based on the parent TextView position and the screen working area.
/// It ensures that the CompletionWindow is completely visible on the screen.
/// </summary>
protected void UpdatePosition()
{
var textView = TextArea.TextView;
var position = _visualLocation - textView.ScrollOffset;
Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
}
// TODO: check if needed
///// <inheritdoc/>
//protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
//{
// base.OnRenderSizeChanged(sizeInfo);
// if (sizeInfo.HeightChanged && IsUp)
// {
// this.Top += sizeInfo.PreviousSize.Height - sizeInfo.NewSize.Height;
// }
//}
/// <summary>
/// Gets/sets whether the completion window should expect text insertion at the start offset,
/// which not go into the completion region, but before it.
/// </summary>
/// <remarks>This property allows only a single insertion, it is reset to false
/// when that insertion has occurred.</remarks>
public bool ExpectInsertionBeforeStart { get; set; }
private void TextArea_Document_Changing(object sender, DocumentChangeEventArgs e)
{
if (e.Offset + e.RemovalLength == StartOffset && e.RemovalLength > 0)
{
Hide(); // removal immediately in front of completion segment: close the window
// this is necessary when pressing backspace after dot-completion
}
if (e.Offset == StartOffset && e.RemovalLength == 0 && ExpectInsertionBeforeStart)
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.AfterInsertion);
ExpectInsertionBeforeStart = false;
}
else
{
StartOffset = e.GetNewOffset(StartOffset, AnchorMovementType.BeforeInsertion);
}
EndOffset = e.GetNewOffset(EndOffset, AnchorMovementType.AfterInsertion);
}
}
}
<MSG> Merge branch 'master' into feature/TextFormatterPort
<DFF> @@ -76,15 +76,20 @@ namespace AvaloniaEdit.CodeCompletion
/// Creates a new CompletionWindowBase.
/// </summary>
public CompletionWindowBase(TextArea textArea) : base()
- {
+ {
TextArea = textArea ?? throw new ArgumentNullException(nameof(textArea));
_parentWindow = textArea.GetVisualRoot() as Window;
-
+
AddHandler(PointerReleasedEvent, OnMouseUp, handledEventsToo: true);
StartOffset = EndOffset = TextArea.Caret.Offset;
-
+
+ PlacementTarget = TextArea.TextView;
+ PlacementMode = PlacementMode.AnchorAndGravity;
+ PlacementAnchor = Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft;
+ PlacementGravity = Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight;
+
//Deactivated += OnDeactivated; //Not needed?
Closed += (sender, args) => DetachEvents();
@@ -113,11 +118,11 @@ namespace AvaloniaEdit.CodeCompletion
public void Show()
{
+ UpdatePosition();
+
Open();
Height = double.NaN;
MinHeight = 0;
-
- UpdatePosition();
}
public void Hide()
@@ -206,7 +211,7 @@ namespace AvaloniaEdit.CodeCompletion
base.Detach();
Window.Hide();
}
-
+
public override void OnPreviewKeyDown(KeyEventArgs e)
{
// prevents crash when typing deadchar while CC window is open
@@ -372,7 +377,8 @@ namespace AvaloniaEdit.CodeCompletion
var position = _visualLocation - textView.ScrollOffset;
- Host?.ConfigurePosition(textView, PlacementMode.AnchorAndGravity, position, Avalonia.Controls.Primitives.PopupPositioning.PopupAnchor.TopLeft, Avalonia.Controls.Primitives.PopupPositioning.PopupGravity.BottomRight);
+ this.HorizontalOffset = position.X;
+ this.VerticalOffset = position.Y;
}
// TODO: check if needed
| 13 | Merge branch 'master' into feature/TextFormatterPort | 7 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059721 | <NME> jsgrid.tests.js
<BEF> $(function() {
var Grid = jsGrid.Grid,
JSGRID = "JSGrid",
JSGRID_DATA_KEY = JSGRID;
Grid.prototype.updateOnResize = false;
module("basic");
test("default creation", function() {
var gridOptions = {
simpleOption: "test",
complexOption: {
a: "subtest",
b: 1,
c: {}
}
},
grid = new Grid("#jsGrid", gridOptions);
equal(grid._container[0], $("#jsGrid")[0], "container saved");
equal(grid.simpleOption, "test", "primitive option extended");
equal(grid.complexOption, gridOptions.complexOption, "non-primitive option extended");
});
test("jquery adapter creation", function() {
var gridOptions = {
option: "test"
},
$element = $("#jsGrid"),
result = $element.jsGrid(gridOptions),
grid = $element.data(JSGRID_DATA_KEY);
equal(result, $element, "jquery fn returned source jQueryElement");
ok(grid instanceof Grid, "jsGrid saved to jquery data");
equal(grid.option, "test", "options provided");
});
test("destroy", function() {
var $element = $("#jsGrid"),
grid;
$element.jsGrid({});
grid = $element.data(JSGRID_DATA_KEY);
grid.destroy();
strictEqual($element.html(), "", "content is removed");
strictEqual($element.data(JSGRID_DATA_KEY), undefined, "jquery data is removed");
});
test("jquery adapter second call changes option value", function() {
var $element = $("#jsGrid"),
gridOptions = {
option: "test"
},
grid;
$element.jsGrid(gridOptions);
grid = $element.data(JSGRID_DATA_KEY);
gridOptions.option = "new test";
$element.jsGrid(gridOptions);
equal(grid, $element.data(JSGRID_DATA_KEY), "instance was not changed");
equal(grid.option, "new test", "option changed");
});
test("jquery adapter invokes jsGrid method", function() {
var methodResult = "",
$element = $("#jsGrid"),
gridOptions = {
method: function(str) {
methodResult = "test_" + str;
}
};
$element.jsGrid(gridOptions);
$element.jsGrid("method", "invoke");
equal(methodResult, "test_invoke", "method invoked");
});
test("onInit callback", function() {
var $element = $("#jsGrid"),
onInitArguments,
gridOptions = {
onInit: function(args) {
onInitArguments = args;
}
};
var grid = new Grid($element, gridOptions);
equal(onInitArguments.grid, grid, "grid instance is provided in onInit callback arguments");
});
test("controller methods are $.noop when not specified", function() {
var $element = $("#jsGrid"),
gridOptions = {
controller: {}
},
testOption;
$element.jsGrid(gridOptions);
deepEqual($element.data(JSGRID_DATA_KEY)._controller, {
loadData: $.noop,
insertItem: $.noop,
updateItem: $.noop,
deleteItem: $.noop
}, "controller has stub methods");
});
test("option method", function() {
var $element = $("#jsGrid"),
gridOptions = {
test: "value"
},
testOption;
$element.jsGrid(gridOptions);
testOption = $element.jsGrid("option", "test");
equal(testOption, "value", "read option value");
$element.jsGrid("option", "test", "new_value");
testOption = $element.jsGrid("option", "test");
equal(testOption, "new_value", "set option value");
});
test("fieldOption method", function() {
var dataLoadedCount = 0;
var $element = $("#jsGrid"),
gridOptions = {
loadMessage: "",
autoload: true,
controller: {
loadData: function() {
dataLoadedCount++;
return [{ prop1: "value1", prop2: "value2", prop3: "value3" }];
}
},
fields: [
{ name: "prop1", title: "_" }
]
};
$element.jsGrid(gridOptions);
var fieldOptionValue = $element.jsGrid("fieldOption", "prop1", "name");
equal(fieldOptionValue, "prop1", "read field option");
$element.jsGrid("fieldOption", "prop1", "name", "prop2");
equal($element.text(), "_value2", "set field option by field name");
equal(dataLoadedCount, 1, "data not reloaded on field option change");
$element.jsGrid("fieldOption", 0, "name", "prop3");
equal($element.text(), "_value3", "set field option by field index");
});
test("option changing event handlers", function() {
var $element = $("#jsGrid"),
optionChangingEventArgs,
optionChangedEventArgs,
gridOptions = {
test: "testValue",
another: "anotherValue",
onOptionChanging: function(e) {
optionChangingEventArgs = $.extend({}, e);
e.option = "another";
e.newValue = e.newValue + "_" + this.another;
},
onOptionChanged: function(e) {
optionChangedEventArgs = $.extend({}, e);
}
},
anotherOption;
$element.jsGrid(gridOptions);
$element.jsGrid("option", "test", "newTestValue");
equal(optionChangingEventArgs.option, "test", "option name is provided in args of optionChanging");
equal(optionChangingEventArgs.oldValue, "testValue", "old option value is provided in args of optionChanging");
equal(optionChangingEventArgs.newValue, "newTestValue", "new option value is provided in args of optionChanging");
anotherOption = $element.jsGrid("option", "another");
equal(anotherOption, "newTestValue_anotherValue", "option changing handler changed option and value");
equal(optionChangedEventArgs.option, "another", "option name is provided in args of optionChanged");
equal(optionChangedEventArgs.value, "newTestValue_anotherValue", "option value is provided in args of optionChanged");
});
test("common layout rendering", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {}),
$headerGrid,
$headerGridTable,
$bodyGrid,
$bodyGridTable;
ok($element.hasClass(grid.containerClass), "container class attached");
ok($element.children().eq(0).hasClass(grid.gridHeaderClass), "grid header");
ok($element.children().eq(1).hasClass(grid.gridBodyClass), "grid body");
ok($element.children().eq(2).hasClass(grid.pagerContainerClass), "pager container");
$headerGrid = $element.children().eq(0);
$headerGridTable = $headerGrid.children().first();
ok($headerGridTable.hasClass(grid.tableClass), "header table");
equal($headerGrid.find("." + grid.headerRowClass).length, 1, "header row");
equal($headerGrid.find("." + grid.filterRowClass).length, 1, "filter row");
equal($headerGrid.find("." + grid.insertRowClass).length, 1, "insert row");
ok(grid._headerRow.hasClass(grid.headerRowClass), "header row class");
ok(grid._filterRow.hasClass(grid.filterRowClass), "filter row class");
ok(grid._insertRow.hasClass(grid.insertRowClass), "insert row class");
$bodyGrid = $element.children().eq(1);
$bodyGridTable = $bodyGrid.children().first();
ok($bodyGridTable.hasClass(grid.tableClass), "body table");
equal(grid._content.parent()[0], $bodyGridTable[0], "content is tbody in body table");
equal($bodyGridTable.find("." + grid.noDataRowClass).length, 1, "no data row");
equal($bodyGridTable.text(), grid.noDataContent, "no data text");
});
test("set default options with setDefaults", function() {
jsGrid.setDefaults({
defaultOption: "test"
});
var $element = $("#jsGrid").jsGrid({});
equal($element.jsGrid("option", "defaultOption"), "test", "default option set");
});
module("loading");
test("loading with controller", function() {
var $element = $("#jsGrid"),
data = [
{ test: "test1" },
{ test: "test2" }
],
gridOptions = {
controller: {
loadData: function() {
return data;
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
equal(grid.option("data"), data, "loadData loads data");
});
test("loadData throws exception when controller method not found", function() {
var $element = $("#jsGrid");
var grid = new Grid($element);
grid._controller = {};
throws(function() {
grid.loadData();
}, /loadData/, "loadData threw an exception");
});
test("onError event should be fired on controller fail", function() {
var errorArgs,
errorFired = 0,
$element = $("#jsGrid"),
gridOptions = {
controller: {
loadData: function() {
return $.Deferred().reject({ value: 1 }, "test").promise();
}
},
onError: function(args) {
errorFired++;
errorArgs = args;
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
equal(errorFired, 1, "onError handler fired");
deepEqual(errorArgs, { grid: grid, args: [{ value: 1 }, "test"] }, "error has correct params");
});
asyncTest("autoload should call loadData after render", 1, function() {
new Grid($("#jsGrid"), {
autoload: true,
controller: {
loadData: function() {
ok(true, "autoload calls loadData on creation");
start();
return [];
}
}
});
});
test("loading filtered data", function() {
var filteredData,
loadingArgs,
loadedArgs,
$element = $("#jsGrid"),
data = [
{ field: "test" },
{ field: "test_another" },
{ field: "test_another" },
{ field: "test" }
],
gridOptions = {
filtering: true,
fields: [
{
name: "field",
filterValue: function(value) {
return "test";
}
}
],
onDataLoading: function(e) {
loadingArgs = $.extend(true, {}, e);
},
onDataLoaded: function(e) {
loadedArgs = $.extend(true, {}, e);
},
controller: {
loadData: function(filter) {
filteredData = $.grep(data, function(item) {
return item.field === filter.field;
});
return filteredData;
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
equal(loadingArgs.filter.field, "test");
equal(grid.option("data").length, 2, "filtered data loaded");
deepEqual(loadedArgs.data, filteredData);
});
asyncTest("loading indication", function() {
var timeout = 10,
stage = "initial",
$element = $("#jsGrid"),
gridOptions = {
loadIndication: true,
loadIndicationDelay: timeout,
loadMessage: "loading...",
loadIndicator: function(config) {
equal(config.message, gridOptions.loadMessage, "message provided");
ok(config.container.jquery, "grid container is provided");
return {
show: function() {
stage = "started";
},
hide: function() {
stage = "finished";
}
};
},
fields: [
{ name: "field" }
],
controller: {
loadData: function() {
var deferred = $.Deferred();
equal(stage, "initial", "initial stage");
setTimeout(function() {
equal(stage, "started", "loading started");
deferred.resolve([]);
equal(stage, "finished", "loading finished");
start();
}, timeout);
return deferred.promise();
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
});
asyncTest("loadingIndication=false should not show loading", 0, function() {
var $element = $("#jsGrid"),
timeout = 10,
gridOptions = {
loadIndication: false,
loadIndicationDelay: timeout,
loadIndicator: function() {
return {
show: function() {
ok(false, "should not call show");
},
hide: function() {
ok(false, "should not call hide");
}
};
},
fields: [
{ name: "field" }
],
controller: {
loadData: function() {
var deferred = $.Deferred();
setTimeout(function() {
deferred.resolve([]);
start();
}, timeout);
return deferred.promise();
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
});
test("search", function() {
var $element = $("#jsGrid"),
data = [
{ field: "test" },
{ field: "test_another" },
{ field: "test_another" },
{ field: "test" }
],
gridOptions = {
pageIndex: 2,
_sortField: "field",
_sortOrder: "desc",
filtering: true,
fields: [
{
name: "field",
filterValue: function(value) {
return "test";
}
}
],
controller: {
loadData: function(filter) {
var filteredData = $.grep(data, function(item) {
return item.field === filter.field;
});
return filteredData;
}
}
},
grid = new Grid($element, gridOptions);
grid.search();
equal(grid.option("data").length, 2, "data filtered");
strictEqual(grid.option("pageIndex"), 1, "pageIndex reset");
strictEqual(grid._sortField, null, "sortField reset");
strictEqual(grid._sortOrder, "asc", "sortOrder reset");
});
test("change loadStrategy on the fly", function() {
var $element = $("#jsGrid");
var gridOptions = {
controller: {
loadData: function() {
return [];
}
}
};
var grid = new Grid($element, gridOptions);
grid.option("loadStrategy", {
firstDisplayIndex: function() {
return 0;
},
lastDisplayIndex: function() {
return 1;
},
loadParams: function() {
return [];
},
finishLoad: function() {
grid.option("data", [{}]);
}
});
grid.loadData();
equal(grid.option("data").length, 1, "new load strategy is applied");
});
module("filtering");
test("filter rendering", function() {
var $element = $("#jsGrid"),
gridOptions = {
filtering: true,
fields: [
{
name: "test",
align: "right",
filtercss: "filter-class",
filterTemplate: function() {
var result = this.filterControl = $("<input>").attr("type", "text").addClass("filter-input");
return result;
}
}
]
},
grid = new Grid($element, gridOptions);
equal(grid._filterRow.find(".filter-class").length, 1, "filtercss class is attached");
equal(grid._filterRow.find(".filter-input").length, 1, "filter control rendered");
equal(grid._filterRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok(grid._filterRow.find(".filter-class").hasClass("jsgrid-align-right"), "align class is attached");
ok(grid.fields[0].filterControl.is("input[type=text]"), "filter control saved in field");
});
test("filter get/clear", function() {
var $element = $("#jsGrid"),
gridOptions = {
filtering: true,
controller: {
loadData: function() {
return [];
}
},
fields: [
{
name: "field",
filterTemplate: function() {
return this.filterControl = $("<input>").attr("type", "text");
},
filterValue: function() {
return this.filterControl.val();
}
}
]
},
grid = new Grid($element, gridOptions);
grid.fields[0].filterControl.val("test");
deepEqual(grid.getFilter(), { field: "test" }, "get filter");
grid.clearFilter();
deepEqual(grid.getFilter(), { field: "" }, "filter cleared");
equal(grid.fields[0].filterControl.val(), "", "grid field filterControl cleared");
});
test("field without filtering", function() {
var $element = $("#jsGrid"),
jsGridFieldConfig = {
filterTemplate: function() {
var result = this.filterControl = $("<input>").attr("type", "text");
return result;
},
filterValue: function(value) {
if(!arguments.length) {
return this.filterControl.val();
}
this.filterControl.val(value);
}
},
gridOptions = {
filtering: true,
fields: [
$.extend({}, jsGridFieldConfig, {
name: "field1",
filtering: false
}),
$.extend({}, jsGridFieldConfig, {
name: "field2"
})
]
},
grid = new Grid($element, gridOptions);
grid.fields[0].filterControl.val("test1");
grid.fields[1].filterControl.val("test2");
deepEqual(grid.getFilter(), { field2: "test2" }, "field with filtering=false is not included in filter");
});
test("search with filter", function() {
var $element = $("#jsGrid"),
data = [
{ field: "test" },
{ field: "test_another" },
{ field: "test_another" },
{ field: "test" }
],
gridOptions = {
fields: [
{
name: "field"
}
],
controller: {
loadData: function(filter) {
var filteredData = $.grep(data, function(item) {
return item.field === filter.field;
});
return filteredData;
}
}
},
grid = new Grid($element, gridOptions);
grid.search({ field: "test" });
equal(grid.option("data").length, 2, "data filtered");
});
test("filtering with static data should not do actual filtering", function() {
var $element = $("#jsGrid"),
gridOptions = {
filtering: true,
fields: [
{ type: "text", name: "field" }
],
data: [
{ name: "value1" },
{ name: "value2" }
]
},
grid = new Grid($element, gridOptions);
grid._filterRow.find("input").val("1");
grid.search();
equal(grid.option("data").length, 2, "data is not filtered");
});
module("nodatarow");
test("nodatarow after bind on empty array", function() {
var $element = $("#jsGrid"),
gridOptions = {},
grid = new Grid($element, gridOptions);
grid.option("data", []);
equal(grid._content.find("." + grid.noDataRowClass).length, 1, "no data row rendered");
equal(grid._content.text(), grid.noDataContent, "no data text rendered");
});
test("nodatarow customize content", function() {
var noDataMessage = "NoData Custom Content",
$element = $("#jsGrid"),
gridOptions = {
noDataContent: function() {
return noDataMessage;
}
},
grid = new Grid($element, gridOptions);
grid.option("data", []);
equal(grid._content.text(), noDataMessage, "custom noDataContent");
});
module("row rendering", {
setup: function() {
this.testData = [
{ id: 1, text: "test1" },
{ id: 2, text: "test2" },
{ id: 3, text: "test3" }
];
}
});
test("rows rendered correctly", function() {
var $element = $("#jsGrid"),
gridOptions = {
data: this.testData
},
grid = new Grid($element, gridOptions);
equal(grid._content.children().length, 3, "rows rendered");
equal(grid._content.find("." + grid.oddRowClass).length, 2, "two odd rows for 3 items");
equal(grid._content.find("." + grid.evenRowClass).length, 1, "one even row for 3 items");
});
test("custom rowClass", function() {
var $element = $("#jsGrid"),
gridOptions = {
data: this.testData,
rowClass: "custom-row-cls"
},
grid = new Grid($element, gridOptions);
equal(grid._content.find("." + grid.oddRowClass).length, 2);
equal(grid._content.find("." + grid.evenRowClass).length, 1);
equal(grid._content.find(".custom-row-cls").length, 3, "custom row class");
});
test("custom rowClass callback", function() {
var $element = $("#jsGrid"),
gridOptions = {
data: this.testData,
rowClass: function(item, index) {
return item.text;
}
},
grid = new Grid($element, gridOptions);
equal(grid._content.find("." + grid.oddRowClass).length, 2);
equal(grid._content.find("." + grid.evenRowClass).length, 1);
equal(grid._content.find(".test1").length, 1, "custom row class");
equal(grid._content.find(".test2").length, 1, "custom row class");
equal(grid._content.find(".test3").length, 1, "custom row class");
});
test("rowClick standard handler", function() {
var $element = $("#jsGrid"),
$secondRow,
grid = new Grid($element, { editing: true });
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("click", $.Event($secondRow));
equal(grid._editingRow.get(0), $secondRow.get(0), "clicked row is editingRow");
});
test("rowClick handler", function() {
var rowClickArgs,
$element = $("#jsGrid"),
$secondRow,
gridOptions = {
rowClick: function(args) {
rowClickArgs = args;
}
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("click", $.Event($secondRow));
ok(rowClickArgs.event instanceof jQuery.Event, "jquery event arg");
equal(rowClickArgs.item, this.testData[1], "item arg");
equal(rowClickArgs.itemIndex, 1, "itemIndex arg");
});
test("row selecting with selectedRowClass", function() {
var $element = $("#jsGrid"),
$secondRow,
gridOptions = {
selecting: true
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("mouseenter", $.Event($secondRow));
ok($secondRow.hasClass(grid.selectedRowClass), "mouseenter adds selectedRowClass");
$secondRow.trigger("mouseleave", $.Event($secondRow));
ok(!$secondRow.hasClass(grid.selectedRowClass), "mouseleave removes selectedRowClass");
});
test("no row selecting while selection is disabled", function() {
var $element = $("#jsGrid"),
$secondRow,
gridOptions = {
selecting: false
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("mouseenter", $.Event($secondRow));
ok(!$secondRow.hasClass(grid.selectedRowClass), "mouseenter doesn't add selectedRowClass");
});
test("refreshing and refreshed callbacks", function() {
var refreshingEventArgs,
refreshedEventArgs,
$element = $("#jsGrid"),
grid = new Grid($element, {});
grid.onRefreshing = function(e) {
refreshingEventArgs = e;
equal(grid._content.find("." + grid.oddRowClass).length, 0, "no items before refresh");
};
grid.onRefreshed = function(e) {
refreshedEventArgs = e;
equal(grid._content.find("." + grid.oddRowClass).length, 2, "items rendered after refresh");
};
grid.option("data", this.testData);
equal(refreshingEventArgs.grid, grid, "grid provided in args for refreshing event");
equal(refreshedEventArgs.grid, grid, "grid provided in args for refreshed event");
equal(grid._content.find("." + grid.oddRowClass).length, 2, "items rendered");
});
test("grid fields normalization", function() {
var CustomField = function(config) {
$.extend(true, this, config);
};
jsGrid.fields.custom = CustomField;
try {
var $element = $("#jsGrid"),
gridOptions = {
fields: [
new jsGrid.Field({
name: "text1",
title: "title1"
}),
{
name: "text2",
title: "title2"
},
{
name: "text3",
type: "custom"
}
]
},
grid = new Grid($element, gridOptions);
var field1 = grid.fields[0];
ok(field1 instanceof jsGrid.Field);
equal(field1.name, "text1", "name is set for field");
equal(field1.title, "title1", "title field");
var field2 = grid.fields[1];
ok(field2 instanceof jsGrid.Field);
equal(field2.name, "text2", "name is set for field");
equal(field2.title, "title2", "title field");
var field3 = grid.fields[2];
ok(field3 instanceof CustomField);
equal(field3.name, "text3", "name is set for field");
} finally {
delete jsGrid.fields.custom;
}
});
test("'0' itemTemplate should be rendered", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}],
fields: [
new jsGrid.Field({ name: "id", itemTemplate: function() { return 0; } })
]
});
equal(grid._bodyGrid.text(), "0", "item template is rendered");
});
test("grid field name used for header if title is not specified", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
fields: [
new jsGrid.Field({ name: "id" })
]
});
equal(grid._headerRow.text(), "id", "name is rendered in header");
});
test("grid fields header and item rendering", function() {
var $element = $("#jsGrid"),
$secondRow,
gridOptions = {
fields: [
new jsGrid.Field({
name: "text",
title: "title",
css: "cell-class",
headercss: "header-class",
align: "right"
})
]
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
equal(grid._headerRow.text(), "title", "header rendered");
equal(grid._headerRow.find("." + grid.headerCellClass).length, 1, "header cell class is attached");
equal(grid._headerRow.find(".header-class").length, 1, "headercss class is attached");
ok(grid._headerRow.find(".header-class").hasClass("jsgrid-align-right"), "align class is attached");
$secondRow = grid._content.find("." + grid.evenRowClass);
equal($secondRow.text(), "test2", "item rendered");
equal($secondRow.find(".cell-class").length, 1, "css class added to cell");
equal($secondRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok($secondRow.find(".cell-class").hasClass("jsgrid-align-right"), "align class added to cell");
});
test("grid field cellRenderer", function() {
var testItem = { text: "test" },
args;
var $grid = $("#jsGrid");
var gridOptions = {
data: [testItem],
fields: [
{
name: "text",
cellRenderer: function(value, item) {
args = {
value: value,
item: item
};
return $("<td>").addClass("custom-class").text(value);
}
}
]
};
var grid = new Grid($grid, gridOptions);
var $customCell = $grid.find(".custom-class");
equal($customCell.length, 1, "custom cell rendered");
equal($customCell.text(), "test");
deepEqual(args, { value: "test", item: testItem }, "cellRenderer args provided");
});
test("grid field 'visible' option", function() {
var $grid = $("#jsGrid");
var gridOptions = {
editing: true,
fields: [
{ name: "id", visible: false },
{ name: "test" }
]
};
var grid = new Grid($grid, gridOptions);
equal($grid.find("." + grid.noDataRowClass).children().eq(0).prop("colspan"), 1, "no data row colspan only for visible cells");
grid.option("data", this.testData);
grid.editItem(this.testData[2]);
equal($grid.find("." + grid.headerRowClass).children().length, 1, "header single cell");
equal($grid.find("." + grid.filterRowClass).children().length, 1, "filter single cell");
equal($grid.find("." + grid.insertRowClass).children().length, 1, "insert single cell");
equal($grid.find("." + grid.editRowClass).children().length, 1, "edit single cell");
equal($grid.find("." + grid.oddRowClass).eq(0).children().length, 1, "odd data row single cell");
equal($grid.find("." + grid.evenRowClass).eq(0).children().length, 1, "even data row single cell");
});
module("inserting");
test("inserting rendering", function() {
var $element = $("#jsGrid"),
gridOptions = {
inserting: true,
fields: [
{
name: "test",
align: "right",
insertcss: "insert-class",
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text").addClass("insert-input");
return result;
}
}
]
},
grid = new Grid($element, gridOptions);
equal(grid._insertRow.find(".insert-class").length, 1, "insertcss class is attached");
equal(grid._insertRow.find(".insert-input").length, 1, "insert control rendered");
equal(grid._insertRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok(grid._insertRow.find(".insert-class").hasClass("jsgrid-align-right"), "align class is attached");
ok(grid.fields[0].insertControl.is("input[type=text]"), "insert control saved in field");
});
test("field without inserting", function() {
var $element = $("#jsGrid"),
jsGridFieldConfig = {
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text");
return result;
},
insertValue: function() {
return this.insertControl.val();
}
},
gridOptions = {
inserting: true,
fields: [
$.extend({}, jsGridFieldConfig, {
name: "field1",
inserting: false
}),
$.extend({}, jsGridFieldConfig, {
name: "field2"
})
]
},
grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test1");
grid.fields[1].insertControl.val("test2");
deepEqual(grid._getInsertItem(), { field2: "test2" }, "field with inserting=false is not included in inserting item");
});
test("insert data with default location", function() {
var $element = $("#jsGrid"),
inserted = false,
insertingArgs,
insertedArgs,
gridOptions = {
inserting: true,
data: [{field: "default"}],
fields: [
{
name: "field",
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text");
return result;
},
insertValue: function() {
return this.insertControl.val();
}
}
],
onItemInserting: function(e) {
insertingArgs = $.extend(true, {}, e);
},
onItemInserted: function(e) {
insertedArgs = $.extend(true, {}, e);
},
controller: {
insertItem: function() {
inserted = true;
}
}
},
grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(insertingArgs.item.field, "test", "field is provided in inserting args");
equal(grid.option("data").length, 2, "data is inserted");
ok(inserted, "controller insertItem was called");
deepEqual(grid.option("data")[1], { field: "test" }, "correct data is inserted");
equal(insertedArgs.item.field, "test", "field is provided in inserted args");
});
test("insert data with specified insert location", function() {
var $element = $("#jsGrid"),
inserted = false,
insertingArgs,
insertedArgs,
gridOptions = {
inserting: true,
insertRowLocation: "top",
data: [{field: "default"}],
fields: [
{
name: "field",
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text");
return result;
},
insertValue: function() {
return this.insertControl.val();
}
}
],
onItemInserting: function(e) {
insertingArgs = $.extend(true, {}, e);
},
onItemInserted: function(e) {
insertedArgs = $.extend(true, {}, e);
},
controller: {
insertItem: function() {
inserted = true;
}
}
},
grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(insertingArgs.item.field, "test", "field is provided in inserting args");
equal(grid.option("data").length, 2, "data is inserted");
ok(inserted, "controller insertItem was called");
deepEqual(grid.option("data")[0], { field: "test" }, "correct data is inserted at the beginning");
equal(insertedArgs.item.field, "test", "field is provided in inserted args");
});
test("insertItem accepts item to insert", function() {
var $element = $("#jsGrid"),
itemToInsert = { field: "test" },
insertedItem,
gridOptions = {
data: [],
fields: [
{
name: "field"
}
],
controller: {
insertItem: function(item) {
insertedItem = item;
}
}
},
grid = new Grid($element, gridOptions);
grid.insertItem(itemToInsert);
deepEqual(grid.option("data")[0], itemToInsert, "data is inserted");
deepEqual(insertedItem, itemToInsert, "controller insertItem was called with correct item");
});
module("editing");
test("editing rendering", function() {
var $element = $("#jsGrid"),
$editRow,
data = [{
test: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "test",
align: "right",
editcss: "edit-class",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value).addClass("edit-input");
return result;
}
}
]
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
equal(grid._content.find("." + grid.editRowClass).length, 0, "no edit row after initial rendering");
grid.editItem(data[0]);
$editRow = grid._content.find("." + grid.editRowClass);
equal($editRow.length, 1, "edit row rendered");
equal($editRow.find(".edit-class").length, 1, "editcss class is attached");
equal($editRow.find(".edit-input").length, 1, "edit control rendered");
equal($editRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok($editRow.find(".edit-class").hasClass("jsgrid-align-right"), "align class is attached");
ok(grid.fields[0].editControl.is("input[type=text]"), "edit control saved in field");
equal(grid.fields[0].editControl.val(), "value", "edit control value");
});
test("editItem accepts row to edit", function() {
var $element = $("#jsGrid"),
$editRow,
data = [
{ test: "value" }
],
gridOptions = {
editing: true,
fields: [
{ name: "test" }
]
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
var $row = $element.find("." + grid.oddRowClass).eq(0);
grid.editItem($row);
$editRow = grid._content.find("." + grid.editRowClass);
equal($editRow.length, 1, "edit row rendered");
grid.cancelEdit();
grid.editItem($row.get(0));
$editRow = grid._content.find("." + grid.editRowClass);
equal($editRow.length, 1, "edit row rendered");
});
test("edit item", function() {
var $element = $("#jsGrid"),
editingArgs,
editingRow,
updated = false,
updatingArgs,
updatingRow,
updatedRow,
updatedArgs,
data = [{
field: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "field",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value);
return result;
},
editValue: function() {
return this.editControl.val();
}
}
],
controller: {
updateItem: function(updatingItem) {
updated = true;
}
},
onItemEditing: function(e) {
editingArgs = $.extend(true, {}, e);
editingRow = grid.rowByItem(data[0])[0];
},
onItemUpdating: function(e) {
updatingArgs = $.extend(true, {}, e);
updatingRow = grid.rowByItem(data[0])[0];
},
onItemUpdated: function(e) {
updatedArgs = $.extend(true, {}, e);
updatedRow = grid.rowByItem(data[0])[0];
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.editItem(data[0]);
deepEqual(editingArgs.item, { field: "value" }, "item before editing is provided in editing event args");
equal(editingArgs.itemIndex, 0, "itemIndex is provided in editing event args");
equal(editingArgs.row[0], editingRow, "row element is provided in editing event args");
grid.fields[0].editControl.val("new value");
grid.updateItem();
deepEqual(updatingArgs.previousItem, { field: "value" }, "item before editing is provided in updating event args");
deepEqual(updatingArgs.item, { field: "new value" }, "updating item is provided in updating event args");
equal(updatingArgs.itemIndex, 0, "itemIndex is provided in updating event args");
equal(updatingArgs.row[0], updatingRow, "row element is provided in updating event args");
ok(updated, "controller updateItem called");
deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");
equal(grid._content.find("." + grid.editRowClass).length, 0, "edit row removed");
equal(grid._content.find("." + grid.oddRowClass).length, 1, "data row rendered");
deepEqual(updatedArgs.previousItem, { field: "value" }, "item before editing is provided in updated event args");
deepEqual(updatedArgs.item, { field: "new value" }, "updated item is provided in updated event args");
equal(updatedArgs.itemIndex, 0, "itemIndex is provided in updated event args");
equal(updatedArgs.row[0], updatedRow, "row element is provided in updated event args");
});
test("failed update should not change original item", function() {
var $element = $("#jsGrid"),
data = [{
field: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "field",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value);
return result;
},
editValue: function() {
return this.editControl.val();
}
}
],
controller: {
updateItem: function(updatingItem) {
return $.Deferred().reject();
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.editItem(data[0]);
grid.fields[0].editControl.val("new value");
grid.updateItem();
deepEqual(grid.option("data")[0], { field: "value" }, "value is not updated");
});
test("cancel edit", function() {
var $element = $("#jsGrid"),
updated = false,
cancellingArgs,
cancellingRow,
data = [{
field: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "field",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value);
return result;
},
editValue: function() {
return this.editControl.val();
}
}
],
controller: {
updateData: function(updatingItem) {
updated = true;
}
},
onItemEditCancelling: function(e) {
cancellingArgs = $.extend(true, {}, e);
cancellingRow = grid.rowByItem(data[0])[0];
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.editItem(data[0]);
grid.fields[0].editControl.val("new value");
grid.cancelEdit();
deepEqual(cancellingArgs.item, { field: "value" }, "item before cancel is provided in cancelling event args");
equal(cancellingArgs.itemIndex, 0, "itemIndex is provided in cancelling event args");
equal(cancellingArgs.row[0], cancellingRow, "row element is provided in cancelling event args");
ok(!updated, "controller updateItem was not called");
deepEqual(grid.option("data")[0], { field: "value" }, "data were not updated");
equal(grid._content.find("." + grid.editRowClass).length, 0, "edit row removed");
equal(grid._content.find("." + grid.oddRowClass).length, 1, "data row restored");
});
test("updateItem accepts item to update and new item", function() {
var $element = $("#jsGrid"),
updatingItem,
data = [{
field: "value"
}],
gridOptions = {
fields: [
{ name: "field" }
],
controller: {
updateItem: function(item) {
return updatingItem = item;
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.updateItem(data[0], { field: "new value" });
deepEqual(updatingItem, { field: "new value" }, "controller updateItem called correctly");
deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");
});
test("updateItem accepts single argument - item to update", function() {
var $element = $("#jsGrid"),
updatingItem,
data = [{
field: "value"
}],
gridOptions = {
fields: [
{ name: "field" }
],
controller: {
updateItem: function(item) {
return updatingItem = item;
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
data[0].field = "new value";
grid.updateItem(data[0]);
deepEqual(updatingItem, { field: "new value" }, "controller updateItem called correctly");
deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");
});
test("editRowRenderer", function() {
var $element = $("#jsGrid"),
data = [
{ value: "test" }
],
gridOptions = {
data: data,
editing: true,
editRowRenderer: function(item, itemIndex) {
return $("<tr>").addClass("custom-edit-row").append($("<td>").text(itemIndex + ":" + item.value));
},
fields: [
{ name: "value" }
]
},
grid = new Grid($element, gridOptions);
grid.editItem(data[0]);
var $editRow = grid._content.find(".custom-edit-row");
equal($editRow.length, 1, "edit row rendered");
equal($editRow.text(), "0:test", "custom edit row renderer rendered");
});
module("deleting");
test("delete item", function() {
var $element = $("#jsGrid"),
deleted = false,
deletingArgs,
deletedArgs,
data = [{
field: "value"
}],
gridOptions = {
confirmDeleting: false,
fields: [
{ name: "field" }
],
controller: {
deleteItem: function(deletingItem) {
deleted = true;
}
},
onItemDeleting: function(e) {
deletingArgs = $.extend(true, {}, e);
},
onItemDeleted: function(e) {
deletedArgs = $.extend(true, {}, e);
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.deleteItem(data[0]);
deepEqual(deletingArgs.item, { field: "value" }, "field and value is provided in deleting event args");
equal(deletingArgs.itemIndex, 0, "itemIndex is provided in updating event args");
equal(deletingArgs.row.length, 1, "row element is provided in updating event args");
ok(deleted, "controller deleteItem called");
equal(grid.option("data").length, 0, "data row deleted");
deepEqual(deletedArgs.item, { field: "value" }, "item is provided in updating event args");
equal(deletedArgs.itemIndex, 0, "itemIndex is provided in updating event args");
equal(deletedArgs.row.length, 1, "row element is provided in updating event args");
});
test("deleteItem accepts row", function() {
var $element = $("#jsGrid"),
deletedItem,
itemToDelete = {
field: "value"
},
data = [itemToDelete],
gridOptions = {
confirmDeleting: false,
fields: [
{ name: "field" }
],
controller: {
deleteItem: function(deletingItem) {
deletedItem = deletingItem;
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
var $row = $element.find("." + grid.oddRowClass).eq(0);
grid.deleteItem($row);
deepEqual(deletedItem, itemToDelete, "controller deleteItem called correctly");
equal(grid.option("data").length, 0, "data row deleted");
});
module("paging");
test("pager is rendered if necessary", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}, {}, {}],
paging: false,
pageSize: 2
});
ok(grid._pagerContainer.is(":hidden"), "pager is hidden when paging=false");
equal(grid._pagerContainer.html(), "", "pager is not rendered when paging=false");
grid.option("paging", true);
ok(grid._pagerContainer.is(":visible"), "pager is visible when paging=true");
ok(grid._pagerContainer.html(), "pager is rendered when paging=true");
grid.option("data", [{}, {}]);
ok(grid._pagerContainer.is(":hidden"), "pager is hidden for single page");
ok(grid._pagerContainer.html(), "pager is rendered for single page when paging=true");
});
test("external pagerContainer", function() {
var $pagerContainer = $("<div>").appendTo("#qunit-fixture").hide(),
$element = $("#jsGrid");
new Grid($element, {
data: [{}, {}, {}],
pagerContainer: $pagerContainer,
paging: true,
pageSize: 2
});
ok($pagerContainer.is(":visible"), "external pager shown");
ok($pagerContainer.html(), "external pager rendered");
});
test("pager functionality", function() {
var $element = $("#jsGrid"),
pager,
pageChangedArgs,
grid = new Grid($element, {
data: [{}, {}, {}, {}, {}, {}, {}, {}, {}],
onPageChanged: function(args) {
pageChangedArgs = args;
},
paging: true,
pageSize: 2,
pageButtonCount: 3
});
equal(grid._pagesCount(), 5, "correct page count");
equal(grid.option("pageIndex"), 1, "pageIndex is initialized");
equal(grid._firstDisplayingPage, 1, "_firstDisplayingPage is initialized");
pager = grid._pagerContainer;
equal(pager.find("." + grid.currentPageClass).length, 1, "there is one current page");
ok(pager.find("." + grid.pageClass).eq(0).hasClass(grid.currentPageClass), "first page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
equal(pager.find("." + grid.pagerNavButtonClass).length, 5, "five nav buttons displayed: Fisrt Prev Next Last ...");
equal(pager.find("." + grid.pagerNavButtonInactiveClass).length, 2, "two nav buttons inactive: Fisrt Prev");
grid.openPage(2);
equal(pager.find("." + grid.currentPageClass).length, 1, "there is one current page");
ok(pager.find("." + grid.pageClass).eq(1).hasClass(grid.currentPageClass), "second page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
equal(pager.find("." + grid.pagerNavButtonClass).length, 5, "five nav buttons displayed: First Prev Next Last and ...");
equal(pageChangedArgs.pageIndex, 2, "onPageChanged callback provides pageIndex in arguments");
grid.showNextPages();
equal(grid._firstDisplayingPage, 3, "navigate by pages forward");
grid.showPrevPages();
equal(grid._firstDisplayingPage, 1, "navigate by pages backward");
grid.openPage(5);
equal(grid._firstDisplayingPage, 3, "opening next non-visible page moves first displaying page forward");
grid.openPage(2);
equal(grid._firstDisplayingPage, 2, "opening prev non-visible page moves first displaying page backward");
});
test("pager format", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}, {}, {}, {}, {}, {}],
paging: true,
pageSize: 2,
pageIndex: 2,
pageButtonCount: 1,
pagerFormat: "a {pageIndex} {first} {prev} {pages} {next} {last} {pageCount} {itemCount} z",
pagePrevText: "<",
pageNextText: ">",
pageFirstText: "<<",
pageLastText: ">>",
pageNavigatorNextText: "np",
pageNavigatorPrevText: "pp"
});
grid._firstDisplayingPage = 2;
grid._refreshPager();
equal($.trim(grid._pagerContainer.text()), "a 2 << < pp2np > >> 3 6 z", "pager text follows the format specified");
});
test("pagerRenderer", function() {
var $element = $("#jsGrid");
var pagerRendererConfig;
var pageSize = 2;
var items = [{}, {}, {}, {}, {}, {}, {}];
var pageCount = Math.ceil(items.length / pageSize);
var grid = new Grid($element, {
data: items,
paging: true,
pageSize: pageSize,
pagerRenderer: function(pagerConfig) {
pagerRendererConfig = pagerConfig;
}
});
deepEqual(pagerRendererConfig, { pageIndex: 1, pageCount: pageCount });
grid.openPage(2);
deepEqual(pagerRendererConfig, { pageIndex: 2, pageCount: pageCount });
});
test("loading by page", function() {
var $element = $("#jsGrid"),
data = [],
itemCount = 20;
for(var i = 1; i <= itemCount; i += 1) {
data.push({
value: i
});
}
var gridOptions = {
pageLoading: true,
paging: true,
pageSize: 7,
fields: [
{ name: "value" }
],
controller: {
loadData: function(filter) {
var startIndex = (filter.pageIndex - 1) * filter.pageSize,
result = data.slice(startIndex, startIndex + filter.pageSize);
return {
data: result,
itemsCount: data.length
};
}
}
};
var grid = new Grid($element, gridOptions);
grid.loadData();
var pager = grid._pagerContainer;
var gridData = grid.option("data");
equal(gridData.length, 7, "loaded one page of data");
equal(gridData[0].value, 1, "loaded right data start value");
equal(gridData[gridData.length - 1].value, 7, "loaded correct data end value");
ok(pager.find("." + grid.pageClass).eq(0).hasClass(grid.currentPageClass), "first page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
grid.openPage(3);
gridData = grid.option("data");
equal(gridData.length, 6, "loaded last page of data");
equal(gridData[0].value, 15, "loaded right data start value");
equal(gridData[gridData.length - 1].value, 20, "loaded right data end value");
ok(pager.find("." + grid.pageClass).eq(2).hasClass(grid.currentPageClass), "third page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
});
test("'openPage' method ignores indexes out of range", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}, {}],
paging: true,
pageSize: 1
});
grid.openPage(0);
equal(grid.option("pageIndex"), 1, "too small index is ignored");
grid.openPage(3);
equal(grid.option("pageIndex"), 1, "too big index is ignored");
});
module("sorting");
test("sorting", function() {
var $element = $("#jsGrid"),
gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal(grid._sortOrder, "asc", "asc sorting order for first click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 1);
equal(gridData[1].value, 2);
equal(gridData[2].value, 3);
ok($th.hasClass(grid.sortableClass));
ok($th.hasClass(grid.sortAscClass));
$th.trigger("click");
equal(grid._sortOrder, "desc", "desc sorting order for second click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 3);
equal(gridData[1].value, 2);
equal(gridData[2].value, 1);
ok(!$th.hasClass(grid.sortAscClass));
ok($th.hasClass(grid.sortDescClass));
});
test("sorting with pageLoading", function() {
var $element = $("#jsGrid"),
loadFilter,
gridOptions = {
sorting: true,
pageLoading: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
controller: {
loadData: function(filter) {
loadFilter = filter;
return {
itemsCount: 0,
data: []
};
}
},
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal(grid._sortOrder, "asc", "asc sorting order for first click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(loadFilter.sortOrder, "asc", "sort direction is provided in loadFilter");
equal(loadFilter.sortField, "value", "sort field is provided in loadFilter");
$th.trigger("click");
equal(grid._sortOrder, "desc", "desc sorting order for second click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(loadFilter.sortOrder, "desc", "sort direction is provided in loadFilter");
equal(loadFilter.sortField, "value", "sort field is provided in loadFilter");
});
test("no sorting for column with sorting = false", function() {
var $element = $("#jsGrid");
var gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorting: false }
]
};
var grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal(grid._sortField, null, "sort field is not set for the field with sorting=false");
equal(gridData[0].value, 3);
equal(gridData[1].value, 2);
equal(gridData[2].value, 1);
equal($th.hasClass(grid.sortableClass), false, "no sorting css for field with sorting=false");
equal($th.hasClass(grid.sortAscClass), false, "no sorting css for field with sorting=false");
});
test("sort accepts sorting config", function() {
var $element = $("#jsGrid"),
gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
grid.sort({ field: "value", order: "asc" });
equal(grid._sortOrder, "asc", "asc sorting order is set");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 1);
equal(gridData[1].value, 2);
equal(gridData[2].value, 3);
grid.sort({ field: 0 });
equal(grid._sortOrder, "desc", "desc sorting order for already set asc sorting");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 3);
equal(gridData[1].value, 2);
equal(gridData[2].value, 1);
grid.sort("value", "asc");
equal(grid._sortOrder, "asc", "asc sorting order is set");
equal(grid._sortField, grid.fields[0], "sort field is set");
grid.sort(0);
equal(grid._sortOrder, "desc", "desc sorting order for already set asc sorting");
equal(grid._sortField, grid.fields[0], "sort field is set");
});
test("getSorting returns current sorting", function() {
var $element = $("#jsGrid"),
gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
deepEqual(grid.getSorting(), { field: undefined, order: undefined }, "undefined field and order before sorting");
grid.sort("value");
deepEqual(grid.getSorting(), { field: "value", order: "asc" }, "current sorting returned");
});
test("sorting css attached correctly when a field is hidden", function() {
var $element = $("#jsGrid");
var gridOptions = {
sorting: true,
data: [],
fields: [
{ name: "field1", visible: false },
{ name: "field2" }
]
};
var grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal($th.hasClass(grid.sortAscClass), true, "sorting css is attached to first field");
});
module("canceling events");
test("cancel item edit", function() {
var $element = $("#jsGrid");
var data = [{}];
var gridOptions = {
editing: true,
onItemEditing: function(e) {
e.cancel = true;
},
controller: {
loadData: function() {
return data;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.loadData();
grid.editItem(data[0]);
strictEqual(grid._editingRow, null, "no editing row");
});
test("cancel controller.loadData", function() {
var $element = $("#jsGrid");
var gridOptions = {
onDataLoading: function(e) {
e.cancel = true;
},
controller: {
loadData: function() {
return [{}];
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.loadData();
equal(grid.option("data").length, 0, "no data loaded");
});
test("cancel controller.insertItem", function() {
var $element = $("#jsGrid");
var insertedItem = null;
var gridOptions = {
onItemInserting: function(e) {
e.cancel = true;
},
controller: {
insertItem: function(item) {
insertedItem = item;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.insertItem({ test: "value" });
strictEqual(insertedItem, null, "item was not inserted");
});
test("cancel controller.updateItem", function() {
var $element = $("#jsGrid");
var updatedItem = null;
var existingItem = { test: "value" };
var gridOptions = {
data: [
existingItem
],
onItemUpdating: function(e) {
e.cancel = true;
},
controller: {
updateItem: function(item) {
updatedItem = item;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.updateItem(existingItem, { test: "new_value" });
strictEqual(updatedItem, null, "item was not updated");
});
test("cancel controller.deleteItem", function() {
var $element = $("#jsGrid");
var deletingItem = { test: "value" };
var deletedItem = null;
var gridOptions = {
data: [
deletingItem
],
confirmDeleting: false,
onItemDeleting: function(e) {
e.cancel = true;
},
controller: {
deleteItem: function(item) {
deletedItem = item;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.deleteItem(deletingItem);
strictEqual(deletedItem, null, "item was not deleted");
});
module("complex properties binding");
test("rendering", function() {
var $element = $("#jsGrid");
var gridOptions = {
loadMessage: "",
data: [
{ complexProp: { prop: "test" } }
],
fields: [
{ name: "complexProp.prop", title: "" }
]
};
new Grid($element, gridOptions);
equal($element.text(), "test", "complex property value rendered");
});
test("editing", function() {
var $element = $("#jsGrid");
var gridOptions = {
editing: true,
data: [
{ complexProp: { prop: "test" } }
],
fields: [
{ type: "text", name: "complexProp.prop" }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
equal(grid.fields[0].editControl.val(), "test", "complex property value set in editor");
});
test("should not fail if property is absent", function() {
var $element = $("#jsGrid");
var gridOptions = {
loadMessage: "",
data: [
{ complexProp: { } }
],
fields: [
{ name: "complexProp.subprop.prop", title: "" }
]
};
new Grid($element, gridOptions);
equal($element.text(), "", "rendered empty value");
});
test("inserting", function() {
var $element = $("#jsGrid");
var insertingItem;
var gridOptions = {
inserting: true,
fields: [
{ type: "text", name: "complexProp.prop" }
],
onItemInserting: function(args) {
insertingItem = args.item;
}
};
var grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
deepEqual(insertingItem, { complexProp: { prop: "test" } }, "inserting item has complex properties");
});
test("filtering", function() {
var $element = $("#jsGrid");
var loadFilter;
var gridOptions = {
filtering: true,
fields: [
{ type: "text", name: "complexProp.prop" }
],
controller: {
loadData: function(filter) {
loadFilter = filter;
}
}
};
var grid = new Grid($element, gridOptions);
grid.fields[0].filterControl.val("test");
grid.search();
deepEqual(loadFilter, { complexProp: { prop: "test" } }, "filter has complex properties");
});
test("updating", function() {
var $element = $("#jsGrid");
var updatingItem;
var gridOptions = {
editing: true,
data: [
{ complexProp: { } }
],
fields: [
{ type: "text", name: "complexProp.prop" }
],
onItemUpdating: function(args) {
updatingItem = args.item;
}
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("test");
grid.updateItem();
deepEqual(updatingItem, { complexProp: { prop: "test" } }, "updating item has complex properties");
});
test("update nested prop", function() {
var $element = $("#jsGrid");
var updatingItem;
var previousItem;
var gridOptions = {
editing: true,
data: [
{ prop: { subprop1: "test1", subprop2: "test2" } }
],
fields: [
{ type: "text", name: "prop.subprop1" },
{ type: "text", name: "prop.subprop2" }
],
onItemUpdating: function(args) {
updatingItem = args.item;
}
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("new_test1");
grid.updateItem();
var expectedUpdatingItem = {
prop: {
subprop1: "new_test1",
subprop2: "test2"
}
};
deepEqual(updatingItem, expectedUpdatingItem, "updating item has nested properties");
});
test("updating deeply nested prop", function() {
var $element = $("#jsGrid");
var updatingItem;
var previousItem;
var gridOptions = {
editing: true,
data: [
{ complexProp: { subprop1: { another_prop: "test" } } }
],
fields: [
{ type: "text", name: "complexProp.subprop1.prop1" },
{ type: "text", name: "complexProp.subprop1.subprop2.prop12" }
],
onItemUpdating: function(args) {
updatingItem = $.extend(true, {}, args.item);
previousItem = $.extend(true, {}, args.previousItem);
}
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("test1");
grid.fields[1].editControl.val("test2");
grid.updateItem();
var expectedUpdatingItem = {
complexProp: {
subprop1: {
another_prop: "test",
prop1: "test1",
subprop2: { prop12: "test2" }
}
}
};
var expectedPreviousItem = {
complexProp: {
subprop1: {
another_prop: "test"
}
}
};
deepEqual(updatingItem, expectedUpdatingItem, "updating item has deeply nested properties");
deepEqual(previousItem, expectedPreviousItem, "previous item preserved correctly");
});
module("validation");
test("insertItem should call validation.validate", function() {
var $element = $("#jsGrid");
var fieldValidationRules = { test: "value" };
var validatingArgs;
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
validation: {
validate: function(args) {
validatingArgs = args;
return [];
}
},
fields: [
{ type: "text", name: "Name", validate: fieldValidationRules }
]
};
var grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
deepEqual(validatingArgs, { value: "test", item: { Name: "test" }, itemIndex: -1,
row: grid._insertRow, rules: fieldValidationRules }, "validating args is provided");
});
test("insertItem rejected when data is not valid", function() {
var $element = $("#jsGrid");
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem().done(function() {
ok(false, "insertItem should not be completed");
}).fail(function() {
ok(true, "insertItem should fail");
});
});
test("invalidClass is attached on invalid cell on inserting", function() {
var $element = $("#jsGrid");
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Id", visible: false },
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
var $insertCell = grid._insertRow.children().eq(0);
grid.insertItem();
ok($insertCell.hasClass(grid.invalidClass), "invalid class is attached");
equal($insertCell.attr("title"), "Error", "cell tooltip contains error message");
});
test("onItemInvalid callback", function() {
var $element = $("#jsGrid");
var errors = ["Error"];
var onItemInvalidCalled = 0;
var onItemInvalidArgs;
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
onItemInvalid: function(args) {
onItemInvalidCalled++;
onItemInvalidArgs = args;
},
validation: {
validate: function(args) {
return !args.value ? errors : [];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.insertItem();
equal(onItemInvalidCalled, 1, "onItemInvalid is called, when item data is invalid");
deepEqual(onItemInvalidArgs, { grid: grid, errors: [{ field: grid.fields[0], message: "Error" }],
item: { Name: "" }, itemIndex: -1, row: grid._insertRow }, "arguments provided");
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(onItemInvalidCalled, 1, "onItemInvalid was not called, when data is valid");
});
test("invalidNotify", function() {
var $element = $("#jsGrid");
var errors = ["Error"];
var invalidNotifyCalled = 0;
var invalidNotifyArgs;
var gridOptions = {
data: [],
inserting: true,
invalidNotify: function(args) {
invalidNotifyCalled++;
invalidNotifyArgs = args;
},
validation: {
validate: function(args) {
return !args.value ? errors : [];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.insertItem();
equal(invalidNotifyCalled, 1, "invalidNotify is called, when item data is invalid");
deepEqual(invalidNotifyArgs, { grid: grid, errors: [{ field: grid.fields[0], message: "Error" }],
row: grid._insertRow, item: { Name: "" }, itemIndex: -1 }, "arguments provided");
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(invalidNotifyCalled, 1, "invalidNotify was not called, when data is valid");
});
test("invalidMessage", function() {
var $element = $("#jsGrid");
var invalidMessage;
var originalAlert = window.alert;
window.alert = function(message) {
invalidMessage = message;
};
try {
Grid.prototype.invalidMessage = "InvalidTest";
Grid.prototype.invalidNotify({ errors: [{ message: "Message1" }, { message: "Message2" }] });
var expectedInvalidMessage = ["InvalidTest", "Message1", "Message2"].join("\n");
equal(invalidMessage, expectedInvalidMessage, "message contains invalidMessage and field error messages");
} finally {
window.alert = originalAlert;
}
});
test("updateItem should call validation.validate", function() {
var $element = $("#jsGrid");
var validatingArgs;
var gridOptions = {
data: [{ Name: "" }],
editing: true,
invalidNotify: $.noop,
validation: {
validate: function(args) {
validatingArgs = args;
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", validate: "required" }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("test");
grid.updateItem();
deepEqual(validatingArgs, { value: "test", item: { Name: "test" }, itemIndex: 0,
row: grid._getEditRow(), rules: "required" }, "validating args is provided");
});
test("invalidClass is attached on invalid cell on updating", function() {
var $element = $("#jsGrid");
var gridOptions = {
data: [{}],
editing: true,
invalidNotify: $.noop,
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
var $editCell = grid._getEditRow().children().eq(0);
grid.updateItem();
ok($editCell.hasClass(grid.invalidClass), "invalid class is attached");
equal($editCell.attr("title"), "Error", "cell tooltip contains error message");
});
test("validation should ignore not editable fields", function() {
var invalidNotifyCalled = 0;
var $element = $("#jsGrid");
var validatingArgs;
var gridOptions = {
data: [{ Name: "" }],
editing: true,
invalidNotify: function() {
invalidNotifyCalled++;
},
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", editing: false, validate: "required" }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.updateItem();
equal(invalidNotifyCalled, 0, "data is valid");
});
module("api");
test("reset method should go the first page when pageLoading is truned on", function() {
var items = [{ Name: "1" }, { Name: "2" }];
var $element = $("#jsGrid");
var gridOptions = {
paging: true,
pageSize: 1,
pageLoading: true,
autoload: true,
controller: {
loadData: function(args) {
return {
data: [items[args.pageIndex - 1]],
itemsCount: items.length
};
}
},
fields: [
{ type: "text", name: "Name" }
]
};
var grid = new Grid($element, gridOptions);
grid.openPage(2);
grid.reset();
equal(grid._bodyGrid.text(), "1", "grid content reset");
});
module("i18n");
test("set locale by name", function() {
jsGrid.locales.my_lang = {
grid: {
test: "test_text"
}
};
jsGrid.locale("my_lang");
var $element = $("#jsGrid").jsGrid({});
equal($element.jsGrid("option", "test"), "test_text", "option localized");
});
test("set locale by config", function() {
jsGrid.locale( {
grid: {
test: "test_text"
}
});
var $element = $("#jsGrid").jsGrid({});
equal($element.jsGrid("option", "test"), "test_text", "option localized");
});
test("locale throws exception for unknown locale", function() {
throws(function() {
jsGrid.locale("unknown_lang");
}, /unknown_lang/, "locale threw an exception");
});
module("controller promise");
asyncTest("should support jQuery promise success callback", 1, function() {
var data = [];
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
var d = $.Deferred();
setTimeout(function() {
d.resolve(data);
start();
});
return d.promise();
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.done(function(result) {
equal(result, data, "data provided to done callback");
});
});
asyncTest("should support jQuery promise fail callback", 1, function() {
var failArgs = {};
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
var d = $.Deferred();
setTimeout(function() {
d.reject(failArgs);
start();
});
return d.promise();
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.fail(function(result) {
equal(result, failArgs, "fail args provided to fail callback");
});
});
asyncTest("should support JS promise success callback", 1, function() {
if(typeof Promise === "undefined") {
ok(true, "Promise not supported");
start();
return;
}
var data = [];
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(data);
start();
});
});
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.done(function(result) {
equal(result, data, "data provided to done callback");
});
});
asyncTest("should support JS promise fail callback", 1, function() {
if(typeof Promise === "undefined") {
ok(true, "Promise not supported");
start();
return;
}
var failArgs = {};
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(failArgs);
start();
});
});
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.fail(function(result) {
equal(result, failArgs, "fail args provided to fail callback");
});
});
test("should support non-promise result", 1, function() {
var data = [];
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
return data;
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.done(function(result) {
equal(result, data, "data provided to done callback");
});
});
module("renderTemplate");
test("should pass undefined and null arguments to the renderer", function() {
var rendererArgs;
var rendererContext;
var context = {};
var renderer = function() {
rendererArgs = arguments;
rendererContext = this;
};
Grid.prototype.renderTemplate(renderer, context, { arg1: undefined, arg2: null, arg3: "test" });
equal(rendererArgs.length, 3);
strictEqual(rendererArgs[0], undefined, "undefined passed");
strictEqual(rendererArgs[1], null, "null passed");
strictEqual(rendererArgs[2], "test", "null passed");
strictEqual(rendererContext, context, "context is preserved");
});
module("Data Export", {
setup: function() {
this.exportConfig = {};
this.exportConfig.type = "csv";
this.exportConfig.subset = "all";
this.exportConfig.delimiter = "|";
this.exportConfig.includeHeaders = true;
this.exportConfig.encapsulate = true;
this.element = $("#jsGrid");
this.gridOptions = {
width: "100%",
height: "400px",
inserting: true,
editing: true,
sorting: true,
paging: true,
pageSize: 2,
data: [
{ "Name": "Otto Clay", "Country": 1, "Married": false },
{ "Name": "Connor Johnston", "Country": 2, "Married": true },
{ "Name": "Lacey Hess", "Country": 2, "Married": false },
{ "Name": "Timothy Henson", "Country": 1, "Married": true }
],
fields: [
{ name: "Name", type: "text", width: 150, validate: "required" },
{ name: "Country", type: "select", items: [{ Name: "United States", Id: 1 },{ Name: "Canada", Id: 2 }], valueField: "Id", textField: "Name" },
{ name: "Married", type: "checkbox", title: "Is Married", sorting: false },
{ type: "control" }
]
}
}
});
/* Base Choice Criteria
type: csv
subset: all
delimiter: |
includeHeaders: true
encapsulate: true
*/
test("Should export data: Base Choice",function(){
var grid = new Grid($(this.element), this.gridOptions);
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
expected += '"Lacey Hess"|"Canada"|"false"\r\n';
expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV configured to Base Choice Criteria -- Check Source");
});
test("Should export data: defaults = BCC",function(){
var grid = new Grid($(this.element), this.gridOptions);
var data = grid.exportData();
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
expected += '"Lacey Hess"|"Canada"|"false"\r\n';
expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV with all defaults -- Should be equal to Base Choice");
});
test("Should export data: subset=visible", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.subset = "visible";
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
//expected += '"Lacey Hess"|"Canada"|"false"\r\n';
//expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV of visible records");
});
test("Should export data: delimiter=;", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.delimiter = ";";
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name";"Country";"Is Married"\r\n';
expected += '"Otto Clay";"United States";"false"\r\n';
expected += '"Connor Johnston";"Canada";"true"\r\n';
expected += '"Lacey Hess";"Canada";"false"\r\n';
expected += '"Timothy Henson";"United States";"true"\r\n';
equal(data, expected, "Output CSV with non-default delimiter");
});
test("Should export data: headers=false", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.includeHeaders = false;
var data = grid.exportData(this.exportConfig);
var expected = "";
//expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
expected += '"Lacey Hess"|"Canada"|"false"\r\n';
expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV without Headers");
});
test("Should export data: encapsulate=false", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.encapsulate = false;
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += 'Name|Country|Is Married\r\n';
expected += 'Otto Clay|United States|false\r\n';
expected += 'Connor Johnston|Canada|true\r\n';
expected += 'Lacey Hess|Canada|false\r\n';
expected += 'Timothy Henson|United States|true\r\n';
equal(data, expected, "Output CSV without encapsulation");
});
test("Should export filtered data", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig['filter'] = function(item){
if (item["Name"].indexOf("O") === 0)
return true
};
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
//expected += '"Connor Johnston"|"Canada"|"true"\r\n';
//expected += '"Lacey Hess"|"Canada"|"false"\r\n';
//expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV filtered to show names starting with O");
});
test("Should export data: transformed value", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig['transforms'] = {};
this.exportConfig.transforms['Married'] = function(value){
if (value === true) return "Yes"
if (value === false) return "No"
};
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"No"\r\n';
expected += '"Connor Johnston"|"Canada"|"Yes"\r\n';
expected += '"Lacey Hess"|"Canada"|"No"\r\n';
expected += '"Timothy Henson"|"United States"|"Yes"\r\n';
equal(data, expected, "Output CSV column value transformed properly");
});
});
<MSG> Core: Fix noDataContent cell class is missing
Fixes #385
<DFF> @@ -688,6 +688,7 @@ $(function() {
grid.option("data", []);
equal(grid._content.find("." + grid.noDataRowClass).length, 1, "no data row rendered");
+ equal(grid._content.find("." + grid.cellClass).length, 1, "grid cell class attached");
equal(grid._content.text(), grid.noDataContent, "no data text rendered");
});
@@ -703,6 +704,7 @@ $(function() {
grid.option("data", []);
+ equal(grid._content.find("." + grid.cellClass).length, 1, "grid cell class attached");
equal(grid._content.text(), noDataMessage, "custom noDataContent");
});
| 2 | Core: Fix noDataContent cell class is missing | 0 | .js | tests | mit | tabalinas/jsgrid |
10059722 | <NME> jsgrid.tests.js
<BEF> $(function() {
var Grid = jsGrid.Grid,
JSGRID = "JSGrid",
JSGRID_DATA_KEY = JSGRID;
Grid.prototype.updateOnResize = false;
module("basic");
test("default creation", function() {
var gridOptions = {
simpleOption: "test",
complexOption: {
a: "subtest",
b: 1,
c: {}
}
},
grid = new Grid("#jsGrid", gridOptions);
equal(grid._container[0], $("#jsGrid")[0], "container saved");
equal(grid.simpleOption, "test", "primitive option extended");
equal(grid.complexOption, gridOptions.complexOption, "non-primitive option extended");
});
test("jquery adapter creation", function() {
var gridOptions = {
option: "test"
},
$element = $("#jsGrid"),
result = $element.jsGrid(gridOptions),
grid = $element.data(JSGRID_DATA_KEY);
equal(result, $element, "jquery fn returned source jQueryElement");
ok(grid instanceof Grid, "jsGrid saved to jquery data");
equal(grid.option, "test", "options provided");
});
test("destroy", function() {
var $element = $("#jsGrid"),
grid;
$element.jsGrid({});
grid = $element.data(JSGRID_DATA_KEY);
grid.destroy();
strictEqual($element.html(), "", "content is removed");
strictEqual($element.data(JSGRID_DATA_KEY), undefined, "jquery data is removed");
});
test("jquery adapter second call changes option value", function() {
var $element = $("#jsGrid"),
gridOptions = {
option: "test"
},
grid;
$element.jsGrid(gridOptions);
grid = $element.data(JSGRID_DATA_KEY);
gridOptions.option = "new test";
$element.jsGrid(gridOptions);
equal(grid, $element.data(JSGRID_DATA_KEY), "instance was not changed");
equal(grid.option, "new test", "option changed");
});
test("jquery adapter invokes jsGrid method", function() {
var methodResult = "",
$element = $("#jsGrid"),
gridOptions = {
method: function(str) {
methodResult = "test_" + str;
}
};
$element.jsGrid(gridOptions);
$element.jsGrid("method", "invoke");
equal(methodResult, "test_invoke", "method invoked");
});
test("onInit callback", function() {
var $element = $("#jsGrid"),
onInitArguments,
gridOptions = {
onInit: function(args) {
onInitArguments = args;
}
};
var grid = new Grid($element, gridOptions);
equal(onInitArguments.grid, grid, "grid instance is provided in onInit callback arguments");
});
test("controller methods are $.noop when not specified", function() {
var $element = $("#jsGrid"),
gridOptions = {
controller: {}
},
testOption;
$element.jsGrid(gridOptions);
deepEqual($element.data(JSGRID_DATA_KEY)._controller, {
loadData: $.noop,
insertItem: $.noop,
updateItem: $.noop,
deleteItem: $.noop
}, "controller has stub methods");
});
test("option method", function() {
var $element = $("#jsGrid"),
gridOptions = {
test: "value"
},
testOption;
$element.jsGrid(gridOptions);
testOption = $element.jsGrid("option", "test");
equal(testOption, "value", "read option value");
$element.jsGrid("option", "test", "new_value");
testOption = $element.jsGrid("option", "test");
equal(testOption, "new_value", "set option value");
});
test("fieldOption method", function() {
var dataLoadedCount = 0;
var $element = $("#jsGrid"),
gridOptions = {
loadMessage: "",
autoload: true,
controller: {
loadData: function() {
dataLoadedCount++;
return [{ prop1: "value1", prop2: "value2", prop3: "value3" }];
}
},
fields: [
{ name: "prop1", title: "_" }
]
};
$element.jsGrid(gridOptions);
var fieldOptionValue = $element.jsGrid("fieldOption", "prop1", "name");
equal(fieldOptionValue, "prop1", "read field option");
$element.jsGrid("fieldOption", "prop1", "name", "prop2");
equal($element.text(), "_value2", "set field option by field name");
equal(dataLoadedCount, 1, "data not reloaded on field option change");
$element.jsGrid("fieldOption", 0, "name", "prop3");
equal($element.text(), "_value3", "set field option by field index");
});
test("option changing event handlers", function() {
var $element = $("#jsGrid"),
optionChangingEventArgs,
optionChangedEventArgs,
gridOptions = {
test: "testValue",
another: "anotherValue",
onOptionChanging: function(e) {
optionChangingEventArgs = $.extend({}, e);
e.option = "another";
e.newValue = e.newValue + "_" + this.another;
},
onOptionChanged: function(e) {
optionChangedEventArgs = $.extend({}, e);
}
},
anotherOption;
$element.jsGrid(gridOptions);
$element.jsGrid("option", "test", "newTestValue");
equal(optionChangingEventArgs.option, "test", "option name is provided in args of optionChanging");
equal(optionChangingEventArgs.oldValue, "testValue", "old option value is provided in args of optionChanging");
equal(optionChangingEventArgs.newValue, "newTestValue", "new option value is provided in args of optionChanging");
anotherOption = $element.jsGrid("option", "another");
equal(anotherOption, "newTestValue_anotherValue", "option changing handler changed option and value");
equal(optionChangedEventArgs.option, "another", "option name is provided in args of optionChanged");
equal(optionChangedEventArgs.value, "newTestValue_anotherValue", "option value is provided in args of optionChanged");
});
test("common layout rendering", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {}),
$headerGrid,
$headerGridTable,
$bodyGrid,
$bodyGridTable;
ok($element.hasClass(grid.containerClass), "container class attached");
ok($element.children().eq(0).hasClass(grid.gridHeaderClass), "grid header");
ok($element.children().eq(1).hasClass(grid.gridBodyClass), "grid body");
ok($element.children().eq(2).hasClass(grid.pagerContainerClass), "pager container");
$headerGrid = $element.children().eq(0);
$headerGridTable = $headerGrid.children().first();
ok($headerGridTable.hasClass(grid.tableClass), "header table");
equal($headerGrid.find("." + grid.headerRowClass).length, 1, "header row");
equal($headerGrid.find("." + grid.filterRowClass).length, 1, "filter row");
equal($headerGrid.find("." + grid.insertRowClass).length, 1, "insert row");
ok(grid._headerRow.hasClass(grid.headerRowClass), "header row class");
ok(grid._filterRow.hasClass(grid.filterRowClass), "filter row class");
ok(grid._insertRow.hasClass(grid.insertRowClass), "insert row class");
$bodyGrid = $element.children().eq(1);
$bodyGridTable = $bodyGrid.children().first();
ok($bodyGridTable.hasClass(grid.tableClass), "body table");
equal(grid._content.parent()[0], $bodyGridTable[0], "content is tbody in body table");
equal($bodyGridTable.find("." + grid.noDataRowClass).length, 1, "no data row");
equal($bodyGridTable.text(), grid.noDataContent, "no data text");
});
test("set default options with setDefaults", function() {
jsGrid.setDefaults({
defaultOption: "test"
});
var $element = $("#jsGrid").jsGrid({});
equal($element.jsGrid("option", "defaultOption"), "test", "default option set");
});
module("loading");
test("loading with controller", function() {
var $element = $("#jsGrid"),
data = [
{ test: "test1" },
{ test: "test2" }
],
gridOptions = {
controller: {
loadData: function() {
return data;
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
equal(grid.option("data"), data, "loadData loads data");
});
test("loadData throws exception when controller method not found", function() {
var $element = $("#jsGrid");
var grid = new Grid($element);
grid._controller = {};
throws(function() {
grid.loadData();
}, /loadData/, "loadData threw an exception");
});
test("onError event should be fired on controller fail", function() {
var errorArgs,
errorFired = 0,
$element = $("#jsGrid"),
gridOptions = {
controller: {
loadData: function() {
return $.Deferred().reject({ value: 1 }, "test").promise();
}
},
onError: function(args) {
errorFired++;
errorArgs = args;
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
equal(errorFired, 1, "onError handler fired");
deepEqual(errorArgs, { grid: grid, args: [{ value: 1 }, "test"] }, "error has correct params");
});
asyncTest("autoload should call loadData after render", 1, function() {
new Grid($("#jsGrid"), {
autoload: true,
controller: {
loadData: function() {
ok(true, "autoload calls loadData on creation");
start();
return [];
}
}
});
});
test("loading filtered data", function() {
var filteredData,
loadingArgs,
loadedArgs,
$element = $("#jsGrid"),
data = [
{ field: "test" },
{ field: "test_another" },
{ field: "test_another" },
{ field: "test" }
],
gridOptions = {
filtering: true,
fields: [
{
name: "field",
filterValue: function(value) {
return "test";
}
}
],
onDataLoading: function(e) {
loadingArgs = $.extend(true, {}, e);
},
onDataLoaded: function(e) {
loadedArgs = $.extend(true, {}, e);
},
controller: {
loadData: function(filter) {
filteredData = $.grep(data, function(item) {
return item.field === filter.field;
});
return filteredData;
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
equal(loadingArgs.filter.field, "test");
equal(grid.option("data").length, 2, "filtered data loaded");
deepEqual(loadedArgs.data, filteredData);
});
asyncTest("loading indication", function() {
var timeout = 10,
stage = "initial",
$element = $("#jsGrid"),
gridOptions = {
loadIndication: true,
loadIndicationDelay: timeout,
loadMessage: "loading...",
loadIndicator: function(config) {
equal(config.message, gridOptions.loadMessage, "message provided");
ok(config.container.jquery, "grid container is provided");
return {
show: function() {
stage = "started";
},
hide: function() {
stage = "finished";
}
};
},
fields: [
{ name: "field" }
],
controller: {
loadData: function() {
var deferred = $.Deferred();
equal(stage, "initial", "initial stage");
setTimeout(function() {
equal(stage, "started", "loading started");
deferred.resolve([]);
equal(stage, "finished", "loading finished");
start();
}, timeout);
return deferred.promise();
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
});
asyncTest("loadingIndication=false should not show loading", 0, function() {
var $element = $("#jsGrid"),
timeout = 10,
gridOptions = {
loadIndication: false,
loadIndicationDelay: timeout,
loadIndicator: function() {
return {
show: function() {
ok(false, "should not call show");
},
hide: function() {
ok(false, "should not call hide");
}
};
},
fields: [
{ name: "field" }
],
controller: {
loadData: function() {
var deferred = $.Deferred();
setTimeout(function() {
deferred.resolve([]);
start();
}, timeout);
return deferred.promise();
}
}
},
grid = new Grid($element, gridOptions);
grid.loadData();
});
test("search", function() {
var $element = $("#jsGrid"),
data = [
{ field: "test" },
{ field: "test_another" },
{ field: "test_another" },
{ field: "test" }
],
gridOptions = {
pageIndex: 2,
_sortField: "field",
_sortOrder: "desc",
filtering: true,
fields: [
{
name: "field",
filterValue: function(value) {
return "test";
}
}
],
controller: {
loadData: function(filter) {
var filteredData = $.grep(data, function(item) {
return item.field === filter.field;
});
return filteredData;
}
}
},
grid = new Grid($element, gridOptions);
grid.search();
equal(grid.option("data").length, 2, "data filtered");
strictEqual(grid.option("pageIndex"), 1, "pageIndex reset");
strictEqual(grid._sortField, null, "sortField reset");
strictEqual(grid._sortOrder, "asc", "sortOrder reset");
});
test("change loadStrategy on the fly", function() {
var $element = $("#jsGrid");
var gridOptions = {
controller: {
loadData: function() {
return [];
}
}
};
var grid = new Grid($element, gridOptions);
grid.option("loadStrategy", {
firstDisplayIndex: function() {
return 0;
},
lastDisplayIndex: function() {
return 1;
},
loadParams: function() {
return [];
},
finishLoad: function() {
grid.option("data", [{}]);
}
});
grid.loadData();
equal(grid.option("data").length, 1, "new load strategy is applied");
});
module("filtering");
test("filter rendering", function() {
var $element = $("#jsGrid"),
gridOptions = {
filtering: true,
fields: [
{
name: "test",
align: "right",
filtercss: "filter-class",
filterTemplate: function() {
var result = this.filterControl = $("<input>").attr("type", "text").addClass("filter-input");
return result;
}
}
]
},
grid = new Grid($element, gridOptions);
equal(grid._filterRow.find(".filter-class").length, 1, "filtercss class is attached");
equal(grid._filterRow.find(".filter-input").length, 1, "filter control rendered");
equal(grid._filterRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok(grid._filterRow.find(".filter-class").hasClass("jsgrid-align-right"), "align class is attached");
ok(grid.fields[0].filterControl.is("input[type=text]"), "filter control saved in field");
});
test("filter get/clear", function() {
var $element = $("#jsGrid"),
gridOptions = {
filtering: true,
controller: {
loadData: function() {
return [];
}
},
fields: [
{
name: "field",
filterTemplate: function() {
return this.filterControl = $("<input>").attr("type", "text");
},
filterValue: function() {
return this.filterControl.val();
}
}
]
},
grid = new Grid($element, gridOptions);
grid.fields[0].filterControl.val("test");
deepEqual(grid.getFilter(), { field: "test" }, "get filter");
grid.clearFilter();
deepEqual(grid.getFilter(), { field: "" }, "filter cleared");
equal(grid.fields[0].filterControl.val(), "", "grid field filterControl cleared");
});
test("field without filtering", function() {
var $element = $("#jsGrid"),
jsGridFieldConfig = {
filterTemplate: function() {
var result = this.filterControl = $("<input>").attr("type", "text");
return result;
},
filterValue: function(value) {
if(!arguments.length) {
return this.filterControl.val();
}
this.filterControl.val(value);
}
},
gridOptions = {
filtering: true,
fields: [
$.extend({}, jsGridFieldConfig, {
name: "field1",
filtering: false
}),
$.extend({}, jsGridFieldConfig, {
name: "field2"
})
]
},
grid = new Grid($element, gridOptions);
grid.fields[0].filterControl.val("test1");
grid.fields[1].filterControl.val("test2");
deepEqual(grid.getFilter(), { field2: "test2" }, "field with filtering=false is not included in filter");
});
test("search with filter", function() {
var $element = $("#jsGrid"),
data = [
{ field: "test" },
{ field: "test_another" },
{ field: "test_another" },
{ field: "test" }
],
gridOptions = {
fields: [
{
name: "field"
}
],
controller: {
loadData: function(filter) {
var filteredData = $.grep(data, function(item) {
return item.field === filter.field;
});
return filteredData;
}
}
},
grid = new Grid($element, gridOptions);
grid.search({ field: "test" });
equal(grid.option("data").length, 2, "data filtered");
});
test("filtering with static data should not do actual filtering", function() {
var $element = $("#jsGrid"),
gridOptions = {
filtering: true,
fields: [
{ type: "text", name: "field" }
],
data: [
{ name: "value1" },
{ name: "value2" }
]
},
grid = new Grid($element, gridOptions);
grid._filterRow.find("input").val("1");
grid.search();
equal(grid.option("data").length, 2, "data is not filtered");
});
module("nodatarow");
test("nodatarow after bind on empty array", function() {
var $element = $("#jsGrid"),
gridOptions = {},
grid = new Grid($element, gridOptions);
grid.option("data", []);
equal(grid._content.find("." + grid.noDataRowClass).length, 1, "no data row rendered");
equal(grid._content.text(), grid.noDataContent, "no data text rendered");
});
test("nodatarow customize content", function() {
var noDataMessage = "NoData Custom Content",
$element = $("#jsGrid"),
gridOptions = {
noDataContent: function() {
return noDataMessage;
}
},
grid = new Grid($element, gridOptions);
grid.option("data", []);
equal(grid._content.text(), noDataMessage, "custom noDataContent");
});
module("row rendering", {
setup: function() {
this.testData = [
{ id: 1, text: "test1" },
{ id: 2, text: "test2" },
{ id: 3, text: "test3" }
];
}
});
test("rows rendered correctly", function() {
var $element = $("#jsGrid"),
gridOptions = {
data: this.testData
},
grid = new Grid($element, gridOptions);
equal(grid._content.children().length, 3, "rows rendered");
equal(grid._content.find("." + grid.oddRowClass).length, 2, "two odd rows for 3 items");
equal(grid._content.find("." + grid.evenRowClass).length, 1, "one even row for 3 items");
});
test("custom rowClass", function() {
var $element = $("#jsGrid"),
gridOptions = {
data: this.testData,
rowClass: "custom-row-cls"
},
grid = new Grid($element, gridOptions);
equal(grid._content.find("." + grid.oddRowClass).length, 2);
equal(grid._content.find("." + grid.evenRowClass).length, 1);
equal(grid._content.find(".custom-row-cls").length, 3, "custom row class");
});
test("custom rowClass callback", function() {
var $element = $("#jsGrid"),
gridOptions = {
data: this.testData,
rowClass: function(item, index) {
return item.text;
}
},
grid = new Grid($element, gridOptions);
equal(grid._content.find("." + grid.oddRowClass).length, 2);
equal(grid._content.find("." + grid.evenRowClass).length, 1);
equal(grid._content.find(".test1").length, 1, "custom row class");
equal(grid._content.find(".test2").length, 1, "custom row class");
equal(grid._content.find(".test3").length, 1, "custom row class");
});
test("rowClick standard handler", function() {
var $element = $("#jsGrid"),
$secondRow,
grid = new Grid($element, { editing: true });
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("click", $.Event($secondRow));
equal(grid._editingRow.get(0), $secondRow.get(0), "clicked row is editingRow");
});
test("rowClick handler", function() {
var rowClickArgs,
$element = $("#jsGrid"),
$secondRow,
gridOptions = {
rowClick: function(args) {
rowClickArgs = args;
}
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("click", $.Event($secondRow));
ok(rowClickArgs.event instanceof jQuery.Event, "jquery event arg");
equal(rowClickArgs.item, this.testData[1], "item arg");
equal(rowClickArgs.itemIndex, 1, "itemIndex arg");
});
test("row selecting with selectedRowClass", function() {
var $element = $("#jsGrid"),
$secondRow,
gridOptions = {
selecting: true
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("mouseenter", $.Event($secondRow));
ok($secondRow.hasClass(grid.selectedRowClass), "mouseenter adds selectedRowClass");
$secondRow.trigger("mouseleave", $.Event($secondRow));
ok(!$secondRow.hasClass(grid.selectedRowClass), "mouseleave removes selectedRowClass");
});
test("no row selecting while selection is disabled", function() {
var $element = $("#jsGrid"),
$secondRow,
gridOptions = {
selecting: false
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
$secondRow = grid._content.find("." + grid.evenRowClass);
$secondRow.trigger("mouseenter", $.Event($secondRow));
ok(!$secondRow.hasClass(grid.selectedRowClass), "mouseenter doesn't add selectedRowClass");
});
test("refreshing and refreshed callbacks", function() {
var refreshingEventArgs,
refreshedEventArgs,
$element = $("#jsGrid"),
grid = new Grid($element, {});
grid.onRefreshing = function(e) {
refreshingEventArgs = e;
equal(grid._content.find("." + grid.oddRowClass).length, 0, "no items before refresh");
};
grid.onRefreshed = function(e) {
refreshedEventArgs = e;
equal(grid._content.find("." + grid.oddRowClass).length, 2, "items rendered after refresh");
};
grid.option("data", this.testData);
equal(refreshingEventArgs.grid, grid, "grid provided in args for refreshing event");
equal(refreshedEventArgs.grid, grid, "grid provided in args for refreshed event");
equal(grid._content.find("." + grid.oddRowClass).length, 2, "items rendered");
});
test("grid fields normalization", function() {
var CustomField = function(config) {
$.extend(true, this, config);
};
jsGrid.fields.custom = CustomField;
try {
var $element = $("#jsGrid"),
gridOptions = {
fields: [
new jsGrid.Field({
name: "text1",
title: "title1"
}),
{
name: "text2",
title: "title2"
},
{
name: "text3",
type: "custom"
}
]
},
grid = new Grid($element, gridOptions);
var field1 = grid.fields[0];
ok(field1 instanceof jsGrid.Field);
equal(field1.name, "text1", "name is set for field");
equal(field1.title, "title1", "title field");
var field2 = grid.fields[1];
ok(field2 instanceof jsGrid.Field);
equal(field2.name, "text2", "name is set for field");
equal(field2.title, "title2", "title field");
var field3 = grid.fields[2];
ok(field3 instanceof CustomField);
equal(field3.name, "text3", "name is set for field");
} finally {
delete jsGrid.fields.custom;
}
});
test("'0' itemTemplate should be rendered", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}],
fields: [
new jsGrid.Field({ name: "id", itemTemplate: function() { return 0; } })
]
});
equal(grid._bodyGrid.text(), "0", "item template is rendered");
});
test("grid field name used for header if title is not specified", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
fields: [
new jsGrid.Field({ name: "id" })
]
});
equal(grid._headerRow.text(), "id", "name is rendered in header");
});
test("grid fields header and item rendering", function() {
var $element = $("#jsGrid"),
$secondRow,
gridOptions = {
fields: [
new jsGrid.Field({
name: "text",
title: "title",
css: "cell-class",
headercss: "header-class",
align: "right"
})
]
},
grid = new Grid($element, gridOptions);
grid.option("data", this.testData);
equal(grid._headerRow.text(), "title", "header rendered");
equal(grid._headerRow.find("." + grid.headerCellClass).length, 1, "header cell class is attached");
equal(grid._headerRow.find(".header-class").length, 1, "headercss class is attached");
ok(grid._headerRow.find(".header-class").hasClass("jsgrid-align-right"), "align class is attached");
$secondRow = grid._content.find("." + grid.evenRowClass);
equal($secondRow.text(), "test2", "item rendered");
equal($secondRow.find(".cell-class").length, 1, "css class added to cell");
equal($secondRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok($secondRow.find(".cell-class").hasClass("jsgrid-align-right"), "align class added to cell");
});
test("grid field cellRenderer", function() {
var testItem = { text: "test" },
args;
var $grid = $("#jsGrid");
var gridOptions = {
data: [testItem],
fields: [
{
name: "text",
cellRenderer: function(value, item) {
args = {
value: value,
item: item
};
return $("<td>").addClass("custom-class").text(value);
}
}
]
};
var grid = new Grid($grid, gridOptions);
var $customCell = $grid.find(".custom-class");
equal($customCell.length, 1, "custom cell rendered");
equal($customCell.text(), "test");
deepEqual(args, { value: "test", item: testItem }, "cellRenderer args provided");
});
test("grid field 'visible' option", function() {
var $grid = $("#jsGrid");
var gridOptions = {
editing: true,
fields: [
{ name: "id", visible: false },
{ name: "test" }
]
};
var grid = new Grid($grid, gridOptions);
equal($grid.find("." + grid.noDataRowClass).children().eq(0).prop("colspan"), 1, "no data row colspan only for visible cells");
grid.option("data", this.testData);
grid.editItem(this.testData[2]);
equal($grid.find("." + grid.headerRowClass).children().length, 1, "header single cell");
equal($grid.find("." + grid.filterRowClass).children().length, 1, "filter single cell");
equal($grid.find("." + grid.insertRowClass).children().length, 1, "insert single cell");
equal($grid.find("." + grid.editRowClass).children().length, 1, "edit single cell");
equal($grid.find("." + grid.oddRowClass).eq(0).children().length, 1, "odd data row single cell");
equal($grid.find("." + grid.evenRowClass).eq(0).children().length, 1, "even data row single cell");
});
module("inserting");
test("inserting rendering", function() {
var $element = $("#jsGrid"),
gridOptions = {
inserting: true,
fields: [
{
name: "test",
align: "right",
insertcss: "insert-class",
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text").addClass("insert-input");
return result;
}
}
]
},
grid = new Grid($element, gridOptions);
equal(grid._insertRow.find(".insert-class").length, 1, "insertcss class is attached");
equal(grid._insertRow.find(".insert-input").length, 1, "insert control rendered");
equal(grid._insertRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok(grid._insertRow.find(".insert-class").hasClass("jsgrid-align-right"), "align class is attached");
ok(grid.fields[0].insertControl.is("input[type=text]"), "insert control saved in field");
});
test("field without inserting", function() {
var $element = $("#jsGrid"),
jsGridFieldConfig = {
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text");
return result;
},
insertValue: function() {
return this.insertControl.val();
}
},
gridOptions = {
inserting: true,
fields: [
$.extend({}, jsGridFieldConfig, {
name: "field1",
inserting: false
}),
$.extend({}, jsGridFieldConfig, {
name: "field2"
})
]
},
grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test1");
grid.fields[1].insertControl.val("test2");
deepEqual(grid._getInsertItem(), { field2: "test2" }, "field with inserting=false is not included in inserting item");
});
test("insert data with default location", function() {
var $element = $("#jsGrid"),
inserted = false,
insertingArgs,
insertedArgs,
gridOptions = {
inserting: true,
data: [{field: "default"}],
fields: [
{
name: "field",
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text");
return result;
},
insertValue: function() {
return this.insertControl.val();
}
}
],
onItemInserting: function(e) {
insertingArgs = $.extend(true, {}, e);
},
onItemInserted: function(e) {
insertedArgs = $.extend(true, {}, e);
},
controller: {
insertItem: function() {
inserted = true;
}
}
},
grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(insertingArgs.item.field, "test", "field is provided in inserting args");
equal(grid.option("data").length, 2, "data is inserted");
ok(inserted, "controller insertItem was called");
deepEqual(grid.option("data")[1], { field: "test" }, "correct data is inserted");
equal(insertedArgs.item.field, "test", "field is provided in inserted args");
});
test("insert data with specified insert location", function() {
var $element = $("#jsGrid"),
inserted = false,
insertingArgs,
insertedArgs,
gridOptions = {
inserting: true,
insertRowLocation: "top",
data: [{field: "default"}],
fields: [
{
name: "field",
insertTemplate: function() {
var result = this.insertControl = $("<input>").attr("type", "text");
return result;
},
insertValue: function() {
return this.insertControl.val();
}
}
],
onItemInserting: function(e) {
insertingArgs = $.extend(true, {}, e);
},
onItemInserted: function(e) {
insertedArgs = $.extend(true, {}, e);
},
controller: {
insertItem: function() {
inserted = true;
}
}
},
grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(insertingArgs.item.field, "test", "field is provided in inserting args");
equal(grid.option("data").length, 2, "data is inserted");
ok(inserted, "controller insertItem was called");
deepEqual(grid.option("data")[0], { field: "test" }, "correct data is inserted at the beginning");
equal(insertedArgs.item.field, "test", "field is provided in inserted args");
});
test("insertItem accepts item to insert", function() {
var $element = $("#jsGrid"),
itemToInsert = { field: "test" },
insertedItem,
gridOptions = {
data: [],
fields: [
{
name: "field"
}
],
controller: {
insertItem: function(item) {
insertedItem = item;
}
}
},
grid = new Grid($element, gridOptions);
grid.insertItem(itemToInsert);
deepEqual(grid.option("data")[0], itemToInsert, "data is inserted");
deepEqual(insertedItem, itemToInsert, "controller insertItem was called with correct item");
});
module("editing");
test("editing rendering", function() {
var $element = $("#jsGrid"),
$editRow,
data = [{
test: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "test",
align: "right",
editcss: "edit-class",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value).addClass("edit-input");
return result;
}
}
]
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
equal(grid._content.find("." + grid.editRowClass).length, 0, "no edit row after initial rendering");
grid.editItem(data[0]);
$editRow = grid._content.find("." + grid.editRowClass);
equal($editRow.length, 1, "edit row rendered");
equal($editRow.find(".edit-class").length, 1, "editcss class is attached");
equal($editRow.find(".edit-input").length, 1, "edit control rendered");
equal($editRow.find("." + grid.cellClass).length, 1, "cell class is attached");
ok($editRow.find(".edit-class").hasClass("jsgrid-align-right"), "align class is attached");
ok(grid.fields[0].editControl.is("input[type=text]"), "edit control saved in field");
equal(grid.fields[0].editControl.val(), "value", "edit control value");
});
test("editItem accepts row to edit", function() {
var $element = $("#jsGrid"),
$editRow,
data = [
{ test: "value" }
],
gridOptions = {
editing: true,
fields: [
{ name: "test" }
]
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
var $row = $element.find("." + grid.oddRowClass).eq(0);
grid.editItem($row);
$editRow = grid._content.find("." + grid.editRowClass);
equal($editRow.length, 1, "edit row rendered");
grid.cancelEdit();
grid.editItem($row.get(0));
$editRow = grid._content.find("." + grid.editRowClass);
equal($editRow.length, 1, "edit row rendered");
});
test("edit item", function() {
var $element = $("#jsGrid"),
editingArgs,
editingRow,
updated = false,
updatingArgs,
updatingRow,
updatedRow,
updatedArgs,
data = [{
field: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "field",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value);
return result;
},
editValue: function() {
return this.editControl.val();
}
}
],
controller: {
updateItem: function(updatingItem) {
updated = true;
}
},
onItemEditing: function(e) {
editingArgs = $.extend(true, {}, e);
editingRow = grid.rowByItem(data[0])[0];
},
onItemUpdating: function(e) {
updatingArgs = $.extend(true, {}, e);
updatingRow = grid.rowByItem(data[0])[0];
},
onItemUpdated: function(e) {
updatedArgs = $.extend(true, {}, e);
updatedRow = grid.rowByItem(data[0])[0];
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.editItem(data[0]);
deepEqual(editingArgs.item, { field: "value" }, "item before editing is provided in editing event args");
equal(editingArgs.itemIndex, 0, "itemIndex is provided in editing event args");
equal(editingArgs.row[0], editingRow, "row element is provided in editing event args");
grid.fields[0].editControl.val("new value");
grid.updateItem();
deepEqual(updatingArgs.previousItem, { field: "value" }, "item before editing is provided in updating event args");
deepEqual(updatingArgs.item, { field: "new value" }, "updating item is provided in updating event args");
equal(updatingArgs.itemIndex, 0, "itemIndex is provided in updating event args");
equal(updatingArgs.row[0], updatingRow, "row element is provided in updating event args");
ok(updated, "controller updateItem called");
deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");
equal(grid._content.find("." + grid.editRowClass).length, 0, "edit row removed");
equal(grid._content.find("." + grid.oddRowClass).length, 1, "data row rendered");
deepEqual(updatedArgs.previousItem, { field: "value" }, "item before editing is provided in updated event args");
deepEqual(updatedArgs.item, { field: "new value" }, "updated item is provided in updated event args");
equal(updatedArgs.itemIndex, 0, "itemIndex is provided in updated event args");
equal(updatedArgs.row[0], updatedRow, "row element is provided in updated event args");
});
test("failed update should not change original item", function() {
var $element = $("#jsGrid"),
data = [{
field: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "field",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value);
return result;
},
editValue: function() {
return this.editControl.val();
}
}
],
controller: {
updateItem: function(updatingItem) {
return $.Deferred().reject();
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.editItem(data[0]);
grid.fields[0].editControl.val("new value");
grid.updateItem();
deepEqual(grid.option("data")[0], { field: "value" }, "value is not updated");
});
test("cancel edit", function() {
var $element = $("#jsGrid"),
updated = false,
cancellingArgs,
cancellingRow,
data = [{
field: "value"
}],
gridOptions = {
editing: true,
fields: [
{
name: "field",
editTemplate: function(value) {
var result = this.editControl = $("<input>").attr("type", "text").val(value);
return result;
},
editValue: function() {
return this.editControl.val();
}
}
],
controller: {
updateData: function(updatingItem) {
updated = true;
}
},
onItemEditCancelling: function(e) {
cancellingArgs = $.extend(true, {}, e);
cancellingRow = grid.rowByItem(data[0])[0];
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.editItem(data[0]);
grid.fields[0].editControl.val("new value");
grid.cancelEdit();
deepEqual(cancellingArgs.item, { field: "value" }, "item before cancel is provided in cancelling event args");
equal(cancellingArgs.itemIndex, 0, "itemIndex is provided in cancelling event args");
equal(cancellingArgs.row[0], cancellingRow, "row element is provided in cancelling event args");
ok(!updated, "controller updateItem was not called");
deepEqual(grid.option("data")[0], { field: "value" }, "data were not updated");
equal(grid._content.find("." + grid.editRowClass).length, 0, "edit row removed");
equal(grid._content.find("." + grid.oddRowClass).length, 1, "data row restored");
});
test("updateItem accepts item to update and new item", function() {
var $element = $("#jsGrid"),
updatingItem,
data = [{
field: "value"
}],
gridOptions = {
fields: [
{ name: "field" }
],
controller: {
updateItem: function(item) {
return updatingItem = item;
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.updateItem(data[0], { field: "new value" });
deepEqual(updatingItem, { field: "new value" }, "controller updateItem called correctly");
deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");
});
test("updateItem accepts single argument - item to update", function() {
var $element = $("#jsGrid"),
updatingItem,
data = [{
field: "value"
}],
gridOptions = {
fields: [
{ name: "field" }
],
controller: {
updateItem: function(item) {
return updatingItem = item;
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
data[0].field = "new value";
grid.updateItem(data[0]);
deepEqual(updatingItem, { field: "new value" }, "controller updateItem called correctly");
deepEqual(grid.option("data")[0], { field: "new value" }, "correct data updated");
});
test("editRowRenderer", function() {
var $element = $("#jsGrid"),
data = [
{ value: "test" }
],
gridOptions = {
data: data,
editing: true,
editRowRenderer: function(item, itemIndex) {
return $("<tr>").addClass("custom-edit-row").append($("<td>").text(itemIndex + ":" + item.value));
},
fields: [
{ name: "value" }
]
},
grid = new Grid($element, gridOptions);
grid.editItem(data[0]);
var $editRow = grid._content.find(".custom-edit-row");
equal($editRow.length, 1, "edit row rendered");
equal($editRow.text(), "0:test", "custom edit row renderer rendered");
});
module("deleting");
test("delete item", function() {
var $element = $("#jsGrid"),
deleted = false,
deletingArgs,
deletedArgs,
data = [{
field: "value"
}],
gridOptions = {
confirmDeleting: false,
fields: [
{ name: "field" }
],
controller: {
deleteItem: function(deletingItem) {
deleted = true;
}
},
onItemDeleting: function(e) {
deletingArgs = $.extend(true, {}, e);
},
onItemDeleted: function(e) {
deletedArgs = $.extend(true, {}, e);
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
grid.deleteItem(data[0]);
deepEqual(deletingArgs.item, { field: "value" }, "field and value is provided in deleting event args");
equal(deletingArgs.itemIndex, 0, "itemIndex is provided in updating event args");
equal(deletingArgs.row.length, 1, "row element is provided in updating event args");
ok(deleted, "controller deleteItem called");
equal(grid.option("data").length, 0, "data row deleted");
deepEqual(deletedArgs.item, { field: "value" }, "item is provided in updating event args");
equal(deletedArgs.itemIndex, 0, "itemIndex is provided in updating event args");
equal(deletedArgs.row.length, 1, "row element is provided in updating event args");
});
test("deleteItem accepts row", function() {
var $element = $("#jsGrid"),
deletedItem,
itemToDelete = {
field: "value"
},
data = [itemToDelete],
gridOptions = {
confirmDeleting: false,
fields: [
{ name: "field" }
],
controller: {
deleteItem: function(deletingItem) {
deletedItem = deletingItem;
}
}
},
grid = new Grid($element, gridOptions);
grid.option("data", data);
var $row = $element.find("." + grid.oddRowClass).eq(0);
grid.deleteItem($row);
deepEqual(deletedItem, itemToDelete, "controller deleteItem called correctly");
equal(grid.option("data").length, 0, "data row deleted");
});
module("paging");
test("pager is rendered if necessary", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}, {}, {}],
paging: false,
pageSize: 2
});
ok(grid._pagerContainer.is(":hidden"), "pager is hidden when paging=false");
equal(grid._pagerContainer.html(), "", "pager is not rendered when paging=false");
grid.option("paging", true);
ok(grid._pagerContainer.is(":visible"), "pager is visible when paging=true");
ok(grid._pagerContainer.html(), "pager is rendered when paging=true");
grid.option("data", [{}, {}]);
ok(grid._pagerContainer.is(":hidden"), "pager is hidden for single page");
ok(grid._pagerContainer.html(), "pager is rendered for single page when paging=true");
});
test("external pagerContainer", function() {
var $pagerContainer = $("<div>").appendTo("#qunit-fixture").hide(),
$element = $("#jsGrid");
new Grid($element, {
data: [{}, {}, {}],
pagerContainer: $pagerContainer,
paging: true,
pageSize: 2
});
ok($pagerContainer.is(":visible"), "external pager shown");
ok($pagerContainer.html(), "external pager rendered");
});
test("pager functionality", function() {
var $element = $("#jsGrid"),
pager,
pageChangedArgs,
grid = new Grid($element, {
data: [{}, {}, {}, {}, {}, {}, {}, {}, {}],
onPageChanged: function(args) {
pageChangedArgs = args;
},
paging: true,
pageSize: 2,
pageButtonCount: 3
});
equal(grid._pagesCount(), 5, "correct page count");
equal(grid.option("pageIndex"), 1, "pageIndex is initialized");
equal(grid._firstDisplayingPage, 1, "_firstDisplayingPage is initialized");
pager = grid._pagerContainer;
equal(pager.find("." + grid.currentPageClass).length, 1, "there is one current page");
ok(pager.find("." + grid.pageClass).eq(0).hasClass(grid.currentPageClass), "first page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
equal(pager.find("." + grid.pagerNavButtonClass).length, 5, "five nav buttons displayed: Fisrt Prev Next Last ...");
equal(pager.find("." + grid.pagerNavButtonInactiveClass).length, 2, "two nav buttons inactive: Fisrt Prev");
grid.openPage(2);
equal(pager.find("." + grid.currentPageClass).length, 1, "there is one current page");
ok(pager.find("." + grid.pageClass).eq(1).hasClass(grid.currentPageClass), "second page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
equal(pager.find("." + grid.pagerNavButtonClass).length, 5, "five nav buttons displayed: First Prev Next Last and ...");
equal(pageChangedArgs.pageIndex, 2, "onPageChanged callback provides pageIndex in arguments");
grid.showNextPages();
equal(grid._firstDisplayingPage, 3, "navigate by pages forward");
grid.showPrevPages();
equal(grid._firstDisplayingPage, 1, "navigate by pages backward");
grid.openPage(5);
equal(grid._firstDisplayingPage, 3, "opening next non-visible page moves first displaying page forward");
grid.openPage(2);
equal(grid._firstDisplayingPage, 2, "opening prev non-visible page moves first displaying page backward");
});
test("pager format", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}, {}, {}, {}, {}, {}],
paging: true,
pageSize: 2,
pageIndex: 2,
pageButtonCount: 1,
pagerFormat: "a {pageIndex} {first} {prev} {pages} {next} {last} {pageCount} {itemCount} z",
pagePrevText: "<",
pageNextText: ">",
pageFirstText: "<<",
pageLastText: ">>",
pageNavigatorNextText: "np",
pageNavigatorPrevText: "pp"
});
grid._firstDisplayingPage = 2;
grid._refreshPager();
equal($.trim(grid._pagerContainer.text()), "a 2 << < pp2np > >> 3 6 z", "pager text follows the format specified");
});
test("pagerRenderer", function() {
var $element = $("#jsGrid");
var pagerRendererConfig;
var pageSize = 2;
var items = [{}, {}, {}, {}, {}, {}, {}];
var pageCount = Math.ceil(items.length / pageSize);
var grid = new Grid($element, {
data: items,
paging: true,
pageSize: pageSize,
pagerRenderer: function(pagerConfig) {
pagerRendererConfig = pagerConfig;
}
});
deepEqual(pagerRendererConfig, { pageIndex: 1, pageCount: pageCount });
grid.openPage(2);
deepEqual(pagerRendererConfig, { pageIndex: 2, pageCount: pageCount });
});
test("loading by page", function() {
var $element = $("#jsGrid"),
data = [],
itemCount = 20;
for(var i = 1; i <= itemCount; i += 1) {
data.push({
value: i
});
}
var gridOptions = {
pageLoading: true,
paging: true,
pageSize: 7,
fields: [
{ name: "value" }
],
controller: {
loadData: function(filter) {
var startIndex = (filter.pageIndex - 1) * filter.pageSize,
result = data.slice(startIndex, startIndex + filter.pageSize);
return {
data: result,
itemsCount: data.length
};
}
}
};
var grid = new Grid($element, gridOptions);
grid.loadData();
var pager = grid._pagerContainer;
var gridData = grid.option("data");
equal(gridData.length, 7, "loaded one page of data");
equal(gridData[0].value, 1, "loaded right data start value");
equal(gridData[gridData.length - 1].value, 7, "loaded correct data end value");
ok(pager.find("." + grid.pageClass).eq(0).hasClass(grid.currentPageClass), "first page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
grid.openPage(3);
gridData = grid.option("data");
equal(gridData.length, 6, "loaded last page of data");
equal(gridData[0].value, 15, "loaded right data start value");
equal(gridData[gridData.length - 1].value, 20, "loaded right data end value");
ok(pager.find("." + grid.pageClass).eq(2).hasClass(grid.currentPageClass), "third page is current");
equal(pager.find("." + grid.pageClass).length, 3, "three pages displayed");
});
test("'openPage' method ignores indexes out of range", function() {
var $element = $("#jsGrid"),
grid = new Grid($element, {
data: [{}, {}],
paging: true,
pageSize: 1
});
grid.openPage(0);
equal(grid.option("pageIndex"), 1, "too small index is ignored");
grid.openPage(3);
equal(grid.option("pageIndex"), 1, "too big index is ignored");
});
module("sorting");
test("sorting", function() {
var $element = $("#jsGrid"),
gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal(grid._sortOrder, "asc", "asc sorting order for first click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 1);
equal(gridData[1].value, 2);
equal(gridData[2].value, 3);
ok($th.hasClass(grid.sortableClass));
ok($th.hasClass(grid.sortAscClass));
$th.trigger("click");
equal(grid._sortOrder, "desc", "desc sorting order for second click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 3);
equal(gridData[1].value, 2);
equal(gridData[2].value, 1);
ok(!$th.hasClass(grid.sortAscClass));
ok($th.hasClass(grid.sortDescClass));
});
test("sorting with pageLoading", function() {
var $element = $("#jsGrid"),
loadFilter,
gridOptions = {
sorting: true,
pageLoading: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
controller: {
loadData: function(filter) {
loadFilter = filter;
return {
itemsCount: 0,
data: []
};
}
},
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal(grid._sortOrder, "asc", "asc sorting order for first click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(loadFilter.sortOrder, "asc", "sort direction is provided in loadFilter");
equal(loadFilter.sortField, "value", "sort field is provided in loadFilter");
$th.trigger("click");
equal(grid._sortOrder, "desc", "desc sorting order for second click");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(loadFilter.sortOrder, "desc", "sort direction is provided in loadFilter");
equal(loadFilter.sortField, "value", "sort field is provided in loadFilter");
});
test("no sorting for column with sorting = false", function() {
var $element = $("#jsGrid");
var gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorting: false }
]
};
var grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal(grid._sortField, null, "sort field is not set for the field with sorting=false");
equal(gridData[0].value, 3);
equal(gridData[1].value, 2);
equal(gridData[2].value, 1);
equal($th.hasClass(grid.sortableClass), false, "no sorting css for field with sorting=false");
equal($th.hasClass(grid.sortAscClass), false, "no sorting css for field with sorting=false");
});
test("sort accepts sorting config", function() {
var $element = $("#jsGrid"),
gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
grid.sort({ field: "value", order: "asc" });
equal(grid._sortOrder, "asc", "asc sorting order is set");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 1);
equal(gridData[1].value, 2);
equal(gridData[2].value, 3);
grid.sort({ field: 0 });
equal(grid._sortOrder, "desc", "desc sorting order for already set asc sorting");
equal(grid._sortField, grid.fields[0], "sort field is set");
equal(gridData[0].value, 3);
equal(gridData[1].value, 2);
equal(gridData[2].value, 1);
grid.sort("value", "asc");
equal(grid._sortOrder, "asc", "asc sorting order is set");
equal(grid._sortField, grid.fields[0], "sort field is set");
grid.sort(0);
equal(grid._sortOrder, "desc", "desc sorting order for already set asc sorting");
equal(grid._sortField, grid.fields[0], "sort field is set");
});
test("getSorting returns current sorting", function() {
var $element = $("#jsGrid"),
gridOptions = {
sorting: true,
data: [
{ value: 3 },
{ value: 2 },
{ value: 1 }
],
fields: [
{ name: "value", sorter: "number" }
]
},
grid = new Grid($element, gridOptions);
deepEqual(grid.getSorting(), { field: undefined, order: undefined }, "undefined field and order before sorting");
grid.sort("value");
deepEqual(grid.getSorting(), { field: "value", order: "asc" }, "current sorting returned");
});
test("sorting css attached correctly when a field is hidden", function() {
var $element = $("#jsGrid");
var gridOptions = {
sorting: true,
data: [],
fields: [
{ name: "field1", visible: false },
{ name: "field2" }
]
};
var grid = new Grid($element, gridOptions);
var gridData = grid.option("data");
var $th = grid._headerRow.find("th").eq(0);
$th.trigger("click");
equal($th.hasClass(grid.sortAscClass), true, "sorting css is attached to first field");
});
module("canceling events");
test("cancel item edit", function() {
var $element = $("#jsGrid");
var data = [{}];
var gridOptions = {
editing: true,
onItemEditing: function(e) {
e.cancel = true;
},
controller: {
loadData: function() {
return data;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.loadData();
grid.editItem(data[0]);
strictEqual(grid._editingRow, null, "no editing row");
});
test("cancel controller.loadData", function() {
var $element = $("#jsGrid");
var gridOptions = {
onDataLoading: function(e) {
e.cancel = true;
},
controller: {
loadData: function() {
return [{}];
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.loadData();
equal(grid.option("data").length, 0, "no data loaded");
});
test("cancel controller.insertItem", function() {
var $element = $("#jsGrid");
var insertedItem = null;
var gridOptions = {
onItemInserting: function(e) {
e.cancel = true;
},
controller: {
insertItem: function(item) {
insertedItem = item;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.insertItem({ test: "value" });
strictEqual(insertedItem, null, "item was not inserted");
});
test("cancel controller.updateItem", function() {
var $element = $("#jsGrid");
var updatedItem = null;
var existingItem = { test: "value" };
var gridOptions = {
data: [
existingItem
],
onItemUpdating: function(e) {
e.cancel = true;
},
controller: {
updateItem: function(item) {
updatedItem = item;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.updateItem(existingItem, { test: "new_value" });
strictEqual(updatedItem, null, "item was not updated");
});
test("cancel controller.deleteItem", function() {
var $element = $("#jsGrid");
var deletingItem = { test: "value" };
var deletedItem = null;
var gridOptions = {
data: [
deletingItem
],
confirmDeleting: false,
onItemDeleting: function(e) {
e.cancel = true;
},
controller: {
deleteItem: function(item) {
deletedItem = item;
}
},
fields: [
{ name: "test" }
]
};
var grid = new Grid($element, gridOptions);
grid.deleteItem(deletingItem);
strictEqual(deletedItem, null, "item was not deleted");
});
module("complex properties binding");
test("rendering", function() {
var $element = $("#jsGrid");
var gridOptions = {
loadMessage: "",
data: [
{ complexProp: { prop: "test" } }
],
fields: [
{ name: "complexProp.prop", title: "" }
]
};
new Grid($element, gridOptions);
equal($element.text(), "test", "complex property value rendered");
});
test("editing", function() {
var $element = $("#jsGrid");
var gridOptions = {
editing: true,
data: [
{ complexProp: { prop: "test" } }
],
fields: [
{ type: "text", name: "complexProp.prop" }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
equal(grid.fields[0].editControl.val(), "test", "complex property value set in editor");
});
test("should not fail if property is absent", function() {
var $element = $("#jsGrid");
var gridOptions = {
loadMessage: "",
data: [
{ complexProp: { } }
],
fields: [
{ name: "complexProp.subprop.prop", title: "" }
]
};
new Grid($element, gridOptions);
equal($element.text(), "", "rendered empty value");
});
test("inserting", function() {
var $element = $("#jsGrid");
var insertingItem;
var gridOptions = {
inserting: true,
fields: [
{ type: "text", name: "complexProp.prop" }
],
onItemInserting: function(args) {
insertingItem = args.item;
}
};
var grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
deepEqual(insertingItem, { complexProp: { prop: "test" } }, "inserting item has complex properties");
});
test("filtering", function() {
var $element = $("#jsGrid");
var loadFilter;
var gridOptions = {
filtering: true,
fields: [
{ type: "text", name: "complexProp.prop" }
],
controller: {
loadData: function(filter) {
loadFilter = filter;
}
}
};
var grid = new Grid($element, gridOptions);
grid.fields[0].filterControl.val("test");
grid.search();
deepEqual(loadFilter, { complexProp: { prop: "test" } }, "filter has complex properties");
});
test("updating", function() {
var $element = $("#jsGrid");
var updatingItem;
var gridOptions = {
editing: true,
data: [
{ complexProp: { } }
],
fields: [
{ type: "text", name: "complexProp.prop" }
],
onItemUpdating: function(args) {
updatingItem = args.item;
}
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("test");
grid.updateItem();
deepEqual(updatingItem, { complexProp: { prop: "test" } }, "updating item has complex properties");
});
test("update nested prop", function() {
var $element = $("#jsGrid");
var updatingItem;
var previousItem;
var gridOptions = {
editing: true,
data: [
{ prop: { subprop1: "test1", subprop2: "test2" } }
],
fields: [
{ type: "text", name: "prop.subprop1" },
{ type: "text", name: "prop.subprop2" }
],
onItemUpdating: function(args) {
updatingItem = args.item;
}
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("new_test1");
grid.updateItem();
var expectedUpdatingItem = {
prop: {
subprop1: "new_test1",
subprop2: "test2"
}
};
deepEqual(updatingItem, expectedUpdatingItem, "updating item has nested properties");
});
test("updating deeply nested prop", function() {
var $element = $("#jsGrid");
var updatingItem;
var previousItem;
var gridOptions = {
editing: true,
data: [
{ complexProp: { subprop1: { another_prop: "test" } } }
],
fields: [
{ type: "text", name: "complexProp.subprop1.prop1" },
{ type: "text", name: "complexProp.subprop1.subprop2.prop12" }
],
onItemUpdating: function(args) {
updatingItem = $.extend(true, {}, args.item);
previousItem = $.extend(true, {}, args.previousItem);
}
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("test1");
grid.fields[1].editControl.val("test2");
grid.updateItem();
var expectedUpdatingItem = {
complexProp: {
subprop1: {
another_prop: "test",
prop1: "test1",
subprop2: { prop12: "test2" }
}
}
};
var expectedPreviousItem = {
complexProp: {
subprop1: {
another_prop: "test"
}
}
};
deepEqual(updatingItem, expectedUpdatingItem, "updating item has deeply nested properties");
deepEqual(previousItem, expectedPreviousItem, "previous item preserved correctly");
});
module("validation");
test("insertItem should call validation.validate", function() {
var $element = $("#jsGrid");
var fieldValidationRules = { test: "value" };
var validatingArgs;
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
validation: {
validate: function(args) {
validatingArgs = args;
return [];
}
},
fields: [
{ type: "text", name: "Name", validate: fieldValidationRules }
]
};
var grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem();
deepEqual(validatingArgs, { value: "test", item: { Name: "test" }, itemIndex: -1,
row: grid._insertRow, rules: fieldValidationRules }, "validating args is provided");
});
test("insertItem rejected when data is not valid", function() {
var $element = $("#jsGrid");
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.fields[0].insertControl.val("test");
grid.insertItem().done(function() {
ok(false, "insertItem should not be completed");
}).fail(function() {
ok(true, "insertItem should fail");
});
});
test("invalidClass is attached on invalid cell on inserting", function() {
var $element = $("#jsGrid");
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Id", visible: false },
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
var $insertCell = grid._insertRow.children().eq(0);
grid.insertItem();
ok($insertCell.hasClass(grid.invalidClass), "invalid class is attached");
equal($insertCell.attr("title"), "Error", "cell tooltip contains error message");
});
test("onItemInvalid callback", function() {
var $element = $("#jsGrid");
var errors = ["Error"];
var onItemInvalidCalled = 0;
var onItemInvalidArgs;
var gridOptions = {
data: [],
inserting: true,
invalidNotify: $.noop,
onItemInvalid: function(args) {
onItemInvalidCalled++;
onItemInvalidArgs = args;
},
validation: {
validate: function(args) {
return !args.value ? errors : [];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.insertItem();
equal(onItemInvalidCalled, 1, "onItemInvalid is called, when item data is invalid");
deepEqual(onItemInvalidArgs, { grid: grid, errors: [{ field: grid.fields[0], message: "Error" }],
item: { Name: "" }, itemIndex: -1, row: grid._insertRow }, "arguments provided");
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(onItemInvalidCalled, 1, "onItemInvalid was not called, when data is valid");
});
test("invalidNotify", function() {
var $element = $("#jsGrid");
var errors = ["Error"];
var invalidNotifyCalled = 0;
var invalidNotifyArgs;
var gridOptions = {
data: [],
inserting: true,
invalidNotify: function(args) {
invalidNotifyCalled++;
invalidNotifyArgs = args;
},
validation: {
validate: function(args) {
return !args.value ? errors : [];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.insertItem();
equal(invalidNotifyCalled, 1, "invalidNotify is called, when item data is invalid");
deepEqual(invalidNotifyArgs, { grid: grid, errors: [{ field: grid.fields[0], message: "Error" }],
row: grid._insertRow, item: { Name: "" }, itemIndex: -1 }, "arguments provided");
grid.fields[0].insertControl.val("test");
grid.insertItem();
equal(invalidNotifyCalled, 1, "invalidNotify was not called, when data is valid");
});
test("invalidMessage", function() {
var $element = $("#jsGrid");
var invalidMessage;
var originalAlert = window.alert;
window.alert = function(message) {
invalidMessage = message;
};
try {
Grid.prototype.invalidMessage = "InvalidTest";
Grid.prototype.invalidNotify({ errors: [{ message: "Message1" }, { message: "Message2" }] });
var expectedInvalidMessage = ["InvalidTest", "Message1", "Message2"].join("\n");
equal(invalidMessage, expectedInvalidMessage, "message contains invalidMessage and field error messages");
} finally {
window.alert = originalAlert;
}
});
test("updateItem should call validation.validate", function() {
var $element = $("#jsGrid");
var validatingArgs;
var gridOptions = {
data: [{ Name: "" }],
editing: true,
invalidNotify: $.noop,
validation: {
validate: function(args) {
validatingArgs = args;
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", validate: "required" }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.fields[0].editControl.val("test");
grid.updateItem();
deepEqual(validatingArgs, { value: "test", item: { Name: "test" }, itemIndex: 0,
row: grid._getEditRow(), rules: "required" }, "validating args is provided");
});
test("invalidClass is attached on invalid cell on updating", function() {
var $element = $("#jsGrid");
var gridOptions = {
data: [{}],
editing: true,
invalidNotify: $.noop,
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", validate: true }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
var $editCell = grid._getEditRow().children().eq(0);
grid.updateItem();
ok($editCell.hasClass(grid.invalidClass), "invalid class is attached");
equal($editCell.attr("title"), "Error", "cell tooltip contains error message");
});
test("validation should ignore not editable fields", function() {
var invalidNotifyCalled = 0;
var $element = $("#jsGrid");
var validatingArgs;
var gridOptions = {
data: [{ Name: "" }],
editing: true,
invalidNotify: function() {
invalidNotifyCalled++;
},
validation: {
validate: function() {
return ["Error"];
}
},
fields: [
{ type: "text", name: "Name", editing: false, validate: "required" }
]
};
var grid = new Grid($element, gridOptions);
grid.editItem(gridOptions.data[0]);
grid.updateItem();
equal(invalidNotifyCalled, 0, "data is valid");
});
module("api");
test("reset method should go the first page when pageLoading is truned on", function() {
var items = [{ Name: "1" }, { Name: "2" }];
var $element = $("#jsGrid");
var gridOptions = {
paging: true,
pageSize: 1,
pageLoading: true,
autoload: true,
controller: {
loadData: function(args) {
return {
data: [items[args.pageIndex - 1]],
itemsCount: items.length
};
}
},
fields: [
{ type: "text", name: "Name" }
]
};
var grid = new Grid($element, gridOptions);
grid.openPage(2);
grid.reset();
equal(grid._bodyGrid.text(), "1", "grid content reset");
});
module("i18n");
test("set locale by name", function() {
jsGrid.locales.my_lang = {
grid: {
test: "test_text"
}
};
jsGrid.locale("my_lang");
var $element = $("#jsGrid").jsGrid({});
equal($element.jsGrid("option", "test"), "test_text", "option localized");
});
test("set locale by config", function() {
jsGrid.locale( {
grid: {
test: "test_text"
}
});
var $element = $("#jsGrid").jsGrid({});
equal($element.jsGrid("option", "test"), "test_text", "option localized");
});
test("locale throws exception for unknown locale", function() {
throws(function() {
jsGrid.locale("unknown_lang");
}, /unknown_lang/, "locale threw an exception");
});
module("controller promise");
asyncTest("should support jQuery promise success callback", 1, function() {
var data = [];
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
var d = $.Deferred();
setTimeout(function() {
d.resolve(data);
start();
});
return d.promise();
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.done(function(result) {
equal(result, data, "data provided to done callback");
});
});
asyncTest("should support jQuery promise fail callback", 1, function() {
var failArgs = {};
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
var d = $.Deferred();
setTimeout(function() {
d.reject(failArgs);
start();
});
return d.promise();
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.fail(function(result) {
equal(result, failArgs, "fail args provided to fail callback");
});
});
asyncTest("should support JS promise success callback", 1, function() {
if(typeof Promise === "undefined") {
ok(true, "Promise not supported");
start();
return;
}
var data = [];
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(data);
start();
});
});
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.done(function(result) {
equal(result, data, "data provided to done callback");
});
});
asyncTest("should support JS promise fail callback", 1, function() {
if(typeof Promise === "undefined") {
ok(true, "Promise not supported");
start();
return;
}
var failArgs = {};
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
reject(failArgs);
start();
});
});
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.fail(function(result) {
equal(result, failArgs, "fail args provided to fail callback");
});
});
test("should support non-promise result", 1, function() {
var data = [];
var gridOptions = {
autoload: false,
controller: {
loadData: function() {
return data;
}
}
};
var grid = new Grid($("#jsGrid"), gridOptions);
var promise = grid._controllerCall("loadData", {}, false, $.noop);
promise.done(function(result) {
equal(result, data, "data provided to done callback");
});
});
module("renderTemplate");
test("should pass undefined and null arguments to the renderer", function() {
var rendererArgs;
var rendererContext;
var context = {};
var renderer = function() {
rendererArgs = arguments;
rendererContext = this;
};
Grid.prototype.renderTemplate(renderer, context, { arg1: undefined, arg2: null, arg3: "test" });
equal(rendererArgs.length, 3);
strictEqual(rendererArgs[0], undefined, "undefined passed");
strictEqual(rendererArgs[1], null, "null passed");
strictEqual(rendererArgs[2], "test", "null passed");
strictEqual(rendererContext, context, "context is preserved");
});
module("Data Export", {
setup: function() {
this.exportConfig = {};
this.exportConfig.type = "csv";
this.exportConfig.subset = "all";
this.exportConfig.delimiter = "|";
this.exportConfig.includeHeaders = true;
this.exportConfig.encapsulate = true;
this.element = $("#jsGrid");
this.gridOptions = {
width: "100%",
height: "400px",
inserting: true,
editing: true,
sorting: true,
paging: true,
pageSize: 2,
data: [
{ "Name": "Otto Clay", "Country": 1, "Married": false },
{ "Name": "Connor Johnston", "Country": 2, "Married": true },
{ "Name": "Lacey Hess", "Country": 2, "Married": false },
{ "Name": "Timothy Henson", "Country": 1, "Married": true }
],
fields: [
{ name: "Name", type: "text", width: 150, validate: "required" },
{ name: "Country", type: "select", items: [{ Name: "United States", Id: 1 },{ Name: "Canada", Id: 2 }], valueField: "Id", textField: "Name" },
{ name: "Married", type: "checkbox", title: "Is Married", sorting: false },
{ type: "control" }
]
}
}
});
/* Base Choice Criteria
type: csv
subset: all
delimiter: |
includeHeaders: true
encapsulate: true
*/
test("Should export data: Base Choice",function(){
var grid = new Grid($(this.element), this.gridOptions);
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
expected += '"Lacey Hess"|"Canada"|"false"\r\n';
expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV configured to Base Choice Criteria -- Check Source");
});
test("Should export data: defaults = BCC",function(){
var grid = new Grid($(this.element), this.gridOptions);
var data = grid.exportData();
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
expected += '"Lacey Hess"|"Canada"|"false"\r\n';
expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV with all defaults -- Should be equal to Base Choice");
});
test("Should export data: subset=visible", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.subset = "visible";
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
//expected += '"Lacey Hess"|"Canada"|"false"\r\n';
//expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV of visible records");
});
test("Should export data: delimiter=;", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.delimiter = ";";
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name";"Country";"Is Married"\r\n';
expected += '"Otto Clay";"United States";"false"\r\n';
expected += '"Connor Johnston";"Canada";"true"\r\n';
expected += '"Lacey Hess";"Canada";"false"\r\n';
expected += '"Timothy Henson";"United States";"true"\r\n';
equal(data, expected, "Output CSV with non-default delimiter");
});
test("Should export data: headers=false", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.includeHeaders = false;
var data = grid.exportData(this.exportConfig);
var expected = "";
//expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
expected += '"Connor Johnston"|"Canada"|"true"\r\n';
expected += '"Lacey Hess"|"Canada"|"false"\r\n';
expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV without Headers");
});
test("Should export data: encapsulate=false", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig.encapsulate = false;
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += 'Name|Country|Is Married\r\n';
expected += 'Otto Clay|United States|false\r\n';
expected += 'Connor Johnston|Canada|true\r\n';
expected += 'Lacey Hess|Canada|false\r\n';
expected += 'Timothy Henson|United States|true\r\n';
equal(data, expected, "Output CSV without encapsulation");
});
test("Should export filtered data", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig['filter'] = function(item){
if (item["Name"].indexOf("O") === 0)
return true
};
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"false"\r\n';
//expected += '"Connor Johnston"|"Canada"|"true"\r\n';
//expected += '"Lacey Hess"|"Canada"|"false"\r\n';
//expected += '"Timothy Henson"|"United States"|"true"\r\n';
equal(data, expected, "Output CSV filtered to show names starting with O");
});
test("Should export data: transformed value", function(){
var grid = new Grid($(this.element), this.gridOptions);
this.exportConfig['transforms'] = {};
this.exportConfig.transforms['Married'] = function(value){
if (value === true) return "Yes"
if (value === false) return "No"
};
var data = grid.exportData(this.exportConfig);
var expected = "";
expected += '"Name"|"Country"|"Is Married"\r\n';
expected += '"Otto Clay"|"United States"|"No"\r\n';
expected += '"Connor Johnston"|"Canada"|"Yes"\r\n';
expected += '"Lacey Hess"|"Canada"|"No"\r\n';
expected += '"Timothy Henson"|"United States"|"Yes"\r\n';
equal(data, expected, "Output CSV column value transformed properly");
});
});
<MSG> Core: Fix noDataContent cell class is missing
Fixes #385
<DFF> @@ -688,6 +688,7 @@ $(function() {
grid.option("data", []);
equal(grid._content.find("." + grid.noDataRowClass).length, 1, "no data row rendered");
+ equal(grid._content.find("." + grid.cellClass).length, 1, "grid cell class attached");
equal(grid._content.text(), grid.noDataContent, "no data text rendered");
});
@@ -703,6 +704,7 @@ $(function() {
grid.option("data", []);
+ equal(grid._content.find("." + grid.cellClass).length, 1, "grid cell class attached");
equal(grid._content.text(), noDataMessage, "custom noDataContent");
});
| 2 | Core: Fix noDataContent cell class is missing | 0 | .js | tests | mit | tabalinas/jsgrid |
10059723 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059724 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059725 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059726 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059727 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059728 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059729 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059730 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059731 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059732 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059733 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059734 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059735 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059736 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059737 | <NME> TextFormatterFactory.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
namespace AvaloniaEdit.Utils
{
/// <summary>
/// Creates TextFormatter instances that with the correct TextFormattingMode, if running on .NET 4.0.
/// </summary>
static class TextFormatterFactory
{
/// <summary>
/// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object.
/// </summary>
public static TextFormatter Create(Control owner)
{
return TextFormatter.Current;
}
/// <summary>
/// Creates formatted text.
/// </summary>
/// <param name="element">The owner element. The text formatter setting are read from this element.</param>
/// <param name="text">The text.</param>
/// <param name="typeface">The typeface to use. If this parameter is null, the typeface of the <paramref name="element"/> will be used.</param>
/// <param name="emSize">The font size. If this parameter is null, the font size of the <paramref name="element"/> will be used.</param>
/// <param name="foreground">The foreground color. If this parameter is null, the foreground of the <paramref name="element"/> will be used.</param>
/// <returns>A FormattedText object using the specified settings.</returns>
public static FormattedText CreateFormattedText(Control element, string text, Typeface typeface, double? emSize, IBrush foreground)
{
if (element == null)
throw new ArgumentNullException(nameof(element));
if (text == null)
throw new ArgumentNullException(nameof(text));
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
emSize = TextBlock.GetFontSize(element);
if (foreground == null)
foreground = TextBlock.GetForeground(element);
return new FormattedText(
text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
typeface,
emSize.Value,
foreground);
}
}
}
<MSG> More fixes
<DFF> @@ -19,6 +19,7 @@
using System;
using System.Globalization;
using Avalonia.Controls;
+using Avalonia.Controls.Documents;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -55,9 +56,9 @@ namespace AvaloniaEdit.Utils
if (typeface == default)
typeface = element.CreateTypeface();
if (emSize == null)
- emSize = TextBlock.GetFontSize(element);
+ emSize = TextElement.GetFontSize(element);
if (foreground == null)
- foreground = TextBlock.GetForeground(element);
+ foreground = TextElement.GetForeground(element);
return new FormattedText(
text,
| 3 | More fixes | 2 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059738 | <NME> defining-modules.md
<BEF> # Defining Modules
```js
var Apple = fruitmachine.define({
name: 'apple',
template: templateFunction,
tag: 'section',
classes: ['class-1', 'class-2'],
// Event callbacks (optional)
initialize: function(options){},
setup: function(){},
mount: function(){},
teardown: function(){},
destroy: function(){}
});
Define does two things:
- It registers a module internally for [Lazy](instantition.md#lazy) module instantiation
- It returns a constructor that can be [Explicitly](instantition.md#explicit) instantiated.
Internally `define` extends the default `fruitmachine.Module.prototype` with the parameters you define. Many of these parameters can be overwritten in the options passed to the constructor on a per instance basis. It is important you don't declare any parameters that conflict with `fruitmachine.Module.prototype` core API (check the [source]() if you are unsure).
### Options
- `name {String}` Your name for this module.
- `template {Function}` A function that will return the module's html (we like [Hogan](http://twitter.github.com/hogan.js/))
- `tag {String}` The html tag to use on the root element (defaults to 'div') *(optional)*
- `initialize {Function}` Define a function to run when the module is first instantiated (only ever runs once) *(optional)*
- `setup {Function}` A function to be run every time `Module#setup()` is called. Should be used to bind any DOM event listeners. You can safely assume the presence of `this.el` at this point. *(optional)*
- `teardown {Function}` A function to be run when `Module#teardown()` or `Module#destroy()` is called. `teardown` will also run if you attempt to setup an already 'setup' module.
- `destroy {Function}` Run when `Module#destroy()` is called (will only ever run once) *(optional)*
- `teardown {Function}` A function to be run when `Module#teardown()` or `Module#destroy()` is called. `teardown` will also run if you attempt to setup an already 'setup' module.
- `destroy {Function}` Run when `Module#destroy()` is called (will only ever run once) *(optional)*
<MSG> Correcting documentation links
<DFF> @@ -17,8 +17,8 @@ var Apple = fruitmachine.define({
Define does two things:
-- It registers a module internally for [Lazy](instantition.md#lazy) module instantiation
-- It returns a constructor that can be [Explicitly](instantition.md#explicit) instantiated.
+- It registers a module internally for [Lazy](module-instantition.md#lazy) module instantiation
+- It returns a constructor that can be [Explicitly](module-instantition.md#explicit) instantiated.
Internally `define` extends the default `fruitmachine.Module.prototype` with the parameters you define. Many of these parameters can be overwritten in the options passed to the constructor on a per instance basis. It is important you don't declare any parameters that conflict with `fruitmachine.Module.prototype` core API (check the [source]() if you are unsure).
@@ -31,4 +31,4 @@ Internally `define` extends the default `fruitmachine.Module.prototype` with the
- `initialize {Function}` Define a function to run when the module is first instantiated (only ever runs once) *(optional)*
- `setup {Function}` A function to be run every time `Module#setup()` is called. Should be used to bind any DOM event listeners. You can safely assume the presence of `this.el` at this point. *(optional)*
- `teardown {Function}` A function to be run when `Module#teardown()` or `Module#destroy()` is called. `teardown` will also run if you attempt to setup an already 'setup' module.
-- `destroy {Function}` Run when `Module#destroy()` is called (will only ever run once) *(optional)*
\ No newline at end of file
+- `destroy {Function}` Run when `Module#destroy()` is called (will only ever run once) *(optional)*
| 3 | Correcting documentation links | 3 | .md | md | mit | ftlabs/fruitmachine |
10059739 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059740 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059741 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059742 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059743 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059744 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059745 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059746 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059747 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059748 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
10059749 | <NME> DottedLineMargin.cs
<BEF> // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Data;
namespace AvaloniaEdit.Editing
{
/// <summary>
/// Margin for use with the text area.
/// A vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static class DottedLineMargin
{
private static readonly object Tag = new object();
/// <summary>
/// Creates a vertical dotted line to separate the line numbers from the text view.
/// </summary>
public static IControl Create()
{
var line = new Line
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
StrokeDashArray = { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
Margin = new Thickness(2, 0, 2, 0),
Tag = Tag
};
return line;
}
/// <summary>
/// Gets whether the specified UIElement is the result of a DottedLineMargin.Create call.
/// </summary>
public static bool IsDottedLineMargin(IControl element)
{
var l = element as Line;
return l != null && l.Tag == Tag;
}
}
}
<MSG> Merge branch 'master' into editor-performance
<DFF> @@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using Avalonia;
+using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Markup.Xaml.Data;
@@ -41,7 +42,7 @@ namespace AvaloniaEdit.Editing
{
StartPoint = new Point(0, 0),
EndPoint = new Point(0, 1),
- StrokeDashArray = { 0, 2 },
+ StrokeDashArray = new AvaloniaList<double> { 0, 2 },
Stretch = Stretch.Fill,
StrokeThickness = 1,
StrokeDashCap = PenLineCap.Round,
| 2 | Merge branch 'master' into editor-performance | 1 | .cs | cs | mit | AvaloniaUI/AvaloniaEdit |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.