-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlocation.go
202 lines (166 loc) · 5.87 KB
/
location.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"image/color"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var (
locationTextColor = color.NRGBA{0xFF, 0xFF, 0xFF, 0xBF}
)
type location struct {
widget.BaseWidget
location *city
session *unsplashSession
app *nomad
button *widget.Button
dots *fyne.Container
dateButton *widget.Button
timeButton *widget.Button
locationTZLabel *canvas.Text
calendar *widget.Calendar
homeContainer *fyne.Container
}
func newLocation(loc *city, n *nomad, homeC *fyne.Container) *location {
l := &location{location: loc, session: n.session, app: n, homeContainer: homeC}
l.ExtendBaseWidget(l)
menu := fyne.NewMenu("",
fyne.NewMenuItem("Delete Place", func() { l.remove(homeC) }),
fyne.NewMenuItem("Photo info", func() {
c := fyne.CurrentApp().Driver().CanvasForObject(l.button)
info := loc.newInfoScreen(c)
info.Resize(c.Size())
c.Overlays().Add(info)
}))
l.button = widget.NewButtonWithIcon("", theme.MoreHorizontalIcon(), func() {
position := fyne.CurrentApp().Driver().AbsolutePositionForObject(l.button)
position.Y += l.button.Size().Height
widget.ShowPopUpMenuAtPosition(menu, fyne.CurrentApp().Driver().CanvasForObject(l.button), position)
})
l.button.Importance = widget.LowImportance
l.dots = container.NewVBox(layout.NewSpacer(), l.button)
l.calendar = widget.NewCalendar(loc.localTime, func(t time.Time) {
currentTimeSelected = false
setDate(t, l.homeContainer.Objects)
})
l.dateButton = widget.NewButtonWithIcon(loc.localTime.Format("Mon 02 Jan 2006"), theme.MenuDropDownIcon(), func() {
position := fyne.CurrentApp().Driver().AbsolutePositionForObject(l.dateButton)
position.Y += l.button.Size().Height
widget.ShowPopUpAtPosition(l.calendar, n.main.Canvas(), position)
})
l.dateButton.Alignment = widget.ButtonAlignLeading
l.dateButton.IconPlacement = widget.ButtonIconTrailingText
l.dateButton.Importance = widget.LowImportance
l.timeButton = widget.NewButtonWithIcon(loc.localTime.Format("15:04"), theme.MenuDropDownIcon(), func() {
position := fyne.CurrentApp().Driver().AbsolutePositionForObject(l.timeButton)
position.Y += l.timeButton.Size().Height
sizedMenuWidth := float32(104)
position.X += l.timeButton.Size().Width - sizedMenuWidth - theme.Padding()*2
times := listTimes()
menuItems := []*fyne.MenuItem{}
for _, t := range times {
v := t
menuItems = append(menuItems, fyne.NewMenuItem(" "+t, func() {
l.onTimeSelect(v)
n.main.Canvas().Overlays().Top().Hide()
}))
}
t := fyne.NewMenu("Times", menuItems...)
m := newTimeMenu(t, fyne.NewSize(sizedMenuWidth+theme.Padding()*2, minHeight))
widget.ShowPopUpAtPosition(m, n.main.Canvas(), position)
})
l.timeButton.Alignment = widget.ButtonAlignLeading
l.timeButton.IconPlacement = widget.ButtonIconTrailingText
l.timeButton.Importance = widget.LowImportance
return l
}
func (l *location) onTimeSelect(t string) {
var hour, minute int
if t == "Now" {
globalAppTime = time.Now()
hour = time.Now().Hour()
minute = time.Now().Minute()
currentTimeSelected = true
} else {
fmt.Sscanf(t, "%d:%d", &hour, &minute)
currentTimeSelected = false
}
localOld := globalAppTime.In(l.location.localTime.Location())
selectedDate := time.Date(localOld.Year(), localOld.Month(), localOld.Day(), hour, minute, 0, 0, l.location.localTime.Location())
setDate(selectedDate, l.homeContainer.Objects)
}
func (l *location) CreateRenderer() fyne.WidgetRenderer {
bg := canvas.NewImageFromResource(theme.FileImageIcon())
op := canvas.NewRectangle(color.NRGBA{0x00, 0x00, 0x00, 0x59})
bg.Translucency = 0.5
city := widget.NewRichTextFromMarkdown("# " + strings.ToUpper(l.location.name))
city.Move(fyne.NewPos(3, 0))
l.locationTZLabel = canvas.NewText(strings.ToUpper(l.location.country)+" · "+l.location.localTime.Format("MST"), locationTextColor)
l.locationTZLabel.TextStyle.Monospace = true
l.locationTZLabel.TextSize = 10
l.locationTZLabel.Move(fyne.NewPos(12, 40))
input := container.NewBorder(nil, nil, l.dateButton, l.timeButton)
c := container.NewMax(bg, op,
container.NewBorder(nil,
container.NewVBox(container.NewHBox(container.NewWithoutLayout(city, l.locationTZLabel), layout.NewSpacer(), l.dots), input), nil, nil))
go func() {
if l.session == nil {
return
}
unsplashBg, err := l.session.get(l.location)
if err != nil {
fyne.LogError("unable to build Unsplash image", err)
return
}
c.Objects[0] = unsplashBg
c.Refresh()
}()
return widget.NewSimpleRenderer(c)
}
func listTimes() (times []string) {
times = append(times, "Now")
for hour := 0; hour < 24; hour++ {
times = append(times,
fmt.Sprintf("%02d:00", hour), fmt.Sprintf("%02d:15", hour),
fmt.Sprintf("%02d:30", hour), fmt.Sprintf("%02d:45", hour))
}
return times
}
func (l *location) updateLocation(locDate time.Time) {
l.timeButton.Text = locDate.Format("15:04")
l.timeButton.Refresh()
l.locationTZLabel.Text = strings.ToUpper(l.location.country + " · " + locDate.Format("MST"))
l.locationTZLabel.Refresh()
l.dateButton.SetText(locDate.Format("Mon 02 Jan 2006"))
}
func (l *location) remove(homeContainer *fyne.Container) {
for i := 0; i < len(l.app.store.list); i++ {
if l.location == l.app.store.list[i] {
l.app.store.remove(i)
l.removeLocationFromContainer(homeContainer)
l.session.removeImageFromCache(l)
l.updateMenu()
break
}
}
}
func (l *location) removeLocationFromContainer(homeContainer *fyne.Container) {
for j := 0; j < len(homeContainer.Objects)-1; j++ {
if l.location.name == homeContainer.Objects[j].(*location).location.name {
homeContainer.Remove(homeContainer.Objects[j])
break
}
}
}
func (l *location) updateMenu() {
if deskApp, ok := fyne.CurrentApp().(desktop.App); ok {
setupSystrayMenu(deskApp, l.app.main, l.app.store)
}
}