Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fixed bug for clas Studio and class TextBox #459

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Myra/Events/TextDeletedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace Myra.Events
{
public class TextDeletedEventArgs : EventArgs
{
public int StartPosition
{
get;
}

public string Value
{
get;
}

public TextDeletedEventArgs(int startPosition, string value)
{
StartPosition = startPosition;
Value = value;
}
}
}
32 changes: 20 additions & 12 deletions src/Myra/Graphics2D/UI/Simple/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ internal set
/// Fires every time when the text had been changed by user(doesnt fire if it had been assigned through code)
/// </summary>
public event EventHandler<ValueChangedEventArgs<string>> TextChangedByUser;


/// <summary>
/// Fires every time when the text had been deleted
/// </summary>
public event EventHandler<TextDeletedEventArgs> TextDeleted;

public event EventHandler CursorPositionChanged;

public TextBox(string styleName = Stylesheet.DefaultStyleName)
Expand All @@ -332,7 +337,7 @@ public TextBox(string styleName = Stylesheet.DefaultStyleName)

MouseCursor = MouseCursorType.IBeam;
}

private void DeleteChars(int pos, int l)
{
if (l == 0)
Expand Down Expand Up @@ -434,8 +439,10 @@ private int Delete(int where, int len)
}

UndoStack.MakeDelete(Text, where, len);
var stringToDelete = Text.Substring(where, len);
DeleteChars(where, len);

TextDeleted?.Invoke(this, new TextDeletedEventArgs(where, stringToDelete));
return len;
}

Expand Down Expand Up @@ -915,19 +922,11 @@ private bool SetText(string value, bool byUser)

InvalidateMeasure();

var ev = TextChanged;
if (ev != null)
{
ev(this, new ValueChangedEventArgs<string>(oldValue, value));
}
TextChanged?.Invoke(this, new ValueChangedEventArgs<string>(oldValue, value));

if (byUser)
{
ev = TextChangedByUser;
if (ev != null)
{
ev(this, new ValueChangedEventArgs<string>(oldValue, value));
}
TextChangedByUser?.Invoke(this, new ValueChangedEventArgs<string>(oldValue, value));
}

return true;
Expand Down Expand Up @@ -1106,6 +1105,8 @@ private void UpdateScrolling()

private void OnCursorIndexChanged()
{
_lastCursorUpdate = DateTime.Now;

UpdateScrolling();

CursorPositionChanged.Invoke(this);
Expand Down Expand Up @@ -1439,6 +1440,13 @@ public override void InternalRender(RenderContext context)
}

var now = DateTime.Now;

if (_lastCursorUpdate > _lastBlinkStamp)
{
_lastBlinkStamp = _lastCursorUpdate;
_cursorOn = true;
}

if ((now - _lastBlinkStamp).TotalMilliseconds >= BlinkIntervalInMs)
{
_cursorOn = !_cursorOn;
Expand Down
74 changes: 74 additions & 0 deletions src/MyraPad/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace MyraPad
{
internal static class StringExtensions
{
/// <summary>
/// Returns the character at the index of string, or returns null when out of range and does not throw exception.
/// </summary>
/// <param name="this"></param>
/// <param name="index"></param>
/// <returns></returns>
internal static char? GetCharSafely(this string @this, int index)
{
if (index < @this.Length && index >= 0)
{
return @this[index];
}

return null;
}

/// <summary>
/// Returns a substring of this string, or returns null when out of range and does not throw exception.
/// </summary>
/// <param name="this"></param>
/// <param name="index"></param>
/// <param name="count"></param>
/// <returns></returns>
internal static string SubstringSafely(this string @this, int index, int count)
{
if (count <= 0)
{
return string.Empty;
}

if (index < 0)
{
return null;
}

if (index >= @this.Length)
{
return null;
}

if (index + count >= @this.Length)
{
return null;
}

return @this.Substring(index, count);

}

public static int LastIndexOfSafely(this string @this, char character, int index)
{
if (index < 0 || index >= @this.Length)
{
return -1;
}

return @this.LastIndexOf(character, index);
}

public static int IndexOfSafely(this string @this, char character, int index)
{
if (index < 0 || index >= @this.Length)
{
return -1;
}

return @this.IndexOf(character, index);
}
}
}
63 changes: 51 additions & 12 deletions src/MyraPad/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ private void BuildUI()
_ui._textSource.TextChanged += _textSource_TextChanged;
_ui._textSource.KeyDown += _textSource_KeyDown;
_ui._textSource.Char += _textSource_Char;

_ui._textSource.TextDeleted += _textSource_TextDeleted;
_ui._textStatus.Text = string.Empty;
_ui._textLocation.Text = "Line: 0, Column: 0, Indent: 0";

Expand All @@ -502,6 +502,38 @@ private void BuildUI()
UpdateMenuFile();
}

private void _textSource_TextDeleted(object _, TextDeletedEventArgs e)
{
if (e.Value.Contains('\n'))
{
return;
}

int startIndexOfLine = _ui._textSource.Text.LastIndexOfSafely('\n', _ui._textSource.CursorPosition - 2);
var endIndexOfLine = _ui._textSource.Text.IndexOfSafely('\n', _ui._textSource.CursorPosition - 2);
if (endIndexOfLine < 0)
{
endIndexOfLine = _ui._textSource.Text.Length;
}

if (startIndexOfLine < 0)
{
startIndexOfLine = 0;
}

var currentLineString = _ui._textSource.Text[startIndexOfLine..endIndexOfLine];
if (currentLineString is null)
{
return;
}

if (string.IsNullOrWhiteSpace(currentLineString))
{
_ui._textSource.Text = _ui._textSource.Text.Remove(startIndexOfLine, currentLineString.Length);
_ui._textSource.CursorPosition = startIndexOfLine + 1;
}
}

private void _textBoxFilter_TextChanged(object sender, ValueChangedEventArgs<string> e)
{
PropertyGrid.Filter = _ui._textBoxFilter.Text;
Expand Down Expand Up @@ -742,20 +774,24 @@ private void ApplyAutoIndent()
return;
}

var il = _indentLevel;
if (pos < text.Length - 2 && text[pos] == '<' && text[pos + 1] == '/')
{
--il;
}
var indentLevel = _indentLevel;
bool wrapAfterIndent = text.SubstringSafely(pos + 1, 2) == "</";

if (il <= 0)
if (indentLevel <= 0)
{
return;
}

// Insert indent
var indent = new string(' ', il * _options.IndentSpacesSize);
_ui._textSource.Insert(pos, indent);
var indent = new string(' ', indentLevel * _options.IndentSpacesSize);
if (wrapAfterIndent)
{
indent += '\n';
}
_ui._textSource.Insert(pos + 1, indent);

_ui._textSource.CursorPosition = pos + 2;
//Move the cursor to the position after indent
}

private void ApplyAutoClose()
Expand All @@ -768,15 +804,18 @@ private void ApplyAutoClose()
_applyAutoClose = false;

var pos = _ui._textSource.CursorPosition;

var currentTag = CurrentTag;
if (string.IsNullOrEmpty(currentTag) || !_needsCloseTag)
{
return;
}

var close = "</" + ExtractTag(currentTag) + ">";
_ui._textSource.Insert(pos, close);
var closeTag = "</" + ExtractTag(currentTag) + ">";
_ui._textSource.Insert(pos + 1, closeTag);

_ui._textSource.CursorPosition = pos;
//Method Insert(int, string) has moved the cursor position
//this will restore the cursor to the position after '<Tag>' and before '<Tag/>'
}

private void _textSource_TextChanged(object sender, ValueChangedEventArgs<string> e)
Expand Down
Loading