-
Notifications
You must be signed in to change notification settings - Fork 888
/
datelabel.go
118 lines (89 loc) · 2.12 KB
/
datelabel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2018 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 (
"time"
)
type DateLabel struct {
static
date time.Time
dateChangedPublisher EventPublisher
format string
formatChangedPublisher EventPublisher
}
func NewDateLabel(parent Container) (*DateLabel, error) {
dl := new(DateLabel)
if err := dl.init(dl, parent, 0); err != nil {
return nil, err
}
dl.SetTextAlignment(AlignFar)
if _, err := dl.updateText(); err != nil {
return nil, err
}
dl.MustRegisterProperty("Date", NewProperty(
func() interface{} {
return dl.Date()
},
func(v interface{}) error {
return dl.SetDate(assertTimeOr(v, time.Time{}))
},
dl.dateChangedPublisher.Event()))
dl.MustRegisterProperty("Format", NewProperty(
func() interface{} {
return dl.Format()
},
func(v interface{}) error {
return dl.SetFormat(assertStringOr(v, ""))
},
dl.formatChangedPublisher.Event()))
return dl, nil
}
func (dl *DateLabel) asStatic() *static {
return &dl.static
}
func (dl *DateLabel) TextAlignment() Alignment1D {
return dl.textAlignment1D()
}
func (dl *DateLabel) SetTextAlignment(alignment Alignment1D) error {
if alignment == AlignDefault {
alignment = AlignNear
}
return dl.setTextAlignment1D(alignment)
}
func (dl *DateLabel) Date() time.Time {
return dl.date
}
func (dl *DateLabel) SetDate(date time.Time) error {
if date == dl.date {
return nil
}
old := dl.date
dl.date = date
if _, err := dl.updateText(); err != nil {
dl.date = old
return err
}
dl.dateChangedPublisher.Publish()
return nil
}
func (dl *DateLabel) Format() string {
return dl.format
}
func (dl *DateLabel) SetFormat(format string) error {
if format == dl.format {
return nil
}
old := dl.format
dl.format = format
if _, err := dl.updateText(); err != nil {
dl.format = old
return err
}
dl.formatChangedPublisher.Publish()
return nil
}
func (dl *DateLabel) updateText() (changed bool, err error) {
return dl.setText(dl.date.Format(dl.format))
}