From 6e4310b9ad2653ef9bbfd2c2fc5241d4d43533c1 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 14 Apr 2023 23:58:14 -0400 Subject: [PATCH] Some X backend fixes --- backends/x/entity.go | 9 ++++++--- backends/x/event.go | 16 ++++++++-------- backends/x/window.go | 20 ++++++++++++-------- elements/button.go | 17 ++++++----------- elements/checkbox.go | 2 +- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/backends/x/entity.go b/backends/x/entity.go index 3a3f5ef..53b62ce 100644 --- a/backends/x/entity.go +++ b/backends/x/entity.go @@ -76,8 +76,11 @@ func (entity *entity) Window () tomo.Window { func (entity *entity) SetMinimumSize (width, height int) { entity.minWidth = width entity.minHeight = height - if entity.parent == nil { return } - entity.parent.element.(tomo.Container).HandleChildMinimumSizeChange() + if entity.parent == nil { + entity.window.setMinimumSize(width, height) + } else { + entity.parent.element.(tomo.Container).HandleChildMinimumSizeChange() + } } func (entity *entity) DrawBackground (destination canvas.Canvas, bounds image.Rectangle) { @@ -169,7 +172,7 @@ func (entity *entity) FocusPrevious () { // ----------- FlexibleEntity ----------- // -func (entity *entity) NotifyFlexibleHeightChange () { +func (entity *entity) NotifyFlexibleHeightChange (child tomo.Flexible) { if entity.parent == nil { return } if parent, ok := entity.parent.element.(tomo.FlexibleContainer); ok { parent.HandleChildFlexibleHeightChange() diff --git a/backends/x/event.go b/backends/x/event.go index 4088b86..73ea3c1 100644 --- a/backends/x/event.go +++ b/backends/x/event.go @@ -121,8 +121,7 @@ func (window *window) handleKeyPress ( connection *xgbutil.XUtil, event xevent.KeyPressEvent, ) { - if window.system.focused == nil { return } - if window.hasModal { return } + if window.hasModal { return } keyEvent := *event.KeyPressEvent key, numberPad := window.backend.keycodeToKey(keyEvent.Detail, keyEvent.State) @@ -149,7 +148,6 @@ func (window *window) handleKeyRelease ( connection *xgbutil.XUtil, event xevent.KeyReleaseEvent, ) { - if window.system.focused == nil { return } keyEvent := *event.KeyReleaseEvent // do not process this event if it was generated from a key repeat @@ -172,11 +170,13 @@ func (window *window) handleKeyRelease ( key, numberPad := window.backend.keycodeToKey(keyEvent.Detail, keyEvent.State) modifiers := window.modifiersFromState(keyEvent.State) modifiers.NumberPad = numberPad - - focused, ok := window.focused.element.(tomo.KeyboardTarget) - if ok { focused.HandleKeyUp(key, modifiers) } - - window.system.afterEvent() + + if window.focused != nil { + focused, ok := window.focused.element.(tomo.KeyboardTarget) + if ok { focused.HandleKeyUp(key, modifiers) } + + window.system.afterEvent() + } } func (window *window) handleButtonPress ( diff --git a/backends/x/window.go b/backends/x/window.go index eb661ac..2bd12f3 100644 --- a/backends/x/window.go +++ b/backends/x/window.go @@ -48,9 +48,6 @@ func (backend *Backend) NewWindow ( ) { if backend == nil { panic("nil backend") } window, err := backend.newWindow(bounds, false) - - window.system.initialize() - window.system.pushFunc = window.paste output = mainWindow { window } return output, err @@ -68,6 +65,9 @@ func (backend *Backend) newWindow ( window := &window { backend: backend } + window.system.initialize() + window.system.pushFunc = window.pasteAndPush + window.xWindow, err = xwindow.Generate(backend.connection) if err != nil { return } @@ -125,7 +125,7 @@ func (backend *Backend) newWindow ( window.SetConfig(backend.config) window.metrics.bounds = bounds - window.childMinimumSizeChangeCallback(8, 8) + window.setMinimumSize(8, 8) window.reallocateCanvas() @@ -404,6 +404,11 @@ func (window *window) reallocateCanvas () { } +func (window *window) pasteAndPush (region image.Rectangle) { + window.paste(region) + window.pushRegion(region) +} + func (window *window) paste (region image.Rectangle) { canvas := canvas.Cut(window.canvas, region) data, stride := canvas.Buffer() @@ -438,7 +443,9 @@ func (window *window) pushRegion (region image.Rectangle) { } } -func (window *window) childMinimumSizeChangeCallback (width, height int) (resized bool) { +func (window *window) setMinimumSize (width, height int) { + if width < 8 { width = 8 } + if height < 8 { height = 8 } icccm.WmNormalHintsSet ( window.backend.connection, window.xWindow.Id, @@ -454,8 +461,5 @@ func (window *window) childMinimumSizeChangeCallback (width, height int) (resize if newWidth != window.metrics.bounds.Dx() || newHeight != window.metrics.bounds.Dy() { window.xWindow.Resize(newWidth, newHeight) - return true } - - return false } diff --git a/elements/button.go b/elements/button.go index caba419..a396d92 100644 --- a/elements/button.go +++ b/elements/button.go @@ -29,7 +29,7 @@ type Button struct { // NewButton creates a new button with the specified label text. func NewButton (text string) (element *Button) { - element = &Button { showText: true } + element = &Button { showText: true, enabled: true } element.theme.Case = tomo.C("tomo", "button") element.drawer.SetFace (element.theme.FontFace ( tomo.FontStyleRegular, @@ -126,7 +126,6 @@ func (element *Button) SetConfig (new tomo.Config) { // Draw causes the element to draw to the specified destination canvas. func (element *Button) Draw (destination canvas.Canvas) { if element.entity == nil { return } - state := element.state() bounds := element.entity.Bounds() pattern := element.theme.Pattern(tomo.PatternButton, state) @@ -183,17 +182,15 @@ func (element *Button) Draw (destination canvas.Canvas) { } func (element *Button) HandleFocusChange () { - if element.entity == nil { return } - element.entity.Invalidate() + if element.entity != nil { element.entity.Invalidate() } } func (element *Button) HandleMouseDown (x, y int, button input.Button) { - if element.entity == nil { return } if !element.Enabled() { return } element.Focus() if button != input.ButtonLeft { return } element.pressed = true - element.entity.Invalidate() + if element.entity != nil { element.entity.Invalidate() } } func (element *Button) HandleMouseUp (x, y int, button input.Button) { @@ -204,23 +201,21 @@ func (element *Button) HandleMouseUp (x, y int, button input.Button) { if element.Enabled() && within && element.onClick != nil { element.onClick() } - element.entity.Invalidate() + if element.entity != nil { element.entity.Invalidate() } } func (element *Button) HandleKeyDown (key input.Key, modifiers input.Modifiers) { - if element.entity == nil { return } if !element.Enabled() { return } if key == input.KeyEnter { element.pressed = true - element.entity.Invalidate() + if element.entity != nil { element.entity.Invalidate() } } } func (element *Button) HandleKeyUp(key input.Key, modifiers input.Modifiers) { - if element.entity == nil { return } if key == input.KeyEnter && element.pressed { element.pressed = false - element.entity.Invalidate() + if element.entity != nil { element.entity.Invalidate() } if !element.Enabled() { return } if element.onClick != nil { element.onClick() diff --git a/elements/checkbox.go b/elements/checkbox.go index 74bdf97..42445a4 100644 --- a/elements/checkbox.go +++ b/elements/checkbox.go @@ -26,7 +26,7 @@ type Checkbox struct { // NewCheckbox creates a new cbeckbox with the specified label text. func NewCheckbox (text string, checked bool) (element *Checkbox) { - element = &Checkbox { checked: checked } + element = &Checkbox { checked: checked, enabled: true } element.theme.Case = tomo.C("tomo", "checkbox") element.drawer.SetFace (element.theme.FontFace ( tomo.FontStyleRegular,