Skip to content

Commit

Permalink
ui/textfield: Add OnChange callback
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed May 22, 2024
1 parent 604bca9 commit fdbaee1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 47 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,22 @@ Explanation of the status column: \
⚠️ Partially implemented /
✅ Done

| Name | Description | Native controls used | Status |
| --------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| [Button](https://pkg.go.dev/github.com/roblillack/spot/ui#Button) | Simple button to initiate an action | [Fl_Button](https://www.fltk.org/doc-1.4/classFl__Button.html) <br> NSButton ||
| [Checkbox](https://pkg.go.dev/github.com/roblillack/spot/ui#Checkbox) | Control offering the user a choice between two mutually exclusive options | [Fl_Check_Button](https://www.fltk.org/doc-1.4/classFl__Check__Button.html) <br> NSButton ||
| ComboBox | A combined dropdown menu with text input | ComboBox <br> NSComboBox | Not started |
| [Dial](https://pkg.go.dev/github.com/roblillack/spot/ui#Dial) | Circular status control | [Fl_Dial](https://www.fltk.org/doc-1.4/classFl__Dial.html) <br> NSProgressIndicator (with `NSCircular` style) | ⚠️ |
| [Dropdown](https://pkg.go.dev/github.com/roblillack/spot/ui#Dropdown) | Drop-down menu to select a single item out of multiple options | [Fl_Choice](https://www.fltk.org/doc-1.4/classFl__Choice.html) <br> NSPopUpButton ||
| Image | An image control | Image <br> NSImageView | Not started |
| [Label](https://pkg.go.dev/github.com/roblillack/spot/ui#Label) | Simple, non-editable text label | [Fl_Box](https://www.fltk.org/doc-1.4/classFl__Box.html) <br> [NSTextField](https://developer.apple.com/documentation/appkit/nstextfield) ||
| ListBox | A list control | List <br> NSTableView | 🚧 |
| [ProgressBar](https://pkg.go.dev/github.com/roblillack/spot/ui#ProgressBar) | Progress bar control to visualize the progression of a long-running operation | Progress <br> NSProgress ||
| [Slider](https://pkg.go.dev/github.com/roblillack/spot/ui#Slider) | Horizontal slider input control | [Fl_Slider](https://www.fltk.org/doc-1.4/classFl__Slider.html) <br> NSSlider ||
| [Spinner](https://pkg.go.dev/github.com/roblillack/spot/ui#Spinner) | Number input control with up/down buttons | [Fl_Spinner](https://www.fltk.org/doc-1.4/classFl__Spinner.html) <br> NSStepper | ⚠️ |
| [TextField](https://pkg.go.dev/github.com/roblillack/spot/ui#TextField) | Control for single-line text input | [Fl_Input](https://www.fltk.org/doc-1.4/classFl__Input.html) <br> NSTextField ||
| TextView/TextEditor | General-purpose text box to view/edit multi-line text content | Text <br> NSTextView | 🚧 |
| [Window](https://pkg.go.dev/github.com/roblillack/spot/ui#Window) | Control representing a (top-level) window on the screen | [Fl_Window](https://www.fltk.org/doc-1.4/classFl__Window.html) <br> NSWindow ||
| Name | Description | Native controls used | Status |
| --------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| [Button](https://pkg.go.dev/github.com/roblillack/spot/ui#Button) | Simple button to initiate an action | [Fl_Button](https://www.fltk.org/doc-1.4/classFl__Button.html) <br> NSButton ||
| [Checkbox](https://pkg.go.dev/github.com/roblillack/spot/ui#Checkbox) | Control offering the user a choice between two mutually exclusive options | [Fl_Check_Button](https://www.fltk.org/doc-1.4/classFl__Check__Button.html) <br> NSButton ||
| ComboBox | A combined dropdown menu with text input | ComboBox <br> NSComboBox | Not started |
| [Dial](https://pkg.go.dev/github.com/roblillack/spot/ui#Dial) | Circular status control | [Fl_Dial](https://www.fltk.org/doc-1.4/classFl__Dial.html) <br> NSProgressIndicator (with `NSCircular` style) | ⚠️ |
| [Dropdown](https://pkg.go.dev/github.com/roblillack/spot/ui#Dropdown) | Drop-down menu to select a single item out of multiple options | [Fl_Choice](https://www.fltk.org/doc-1.4/classFl__Choice.html) <br> NSPopUpButton ||
| Image | An image control | Image <br> NSImageView | Not started |
| [Label](https://pkg.go.dev/github.com/roblillack/spot/ui#Label) | Simple, non-editable text label | [Fl_Box](https://www.fltk.org/doc-1.4/classFl__Box.html) <br> [NSTextField](https://developer.apple.com/documentation/appkit/nstextfield) ||
| ListBox | A list control | List <br> NSTableView | 🚧 |
| [ProgressBar](https://pkg.go.dev/github.com/roblillack/spot/ui#ProgressBar) | Progress bar control to visualize the progression of a long-running operation | Progress <br> NSProgress ||
| [Slider](https://pkg.go.dev/github.com/roblillack/spot/ui#Slider) | Horizontal slider input control | [Fl_Slider](https://www.fltk.org/doc-1.4/classFl__Slider.html) <br> NSSlider ||
| [Spinner](https://pkg.go.dev/github.com/roblillack/spot/ui#Spinner) | Number input control with up/down buttons | [Fl_Spinner](https://www.fltk.org/doc-1.4/classFl__Spinner.html) <br> NSStepper | ⚠️ |
| [TextField](https://pkg.go.dev/github.com/roblillack/spot/ui#TextField) | Control for single-line text input | [Fl_Input](https://www.fltk.org/doc-1.4/classFl__Input.html) <br> [NSTextField](https://developer.apple.com/documentation/appkit/nstextfield) ||
| TextView/TextEditor | General-purpose text box to view/edit multi-line text content | Text <br> NSTextView | 🚧 |
| [Window](https://pkg.go.dev/github.com/roblillack/spot/ui#Window) | Control representing a (top-level) window on the screen | [Fl_Window](https://www.fltk.org/doc-1.4/classFl__Window.html) <br> NSWindow ||

## Potential future backends to look at

Expand Down
13 changes: 7 additions & 6 deletions ui/textfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
)

type TextField struct {
X int
Y int
Width int
Height int
Value string
FontSize int
X int
Y int
Width int
Height int
Value string
// FontSize int
OnChange func(value string)
// Editable bool
// Bezeled bool
// Selectable bool
Expand Down
18 changes: 11 additions & 7 deletions ui/textfield_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (w *TextField) Update(nextComponent spot.Control) bool {
// }
// }

if next.FontSize != w.FontSize {
w.FontSize = next.FontSize
if w.ref != nil {
w.ref.SetFontSize(w.FontSize)
}
}
// if next.FontSize != w.FontSize {
// w.FontSize = next.FontSize
// if w.ref != nil {
// w.ref.SetFontSize(w.FontSize)
// }
// }

// if next.NoBackground != w.NoBackground {
// w.NoBackground = next.NoBackground
Expand All @@ -57,6 +57,9 @@ func (w *TextField) Update(nextComponent spot.Control) bool {
// }
// }

w.OnChange = next.OnChange
w.ref.OnChange(w.OnChange)

return true
}

Expand All @@ -71,7 +74,8 @@ func (w *TextField) Mount(parent spot.Control) any {
// w.ref.SetSelectable(w.Selectable)
w.ref.SetStringValue(w.Value)
w.ref.SetFontFamily("Arial")
w.ref.SetFontSize(w.FontSize)
w.ref.OnChange(w.OnChange)
// w.ref.SetFontSize(w.FontSize)
// w.ref.SetDrawsBackground(!w.NoBackground)

if window, ok := parent.(*Window); ok && window != nil && window.ref != nil {
Expand Down
52 changes: 34 additions & 18 deletions ui/textfield_fltk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/roblillack/spot"
)

type nativeTypeTextField = *goFltk.TextEditor
type nativeTypeTextField = *goFltk.Input

func (w *TextField) Update(nextComponent spot.Control) bool {
next, ok := nextComponent.(*TextField)
Expand All @@ -22,19 +22,22 @@ func (w *TextField) Update(nextComponent spot.Control) bool {
if next.Value != w.Value {
w.Value = next.Value
if w.ref != nil {
if w.ref.Buffer() == nil {
w.ref.SetBuffer(goFltk.NewTextBuffer())
}
w.ref.Buffer().SetText(w.Value)
// if w.ref.Buffer() == nil {
// w.ref.SetBuffer(goFltk.NewTextBuffer())
// }
// w.ref.Buffer().SetText(w.Value)
w.ref.SetValue(w.Value)
}
}

if next.FontSize != w.FontSize {
w.FontSize = next.FontSize
if w.ref != nil && w.FontSize > 0 {
w.ref.SetTextSize(w.FontSize)
}
}
// if next.FontSize != w.FontSize {
// w.FontSize = next.FontSize
// if w.ref != nil && w.FontSize > 0 {
// w.ref.SetTextSize(w.FontSize)
// }
// }

w.OnChange = next.OnChange

return true
}
Expand All @@ -44,14 +47,17 @@ func (w *TextField) Mount(parent spot.Control) any {
return w.ref
}

w.ref = goFltk.NewTextEditor(w.X, w.Y, w.Width, w.Height)
w.ref.SetBuffer(goFltk.NewTextBuffer())
w.ref.Buffer().SetText(w.Value)
w.ref = goFltk.NewInput(w.X, w.Y, w.Width, w.Height)
// w.ref.SetBuffer(goFltk.NewTextBuffer())
// w.ref.Buffer().SetText(w.Value)
w.ref.SetValue(w.Value)
// w.ref.Deactivate()
w.ref.SetWrapMode(goFltk.WRAP_AT_BOUNDS, 0)
if w.FontSize > 0 {
w.ref.SetTextSize(w.FontSize)
}
// w.ref.SetWrapMode(goFltk.WRAP_AT_BOUNDS, 0)
// if w.FontSize > 0 {
// w.ref.SetTextSize(w.FontSize)
// }
w.ref.SetCallback(w.callback)
w.ref.SetCallbackCondition(goFltk.WhenChanged)

if window, ok := parent.(*Window); ok && window != nil && window.ref != nil {
window.ref.Add(w.ref)
Expand All @@ -60,4 +66,14 @@ func (w *TextField) Mount(parent spot.Control) any {
return w.ref
}

func (w *TextField) callback() {
if w.OnChange != nil {
val := w.ref.Value()
if val != w.Value {
w.Value = val
w.OnChange(val)
}
}
}

var _ spot.Control = &TextField{}

0 comments on commit fdbaee1

Please sign in to comment.