Skip to content

Commit

Permalink
feat: Implement SelectionStart, SelectionLength on Skia
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Aug 21, 2021
1 parent b0f7646 commit e10659e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,5 +297,37 @@ public void Select(int start, int length)
}
// TODO: Handle TextView..
}

public int GetSelectionStart()
{
if (_currentInputWidget is Entry entry)
{
entry.GetSelectionBounds(out var start, out _);
return start;
}
else if (_currentInputWidget is TextView textView)
{
textView.Buffer.GetSelectionBounds(out var start, out _);
return start.Offset; // TODO: Confirm this implementation is correct.
}

return 0;
}

public int GetSelectionLength()
{
if (_currentInputWidget is Entry entry)
{
entry.GetSelectionBounds(out var start, out var end);
return end - start;
}
else if (_currentInputWidget is TextView textView)
{
textView.Buffer.GetSelectionBounds(out var start, out var end);
return end.Offset - start.Offset;
}

return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,9 @@ public void SetIsPassword(bool isPassword)
}

public void Select(int start, int length) => _currentInputWidget?.Select(start, length);

public int GetSelectionStart() => _currentInputWidget?.SelectionStart ?? 0;

public int GetSelectionLength() => _currentInputWidget?.SelectionLength ?? 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class TextBox
// Skipping already declared property TextWrapping
// Skipping already declared property TextAlignment
// Skipping already declared property Text
#if false || false || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || false
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented("NET461", "__SKIA__", "__NETSTD_REFERENCE__")]
public int SelectionStart
{
Expand All @@ -24,7 +24,7 @@ public int SelectionStart
}
}
#endif
#if false || false || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || false
#if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented("NET461", "__SKIA__", "__NETSTD_REFERENCE__")]
public int SelectionLength
{
Expand Down
4 changes: 4 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/ITextBoxExtension.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ internal interface ITextBoxViewExtension
void SetIsPassword(bool isPassword);

void Select(int start, int length);

int GetSelectionStart();

int GetSelectionLength();
}
}
6 changes: 6 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,13 @@ public void Select(int start, int length)
length = textLength - start;
}

if (SelectionStart == start && SelectionLength == length)
{
return;
}

SelectPartial(start, length);
OnSelectionChanged();
}

partial void SelectPartial(int start, int length);
Expand Down
13 changes: 13 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ partial void SelectPartial(int start, int length)

public void SelectAll() => Select(0, Text.Length);

public int SelectionStart
{
get => _textBoxView?.GetSelectionStart() ?? 0;
set => Select(start: value, length: SelectionLength);
}

public int SelectionLength
{
get => _textBoxView?.GetSelectionLength() ?? 0;
set => Select(SelectionStart, value);
}


protected void SetIsPassword(bool isPassword) => _textBoxView?.SetIsPassword(isPassword);
}
}
5 changes: 5 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ public TextBox? TextBox
}
}

internal int GetSelectionStart() => _textBoxExtension?.GetSelectionStart() ?? 0;

internal int GetSelectionLength() => _textBoxExtension?.GetSelectionLength() ?? 0;

public TextBlock DisplayBlock { get; } = new TextBlock();


internal void SetTextNative(string text)
{
// TODO: Inheritance hierarchy is wrong in Uno. PasswordBox shouldn't inherit TextBox.
Expand Down

0 comments on commit e10659e

Please sign in to comment.