2018-06-02 10:47:37 +00:00
|
|
|
package browsh
|
|
|
|
|
|
|
|
func (i *inputBox) renderCursor() {
|
2018-07-11 09:00:06 +00:00
|
|
|
if !i.isActive {
|
|
|
|
return
|
|
|
|
}
|
2018-06-03 05:12:31 +00:00
|
|
|
if i.isSelection() {
|
|
|
|
i.renderSelectionCursor()
|
|
|
|
} else {
|
|
|
|
i.renderSingleCursor()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) isSelection() bool {
|
|
|
|
return i.selectionStart > 0 || i.selectionEnd > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) renderSingleCursor() {
|
|
|
|
x, y := i.getCoordsOfCursor()
|
|
|
|
reverseCellColour(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) renderSelectionCursor() {
|
|
|
|
var x, y int
|
2018-07-16 09:58:53 +00:00
|
|
|
textLength := len(i.text)
|
2018-06-03 05:12:31 +00:00
|
|
|
for index := 0; index < textLength; index++ {
|
|
|
|
x, y = i.getCoordsOfIndex(index)
|
|
|
|
if x >= i.selectionStart && x < i.selectionEnd {
|
|
|
|
reverseCellColour(x, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) getCoordsOfCursor() (int, int) {
|
|
|
|
var index int
|
|
|
|
if i.isMultiLine() {
|
|
|
|
index = i.xCursor
|
|
|
|
} else {
|
|
|
|
index = i.textCursor
|
|
|
|
}
|
|
|
|
return i.getCoordsOfIndex(index)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) getCoordsOfIndex(index int) (int, int) {
|
2018-06-02 10:47:37 +00:00
|
|
|
xFrameOffset := CurrentTab.frame.xScroll
|
|
|
|
yFrameOffset := CurrentTab.frame.yScroll - uiHeight
|
|
|
|
if urlInputBox.isActive {
|
|
|
|
xFrameOffset = 0
|
|
|
|
yFrameOffset = 0
|
|
|
|
}
|
2018-06-03 05:12:31 +00:00
|
|
|
x := (i.X + index) - i.xScroll - xFrameOffset
|
2018-06-02 10:47:37 +00:00
|
|
|
y := (i.Y + i.yCursor) - i.yScroll - yFrameOffset
|
2018-06-03 05:12:31 +00:00
|
|
|
return x, y
|
2018-06-02 10:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) cursorLeft() {
|
|
|
|
i.xCursor--
|
|
|
|
i.textCursor--
|
|
|
|
i.updateAllCursors()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) cursorRight() {
|
|
|
|
i.xCursor++
|
|
|
|
i.textCursor++
|
|
|
|
i.updateAllCursors()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) cursorUp() {
|
|
|
|
i.multiLiner.moveYCursorBy(-1)
|
|
|
|
i.updateAllCursors()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) cursorDown() {
|
|
|
|
i.multiLiner.moveYCursorBy(1)
|
|
|
|
i.updateAllCursors()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) cursorBackspace() {
|
2018-07-16 09:58:53 +00:00
|
|
|
if len(i.text) == 0 {
|
2018-07-11 09:00:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if i.textCursor == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
start := i.text[:i.textCursor-1]
|
2018-06-02 10:47:37 +00:00
|
|
|
end := i.text[i.textCursor:]
|
2018-07-16 09:58:53 +00:00
|
|
|
i.text = append(start, end...)
|
2018-06-02 10:47:37 +00:00
|
|
|
i.cursorLeft()
|
|
|
|
i.sendInputBoxToBrowser()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) cursorInsertRune(theRune rune) {
|
|
|
|
start := i.text[:i.textCursor]
|
|
|
|
end := i.text[i.textCursor:]
|
2018-07-16 09:58:53 +00:00
|
|
|
endWithRune := append([]rune{theRune}, end...)
|
|
|
|
i.text = append(start, endWithRune...)
|
2018-06-02 10:47:37 +00:00
|
|
|
i.cursorRight()
|
|
|
|
i.sendInputBoxToBrowser()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) isCursorOverRightEdge() bool {
|
2018-07-11 09:00:06 +00:00
|
|
|
return i.textCursor-i.xScroll >= i.Width
|
2018-06-02 10:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) isCursorOverLeftEdge() bool {
|
2018-07-11 09:00:06 +00:00
|
|
|
return i.textCursor-i.xScroll <= -1
|
2018-06-02 10:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) isCursorOverTopEdge() bool {
|
2018-07-11 09:00:06 +00:00
|
|
|
return i.yCursor-i.yScroll <= -1
|
2018-06-02 10:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) isCursorOverBottomEdge() bool {
|
2018-07-11 09:00:06 +00:00
|
|
|
return i.yCursor-i.yScroll > i.Height
|
2018-06-02 10:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) putCursorAtEnd() {
|
2018-07-16 09:58:53 +00:00
|
|
|
i.textCursor = len(urlInputBox.text)
|
2018-06-02 10:47:37 +00:00
|
|
|
// TODO: Do for multiline
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) updateAllCursors() {
|
|
|
|
i.updateXYCursors()
|
2018-07-11 09:00:06 +00:00
|
|
|
if i.isCursorOverLeftEdge() || !i.isBestFit() {
|
|
|
|
i.xScrollBy(-1)
|
|
|
|
}
|
|
|
|
if i.isCursorOverTopEdge() {
|
|
|
|
i.yScrollBy(-1)
|
|
|
|
}
|
|
|
|
if i.isCursorOverRightEdge() {
|
|
|
|
i.xScrollBy(1)
|
|
|
|
}
|
|
|
|
if i.isCursorOverBottomEdge() {
|
|
|
|
i.yScrollBy(1)
|
|
|
|
}
|
2018-06-02 10:47:37 +00:00
|
|
|
i.limitTextCursor()
|
|
|
|
i.updateXYCursors()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) limitTextCursor() {
|
2018-07-11 09:00:06 +00:00
|
|
|
if i.textCursor < 0 {
|
2018-06-02 10:47:37 +00:00
|
|
|
i.textCursor = 0
|
|
|
|
}
|
2018-07-16 09:58:53 +00:00
|
|
|
if i.textCursor > len(i.text) {
|
|
|
|
i.textCursor = len(i.text)
|
2018-06-02 10:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inputBox) updateXYCursors() {
|
2018-07-11 09:00:06 +00:00
|
|
|
if !i.isMultiLine() {
|
|
|
|
return
|
|
|
|
}
|
2018-06-02 10:47:37 +00:00
|
|
|
i.multiLiner.updateCursor()
|
|
|
|
i.renderCursor()
|
|
|
|
}
|