From f3c1c95a57f0af43bdfa6fa7146bc996f1ccc557 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 7 Mar 2023 19:13:08 -0500 Subject: [PATCH] Keyboard control for sliders --- backends/x/window.go | 30 ++++++++++++++++++++---------- elements/basic/slider.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/backends/x/window.go b/backends/x/window.go index 37d2466..8150076 100644 --- a/backends/x/window.go +++ b/backends/x/window.go @@ -238,6 +238,9 @@ func (window *Window) reallocateCanvas () { newHeight := window.metrics.height larger := newWidth > previousWidth || newHeight > previousHeight smaller := newWidth < previousWidth / 2 || newHeight < previousHeight / 2 + + allocStep := 128 + if larger || smaller { if window.xCanvas != nil { window.xCanvas.Destroy() @@ -246,8 +249,8 @@ func (window *Window) reallocateCanvas () { window.backend.connection, image.Rect ( 0, 0, - (newWidth / 64) * 64 + 64, - (newHeight / 64) * 64 + 64)) + (newWidth / allocStep + 1) * allocStep, + (newHeight / allocStep + 1) * allocStep)) window.xCanvas.CreatePixmap() } @@ -291,16 +294,23 @@ func (window *Window) childDrawCallback (region canvas.Canvas) { func (window *Window) paste (canvas canvas.Canvas) (updatedRegion image.Rectangle) { data, stride := canvas.Buffer() bounds := canvas.Bounds().Intersect(window.xCanvas.Bounds()) + + dstStride := window.xCanvas.Stride + dstData := window.xCanvas.Pix + // debug.PrintStack() - for x := bounds.Min.X; x < bounds.Max.X; x ++ { for y := bounds.Min.Y; y < bounds.Max.Y; y ++ { - rgba := data[x + y * stride] - index := x * 4 + y * window.xCanvas.Stride - window.xCanvas.Pix[index + 0] = rgba.B - window.xCanvas.Pix[index + 1] = rgba.G - window.xCanvas.Pix[index + 2] = rgba.R - window.xCanvas.Pix[index + 3] = rgba.A - }} + srcYComponent := y * stride + dstYComponent := y * dstStride + for x := bounds.Min.X; x < bounds.Max.X; x ++ { + rgba := data[srcYComponent + x] + index := dstYComponent + x * 4 + dstData[index + 0] = rgba.B + dstData[index + 1] = rgba.G + dstData[index + 2] = rgba.R + dstData[index + 3] = rgba.A + } + } return bounds } diff --git a/elements/basic/slider.go b/elements/basic/slider.go index bf6efaa..78450cc 100644 --- a/elements/basic/slider.go +++ b/elements/basic/slider.go @@ -83,7 +83,24 @@ func (element *Slider) HandleMouseMove (x, y int) { func (element *Slider) HandleMouseScroll (x, y int, deltaX, deltaY float64) { } func (element *Slider) HandleKeyDown (key input.Key, modifiers input.Modifiers) { - // TODO: handle left and right arrows + switch key { + case input.KeyUp: + element.changeValue(0.1) + case input.KeyDown: + element.changeValue(-0.1) + case input.KeyRight: + if element.vertical { + element.changeValue(-0.1) + } else { + element.changeValue(0.1) + } + case input.KeyLeft: + if element.vertical { + element.changeValue(0.1) + } else { + element.changeValue(-0.1) + } + } } func (element *Slider) HandleKeyUp (key input.Key, modifiers input.Modifiers) { } @@ -135,6 +152,20 @@ func (element *Slider) SetConfig (new config.Config) { element.redo() } +func (element *Slider) changeValue (delta float64) { + element.value += delta + if element.value < 0 { + element.value = 0 + } + if element.value > 1 { + element.value = 1 + } + if element.onRelease != nil { + element.onRelease() + } + element.redo() +} + func (element *Slider) valueFor (x, y int) (value float64) { if element.vertical { value =