Permalink
Browse files

Simplify events

  • Loading branch information...
1 parent 7ce1e4d commit 5731dafd012efe19b7cd036973a2f72b6b3b79bd @lxn committed Feb 4, 2011
@@ -154,7 +154,7 @@ func main() {
exitAction := gui.NewAction()
panicIfErr(exitAction.SetText("Exit"))
- exitAction.Triggered().Subscribe(func(args *gui.EventArgs) { gui.App().Exit(0) })
+ exitAction.Triggered().Attach(func() { gui.App().Exit(0) })
panicIfErr(fileMenu.Actions().Add(exitAction))
helpMenu, err := gui.NewMenu()
@@ -165,7 +165,7 @@ func main() {
aboutAction := gui.NewAction()
panicIfErr(aboutAction.SetText("About"))
- aboutAction.Triggered().Subscribe(func(args *gui.EventArgs) {
+ aboutAction.Triggered().Attach(func() {
gui.MsgBox(mw, "About", "Walk File Browser Example", gui.MsgBoxOK|gui.MsgBoxIconInformation)
})
panicIfErr(helpMenu.Actions().Add(aboutAction))
@@ -177,17 +177,16 @@ func main() {
panicIfErr(err)
panicIfErr(mw.treeView.SetMaxSize(drawing.Size{200, 0}))
- mw.treeView.ItemExpanded().Subscribe(func(args *gui.TreeViewItemEventArgs) {
- item := args.Item()
+ mw.treeView.ItemExpanded().Attach(func(item *gui.TreeViewItem) {
children := item.Children()
if children.Len() == 1 && children.At(0).Text() == "" {
mw.populateTreeViewItem(item)
}
})
- mw.treeView.SelectionChanged().Subscribe(func(args *gui.TreeViewItemSelectionEventArgs) {
- mw.selTvwItem = args.New()
- mw.populateListView(pathForTreeViewItem(mw.selTvwItem))
+ mw.treeView.SelectionChanged().Attach(func(old, new *gui.TreeViewItem) {
+ mw.selTvwItem = new
+ mw.populateListView(pathForTreeViewItem(new))
})
drives, err := wpath.DriveNames()
@@ -204,7 +203,7 @@ func main() {
panicIfErr(err)
panicIfErr(mw.listView.SetMaxSize(drawing.Size{422, 0}))
- mw.listView.SelectedIndexChanged().Subscribe(func(args *gui.EventArgs) {
+ mw.listView.SelectedIndexChanged().Attach(func() {
index := mw.listView.SelectedIndex()
var url string
if index > -1 {
@@ -109,13 +109,13 @@ func main() {
openAction := gui.NewAction()
openAction.SetImage(openBmp)
panicIfErr(openAction.SetText("Open"))
- openAction.Triggered().Subscribe(func(args *gui.EventArgs) { mw.openImage() })
+ openAction.Triggered().Attach(func() { mw.openImage() })
panicIfErr(fileMenu.Actions().Add(openAction))
panicIfErr(mw.ToolBar().Actions().Add(openAction))
exitAction := gui.NewAction()
panicIfErr(exitAction.SetText("Exit"))
- exitAction.Triggered().Subscribe(func(args *gui.EventArgs) { gui.App().Exit(0) })
+ exitAction.Triggered().Attach(func() { gui.App().Exit(0) })
panicIfErr(fileMenu.Actions().Add(exitAction))
helpMenu, err := gui.NewMenu()
@@ -126,7 +126,7 @@ func main() {
aboutAction := gui.NewAction()
panicIfErr(aboutAction.SetText("About"))
- aboutAction.Triggered().Subscribe(func(args *gui.EventArgs) {
+ aboutAction.Triggered().Attach(func() {
gui.MsgBox(mw, "About", "Walk Image Viewer Example", gui.MsgBoxOK|gui.MsgBoxIconInformation)
})
panicIfErr(helpMenu.Actions().Add(aboutAction))
@@ -48,7 +48,7 @@ func main() {
exitAction := gui.NewAction()
panicIfErr(exitAction.SetText("Exit"))
- exitAction.Triggered().Subscribe(func(args *gui.EventArgs) { gui.App().Exit(0) })
+ exitAction.Triggered().Attach(func() { gui.App().Exit(0) })
panicIfErr(fileMenu.Actions().Add(exitAction))
helpMenu, err := gui.NewMenu()
@@ -59,15 +59,15 @@ func main() {
aboutAction := gui.NewAction()
panicIfErr(aboutAction.SetText("About"))
- aboutAction.Triggered().Subscribe(func(args *gui.EventArgs) {
+ aboutAction.Triggered().Attach(func() {
gui.MsgBox(mw, "About", "Walk Web Browser Example", gui.MsgBoxOK|gui.MsgBoxIconInformation)
})
panicIfErr(helpMenu.Actions().Add(aboutAction))
mw.urlLineEdit, err = gui.NewLineEdit(mw.ClientArea())
panicIfErr(err)
- mw.urlLineEdit.KeyDown().Subscribe(func(args *gui.KeyEventArgs) {
- if args.Key() == user32.VK_RETURN {
+ mw.urlLineEdit.KeyDown().Attach(func(key int) {
+ if key == user32.VK_RETURN {
panicIfErr(mw.webView.SetURL(mw.urlLineEdit.Text()))
}
})
@@ -218,7 +218,7 @@ func (a *Action) Triggered() *Event {
}
func (a *Action) raiseTriggered() {
- a.triggeredPublisher.Publish(NewEventArgs(a))
+ a.triggeredPublisher.Publish()
}
func (a *Action) addChangedHandler(handler actionChangedHandler) {
@@ -38,5 +38,5 @@ func (b *Button) Clicked() *Event {
}
func (b *Button) raiseClicked() {
- b.clickedPublisher.Publish(NewEventArgs(b))
+ b.clickedPublisher.Publish()
}
@@ -4,38 +4,17 @@
package gui
-type CancelEventArgs struct {
- EventArgs
- canceled bool
-}
-
-func NewCancelEventArgs(sender interface{}) *CancelEventArgs {
- return &CancelEventArgs{
- EventArgs: EventArgs{
- sender: sender,
- },
- }
-}
-
-func (a *CancelEventArgs) Canceled() bool {
- return a.canceled
-}
-
-func (a *CancelEventArgs) SetCanceled(value bool) {
- a.canceled = value
-}
-
-type CancelEventHandler func(args *CancelEventArgs)
+type CancelEventHandler func(canceled *bool)
type CancelEvent struct {
handlers []CancelEventHandler
}
-func (e *CancelEvent) Subscribe(handler CancelEventHandler) {
+func (e *CancelEvent) Attach(handler CancelEventHandler) {
e.handlers = append(e.handlers, handler)
}
-func (e *CancelEvent) Unsubscribe(handler CancelEventHandler) {
+func (e *CancelEvent) Detach(handler CancelEventHandler) {
for i, h := range e.handlers {
if h == handler {
e.handlers = append(e.handlers[:i], e.handlers[i+1:]...)
@@ -52,8 +31,8 @@ func (p *CancelEventPublisher) Event() *CancelEvent {
return &p.event
}
-func (p *CancelEventPublisher) Publish(args *CancelEventArgs) {
+func (p *CancelEventPublisher) Publish(canceled *bool) {
for _, handler := range p.event.handlers {
- handler(args)
+ handler(canceled)
}
}
@@ -4,37 +4,17 @@
package gui
-type CloseEventArgs struct {
- CancelEventArgs
- reason CloseReason
-}
-
-func NewCloseEventArgs(sender interface{}, reason CloseReason) *CloseEventArgs {
- return &CloseEventArgs{
- CancelEventArgs: CancelEventArgs{
- EventArgs: EventArgs{
- sender: sender,
- },
- },
- reason: reason,
- }
-}
-
-func (a *CloseEventArgs) Reason() CloseReason {
- return a.reason
-}
-
-type CloseEventHandler func(args *CloseEventArgs)
+type CloseEventHandler func(canceled *bool, reason CloseReason)
type CloseEvent struct {
handlers []CloseEventHandler
}
-func (e *CloseEvent) Subscribe(handler CloseEventHandler) {
+func (e *CloseEvent) Attach(handler CloseEventHandler) {
e.handlers = append(e.handlers, handler)
}
-func (e *CloseEvent) Unsubscribe(handler CloseEventHandler) {
+func (e *CloseEvent) Detach(handler CloseEventHandler) {
for i, h := range e.handlers {
if h == handler {
e.handlers = append(e.handlers[:i], e.handlers[i+1:]...)
@@ -51,8 +31,8 @@ func (p *CloseEventPublisher) Event() *CloseEvent {
return &p.event
}
-func (p *CloseEventPublisher) Publish(args *CloseEventArgs) {
+func (p *CloseEventPublisher) Publish(canceled *bool, reason CloseReason) {
for _, handler := range p.event.handlers {
- handler(args)
+ handler(canceled, reason)
}
}
@@ -95,7 +95,7 @@ func (cb *ComboBox) wndProc(hwnd HWND, msg uint, wParam, lParam uintptr, origWnd
switch HIWORD(uint(wParam)) {
case CBN_SELENDOK:
if selIndex := cb.SelectedIndex(); selIndex != cb.prevSelIndex {
- cb.selectedIndexChangedPublisher.Publish(NewEventArgs(cb))
+ cb.selectedIndexChangedPublisher.Publish()
cb.prevSelIndex = selIndex
return 0
}
View
@@ -4,31 +4,17 @@
package gui
-type EventArgs struct {
- sender interface{}
-}
-
-func NewEventArgs(sender interface{}) *EventArgs {
- return &EventArgs{
- sender: sender,
- }
-}
-
-func (a *EventArgs) Sender() interface{} {
- return a.sender
-}
-
-type EventHandler func(args *EventArgs)
+type EventHandler func()
type Event struct {
handlers []EventHandler
}
-func (e *Event) Subscribe(handler EventHandler) {
+func (e *Event) Attach(handler EventHandler) {
e.handlers = append(e.handlers, handler)
}
-func (e *Event) Unsubscribe(handler EventHandler) {
+func (e *Event) Detach(handler EventHandler) {
for i, h := range e.handlers {
if h == handler {
e.handlers = append(e.handlers[:i], e.handlers[i+1:]...)
@@ -45,8 +31,8 @@ func (p *EventPublisher) Event() *Event {
return &p.event
}
-func (p *EventPublisher) Publish(args *EventArgs) {
+func (p *EventPublisher) Publish() {
for _, handler := range p.event.handlers {
- handler(args)
+ handler()
}
}
@@ -4,35 +4,17 @@
package gui
-type KeyEventArgs struct {
- EventArgs
- key int
-}
-
-func NewKeyEventArgs(sender interface{}, key int) *KeyEventArgs {
- return &KeyEventArgs{
- EventArgs: EventArgs{
- sender: sender,
- },
- key: key,
- }
-}
-
-func (a *KeyEventArgs) Key() int {
- return a.key
-}
-
-type KeyEventHandler func(args *KeyEventArgs)
+type KeyEventHandler func(key int)
type KeyEvent struct {
handlers []KeyEventHandler
}
-func (e *KeyEvent) Subscribe(handler KeyEventHandler) {
+func (e *KeyEvent) Attach(handler KeyEventHandler) {
e.handlers = append(e.handlers, handler)
}
-func (e *KeyEvent) Unsubscribe(handler KeyEventHandler) {
+func (e *KeyEvent) Detach(handler KeyEventHandler) {
for i, h := range e.handlers {
if h == handler {
e.handlers = append(e.handlers[:i], e.handlers[i+1:]...)
@@ -49,8 +31,8 @@ func (p *KeyEventPublisher) Event() *KeyEvent {
return &p.event
}
-func (p *KeyEventPublisher) Publish(args *KeyEventArgs) {
+func (p *KeyEventPublisher) Publish(key int) {
for _, handler := range p.event.handlers {
- handler(args)
+ handler(key)
}
}
@@ -142,7 +142,7 @@ func (lv *ListView) SetSelectedIndex(value int) os.Error {
lv.selectedIndex = value
if value == -1 {
- lv.selectedIndexChangedPublisher.Publish(NewEventArgs(lv))
+ lv.selectedIndexChangedPublisher.Publish()
}
return nil
@@ -253,7 +253,7 @@ func (lv *ListView) wndProc(hwnd HWND, msg uint, wParam, lParam uintptr, origWnd
case WM_KEYDOWN:
if wParam == VK_RETURN && lv.SelectedIndex() > -1 {
- lv.itemActivatedPublisher.Publish(NewEventArgs(lv))
+ lv.itemActivatedPublisher.Publish()
}
case WM_NOTIFY:
@@ -264,11 +264,11 @@ func (lv *ListView) wndProc(hwnd HWND, msg uint, wParam, lParam uintptr, origWnd
selectedBefore := nmlv.UOldState&LVIS_SELECTED > 0
if selectedNow && !selectedBefore {
lv.selectedIndex = nmlv.IItem
- lv.selectedIndexChangedPublisher.Publish(NewEventArgs(lv))
+ lv.selectedIndexChangedPublisher.Publish()
}
case LVN_ITEMACTIVATE:
- lv.itemActivatedPublisher.Publish(NewEventArgs(lv))
+ lv.itemActivatedPublisher.Publish()
}
}
Oops, something went wrong.

0 comments on commit 5731daf

Please sign in to comment.