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

move the cursor when entry is resized #3430

Merged
merged 2 commits into from
Nov 28, 2022
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
18 changes: 18 additions & 0 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,13 +1302,31 @@ func (r *entryRenderer) Layout(size fyne.Size) {
r.entry.placeholderProvider().inset = fyne.NewSize(0, theme.InputBorderSize())
entrySize := size.Subtract(fyne.NewSize(r.trailingInset(), theme.InputBorderSize()*2))
entryPos := fyne.NewPos(0, theme.InputBorderSize())

r.entry.propertyLock.Lock()
textPos := r.entry.textPosFromRowCol(r.entry.CursorRow, r.entry.CursorColumn)
selectPos := r.entry.textPosFromRowCol(r.entry.selectRow, r.entry.selectColumn)
r.entry.propertyLock.Unlock()
if r.entry.Wrapping == fyne.TextWrapOff {
r.entry.content.Resize(entrySize)
r.entry.content.Move(entryPos)
} else {
r.scroll.Resize(entrySize)
r.scroll.Move(entryPos)
}

r.entry.propertyLock.Lock()
resizedTextPos := r.entry.textPosFromRowCol(r.entry.CursorRow, r.entry.CursorColumn)
r.entry.propertyLock.Unlock()
if textPos != resizedTextPos {
r.entry.setFieldsAndRefresh(func() {
r.entry.CursorRow, r.entry.CursorColumn = r.entry.rowColFromTextPos(textPos)

if r.entry.selecting {
r.entry.selectRow, r.entry.selectColumn = r.entry.rowColFromTextPos(selectPos)
}
})
}
}

// MinSize calculates the minimum size of an entry widget.
Expand Down
16 changes: 12 additions & 4 deletions widget/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,26 @@ func TestEntry_CursorColumn_Wrap(t *testing.T) {
func TestEntry_CursorColumn_Wrap2(t *testing.T) {
entry := widget.NewMultiLineEntry()
entry.Wrapping = fyne.TextWrapWord
entry.Resize(fyne.NewSize(68, 64))
entry.SetText("1234")
entry.CursorColumn = 3

w := test.NewWindow(entry)
w.Resize(fyne.NewSize(72, 64))

test.Type(entry, "a")
test.Type(entry, "b")
test.Type(entry, "c")
assert.Equal(t, 0, entry.CursorColumn)
assert.Equal(t, 1, entry.CursorRow)
w := test.NewWindow(entry)
// TODO this was broken a while back, raised with https://github.com/fyne-io/fyne/issues/3024
//w.Resize(fyne.NewSize(64, 70))
w.Resize(fyne.NewSize(70, 70))
assert.Equal(t, 1, entry.CursorColumn)
test.AssertImageMatches(t, "entry/wrap_multi_line_cursor.png", w.Canvas().Capture())

typeKeys(entry, keyShiftLeftDown, fyne.KeyLeft, keyShiftLeftUp)
assert.Equal(t, "c", entry.SelectedText())
assert.Equal(t, 0, entry.CursorColumn)
w.Resize(fyne.NewSize(64, 78))
assert.Equal(t, "c", entry.SelectedText())
}

func TestEntry_CursorPasswordRevealer(t *testing.T) {
Expand Down
Binary file modified widget/testdata/entry/wrap_multi_line_cursor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.