From d24e6b88f2302313fdf466c42be6cfce7b662a9e Mon Sep 17 00:00:00 2001 From: Robert Lillack Date: Mon, 27 May 2024 12:24:09 +0200 Subject: [PATCH] slider: Improve handling value updates. --- ui/slider.go | 19 +++++++++---------- ui/slider_cocoa.go | 31 +++++++++++++++++++++---------- ui/slider_fltk.go | 1 - 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/ui/slider.go b/ui/slider.go index a3a491c..1218df3 100644 --- a/ui/slider.go +++ b/ui/slider.go @@ -3,17 +3,16 @@ package ui import "github.com/roblillack/spot" type Slider struct { - X int - Y int - Width int - Height int - Min float64 - Max float64 - Value float64 - // Type goFltk.SliderType - // Type gocoa.SliderType + X int + Y int + Width int + Height int + Min float64 + Max float64 + Value float64 OnValueChanged func(float64) - ref nativeTypeSlider + + ref nativeTypeSlider } var _ spot.Component = &Slider{} diff --git a/ui/slider_cocoa.go b/ui/slider_cocoa.go index 54d6028..be4f1aa 100644 --- a/ui/slider_cocoa.go +++ b/ui/slider_cocoa.go @@ -34,10 +34,7 @@ func (b *Slider) Update(nextComponent spot.Control) bool { b.ref.SetValue(b.Value) } - // if next.Type != b.Type { - // b.Type = next.Type - // b.ref.SetSliderType(b.Type) - // } + b.OnValueChanged = next.OnValueChanged return true } @@ -51,12 +48,7 @@ func (b *Slider) Mount(parent spot.Control) any { b.ref.SetMaximumValue(b.Max) b.ref.SetMinimumValue(b.Min) b.ref.SetValue(b.Value) - // b.ref.SetSliderType(b.Type) - b.ref.OnSliderValueChanged(func() { - if b.OnValueChanged != nil { - b.OnValueChanged(b.ref.Value()) - } - }) + b.ref.OnSliderValueChanged(b.callback) if window, ok := parent.(*Window); ok && window != nil && window.ref != nil { window.ref.AddSlider(b.ref) @@ -64,3 +56,22 @@ func (b *Slider) Mount(parent spot.Control) any { return b.ref } + +func (c *Slider) Unmount() { + if c.ref == nil { + return + } + + c.ref.Remove() + c.ref = nil +} + +func (c *Slider) callback() { + if c.ref == nil { + return + } + + if c.OnValueChanged != nil { + c.OnValueChanged(c.ref.Value()) + } +} diff --git a/ui/slider_fltk.go b/ui/slider_fltk.go index e54f41b..c066e62 100644 --- a/ui/slider_fltk.go +++ b/ui/slider_fltk.go @@ -69,7 +69,6 @@ func (c *Slider) Mount(parent spot.Control) any { func (c *Slider) Unmount() { if c.ref != nil { - // c.ref.Parent().Remove(c.ref) c.ref.Destroy() c.ref = nil }