Skip to content

Commit

Permalink
exp/textinput: refactoring: better variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Dec 26, 2024
1 parent 38bff15 commit 25b575a
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions exp/textinput/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func isFieldFocused(f *Field) bool {
//
// For an actual usage, see the examples "textinput".
type Field struct {
text string
selectionStart int
selectionEnd int
text string
selectionStartInBytes int
selectionEndInBytes int

ch chan State
end func()
Expand Down Expand Up @@ -126,9 +126,9 @@ func (f *Field) HandleInput(x, y int) (handled bool, err error) {
}
handled = true
if state.Committed {
f.text = f.text[:f.selectionStart] + state.Text + f.text[f.selectionEnd:]
f.selectionStart += len(state.Text)
f.selectionEnd = f.selectionStart
f.text = f.text[:f.selectionStartInBytes] + state.Text + f.text[f.selectionEndInBytes:]
f.selectionStartInBytes += len(state.Text)
f.selectionEndInBytes = f.selectionStartInBytes
f.state = State{}
continue
}
Expand Down Expand Up @@ -181,9 +181,9 @@ func (f *Field) cleanUp() {
return
}
if ok && state.Committed {
f.text = f.text[:f.selectionStart] + state.Text + f.text[f.selectionEnd:]
f.selectionStart += len(state.Text)
f.selectionEnd = f.selectionStart
f.text = f.text[:f.selectionStartInBytes] + state.Text + f.text[f.selectionEndInBytes:]
f.selectionStartInBytes += len(state.Text)
f.selectionEndInBytes = f.selectionStartInBytes
f.state = State{}
}
f.state = state
Expand All @@ -201,25 +201,25 @@ func (f *Field) cleanUp() {
}

// Selection returns the current selection range in bytes.
func (f *Field) Selection() (start, end int) {
return f.selectionStart, f.selectionEnd
func (f *Field) Selection() (startInBytes, endInBytes int) {
return f.selectionStartInBytes, f.selectionEndInBytes
}

// CompositionSelection returns the current composition selection in bytes if a text is composited.
// If a text is not composited, this returns 0s and false.
// The returned values indicate relative positions in bytes where the current composition text's start is 0.
func (f *Field) CompositionSelection() (start, end int, ok bool) {
func (f *Field) CompositionSelection() (startInBytes, endInBytes int, ok bool) {
if f.IsFocused() && f.state.Text != "" {
return f.state.CompositionSelectionStartInBytes, f.state.CompositionSelectionEndInBytes, true
}
return 0, 0, false
}

// SetSelection sets the selection range.
func (f *Field) SetSelection(start, end int) {
func (f *Field) SetSelection(startInBytes, endInBytes int) {
f.cleanUp()
f.selectionStart = start
f.selectionEnd = end
f.selectionStartInBytes = startInBytes
f.selectionEndInBytes = endInBytes
}

// Text returns the current text.
Expand All @@ -232,15 +232,15 @@ func (f *Field) Text() string {
// The returned value includes compositing texts.
func (f *Field) TextForRendering() string {
if f.IsFocused() && f.state.Text != "" {
return f.text[:f.selectionStart] + f.state.Text + f.text[f.selectionEnd:]
return f.text[:f.selectionStartInBytes] + f.state.Text + f.text[f.selectionEndInBytes:]
}
return f.text
}

// SetTextAndSelection sets the text and the selection range.
func (f *Field) SetTextAndSelection(text string, selectionStart, selectionEnd int) {
func (f *Field) SetTextAndSelection(text string, selectionStartInBytes, selectionEndInBytes int) {
f.cleanUp()
f.text = text
f.selectionStart = selectionStart
f.selectionEnd = selectionEnd
f.selectionStartInBytes = selectionStartInBytes
f.selectionEndInBytes = selectionEndInBytes
}

0 comments on commit 25b575a

Please sign in to comment.