Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Function Listen To Device Change #450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ Shawn Sun <[email protected]>
Tim Dufrane <[email protected]>
Vincent Vanackere <[email protected]>
gonutz
xaxys
30 changes: 21 additions & 9 deletions declarative/mainwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ type MainWindow struct {

// MainWindow

AssignTo **walk.MainWindow
Expressions func() map[string]walk.Expression
Functions map[string]func(args ...interface{}) (interface{}, error)
MenuItems []MenuItem
OnDropFiles walk.DropFilesEventHandler
StatusBarItems []StatusBarItem
ToolBar ToolBar
ToolBarItems []MenuItem // Deprecated: use ToolBar instead
AssignTo **walk.MainWindow
Expressions func() map[string]walk.Expression
Functions map[string]func(args ...interface{}) (interface{}, error)
MenuItems []MenuItem
OnDeviceArrival walk.DeviceArrivalEventHandler
OnDeviceRemove walk.DeviceRemoveEventHandler
OnDropFiles walk.DropFilesEventHandler
StatusBarItems []StatusBarItem
ToolBar ToolBar
ToolBarItems []MenuItem // Deprecated: use ToolBar instead
}

func (mw MainWindow) Create() error {
Expand Down Expand Up @@ -157,7 +159,17 @@ func (mw MainWindow) Create() error {
}
w.ToolBar().SetImageList(imageList)

if mw.OnDropFiles != nil {
if mw.OnDeviceArrival != nil || mw.OnDeviceRemove != nil{
event := w.DeviceChange()
if mw.OnDeviceArrival != nil {
event.AttachArrival(mw.OnDeviceArrival)
}
if mw.OnDeviceRemove != nil {
event.AttachRemove(mw.OnDeviceRemove)
}
}

if mw.OnDropFiles != nil{
w.DropFiles().Attach(mw.OnDropFiles)
}

Expand Down
94 changes: 94 additions & 0 deletions devicechangeevent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package walk

import (
"github.com/lxn/win"
)

const (
DBT_DEVICEARRIVAL = 0x8000
DBT_DEVICEREMOVECOMPLETE = 0x8004
)

type DeviceArrivalEventHandler func()
type DeviceRemoveEventHandler func()

type DeviceChangeEvent struct {
hWnd win.HWND
arrivalhandlers []DeviceArrivalEventHandler
removehandlers []DeviceRemoveEventHandler
}


func (e *DeviceChangeEvent) AttachArrival(handler DeviceArrivalEventHandler) int {
for i, h := range e.arrivalhandlers {
if h == nil {
e.arrivalhandlers[i] = handler
return i
}
}

e.arrivalhandlers = append(e.arrivalhandlers, handler)
return len(e.arrivalhandlers) - 1
}

func (e *DeviceChangeEvent) AttachRemove(handler DeviceRemoveEventHandler) int {
for i, h := range e.removehandlers {
if h == nil {
e.removehandlers[i] = handler
return i
}
}

e.removehandlers = append(e.removehandlers, handler)
return len(e.removehandlers) - 1
}

func (e *DeviceChangeEvent) DetachArrival(handle int) {
e.arrivalhandlers[handle] = nil
for _, h := range e.arrivalhandlers {
if h != nil {
return
}
}
}

func (e *DeviceChangeEvent) DetachRemove(handle int) {
e.removehandlers[handle] = nil
for _, h := range e.removehandlers {
if h != nil {
return
}
}
}

type DeviceChangeEventPublisher struct {
event DeviceChangeEvent
}

func (p *DeviceChangeEventPublisher) Event(hWnd win.HWND) *DeviceChangeEvent {
p.event.hWnd = hWnd
return &p.event
}

func (p *DeviceChangeEventPublisher) Publish(wParam int) {
switch wParam {
case DBT_DEVICEARRIVAL:
for _, handler := range p.event.arrivalhandlers {
if handler != nil {
handler()
}
}
case DBT_DEVICEREMOVECOMPLETE:
for _, handler := range p.event.removehandlers {
if handler != nil {
handler()
}
}
}
}
10 changes: 10 additions & 0 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ type WindowBase struct {
contextMenu *Menu
disposables []Disposable
disposingPublisher EventPublisher
devicechangePublisher DeviceChangeEventPublisher
dropFilesPublisher DropFilesEventPublisher
keyDownPublisher KeyEventPublisher
keyPressPublisher KeyEventPublisher
Expand Down Expand Up @@ -1443,6 +1444,12 @@ func (wb *WindowBase) KeyUp() *KeyEvent {
return wb.keyUpPublisher.Event()
}

// DeviceChange returns a *DeviceChangeEvent that you can attach to for handling
// device change events for the *WindowBase.
func (wb *WindowBase) DeviceChange() *DeviceChangeEvent {
return wb.devicechangePublisher.Event(wb.hWnd)
}

// DropFiles returns a *DropFilesEvent that you can attach to for handling
// drop file events for the *WindowBase.
func (wb *WindowBase) DropFiles() *DropFilesEvent {
Expand Down Expand Up @@ -1868,6 +1875,9 @@ func (wb *WindowBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr)
case win.WM_KEYUP:
wb.handleKeyUp(wParam, lParam)

case win.WM_DEVICECHANGE:
wb.devicechangePublisher.Publish((int)(wParam))

case win.WM_DROPFILES:
wb.dropFilesPublisher.Publish(win.HDROP(wParam))

Expand Down