Skip to content

Commit

Permalink
Fix glitch where the initially focused LineEdit would scroll part of …
Browse files Browse the repository at this point in the history
…its content out of view
  • Loading branch information
lxn committed Apr 11, 2017
1 parent 42ba286 commit 8bf5181
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 51 deletions.
51 changes: 0 additions & 51 deletions dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package walk

import (
"syscall"
"unsafe"
)

Expand Down Expand Up @@ -156,54 +155,6 @@ func (dlg *Dialog) Close(result int) {
dlg.FormBase.Close()
}

func firstFocusableDescendantCallback(hwnd win.HWND, lParam uintptr) uintptr {
widget := windowFromHandle(hwnd)

if widget == nil || !widget.Visible() || !widget.Enabled() {
return 1
}

style := uint(win.GetWindowLong(hwnd, win.GWL_STYLE))
// FIXME: Ugly workaround for NumberEdit
_, isTextSelectable := widget.(textSelectable)
if style&win.WS_TABSTOP > 0 || isTextSelectable {
hwndPtr := (*win.HWND)(unsafe.Pointer(lParam))
*hwndPtr = hwnd
return 0
}

return 1
}

var firstFocusableDescendantCallbackPtr = syscall.NewCallback(firstFocusableDescendantCallback)

func firstFocusableDescendant(container Container) Window {
var hwnd win.HWND

win.EnumChildWindows(container.Handle(), firstFocusableDescendantCallbackPtr, uintptr(unsafe.Pointer(&hwnd)))

return windowFromHandle(hwnd)
}

type textSelectable interface {
SetTextSelection(start, end int)
}

func (dlg *Dialog) focusFirstCandidateDescendant() {
window := firstFocusableDescendant(dlg)
if window == nil {
return
}

if err := window.SetFocus(); err != nil {
return
}

if textSel, ok := window.(textSelectable); ok {
textSel.SetTextSelection(0, -1)
}
}

func (dlg *Dialog) Show() {
if dlg.owner != nil {
var size Size
Expand Down Expand Up @@ -231,8 +182,6 @@ func (dlg *Dialog) Show() {
}

dlg.FormBase.Show()

dlg.focusFirstCandidateDescendant()
}

func fitRectToScreen(hWnd win.HWND, r Rectangle) Rectangle {
Expand Down
50 changes: 50 additions & 0 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ func (fb *FormBase) Run() int {
layout.Update(false)
}

fb.focusFirstCandidateDescendant()

fb.startingPublisher.Publish()

var msg win.MSG
Expand Down Expand Up @@ -569,3 +571,51 @@ func (fb *FormBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) u

return fb.WindowBase.WndProc(hwnd, msg, wParam, lParam)
}

func (fb *FormBase) focusFirstCandidateDescendant() {
window := firstFocusableDescendant(fb)
if window == nil {
return
}

if err := window.SetFocus(); err != nil {
return
}

if textSel, ok := window.(textSelectable); ok {
textSel.SetTextSelection(0, -1)
}
}

func firstFocusableDescendantCallback(hwnd win.HWND, lParam uintptr) uintptr {
widget := windowFromHandle(hwnd)

if widget == nil || !widget.Visible() || !widget.Enabled() {
return 1
}

style := uint(win.GetWindowLong(hwnd, win.GWL_STYLE))
// FIXME: Ugly workaround for NumberEdit
_, isTextSelectable := widget.(textSelectable)
if style&win.WS_TABSTOP > 0 || isTextSelectable {
hwndPtr := (*win.HWND)(unsafe.Pointer(lParam))
*hwndPtr = hwnd
return 0
}

return 1
}

var firstFocusableDescendantCallbackPtr = syscall.NewCallback(firstFocusableDescendantCallback)

func firstFocusableDescendant(container Container) Window {
var hwnd win.HWND

win.EnumChildWindows(container.Handle(), firstFocusableDescendantCallbackPtr, uintptr(unsafe.Pointer(&hwnd)))

return windowFromHandle(hwnd)
}

type textSelectable interface {
SetTextSelection(start, end int)
}

0 comments on commit 8bf5181

Please sign in to comment.