Skip to content

Commit

Permalink
Don't store Property values in widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed May 1, 2013
1 parent 7b076a3 commit 13be995
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 100 deletions.
13 changes: 4 additions & 9 deletions button.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,29 @@ type clickable interface {

type Button struct {
WidgetBase
checkedProperty Property
clickedPublisher EventPublisher
textProperty Property
textChangedPublisher EventPublisher
}

func (b *Button) init() {
b.checkedProperty = NewBoolProperty(
b.MustRegisterProperty("Checked", NewBoolProperty(
func() bool {
return b.Checked()
},
func(v bool) error {
b.SetChecked(v)
return nil
},
b.Clicked())
b.Clicked()))

b.textProperty = NewProperty(
b.MustRegisterProperty("Text", NewProperty(
func() interface{} {
return b.Text()
},
func(v interface{}) error {
return b.SetText(v.(string))
},
b.textChangedPublisher.Event())

b.MustRegisterProperty("Checked", b.checkedProperty)
b.MustRegisterProperty("Text", b.textProperty)
b.textChangedPublisher.Event()))
}

func (b *Button) Text() string {
Expand Down
27 changes: 11 additions & 16 deletions combobox.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type ComboBox struct {
prevCurIndex int
selChangeIndex int
currentIndexChangedPublisher EventPublisher
currentIndexProperty Property
valueProperty Property
}

func NewComboBox(parent Container) (*ComboBox, error) {
Expand All @@ -45,7 +43,16 @@ func NewComboBox(parent Container) (*ComboBox, error) {
return nil, err
}

cb.valueProperty = NewProperty(
cb.MustRegisterProperty("CurrentIndex", NewProperty(
func() interface{} {
return cb.CurrentIndex()
},
func(v interface{}) error {
return cb.SetCurrentIndex(v.(int))
},
cb.CurrentIndexChanged()))

cb.MustRegisterProperty("Value", NewProperty(
func() interface{} {
index := cb.CurrentIndex()

Expand All @@ -72,19 +79,7 @@ func NewComboBox(parent Container) (*ComboBox, error) {

return cb.SetCurrentIndex(index)
},
cb.CurrentIndexChanged())

cb.currentIndexProperty = NewProperty(
func() interface{} {
return cb.CurrentIndex()
},
func(v interface{}) error {
return cb.SetCurrentIndex(v.(int))
},
cb.CurrentIndexChanged())

cb.MustRegisterProperty("Value", cb.valueProperty)
cb.MustRegisterProperty("CurrentIndex", cb.currentIndexProperty)
cb.CurrentIndexChanged()))

return cb, nil
}
Expand Down
7 changes: 2 additions & 5 deletions dateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import . "github.com/lxn/go-winapi"
type DateEdit struct {
WidgetBase
valueChangedPublisher EventPublisher
dateProperty Property
}

func newDateEdit(parent Container, style uint32) (*DateEdit, error) {
Expand All @@ -29,16 +28,14 @@ func newDateEdit(parent Container, style uint32) (*DateEdit, error) {
return nil, err
}

de.dateProperty = NewProperty(
de.MustRegisterProperty("Date", NewProperty(
func() interface{} {
return de.Value()
},
func(v interface{}) error {
return de.SetValue(v.(time.Time))
},
de.valueChangedPublisher.Event())

de.MustRegisterProperty("Date", de.dateProperty)
de.valueChangedPublisher.Event()))

return de, nil
}
Expand Down
7 changes: 2 additions & 5 deletions groupbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type GroupBox struct {
WidgetBase
hWndGroupBox HWND
composite *Composite
titleProperty Property
titleChangedPublisher EventPublisher
}

Expand Down Expand Up @@ -59,16 +58,14 @@ func NewGroupBox(parent Container) (*GroupBox, error) {
gb.font = nil
gb.SetFont(defaultFont)

gb.titleProperty = NewProperty(
gb.MustRegisterProperty("Title", NewProperty(
func() interface{} {
return gb.Title()
},
func(v interface{}) error {
return gb.SetTitle(v.(string))
},
gb.titleChangedPublisher.Event())

gb.MustRegisterProperty("Title", gb.titleProperty)
gb.titleChangedPublisher.Event()))

succeeded = true

Expand Down
7 changes: 2 additions & 5 deletions label.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import . "github.com/lxn/go-winapi"

type Label struct {
WidgetBase
textProperty Property
textChangedPublisher EventPublisher
}

Expand All @@ -24,16 +23,14 @@ func NewLabel(parent Container) (*Label, error) {
return nil, err
}

l.textProperty = NewProperty(
l.MustRegisterProperty("Text", NewProperty(
func() interface{} {
return l.Text()
},
func(v interface{}) error {
return l.SetText(v.(string))
},
l.textChangedPublisher.Event())

l.MustRegisterProperty("Text", l.textProperty)
l.textChangedPublisher.Event()))

return l, nil
}
Expand Down
25 changes: 10 additions & 15 deletions lineedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ const (
type LineEdit struct {
WidgetBase
editingFinishedPublisher EventPublisher
readOnlyChangedPublisher EventPublisher
returnPressedPublisher EventPublisher
textProperty Property
textChangedPublisher EventPublisher
readOnlyProperty Property
readOnlyChangedPublisher EventPublisher
charWidthFont *Font
charWidth int
}
Expand All @@ -40,26 +38,23 @@ func newLineEdit(parent Widget) (*LineEdit, error) {
return nil, err
}

le.textProperty = NewProperty(
le.MustRegisterProperty("ReadOnly", NewProperty(
func() interface{} {
return le.Text()
return le.ReadOnly()
},
func(v interface{}) error {
return le.SetText(v.(string))
return le.SetReadOnly(v.(bool))
},
le.textChangedPublisher.Event())
le.readOnlyChangedPublisher.Event()))

le.readOnlyProperty = NewProperty(
le.MustRegisterProperty("Text", NewProperty(
func() interface{} {
return le.ReadOnly()
return le.Text()
},
func(v interface{}) error {
return le.SetReadOnly(v.(bool))
return le.SetText(v.(string))
},
le.readOnlyChangedPublisher.Event())

le.MustRegisterProperty("Text", le.textProperty)
le.MustRegisterProperty("ReadOnly", le.readOnlyProperty)
le.textChangedPublisher.Event()))

return le, nil
}
Expand Down Expand Up @@ -225,7 +220,7 @@ func (le *LineEdit) ReturnPressed() *Event {
}

func (le *LineEdit) TextChanged() *Event {
return le.textProperty.Changed()
return le.textChangedPublisher.Event()
}

func (le *LineEdit) WndProc(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr {
Expand Down
7 changes: 2 additions & 5 deletions numberedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type NumberEdit struct {
increment float64
oldValue float64
valueChangedPublisher EventPublisher
valueProperty Property
}

func NewNumberEdit(parent Container) (*NumberEdit, error) {
Expand Down Expand Up @@ -74,16 +73,14 @@ func NewNumberEdit(parent Container) (*NumberEdit, error) {
return nil, err
}

ne.valueProperty = NewProperty(
ne.MustRegisterProperty("Value", NewProperty(
func() interface{} {
return ne.Value()
},
func(v interface{}) error {
return ne.SetValue(v.(float64))
},
ne.valueChangedPublisher.Event())

ne.MustRegisterProperty("Value", ne.valueProperty)
ne.valueChangedPublisher.Event()))

succeeded = true

Expand Down
7 changes: 2 additions & 5 deletions tabpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type TabPage struct {
ContainerBase
title string
tabWidget *TabWidget
titleProperty Property
titleChangedPublisher EventPublisher
}

Expand All @@ -42,16 +41,14 @@ func NewTabPage() (*TabPage, error) {

tp.SetBackground(tabPageBackgroundBrush)

tp.titleProperty = NewProperty(
tp.MustRegisterProperty("Title", NewProperty(
func() interface{} {
return tp.Title()
},
func(v interface{}) error {
return tp.SetTitle(v.(string))
},
tp.titleChangedPublisher.Event())

tp.MustRegisterProperty("Title", tp.titleProperty)
tp.titleChangedPublisher.Event()))

return tp, nil
}
Expand Down
11 changes: 5 additions & 6 deletions tabwidget.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type TabWidget struct {
pages *TabPageList
currentIndex int
currentIndexChangedPublisher EventPublisher
hasCurrentPageProperty Property
persistent bool
}

Expand Down Expand Up @@ -59,11 +58,11 @@ func NewTabWidget(parent Container) (*TabWidget, error) {
}
SendMessage(tw.hWndTab, WM_SETFONT, uintptr(defaultFont.handleForDPI(0)), 1)

tw.hasCurrentPageProperty = NewReadOnlyBoolProperty(func() bool {
return tw.CurrentIndex() != -1
}, tw.CurrentIndexChanged())

tw.MustRegisterProperty("HasCurrentPage", tw.hasCurrentPageProperty)
tw.MustRegisterProperty("HasCurrentPage", NewReadOnlyBoolProperty(
func() bool {
return tw.CurrentIndex() != -1
},
tw.CurrentIndexChanged()))

succeeded = true

Expand Down
23 changes: 9 additions & 14 deletions textedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import (

type TextEdit struct {
WidgetBase
textProperty Property
textChangedPublisher EventPublisher
readOnlyProperty Property
readOnlyChangedPublisher EventPublisher
textChangedPublisher EventPublisher
}

func NewTextEdit(parent Container) (*TextEdit, error) {
Expand All @@ -33,26 +31,23 @@ func NewTextEdit(parent Container) (*TextEdit, error) {
return nil, err
}

te.textProperty = NewProperty(
te.MustRegisterProperty("ReadOnly", NewProperty(
func() interface{} {
return te.Text()
return te.ReadOnly()
},
func(v interface{}) error {
return te.SetText(v.(string))
return te.SetReadOnly(v.(bool))
},
te.textChangedPublisher.Event())
te.readOnlyChangedPublisher.Event()))

te.readOnlyProperty = NewProperty(
te.MustRegisterProperty("Text", NewProperty(
func() interface{} {
return te.ReadOnly()
return te.Text()
},
func(v interface{}) error {
return te.SetReadOnly(v.(bool))
return te.SetText(v.(string))
},
te.readOnlyChangedPublisher.Event())

te.MustRegisterProperty("Text", te.textProperty)
te.MustRegisterProperty("ReadOnly", te.readOnlyProperty)
te.textChangedPublisher.Event()))

return te, nil
}
Expand Down
17 changes: 7 additions & 10 deletions toplevelwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

import . "github.com/lxn/go-winapi"

type CloseReason int
type CloseReason byte

const (
CloseReasonUnknown CloseReason = iota
Expand Down Expand Up @@ -55,27 +55,24 @@ type TopLevelWindow struct {
ContainerBase
owner RootWidget
closingPublisher CloseEventPublisher
closeReason CloseReason
prevFocusHWnd HWND
isInRestoreState bool
startingPublisher EventPublisher
titleChangedPublisher EventPublisher
progressIndicator *ProgressIndicator
icon *Icon
titleProperty Property
titleChangedPublisher EventPublisher
prevFocusHWnd HWND
isInRestoreState bool
closeReason CloseReason
}

func (tlw *TopLevelWindow) init() {
tlw.titleProperty = NewProperty(
tlw.MustRegisterProperty("Title", NewProperty(
func() interface{} {
return tlw.Title()
},
func(v interface{}) error {
return tlw.SetTitle(v.(string))
},
tlw.titleChangedPublisher.Event())

tlw.MustRegisterProperty("Title", tlw.titleProperty)
tlw.titleChangedPublisher.Event()))
}

func (tlw *TopLevelWindow) LayoutFlags() LayoutFlags {
Expand Down
Loading

0 comments on commit 13be995

Please sign in to comment.