Add OnEnter to Swatch and ColorPicker

This commit is contained in:
Sasha Koshka 2024-06-26 11:20:53 -04:00
parent b9f980e7fd
commit d0ee6c432c
2 changed files with 30 additions and 4 deletions

View File

@ -49,11 +49,17 @@ func (this *LabelSwatch) RGBA () (r, g, b, a uint32) {
}
// OnValueChange specifies a function to be called when the swatch's color
// changes.
// is changed by the user.
func (this *LabelSwatch) OnValueChange (callback func ()) event.Cookie {
return this.swatch.OnValueChange(callback)
}
// OnEnter specifies a function to be called when the user selects "OK" in the
// color picker.
func (this *LabelSwatch) OnEnter (callback func ()) event.Cookie {
return this.swatch.OnEnter(callback)
}
func (this *LabelSwatch) handleMouseUp (button input.Button) {
if button != input.ButtonLeft { return }
if this.MousePosition().In(this.Bounds()) {

View File

@ -17,6 +17,7 @@ type Swatch struct {
label string
on struct {
valueChange event.FuncBroadcaster
enter event.FuncBroadcaster
}
}
@ -49,15 +50,22 @@ func (this *Swatch) Value () color.Color {
// RGBA satisfies the color.Color interface
func (this *Swatch) RGBA () (r, g, b, a uint32) {
if this.value == nil { return }
return this.value.RGBA()
}
// OnValueChange specifies a function to be called when the swatch's color
// changes.
// is changed by the user.
func (this *Swatch) OnValueChange (callback func ()) event.Cookie {
return this.on.valueChange.Connect(callback)
}
// OnEnter specifies a function to be called when the user selects "OK" in the
// color picker.
func (this *Swatch) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
// Choose creates a modal that allows the user to edit the color of the swatch.
func (this *Swatch) Choose () {
if this.editing { return }
@ -65,7 +73,7 @@ func (this *Swatch) Choose () {
var err error
var window tomo.Window
if parent := this.Window(); parent != nil {
window, err = parent.NewModal(image.Rectangle { })
window, err = parent.NewChild(image.Rectangle { })
} else {
window, err = tomo.NewWindow(image.Rectangle { })
}
@ -79,22 +87,26 @@ func (this *Swatch) Choose () {
window.SetTitle(this.label)
}
committed := false
colorPicker := NewColorPicker(this.Value())
colorPicker.OnValueChange(func () {
this.userSetValue(colorPicker.Value())
})
hexInput := NewTextInput("TODO")
colorMemory := this.value
cancelButton := NewButton("Cancel")
cancelButton.SetIcon(tomo.IconDialogCancel)
cancelButton.OnClick(func () {
this.userSetValue(colorMemory)
window.Close()
})
okButton := NewButton("OK")
okButton.SetFocused(true)
okButton.SetIcon(tomo.IconDialogOkay)
okButton.OnClick(func () {
committed = true
window.Close()
})
@ -106,8 +118,16 @@ func (this *Swatch) Choose () {
window.SetRoot(NewOuterContainer (
layouts.Column { true, false },
colorPicker,
NewInnerContainer(layouts.Row { false, true },
NewLabel("Hex"),
hexInput),
controlRow))
window.OnClose(func () {
if committed {
this.on.enter.Broadcast()
} else {
this.userSetValue(colorMemory)
}
this.editing = false
})
this.editing = true