Make Editors respect onKeyEvent

This commit is contained in:
Grim Maple 2022-12-02 18:45:15 +03:00
parent 7895e417d9
commit 25bc5ef4b9
1 changed files with 39 additions and 35 deletions

View File

@ -385,10 +385,10 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
/// Override for EditBox /// Override for EditBox
void wordWrapRefresh(){return;} void wordWrapRefresh(){return;}
/// To hold _scrollpos.x toggling between normal and word wrap mode /// To hold _scrollpos.x toggling between normal and word wrap mode
int previousXScrollPos; int previousXScrollPos;
protected bool _wordWrap; protected bool _wordWrap;
/// true if word wrap mode is set /// true if word wrap mode is set
@property bool wordWrap() { @property bool wordWrap() {
@ -416,7 +416,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
/// Characters at which content is split for word wrap mode /// Characters at which content is split for word wrap mode
dchar[] splitChars = [' ', '-', '\t']; dchar[] splitChars = [' ', '-', '\t'];
/// Divides up a string for word wrapping, sets info in _span /// Divides up a string for word wrapping, sets info in _span
dstring[] wrapLine(dstring str, int lineNumber) { dstring[] wrapLine(dstring str, int lineNumber) {
FontRef font = font(); FontRef font = font();
@ -473,17 +473,17 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
while (true) while (true)
{ {
int index = to!int(str.indexOfAny(splitChars, startIndex)); int index = to!int(str.indexOfAny(splitChars, startIndex));
if (index == -1) if (index == -1)
{ {
parts ~= str[startIndex .. $]; parts ~= str[startIndex .. $];
//Log.d("Explode output: ", parts); //Log.d("Explode output: ", parts);
return parts; return parts;
} }
dstring word = str[startIndex .. index]; dstring word = str[startIndex .. index];
dchar nextChar = (str[index .. index + 1])[0]; dchar nextChar = (str[index .. index + 1])[0];
import std.ascii:isWhite; import std.ascii:isWhite;
if (isWhite(nextChar)) if (isWhite(nextChar))
{ {
@ -497,11 +497,11 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
startIndex = index + 1; startIndex = index + 1;
} }
} }
/// information about line span into several lines - in word wrap mode /// information about line span into several lines - in word wrap mode
protected LineSpan[] _span; protected LineSpan[] _span;
protected LineSpan[] _spanCache; protected LineSpan[] _spanCache;
/// Finds good visual wrapping point for string /// Finds good visual wrapping point for string
int findWrapPoint(dstring text) int findWrapPoint(dstring text)
{ {
@ -519,7 +519,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
} }
} }
} }
/// Calls measureText for word wrap /// Calls measureText for word wrap
int measureWrappedText(dstring text) int measureWrappedText(dstring text)
{ {
@ -532,7 +532,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
return measuredWidths[$-1]; return measuredWidths[$-1];
return 0; return 0;
} }
/// Returns number of visible wraps up to a line (not including the first wrapLines themselves) /// Returns number of visible wraps up to a line (not including the first wrapLines themselves)
int wrapsUpTo(int line) int wrapsUpTo(int line)
{ {
@ -544,7 +544,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
}); });
return sum; return sum;
} }
/// Returns LineSpan for line based on actual line number /// Returns LineSpan for line based on actual line number
LineSpan getSpan(int lineNumber) LineSpan getSpan(int lineNumber)
{ {
@ -556,7 +556,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
}); });
return lineSpan; return lineSpan;
} }
/// Based on a TextPosition, finds which wrapLine it is on for its current line /// Based on a TextPosition, finds which wrapLine it is on for its current line
int findWrapLine(TextPosition textPos) int findWrapLine(TextPosition textPos)
{ {
@ -569,13 +569,13 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
return curWrapLine; return curWrapLine;
curPosition -= curSpan.wrapPoints[curWrapLine].wrapPos; curPosition -= curSpan.wrapPoints[curWrapLine].wrapPos;
if (curPosition < 0) if (curPosition < 0)
{ {
return curWrapLine; return curWrapLine;
} }
curWrapLine++; curWrapLine++;
} }
} }
/// Simple way of iterating through _span /// Simple way of iterating through _span
void lineSpanIterate(void delegate(LineSpan curSpan) iterator) void lineSpanIterate(void delegate(LineSpan curSpan) iterator)
{ {
@ -1290,7 +1290,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
//In word wrap mode, set by caretRect so ensureCaretVisible will know when to scroll //In word wrap mode, set by caretRect so ensureCaretVisible will know when to scroll
protected int caretHeightOffset; protected int caretHeightOffset;
/// returns cursor rectangle /// returns cursor rectangle
protected Rect caretRect() { protected Rect caretRect() {
Rect caretRc = textPosToClient(_caretPos); Rect caretRc = textPosToClient(_caretPos);
@ -1458,22 +1458,22 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
_textToHighlightOptions = textToHighlightOptions; _textToHighlightOptions = textToHighlightOptions;
invalidate(); invalidate();
} }
/// Used instead of using clientToTextPos for mouse input when in word wrap mode /// Used instead of using clientToTextPos for mouse input when in word wrap mode
protected TextPosition wordWrapMouseOffset(int x, int y) protected TextPosition wordWrapMouseOffset(int x, int y)
{ {
if(_span.length == 0) if(_span.length == 0)
return clientToTextPos(Point(x,y)); return clientToTextPos(Point(x,y));
int selectedVisibleLine = y / _lineHeight; int selectedVisibleLine = y / _lineHeight;
LineSpan _curSpan; LineSpan _curSpan;
int wrapLine = 0; int wrapLine = 0;
int curLine = 0; int curLine = 0;
bool foundWrap = false; bool foundWrap = false;
int accumulativeWidths = 0; int accumulativeWidths = 0;
int curWrapOfSpan = 0; int curWrapOfSpan = 0;
lineSpanIterate(delegate(LineSpan curSpan){ lineSpanIterate(delegate(LineSpan curSpan){
while (!foundWrap) while (!foundWrap)
{ {
@ -1497,7 +1497,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
} }
curWrapOfSpan = 0; curWrapOfSpan = 0;
}); });
int fakeLineHeight = curLine * _lineHeight; int fakeLineHeight = curLine * _lineHeight;
return clientToTextPos(Point(x + accumulativeWidths,fakeLineHeight)); return clientToTextPos(Point(x + accumulativeWidths,fakeLineHeight));
} }
@ -2103,6 +2103,8 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
/// handle keys /// handle keys
override bool onKeyEvent(KeyEvent event) { override bool onKeyEvent(KeyEvent event) {
//Log.d("onKeyEvent ", event.action, " ", event.keyCode, " flags ", event.flags); //Log.d("onKeyEvent ", event.action, " ", event.keyCode, " flags ", event.flags);
if(super.onKeyEvent(event))
return true;
if (focused) startCaretBlinking(); if (focused) startCaretBlinking();
cancelHoverTimer(); cancelHoverTimer();
bool ctrlOrAltPressed = !!(event.flags & KeyFlag.Control); //(event.flags & (KeyFlag.Control /* | KeyFlag.Alt */)); bool ctrlOrAltPressed = !!(event.flags & KeyFlag.Control); //(event.flags & (KeyFlag.Control /* | KeyFlag.Alt */));
@ -2133,7 +2135,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction
//if (event.keyCode == KeyCode.RETURN && !readOnly && !_content.multiline) { //if (event.keyCode == KeyCode.RETURN && !readOnly && !_content.multiline) {
// return true; // return true;
//} //}
return super.onKeyEvent(event); return true;
} }
/// Handle Ctrl + Left mouse click on text /// Handle Ctrl + Left mouse click on text
@ -2311,7 +2313,7 @@ class EditLine : EditWidgetBase {
protected Point _measuredTextToSetWidgetSize; protected Point _measuredTextToSetWidgetSize;
protected dstring _textToSetWidgetSize = "aaaaa"d; protected dstring _textToSetWidgetSize = "aaaaa"d;
@property void textToSetWidgetSize(dstring newText) { @property void textToSetWidgetSize(dstring newText) {
_textToSetWidgetSize = newText; _textToSetWidgetSize = newText;
requestLayout(); requestLayout();
@ -2320,7 +2322,7 @@ class EditLine : EditWidgetBase {
@property dstring textToSetWidgetSize() { @property dstring textToSetWidgetSize() {
return _textToSetWidgetSize; return _textToSetWidgetSize;
} }
protected int[] _measuredTextToSetWidgetSizeWidths; protected int[] _measuredTextToSetWidgetSizeWidths;
protected dchar _passwordChar = 0; protected dchar _passwordChar = 0;
@ -2449,6 +2451,8 @@ class EditLine : EditWidgetBase {
/// handle keys /// handle keys
override bool onKeyEvent(KeyEvent event) { override bool onKeyEvent(KeyEvent event) {
if(super.onKeyEvent(event))
return true;
if (enterKey.assigned) { if (enterKey.assigned) {
if (event.keyCode == KeyCode.RETURN && event.modifiers == 0) { if (event.keyCode == KeyCode.RETURN && event.modifiers == 0) {
if (event.action == KeyAction.KeyDown) if (event.action == KeyAction.KeyDown)
@ -2459,7 +2463,7 @@ class EditLine : EditWidgetBase {
} }
} }
} }
return super.onKeyEvent(event); return true;
} }
/// process mouse event; return true if event is processed by widget. /// process mouse event; return true if event is processed by widget.
@ -2537,16 +2541,16 @@ class SpinCtrl : HorizontalLayout {
TextWidget label; TextWidget label;
int min, max; int min, max;
private EditLine linEdit; private EditLine linEdit;
private Button butUp, butDown; private Button butUp, butDown;
@property int value() { return linEdit.text.to!int; } @property int value() { return linEdit.text.to!int; }
@property void value(int val) { @property void value(int val) {
linEdit.text = val.to!dstring; linEdit.text = val.to!dstring;
} }
override @property bool enabled() { return linEdit.enabled; } override @property bool enabled() { return linEdit.enabled; }
alias enabled = Widget.enabled; alias enabled = Widget.enabled;
@property void enabled(bool status) { @property void enabled(bool status) {
@ -2634,10 +2638,10 @@ class SpinCtrl : HorizontalLayout {
linEdit.text = (val - 1).to!dstring; linEdit.text = (val - 1).to!dstring;
return true; return true;
}; };
enabled = true; enabled = true;
} }
} }
/// multiline editor /// multiline editor
@ -2687,14 +2691,14 @@ class EditBox : EditWidgetBase {
{ {
_needRewrap = true; _needRewrap = true;
} }
override @property int fontSize() const { return super.fontSize(); } override @property int fontSize() const { return super.fontSize(); }
override @property Widget fontSize(int size) { override @property Widget fontSize(int size) {
// Need to rewrap if fontSize changed // Need to rewrap if fontSize changed
_needRewrap = true; _needRewrap = true;
return super.fontSize(size); return super.fontSize(size);
} }
override protected int lineCount() { override protected int lineCount() {
return _content.length; return _content.length;
} }
@ -3545,7 +3549,7 @@ class EditBox : EditWidgetBase {
buf.fillRect(rc, color); buf.fillRect(rc, color);
} }
} }
/// Used in place of directly calling buf.fillRect in word wrap mode /// Used in place of directly calling buf.fillRect in word wrap mode
void wordWrapFillRect(DrawBuf buf, int line, Rect lineToDivide, uint color) void wordWrapFillRect(DrawBuf buf, int line, Rect lineToDivide, uint color)
{ {
@ -3828,10 +3832,10 @@ class EditBox : EditWidgetBase {
//TODO: Don't erase spans which have not been modified, cache them //TODO: Don't erase spans which have not been modified, cache them
_span = []; _span = [];
} }
private bool _needRewrap = true; private bool _needRewrap = true;
private int lastStartingLine; private int lastStartingLine;
override protected void drawClient(DrawBuf buf) { override protected void drawClient(DrawBuf buf) {
// update matched braces // update matched braces
if (!content.findMatchedBraces(_caretPos, _matchingBraces)) { if (!content.findMatchedBraces(_caretPos, _matchingBraces)) {
@ -3840,7 +3844,7 @@ class EditBox : EditWidgetBase {
} }
Rect rc = _clientRect; Rect rc = _clientRect;
if (_contentChanged) if (_contentChanged)
_needRewrap = true; _needRewrap = true;
if (lastStartingLine != _firstVisibleLine) if (lastStartingLine != _firstVisibleLine)