exp/textinput: refactoring: better variable names

This commit is contained in:
Hajime Hoshi 2024-12-26 19:03:43 +09:00
parent 38bff15e02
commit 25b575a7b5

View File

@ -71,9 +71,9 @@ func isFieldFocused(f *Field) bool {
// //
// For an actual usage, see the examples "textinput". // For an actual usage, see the examples "textinput".
type Field struct { type Field struct {
text string text string
selectionStart int selectionStartInBytes int
selectionEnd int selectionEndInBytes int
ch chan State ch chan State
end func() end func()
@ -126,9 +126,9 @@ func (f *Field) HandleInput(x, y int) (handled bool, err error) {
} }
handled = true handled = true
if state.Committed { if state.Committed {
f.text = f.text[:f.selectionStart] + state.Text + f.text[f.selectionEnd:] f.text = f.text[:f.selectionStartInBytes] + state.Text + f.text[f.selectionEndInBytes:]
f.selectionStart += len(state.Text) f.selectionStartInBytes += len(state.Text)
f.selectionEnd = f.selectionStart f.selectionEndInBytes = f.selectionStartInBytes
f.state = State{} f.state = State{}
continue continue
} }
@ -181,9 +181,9 @@ func (f *Field) cleanUp() {
return return
} }
if ok && state.Committed { if ok && state.Committed {
f.text = f.text[:f.selectionStart] + state.Text + f.text[f.selectionEnd:] f.text = f.text[:f.selectionStartInBytes] + state.Text + f.text[f.selectionEndInBytes:]
f.selectionStart += len(state.Text) f.selectionStartInBytes += len(state.Text)
f.selectionEnd = f.selectionStart f.selectionEndInBytes = f.selectionStartInBytes
f.state = State{} f.state = State{}
} }
f.state = state f.state = state
@ -201,14 +201,14 @@ func (f *Field) cleanUp() {
} }
// Selection returns the current selection range in bytes. // Selection returns the current selection range in bytes.
func (f *Field) Selection() (start, end int) { func (f *Field) Selection() (startInBytes, endInBytes int) {
return f.selectionStart, f.selectionEnd return f.selectionStartInBytes, f.selectionEndInBytes
} }
// CompositionSelection returns the current composition selection in bytes if a text is composited. // CompositionSelection returns the current composition selection in bytes if a text is composited.
// If a text is not composited, this returns 0s and false. // 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. // 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 != "" { if f.IsFocused() && f.state.Text != "" {
return f.state.CompositionSelectionStartInBytes, f.state.CompositionSelectionEndInBytes, true return f.state.CompositionSelectionStartInBytes, f.state.CompositionSelectionEndInBytes, true
} }
@ -216,10 +216,10 @@ func (f *Field) CompositionSelection() (start, end int, ok bool) {
} }
// SetSelection sets the selection range. // SetSelection sets the selection range.
func (f *Field) SetSelection(start, end int) { func (f *Field) SetSelection(startInBytes, endInBytes int) {
f.cleanUp() f.cleanUp()
f.selectionStart = start f.selectionStartInBytes = startInBytes
f.selectionEnd = end f.selectionEndInBytes = endInBytes
} }
// Text returns the current text. // Text returns the current text.
@ -232,15 +232,15 @@ func (f *Field) Text() string {
// The returned value includes compositing texts. // The returned value includes compositing texts.
func (f *Field) TextForRendering() string { func (f *Field) TextForRendering() string {
if f.IsFocused() && f.state.Text != "" { 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 return f.text
} }
// SetTextAndSelection sets the text and the selection range. // 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.cleanUp()
f.text = text f.text = text
f.selectionStart = selectionStart f.selectionStartInBytes = selectionStartInBytes
f.selectionEnd = selectionEnd f.selectionEndInBytes = selectionEndInBytes
} }