From e38e6fcfb537352d8b07af26ca93d0f830d044b0 Mon Sep 17 00:00:00 2001 From: Lorenzo Cogotti Date: Sun, 2 Oct 2022 18:45:54 +0200 Subject: [PATCH] [ui] Fix focus stealing, minor style fixes. --- ui.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ui.lua b/ui.lua index d063f65..8255c67 100644 --- a/ui.lua +++ b/ui.lua @@ -99,7 +99,7 @@ function Ui.new(args) self.w,self.h = root.w,root.h local firstfocus, cancelfocus = resolveautofocus(root) - if firstfocus == nil then + if not firstfocus then firstfocus = isinstance(root, Layout) and root:first() or root @@ -115,34 +115,34 @@ end function Ui:keypressed(key, scancode, isrepeat) local focused = self.focused - if focused ~= nil and focused.grabkeyboard then + if focused and focused.grabkeyboard then focused:keypressed(key, scancode, isrepeat) end end function Ui:keyreleased(key, scancode) local focused = self.focused - if focused ~= nil and focused.grabkeyboard then + if focused and focused.grabkeyboard then focused:keyreleased(key, scancode) end end function Ui:textinput(text) local focused = self.focused - if focused ~= nil and focused.grabkeyboard then + if focused and focused.grabkeyboard then focused:textinput(text) end end function Ui:textedited(text, start, length) local focused = self.focused - if focused ~= nil and focused.grabkeyboard then + if focused and focused.grabkeyboard then focused:textedited(text, start, length) end end local function actionpropagate(widget, action) - while widget ~= nil do + while widget do if widget:onActionInput(action) then return true -- action consumed end @@ -166,7 +166,10 @@ end local function eventpropagate(ui, snap) -- 1. Pointer events - if snap.pointer and not ui.grabpointer then + local dropPointer = ui.focused and ui.focused.grabpointer + local dropAction = ui.focused and ui.focused.grabkeyboard + + if snap.pointer and not dropPointer then local root = ui[1] local x,y,w,h = root.x,root.y,root.w,root.h @@ -176,7 +179,7 @@ local function eventpropagate(ui, snap) end -- 2. Actions (keyboard/buttons) - if snap.action and not ui.grabkeyboard then + if snap.action and not dropAction then local consumed = actionpropagate(ui.focused, snap) if not consumed then