Skip to content

Commit

Permalink
Add some focus management stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Aug 11, 2015
1 parent ff4309c commit c6aa0c8
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions window.go
Expand Up @@ -84,6 +84,13 @@ type Window interface {
// Enabled returns if the Window is enabled for user interaction.
Enabled() bool

// Focused returns whether the Window has the keyboard input focus.
Focused() bool

// FocusedChanged returns an Event that you can attach to for handling focus
// changed events for the Window.
FocusedChanged() *Event

// Font returns the *Font of the Window.
//
// By default this is a MS Shell Dlg 2, 8 point font.
Expand Down Expand Up @@ -285,6 +292,8 @@ type WindowBase struct {
enabledChangedPublisher EventPublisher
visibleProperty Property
visibleChangedPublisher EventPublisher
focusedProperty Property
focusedChangedPublisher EventPublisher
}

var (
Expand Down Expand Up @@ -339,11 +348,6 @@ func MustRegisterWindowClassWithWndProcPtr(className string, wndProcPtr uintptr)
registeredWindowClasses[className] = true
}

// FocusedWindow returns the Window that has the keyboard input focus.
func FocusedWindow() Window {
return windowFromHandle(win.GetFocus())
}

// InitWindow initializes a window.
//
// Widgets should be initialized using InitWidget instead.
Expand Down Expand Up @@ -438,8 +442,15 @@ func InitWindow(window, parent Window, className string, style, exStyle uint32)
},
wb.visibleChangedPublisher.Event())

wb.focusedProperty = NewReadOnlyBoolProperty(
func() bool {
return wb.Focused()
},
wb.focusedChangedPublisher.Event())

wb.MustRegisterProperty("Enabled", wb.enabledProperty)
wb.MustRegisterProperty("Visible", wb.visibleProperty)
wb.MustRegisterProperty("Focused", wb.focusedProperty)

succeeded = true

Expand Down Expand Up @@ -1057,6 +1068,16 @@ func (wb *WindowBase) SetClientSize(value Size) error {
return wb.SetSize(wb.sizeFromClientSize(value))
}

// FocusedWindow returns the Window that has the keyboard input focus.
func FocusedWindow() Window {
return windowFromHandle(win.GetFocus())
}

// Focused returns whether the Window has the keyboard input focus.
func (wb *WindowBase) Focused() bool {
return wb.hWnd == win.GetFocus()
}

// SetFocus sets the keyboard input focus to the *WindowBase.
func (wb *WindowBase) SetFocus() error {
if win.SetFocus(wb.hWnd) == 0 {
Expand All @@ -1066,6 +1087,12 @@ func (wb *WindowBase) SetFocus() error {
return nil
}

// FocusedChanged returns an Event that you can attach to for handling focus
// change events for the WindowBase.
func (wb *WindowBase) FocusedChanged() *Event {
return wb.focusedChangedPublisher.Event()
}

// CreateCanvas creates and returns a *Canvas that can be used to draw
// inside the ClientBounds of the *WindowBase.
//
Expand Down Expand Up @@ -1319,6 +1346,9 @@ func (wb *WindowBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr)
case win.WM_MOUSEMOVE:
wb.publishMouseEvent(&wb.mouseMovePublisher, wParam, lParam)

case win.WM_SETFOCUS, win.WM_KILLFOCUS:
wb.focusedChangedPublisher.Publish()

case win.WM_SETCURSOR:
if wb.cursor != nil {
win.SetCursor(wb.cursor.handle())
Expand Down

0 comments on commit c6aa0c8

Please sign in to comment.