v27: weather flyout with sun and cloud icon, ZIP code in settings
This commit is contained in:
@@ -0,0 +1,585 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// The weather flyout: a storm cloud on the bar that opens a small panel above
|
||||||
|
// it with the current weather, the next five days and the moon phase.
|
||||||
|
//
|
||||||
|
// The panel is its own window, like the hover preview: it never takes focus
|
||||||
|
// from what you are working in. It goes away when you click the cloud again or
|
||||||
|
// click anywhere else. It is painted at double size and shrunk down so the
|
||||||
|
// round pictures (sun, clouds, moon) have smooth edges.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
pEllipse = gdi32.NewProc("Ellipse")
|
||||||
|
pStretchBlt = gdi32.NewProc("StretchBlt")
|
||||||
|
pSetStretchBltMode = gdi32.NewProc("SetStretchBltMode")
|
||||||
|
pSetBrushOrgEx = gdi32.NewProc("SetBrushOrgEx")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
wxLayoutW = 340.0 // panel width, in 96 dpi units
|
||||||
|
wxFullH = 428.0 // panel height when there is a forecast to show
|
||||||
|
wxShortH = 150.0 // panel height for the "add a ZIP code" and error messages
|
||||||
|
|
||||||
|
wxPollTimerID = 1
|
||||||
|
vkRButton = 0x02
|
||||||
|
srcCopy = 0x00CC0020
|
||||||
|
halftone = 4
|
||||||
|
dtWordBreak = 0x0010
|
||||||
|
dtRight = 0x0002
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
wxHwnd uintptr
|
||||||
|
wxShown bool
|
||||||
|
wxPrevBtn bool // a mouse button was down at the last look
|
||||||
|
|
||||||
|
wxFontsMade bool
|
||||||
|
fBig, fMed, fText, fBold, fSmall uintptr
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---------- the button on the bar ----------
|
||||||
|
|
||||||
|
// circleRaw paints a filled circle with no outline.
|
||||||
|
func circleRaw(hdc uintptr, cx, cy, rad int32, color uintptr) {
|
||||||
|
brush, _, _ := pCreateSolidBrush.Call(color)
|
||||||
|
nullPen, _, _ := pGetStockObject.Call(8) // NULL_PEN
|
||||||
|
oldBrush, _, _ := pSelectObject.Call(hdc, brush)
|
||||||
|
oldPen, _, _ := pSelectObject.Call(hdc, nullPen)
|
||||||
|
pEllipse.Call(hdc, uintptr(cx-rad), uintptr(cy-rad), uintptr(cx+rad+1), uintptr(cy+rad+1))
|
||||||
|
pSelectObject.Call(hdc, oldPen)
|
||||||
|
pSelectObject.Call(hdc, oldBrush)
|
||||||
|
pDeleteObject.Call(brush)
|
||||||
|
}
|
||||||
|
|
||||||
|
// drawWeatherIcon draws the little storm cloud on the bar.
|
||||||
|
func drawWeatherIcon(hdc uintptr, r rect) {
|
||||||
|
if !layerFg {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cx, cy := center(r)
|
||||||
|
cx -= px(0.5)
|
||||||
|
|
||||||
|
// A small sun up and to the left, with a cloud drifting in front of its lower right.
|
||||||
|
br, bg, bb := styleColor(255, 205, 60)
|
||||||
|
sun := rgb(br, bg, bb)
|
||||||
|
sx, sy := cx-px(2), cy-px(2)
|
||||||
|
circleRaw(hdc, sx, sy, px(3), sun)
|
||||||
|
for k := 0; k < 8; k++ {
|
||||||
|
a := float64(k) * math.Pi / 4
|
||||||
|
c, s := math.Cos(a), math.Sin(a)
|
||||||
|
polyline(hdc, []point{
|
||||||
|
{sx + int32(math.Round(c*float64(px(4.3)))), sy + int32(math.Round(s*float64(px(4.3))))},
|
||||||
|
{sx + int32(math.Round(c*float64(px(5.5)))), sy + int32(math.Round(s*float64(px(5.5))))},
|
||||||
|
}, px(1), sun)
|
||||||
|
}
|
||||||
|
|
||||||
|
col := iconColor()
|
||||||
|
circleRaw(hdc, cx-px(0.5), cy+px(3.5), px(2.5), col)
|
||||||
|
circleRaw(hdc, cx+px(2.5), cy+px(1.5), px(3.2), col)
|
||||||
|
circleRaw(hdc, cx+px(5.5), cy+px(3.5), px(2.3), col)
|
||||||
|
fillRaw(hdc, rect{cx - px(0.5), cy + px(3.5), cx + px(5.5), cy + px(6)}, col)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- the panel window ----------
|
||||||
|
|
||||||
|
func weatherInit(inst uintptr) {
|
||||||
|
cursor, _, _ := pLoadCursor.Call(0, idcArrow)
|
||||||
|
className := u16("MirrorBarWeather")
|
||||||
|
wc := wndClassEx{
|
||||||
|
WndProc: syscall.NewCallback(weatherProc),
|
||||||
|
Instance: inst,
|
||||||
|
Cursor: cursor,
|
||||||
|
ClassName: className,
|
||||||
|
}
|
||||||
|
wc.Size = uint32(unsafe.Sizeof(wc))
|
||||||
|
pRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
|
||||||
|
wxHwnd, _, _ = pCreateWindowEx.Call(
|
||||||
|
wsExToolWindow|wsExTopmost|wsExNoActivate,
|
||||||
|
uintptr(unsafe.Pointer(className)),
|
||||||
|
uintptr(unsafe.Pointer(u16(""))),
|
||||||
|
wsPopup,
|
||||||
|
0, 0, 10, 10,
|
||||||
|
0, 0, inst, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func weatherProc(hwnd, m, wparam, lparam uintptr) (result uintptr) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
result = 0 // the flyout is optional; never take the bar down with it
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
switch uint32(m) {
|
||||||
|
case wmEraseBkgnd:
|
||||||
|
return 1
|
||||||
|
case wmMouseActivate:
|
||||||
|
return maNoActivate
|
||||||
|
case wmPaint:
|
||||||
|
wxPaint(hwnd)
|
||||||
|
return 0
|
||||||
|
case wmTimer:
|
||||||
|
weatherPoll()
|
||||||
|
return 0
|
||||||
|
case wmLButtonUp:
|
||||||
|
weatherClick()
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
r, _, _ := pDefWindowProc.Call(hwnd, m, wparam, lparam)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherToggle is what clicking the cloud does.
|
||||||
|
func weatherToggle() {
|
||||||
|
if wxShown {
|
||||||
|
weatherHide()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if wxHwnd == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
previewHide()
|
||||||
|
weatherRefresh(false)
|
||||||
|
wxShown = true
|
||||||
|
wxPrevBtn = true // ignore a button that is still held from the click that opened this
|
||||||
|
weatherPlace()
|
||||||
|
pSetTimer.Call(wxHwnd, wxPollTimerID, 80, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func weatherHide() {
|
||||||
|
if !wxShown {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wxShown = false
|
||||||
|
pKillTimer.Call(wxHwnd, wxPollTimerID)
|
||||||
|
pShowWindow.Call(wxHwnd, swHide)
|
||||||
|
}
|
||||||
|
|
||||||
|
func weatherShutdown() {
|
||||||
|
weatherHide()
|
||||||
|
if wxHwnd != 0 {
|
||||||
|
pDestroyWindow.Call(wxHwnd)
|
||||||
|
wxHwnd = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherAnchor is where the cloud sits on the screen.
|
||||||
|
func weatherAnchor() rect {
|
||||||
|
var wr rect
|
||||||
|
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
|
||||||
|
w, h := clientSize(hwndMain)
|
||||||
|
for _, it := range trayItems(w, h) {
|
||||||
|
if it.kind == trayWeather {
|
||||||
|
r := it.r
|
||||||
|
if vertical {
|
||||||
|
r = mapRect(r, h)
|
||||||
|
}
|
||||||
|
return rect{wr.Left + r.Left, wr.Top + r.Top, wr.Left + r.Right, wr.Top + r.Bottom}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wr
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherPlace sizes the panel for what it has to show and puts it next to the
|
||||||
|
// cloud: above a horizontal bar, to the left of a vertical one.
|
||||||
|
func weatherPlace() {
|
||||||
|
if wxHwnd == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s := wxSnapshot()
|
||||||
|
lh := wxShortH
|
||||||
|
if s.ok && wxZip != "" {
|
||||||
|
lh = wxFullH
|
||||||
|
}
|
||||||
|
cw, ch := px(wxLayoutW), px(lh)
|
||||||
|
|
||||||
|
ar := weatherAnchor()
|
||||||
|
var wr rect
|
||||||
|
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
|
||||||
|
sw, sh := screenSize()
|
||||||
|
space := px(8)
|
||||||
|
var x, y int32
|
||||||
|
if vertical {
|
||||||
|
x = wr.Left - cw - space
|
||||||
|
if x < 0 {
|
||||||
|
x = wr.Right + space
|
||||||
|
}
|
||||||
|
y = (ar.Top+ar.Bottom)/2 - ch/2
|
||||||
|
} else {
|
||||||
|
x = (ar.Left+ar.Right)/2 - cw/2
|
||||||
|
y = wr.Top - ch - space
|
||||||
|
if y < 0 {
|
||||||
|
y = wr.Bottom + space
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x = clampI(x, 0, sw-cw)
|
||||||
|
y = clampI(y, 0, sh-ch)
|
||||||
|
pSetWindowPos.Call(wxHwnd, hwndTopmost, uintptr(x), uintptr(y), uintptr(cw), uintptr(ch), swpNoActivate|swpShowWindow)
|
||||||
|
pInvalidateRect.Call(wxHwnd, 0, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherPoll closes the panel when a mouse button goes down anywhere outside
|
||||||
|
// it (the cloud itself is left alone, so clicking it can close the panel).
|
||||||
|
func weatherPoll() {
|
||||||
|
l, _, _ := pGetAsyncKeyState.Call(vkLButton)
|
||||||
|
r, _, _ := pGetAsyncKeyState.Call(vkRButton)
|
||||||
|
down := (l|r)&0x8000 != 0
|
||||||
|
if down && !wxPrevBtn {
|
||||||
|
var cp point
|
||||||
|
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
|
||||||
|
var fr rect
|
||||||
|
pGetWindowRect.Call(wxHwnd, uintptr(unsafe.Pointer(&fr)))
|
||||||
|
if !inRect(fr, cp) && !inRect(weatherAnchor(), cp) {
|
||||||
|
weatherHide()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wxPrevBtn = down
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherClick runs when the panel itself is clicked: the messages in it are
|
||||||
|
// buttons (open the settings, try again).
|
||||||
|
func weatherClick() {
|
||||||
|
s := wxSnapshot()
|
||||||
|
switch {
|
||||||
|
case wxZip == "":
|
||||||
|
weatherHide()
|
||||||
|
openSettings()
|
||||||
|
case !s.ok && !s.loading:
|
||||||
|
weatherRefresh(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherDone runs when a lookup finishes.
|
||||||
|
func weatherDone() {
|
||||||
|
if wxShown {
|
||||||
|
weatherPlace()
|
||||||
|
}
|
||||||
|
settingsZipStatusUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherStatusText is the one line the settings window shows under the ZIP box.
|
||||||
|
func weatherStatusText() string {
|
||||||
|
if wxZip == "" {
|
||||||
|
return "Type a 5 digit ZIP code to turn the weather on."
|
||||||
|
}
|
||||||
|
s := wxSnapshot()
|
||||||
|
switch {
|
||||||
|
case s.ok && s.place != "":
|
||||||
|
return "Showing weather for " + s.place
|
||||||
|
case s.loading:
|
||||||
|
return "Looking up " + wxZip + "..."
|
||||||
|
case s.err != "":
|
||||||
|
return s.err
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- painting ----------
|
||||||
|
|
||||||
|
func wxMakeFonts() {
|
||||||
|
if wxFontsMade {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Called while the scale is doubled, so these are double size fonts.
|
||||||
|
fBig = makeFont(px(34), 400)
|
||||||
|
fMed = makeFont(px(15), 600)
|
||||||
|
fText = makeFont(px(13), 400)
|
||||||
|
fBold = makeFont(px(13), 600)
|
||||||
|
fSmall = makeFont(px(11), 400)
|
||||||
|
wxFontsMade = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxPaint(hwnd uintptr) {
|
||||||
|
var ps paintStruct
|
||||||
|
hdc, _, _ := pBeginPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps)))
|
||||||
|
defer pEndPaint.Call(hwnd, uintptr(unsafe.Pointer(&ps)))
|
||||||
|
|
||||||
|
var rc rect
|
||||||
|
pGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&rc)))
|
||||||
|
W, H := rc.Right-rc.Left, rc.Bottom-rc.Top
|
||||||
|
if W <= 0 || H <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const ss = 2
|
||||||
|
|
||||||
|
mem, _, _ := pCreateCompatibleDC.Call(hdc)
|
||||||
|
defer pDeleteDC.Call(mem)
|
||||||
|
bmi := bitmapInfoHeader{Width: W * ss, Height: -(H * ss), Planes: 1, BitCount: 32}
|
||||||
|
bmi.Size = uint32(unsafe.Sizeof(bmi))
|
||||||
|
var bits unsafe.Pointer
|
||||||
|
bmp, _, _ := pCreateDIBSection.Call(hdc, uintptr(unsafe.Pointer(&bmi)), 0, uintptr(unsafe.Pointer(&bits)), 0, 0)
|
||||||
|
if bmp == 0 || bits == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer pDeleteObject.Call(bmp)
|
||||||
|
old, _, _ := pSelectObject.Call(mem, bmp)
|
||||||
|
defer pSelectObject.Call(mem, old)
|
||||||
|
|
||||||
|
saved := scale
|
||||||
|
defer func() { scale = saved }()
|
||||||
|
scale *= ss
|
||||||
|
wxMakeFonts()
|
||||||
|
wxDraw(mem, W*ss, H*ss)
|
||||||
|
scale = saved
|
||||||
|
|
||||||
|
pSetStretchBltMode.Call(hdc, halftone)
|
||||||
|
pSetBrushOrgEx.Call(hdc, 0, 0, 0)
|
||||||
|
pStretchBlt.Call(hdc, 0, 0, uintptr(W), uintptr(H), mem, 0, 0, uintptr(W*ss), uintptr(H*ss), srcCopy)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
colBack = rgb(28, 28, 34)
|
||||||
|
colEdge = rgb(78, 78, 92)
|
||||||
|
colRule = rgb(52, 52, 62)
|
||||||
|
colText = rgb(240, 240, 245)
|
||||||
|
colDim = rgb(150, 150, 165)
|
||||||
|
colAccent = rgb(90, 190, 255)
|
||||||
|
)
|
||||||
|
|
||||||
|
func wxText(hdc uintptr, s string, r rect, font, col, flags uintptr) {
|
||||||
|
pSetBkMode.Call(hdc, transparent)
|
||||||
|
pSetTextColor.Call(hdc, col)
|
||||||
|
old, _, _ := pSelectObject.Call(hdc, font)
|
||||||
|
drawTextFmt(hdc, s, r, flags|dtNoPrefix)
|
||||||
|
pSelectObject.Call(hdc, old)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wxDraw paints the whole panel. The scale is already doubled, so every px()
|
||||||
|
// here is a double size distance.
|
||||||
|
func wxDraw(hdc uintptr, W, H int32) {
|
||||||
|
u := px
|
||||||
|
fillRaw(hdc, rect{0, 0, W, H}, colEdge)
|
||||||
|
fillRaw(hdc, rect{u(1), u(1), W - u(1), H - u(1)}, colBack)
|
||||||
|
|
||||||
|
pad := u(14)
|
||||||
|
inner := W - 2*pad
|
||||||
|
s := wxSnapshot()
|
||||||
|
line := uintptr(dtLeft | dtVCenter | dtSingleLine | dtEndEllipsis)
|
||||||
|
rule := func(y int32) { fillRaw(hdc, rect{pad, y, W - pad, y + u(1)}, colRule) }
|
||||||
|
|
||||||
|
// Header: the place on the left, how fresh the data is on the right.
|
||||||
|
title := "Weather"
|
||||||
|
if s.place != "" && wxZip != "" {
|
||||||
|
title = s.place
|
||||||
|
}
|
||||||
|
wxText(hdc, title, rect{pad, u(10), W - pad - u(108), u(34)}, fMed, colText, line)
|
||||||
|
status := ""
|
||||||
|
switch {
|
||||||
|
case s.loading:
|
||||||
|
status = "Updating..."
|
||||||
|
case s.ok && s.err != "":
|
||||||
|
status = "Couldn't update"
|
||||||
|
case s.ok:
|
||||||
|
status = "Updated " + s.fetched.Format("3:04 PM")
|
||||||
|
}
|
||||||
|
wxText(hdc, status, rect{W - pad - u(108), u(12), W - pad, u(32)}, fSmall, colDim, dtRight|dtVCenter|dtSingleLine)
|
||||||
|
|
||||||
|
// Nothing to show yet: say why. These messages are clickable.
|
||||||
|
if wxZip == "" || !s.ok {
|
||||||
|
body := rect{pad, u(48), W - pad, H - pad}
|
||||||
|
var head, sub string
|
||||||
|
switch {
|
||||||
|
case wxZip == "":
|
||||||
|
head, sub = "No ZIP code yet", "Click here to open the settings and type your ZIP code."
|
||||||
|
case s.loading:
|
||||||
|
head, sub = "Getting the forecast...", ""
|
||||||
|
default:
|
||||||
|
head, sub = "No weather right now", s.err+" Click here to try again."
|
||||||
|
}
|
||||||
|
wxText(hdc, head, rect{body.Left, body.Top, body.Right, body.Top + u(26)}, fMed, colText, dtCenter|dtVCenter|dtSingleLine)
|
||||||
|
if sub != "" {
|
||||||
|
wxText(hdc, sub, rect{body.Left, body.Top + u(30), body.Right, body.Bottom}, fText, colDim, dtCenter|dtWordBreak)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
now, today := s.now, s.days[0]
|
||||||
|
|
||||||
|
// Current conditions.
|
||||||
|
wxIcon(hdc, wxKind(now.code), rect{pad, u(42), pad + u(64), u(42) + u(64)}, now.day)
|
||||||
|
wxText(hdc, wxF(now.temp), rect{pad + u(76), u(38), pad + u(76) + u(120), u(88)}, fBig, colText, dtLeft|dtVCenter|dtSingleLine)
|
||||||
|
wxText(hdc, wxDesc(now.code), rect{pad + u(76), u(88), W - pad, u(110)}, fText, colDim, line)
|
||||||
|
wxText(hdc, "Feels like "+wxF(now.feels), rect{W - pad - u(124), u(44), W - pad, u(62)}, fText, colText, dtRight|dtVCenter|dtSingleLine)
|
||||||
|
wxText(hdc, "High "+wxDeg(today.hi)+" Low "+wxDeg(today.lo), rect{W - pad - u(124), u(64), W - pad, u(82)}, fText, colText, dtRight|dtVCenter|dtSingleLine)
|
||||||
|
rule(u(124))
|
||||||
|
|
||||||
|
// Details, three across.
|
||||||
|
colW := inner / 3
|
||||||
|
cell := func(i int, row int32, label, value string) {
|
||||||
|
x := pad + int32(i)*colW
|
||||||
|
y := u(132) + row*u(44)
|
||||||
|
wxText(hdc, label, rect{x, y, x + colW, y + u(14)}, fSmall, colDim, line)
|
||||||
|
wxText(hdc, value, rect{x, y + u(14), x + colW, y + u(36)}, fBold, colText, line)
|
||||||
|
}
|
||||||
|
dash := func(v string) string {
|
||||||
|
if v == "" {
|
||||||
|
return "-"
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
cell(0, 0, "Humidity", strconv.Itoa(now.humidity)+"%")
|
||||||
|
cell(1, 0, "Wind", strconv.Itoa(int(math.Round(now.wind)))+" mph")
|
||||||
|
cell(2, 0, "Chance of rain", strconv.Itoa(today.rain)+"%")
|
||||||
|
cell(0, 1, "Sunrise", dash(today.sunrise))
|
||||||
|
cell(1, 1, "Sunset", dash(today.sunset))
|
||||||
|
cell(2, 1, "UV index", strconv.Itoa(int(math.Round(today.uv))))
|
||||||
|
rule(u(218))
|
||||||
|
|
||||||
|
// Moon.
|
||||||
|
moon := moonAt(time.Now())
|
||||||
|
wxMoonDraw(hdc, pad+u(24), u(254), u(22), moon, s.lat >= 0)
|
||||||
|
tx := pad + u(60)
|
||||||
|
wxText(hdc, moon.name, rect{tx, u(232), W - pad, u(252)}, fMed, colText, line)
|
||||||
|
wxText(hdc, strconv.Itoa(int(math.Round(moon.illum*100)))+"% lit", rect{tx, u(254), W - pad, u(272)}, fText, colDim, line)
|
||||||
|
wxText(hdc, moon.fullText(), rect{tx, u(272), W - pad, u(288)}, fSmall, colDim, line)
|
||||||
|
rule(u(296))
|
||||||
|
|
||||||
|
// The next five days.
|
||||||
|
wxText(hdc, "5-DAY FORECAST", rect{pad, u(304), W - pad, u(320)}, fSmall, colDim, line)
|
||||||
|
days := s.days[1:]
|
||||||
|
if len(days) > 5 {
|
||||||
|
days = days[:5]
|
||||||
|
}
|
||||||
|
cw := inner / 5
|
||||||
|
for i, d := range days {
|
||||||
|
x := pad + int32(i)*cw
|
||||||
|
col := rect{x, 0, x + cw, 0}
|
||||||
|
wxText(hdc, d.date.Weekday().String()[:3], rect{col.Left, u(324), col.Right, u(342)}, fBold, colText, dtCenter|dtVCenter|dtSingleLine)
|
||||||
|
ic := u(32)
|
||||||
|
ix := x + (cw-ic)/2
|
||||||
|
wxIcon(hdc, wxKind(d.code), rect{ix, u(344), ix + ic, u(344) + ic}, true)
|
||||||
|
wxText(hdc, wxDeg(d.hi), rect{col.Left, u(380), col.Right, u(400)}, fBold, colText, dtCenter|dtVCenter|dtSingleLine)
|
||||||
|
wxText(hdc, wxDeg(d.lo), rect{col.Left, u(400), col.Right, u(418)}, fText, colDim, dtCenter|dtVCenter|dtSingleLine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- pictures ----------
|
||||||
|
|
||||||
|
func wxCircle(hdc uintptr, cx, cy, rad float64, color uintptr) {
|
||||||
|
circleRaw(hdc, int32(math.Round(cx)), int32(math.Round(cy)), int32(math.Round(rad)), color)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wxCloud draws a cloud about 0.67 of s wide, centered on cx and cy.
|
||||||
|
func wxCloud(hdc uintptr, cx, cy, s float64, color uintptr) {
|
||||||
|
cy += 0.045 * s // the cloud's own middle sits a little above its circles
|
||||||
|
wxCircle(hdc, cx-0.20*s, cy+0.03*s, 0.14*s, color)
|
||||||
|
wxCircle(hdc, cx-0.02*s, cy-0.07*s, 0.19*s, color)
|
||||||
|
wxCircle(hdc, cx+0.20*s, cy+0.04*s, 0.13*s, color)
|
||||||
|
fillRaw(hdc, rect{
|
||||||
|
int32(math.Round(cx - 0.20*s)), int32(math.Round(cy + 0.02*s)),
|
||||||
|
int32(math.Round(cx + 0.20*s)), int32(math.Round(cy + 0.17*s)),
|
||||||
|
}, color)
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxLine(hdc uintptr, x0, y0, x1, y1, width float64, color uintptr) {
|
||||||
|
polyline(hdc, []point{
|
||||||
|
{int32(math.Round(x0)), int32(math.Round(y0))},
|
||||||
|
{int32(math.Round(x1)), int32(math.Round(y1))},
|
||||||
|
}, max(1, int32(math.Round(width))), color)
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxSunGlyph(hdc uintptr, cx, cy, s float64) {
|
||||||
|
col := rgb(255, 200, 60)
|
||||||
|
wxCircle(hdc, cx, cy, 0.2*s, col)
|
||||||
|
for k := 0; k < 8; k++ {
|
||||||
|
a := float64(k) * math.Pi / 4
|
||||||
|
c, si := math.Cos(a), math.Sin(a)
|
||||||
|
wxLine(hdc, cx+c*0.30*s, cy+si*0.30*s, cx+c*0.43*s, cy+si*0.43*s, 0.05*s, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxCrescent(hdc uintptr, cx, cy, rad float64) {
|
||||||
|
wxCircle(hdc, cx, cy, rad, rgb(235, 232, 205))
|
||||||
|
wxCircle(hdc, cx+0.45*rad, cy-0.25*rad, 0.85*rad, colBack)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wxIcon draws the picture for a kind of weather inside the square r.
|
||||||
|
func wxIcon(hdc uintptr, kind int, r rect, day bool) {
|
||||||
|
s := float64(r.Right - r.Left)
|
||||||
|
cx := float64(r.Left+r.Right) / 2
|
||||||
|
cy := float64(r.Top+r.Bottom) / 2
|
||||||
|
light := rgb(205, 212, 225)
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case wxSun:
|
||||||
|
if day {
|
||||||
|
wxSunGlyph(hdc, cx, cy, s)
|
||||||
|
} else {
|
||||||
|
wxCrescent(hdc, cx, cy, 0.3*s)
|
||||||
|
}
|
||||||
|
case wxPartly:
|
||||||
|
if day {
|
||||||
|
wxSunGlyph(hdc, cx-0.14*s, cy-0.15*s, 0.8*s)
|
||||||
|
} else {
|
||||||
|
wxCrescent(hdc, cx-0.14*s, cy-0.15*s, 0.22*s)
|
||||||
|
}
|
||||||
|
wxCloud(hdc, cx+0.04*s, cy+0.08*s, 0.9*s, light)
|
||||||
|
case wxCloudy:
|
||||||
|
wxCloud(hdc, cx, cy, 1.1*s, light)
|
||||||
|
case wxFog:
|
||||||
|
fog := rgb(150, 158, 172)
|
||||||
|
wxCloud(hdc, cx, cy-0.1*s, s, fog)
|
||||||
|
for i, w := range []float64{0.26, 0.20, 0.26} {
|
||||||
|
y := cy + (0.16+0.10*float64(i))*s
|
||||||
|
wxLine(hdc, cx-w*s, y, cx+w*s, y, 0.04*s, fog)
|
||||||
|
}
|
||||||
|
case wxRain:
|
||||||
|
wxCloud(hdc, cx, cy-0.1*s, s, rgb(170, 180, 198))
|
||||||
|
for _, dx := range []float64{-0.16, 0, 0.16} {
|
||||||
|
x := cx + dx*s
|
||||||
|
wxLine(hdc, x, cy+0.14*s, x-0.04*s, cy+0.32*s, 0.045*s, rgb(80, 165, 255))
|
||||||
|
}
|
||||||
|
case wxSnow:
|
||||||
|
wxCloud(hdc, cx, cy-0.1*s, s, rgb(185, 195, 212))
|
||||||
|
wxCircle(hdc, cx-0.15*s, cy+0.17*s, 0.035*s, colText)
|
||||||
|
wxCircle(hdc, cx, cy+0.29*s, 0.035*s, colText)
|
||||||
|
wxCircle(hdc, cx+0.15*s, cy+0.17*s, 0.035*s, colText)
|
||||||
|
case wxStorm:
|
||||||
|
wxCloud(hdc, cx, cy-0.12*s, s, rgb(115, 122, 140))
|
||||||
|
pts := [][2]float64{{0.07, 0.02}, {-0.09, 0.24}, {0.01, 0.24}, {-0.07, 0.42}, {0.13, 0.16}, {0.03, 0.16}}
|
||||||
|
bolt := make([]point, len(pts))
|
||||||
|
for i, p := range pts {
|
||||||
|
bolt[i] = point{int32(math.Round(cx + p[0]*s)), int32(math.Round(cy + p[1]*s))}
|
||||||
|
}
|
||||||
|
polygon(hdc, bolt, rgb(255, 205, 60))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wxMoonDraw draws the moon with its lit part. A waxing moon is lit on the
|
||||||
|
// right and a waning one on the left; south of the equator that is the other
|
||||||
|
// way around.
|
||||||
|
func wxMoonDraw(hdc uintptr, cx, cy, r int32, m moonInfo, north bool) {
|
||||||
|
wxCircle(hdc, float64(cx), float64(cy), float64(r), rgb(58, 60, 74))
|
||||||
|
|
||||||
|
brush, _, _ := pCreateSolidBrush.Call(rgb(236, 232, 208))
|
||||||
|
defer pDeleteObject.Call(brush)
|
||||||
|
|
||||||
|
waxing := m.frac < 0.5
|
||||||
|
theta := 2 * math.Pi * m.frac
|
||||||
|
if !waxing {
|
||||||
|
theta = 2 * math.Pi * (1 - m.frac)
|
||||||
|
}
|
||||||
|
ct := math.Cos(theta)
|
||||||
|
for dy := -r; dy <= r; dy++ {
|
||||||
|
hw := math.Sqrt(math.Max(0, float64(r)*float64(r)-float64(dy)*float64(dy)))
|
||||||
|
xt := hw * ct
|
||||||
|
x0, x1 := xt, hw
|
||||||
|
if !waxing {
|
||||||
|
x0, x1 = -hw, -xt
|
||||||
|
}
|
||||||
|
if !north {
|
||||||
|
x0, x1 = -x1, -x0
|
||||||
|
}
|
||||||
|
if x1-x0 < 0.5 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
line := rect{cx + int32(math.Round(x0)), cy + dy, cx + int32(math.Round(x1)), cy + dy + 1}
|
||||||
|
pFillRect.Call(hdc, uintptr(unsafe.Pointer(&line)), brush)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1127,6 +1127,9 @@ func wndProc(hwnd, m, wparam, lparam uintptr) (result uintptr) {
|
|||||||
case wmDropFiles:
|
case wmDropFiles:
|
||||||
dropFiles(hwnd, wparam)
|
dropFiles(hwnd, wparam)
|
||||||
return 0
|
return 0
|
||||||
|
case wmWxDone:
|
||||||
|
weatherDone()
|
||||||
|
return 0
|
||||||
case wmMouseWheel:
|
case wmMouseWheel:
|
||||||
// Scrolling over the volume icon changes the volume. The wheel
|
// Scrolling over the volume icon changes the volume. The wheel
|
||||||
// position arrives in screen coordinates, so make it relative to the bar.
|
// position arrives in screen coordinates, so make it relative to the bar.
|
||||||
@@ -1184,6 +1187,7 @@ func wndProc(hwnd, m, wparam, lparam uintptr) (result uintptr) {
|
|||||||
return 0
|
return 0
|
||||||
case wmDestroy:
|
case wmDestroy:
|
||||||
previewShutdown()
|
previewShutdown()
|
||||||
|
weatherShutdown()
|
||||||
abd := appBarData{Hwnd: hwnd}
|
abd := appBarData{Hwnd: hwnd}
|
||||||
appBar(abmRemove, &abd)
|
appBar(abmRemove, &abd)
|
||||||
pKillTimer.Call(hwnd, timerID)
|
pKillTimer.Call(hwnd, timerID)
|
||||||
@@ -1244,6 +1248,7 @@ func main() {
|
|||||||
loadPinned()
|
loadPinned()
|
||||||
loadOrderKeys()
|
loadOrderKeys()
|
||||||
loadIconMode()
|
loadIconMode()
|
||||||
|
loadZip()
|
||||||
|
|
||||||
hdc, _, _ := pGetDC.Call(0)
|
hdc, _, _ := pGetDC.Call(0)
|
||||||
dpi, _, _ := pGetDeviceCaps.Call(hdc, logPixelsX)
|
dpi, _, _ := pGetDeviceCaps.Call(hdc, logPixelsX)
|
||||||
@@ -1297,6 +1302,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
hwndMain = hwnd
|
hwndMain = hwnd
|
||||||
previewInit(inst)
|
previewInit(inst)
|
||||||
|
weatherInit(inst)
|
||||||
enableFileDrop(hwnd)
|
enableFileDrop(hwnd)
|
||||||
refreshAutostartPath()
|
refreshAutostartPath()
|
||||||
|
|
||||||
@@ -1325,6 +1331,7 @@ func main() {
|
|||||||
refreshApps(true)
|
refreshApps(true)
|
||||||
positionBar(hwnd)
|
positionBar(hwnd)
|
||||||
pSetTimer.Call(hwnd, timerID, 250, 0)
|
pSetTimer.Call(hwnd, timerID, 250, 0)
|
||||||
|
weatherRefresh(false) // have the forecast ready before the cloud is first clicked
|
||||||
|
|
||||||
var m msg
|
var m msg
|
||||||
for {
|
for {
|
||||||
|
|||||||
+65
-6
@@ -16,6 +16,7 @@ var (
|
|||||||
pIsDialogMessage = user32.NewProc("IsDialogMessageW")
|
pIsDialogMessage = user32.NewProc("IsDialogMessageW")
|
||||||
pAdjustWindowRectEx = user32.NewProc("AdjustWindowRectEx")
|
pAdjustWindowRectEx = user32.NewProc("AdjustWindowRectEx")
|
||||||
pInitCommonControls = syscall.NewLazyDLL("comctl32.dll").NewProc("InitCommonControlsEx")
|
pInitCommonControls = syscall.NewLazyDLL("comctl32.dll").NewProc("InitCommonControlsEx")
|
||||||
|
pGetFocus = user32.NewProc("GetFocus")
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -29,11 +30,17 @@ const (
|
|||||||
wsTabStop = 0x00010000
|
wsTabStop = 0x00010000
|
||||||
wsVScroll = 0x00200000
|
wsVScroll = 0x00200000
|
||||||
wsWindowFrame = 0x00C80000 // caption, system menu, border: a plain window that can't be resized
|
wsWindowFrame = 0x00C80000 // caption, system menu, border: a plain window that can't be resized
|
||||||
|
wsBorder = 0x00800000
|
||||||
bsPushButton = 0x0000
|
bsPushButton = 0x0000
|
||||||
bsDefPushButton = 0x0001
|
bsDefPushButton = 0x0001
|
||||||
bsAutoCheckBox = 0x0003
|
bsAutoCheckBox = 0x0003
|
||||||
cbsDropDownList = 0x0003
|
cbsDropDownList = 0x0003
|
||||||
tbsNoTicks = 0x0010
|
tbsNoTicks = 0x0010
|
||||||
|
esAutoHScroll = 0x0080
|
||||||
|
esNumber = 0x2000
|
||||||
|
|
||||||
|
emSetLimitText = 0x00C5
|
||||||
|
enChange = 0x0300
|
||||||
|
|
||||||
bmGetCheck = 0x00F0
|
bmGetCheck = 0x00F0
|
||||||
bmSetCheck = 0x00F1
|
bmSetCheck = 0x00F1
|
||||||
@@ -59,6 +66,8 @@ const (
|
|||||||
idResetPos = 208
|
idResetPos = 208
|
||||||
idResetWidth = 209
|
idResetWidth = 209
|
||||||
idClose = 210
|
idClose = 210
|
||||||
|
idZip = 211
|
||||||
|
idZipStatus = 212
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -68,6 +77,8 @@ var (
|
|||||||
|
|
||||||
ctlAlpha, ctlAlphaValue, ctlLogo, ctlClock, ctlTrayIcons, ctlStyle uintptr
|
ctlAlpha, ctlAlphaValue, ctlLogo, ctlClock, ctlTrayIcons, ctlStyle uintptr
|
||||||
ctlVertical, ctlAutostart uintptr
|
ctlVertical, ctlAutostart uintptr
|
||||||
|
ctlZip, ctlZipStatus uintptr
|
||||||
|
zipSyncing bool // true while the program itself is filling in the ZIP box
|
||||||
)
|
)
|
||||||
|
|
||||||
func boolWP(b bool) uintptr {
|
func boolWP(b bool) uintptr {
|
||||||
@@ -118,7 +129,7 @@ func openSettings() {
|
|||||||
settingsFont = makeFont(px(13), 400)
|
settingsFont = makeFont(px(13), 400)
|
||||||
}
|
}
|
||||||
|
|
||||||
r := rect{0, 0, px(400), px(330)}
|
r := rect{0, 0, px(400), px(376)}
|
||||||
pAdjustWindowRectEx.Call(uintptr(unsafe.Pointer(&r)), wsWindowFrame, 0, 0)
|
pAdjustWindowRectEx.Call(uintptr(unsafe.Pointer(&r)), wsWindowFrame, 0, 0)
|
||||||
ow, oh := r.Right-r.Left, r.Bottom-r.Top
|
ow, oh := r.Right-r.Left, r.Bottom-r.Top
|
||||||
sw, sh := screenSize()
|
sw, sh := screenSize()
|
||||||
@@ -160,12 +171,17 @@ func openSettings() {
|
|||||||
pSendMessage.Call(ctlStyle, cbAddString, 0, uintptr(unsafe.Pointer(u16(n))))
|
pSendMessage.Call(ctlStyle, cbAddString, 0, uintptr(unsafe.Pointer(u16(n))))
|
||||||
}
|
}
|
||||||
|
|
||||||
ctlVertical = makeControl(hw, inst, "BUTTON", "Stand the bar up at the right edge (vertical)", bsAutoCheckBox|wsTabStop, 16, 214, 368, 22, idVertical)
|
label("Weather ZIP code", 204)
|
||||||
ctlAutostart = makeControl(hw, inst, "BUTTON", "Start MirrorBar when I sign in", bsAutoCheckBox|wsTabStop, 16, 242, 368, 22, idAutostart)
|
ctlZip = makeControl(hw, inst, "EDIT", "", esNumber|esAutoHScroll|wsBorder|wsTabStop, 170, 204, 90, 24, idZip)
|
||||||
|
pSendMessage.Call(ctlZip, emSetLimitText, 5, 0)
|
||||||
|
ctlZipStatus = makeControl(hw, inst, "STATIC", "", 0, 16, 236, 368, 20, idZipStatus)
|
||||||
|
|
||||||
makeControl(hw, inst, "BUTTON", "Reset position", bsPushButton|wsTabStop, 16, 284, 116, 28, idResetPos)
|
ctlVertical = makeControl(hw, inst, "BUTTON", "Stand the bar up at the right edge (vertical)", bsAutoCheckBox|wsTabStop, 16, 262, 368, 22, idVertical)
|
||||||
makeControl(hw, inst, "BUTTON", "Reset bar width", bsPushButton|wsTabStop, 140, 284, 116, 28, idResetWidth)
|
ctlAutostart = makeControl(hw, inst, "BUTTON", "Start MirrorBar when I sign in", bsAutoCheckBox|wsTabStop, 16, 290, 368, 22, idAutostart)
|
||||||
makeControl(hw, inst, "BUTTON", "Close", bsDefPushButton|wsTabStop, 292, 284, 92, 28, idClose)
|
|
||||||
|
makeControl(hw, inst, "BUTTON", "Reset position", bsPushButton|wsTabStop, 16, 330, 116, 28, idResetPos)
|
||||||
|
makeControl(hw, inst, "BUTTON", "Reset bar width", bsPushButton|wsTabStop, 140, 330, 116, 28, idResetWidth)
|
||||||
|
makeControl(hw, inst, "BUTTON", "Close", bsDefPushButton|wsTabStop, 292, 330, 92, 28, idClose)
|
||||||
|
|
||||||
hwndSettings = hw
|
hwndSettings = hw
|
||||||
settingsSync()
|
settingsSync()
|
||||||
@@ -186,6 +202,45 @@ func settingsSync() {
|
|||||||
pSendMessage.Call(ctlStyle, cbSetCurSel, uintptr(iconMode), 0)
|
pSendMessage.Call(ctlStyle, cbSetCurSel, uintptr(iconMode), 0)
|
||||||
pSendMessage.Call(ctlVertical, bmSetCheck, boolWP(vertical), 0)
|
pSendMessage.Call(ctlVertical, bmSetCheck, boolWP(vertical), 0)
|
||||||
pSendMessage.Call(ctlAutostart, bmSetCheck, boolWP(autostartEnabled()), 0)
|
pSendMessage.Call(ctlAutostart, bmSetCheck, boolWP(autostartEnabled()), 0)
|
||||||
|
|
||||||
|
// Leave the ZIP box alone while the person is typing in it.
|
||||||
|
if f, _, _ := pGetFocus.Call(); f != ctlZip && zipText() != wxZip {
|
||||||
|
zipSyncing = true
|
||||||
|
pSetWindowText.Call(ctlZip, uintptr(unsafe.Pointer(u16(wxZip))))
|
||||||
|
zipSyncing = false
|
||||||
|
}
|
||||||
|
settingsZipStatusUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
// zipText is what is typed in the ZIP box right now.
|
||||||
|
func zipText() string {
|
||||||
|
var buf [32]uint16
|
||||||
|
n, _, _ := pGetWindowText.Call(ctlZip, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
|
||||||
|
return syscall.UTF16ToString(buf[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
// settingsZipStatusUpdate refreshes the line under the ZIP box ("Showing weather for ...").
|
||||||
|
func settingsZipStatusUpdate() {
|
||||||
|
if hwndSettings == 0 || ctlZipStatus == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pSetWindowText.Call(ctlZipStatus, uintptr(unsafe.Pointer(u16(weatherStatusText()))))
|
||||||
|
}
|
||||||
|
|
||||||
|
// zipEdited runs each time the ZIP box changes. Five digits turn the weather on
|
||||||
|
// for that ZIP code; an empty box turns it off.
|
||||||
|
func zipEdited() {
|
||||||
|
z := onlyDigits(zipText())
|
||||||
|
switch {
|
||||||
|
case len(z) == 5:
|
||||||
|
weatherSetZip(z)
|
||||||
|
case z == "":
|
||||||
|
weatherSetZip("")
|
||||||
|
default:
|
||||||
|
pSetWindowText.Call(ctlZipStatus, uintptr(unsafe.Pointer(u16("A ZIP code has 5 digits."))))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
settingsZipStatusUpdate()
|
||||||
}
|
}
|
||||||
|
|
||||||
func comboChoice(c uintptr) int {
|
func comboChoice(c uintptr) int {
|
||||||
@@ -234,6 +289,10 @@ func settingsCommand(id, note uintptr) {
|
|||||||
styleCacheClear()
|
styleCacheClear()
|
||||||
redraw(hwndMain)
|
redraw(hwndMain)
|
||||||
}
|
}
|
||||||
|
case idZip:
|
||||||
|
if note == enChange && !zipSyncing {
|
||||||
|
zipEdited()
|
||||||
|
}
|
||||||
case idVertical:
|
case idVertical:
|
||||||
vertical = checked(ctlVertical)
|
vertical = checked(ctlVertical)
|
||||||
saveOrient()
|
saveOrient()
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ const (
|
|||||||
trayVolume
|
trayVolume
|
||||||
trayWifi
|
trayWifi
|
||||||
trayChevron
|
trayChevron
|
||||||
|
trayWeather // added after the others so saved orders from older versions still mean the same thing
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -41,6 +42,9 @@ const (
|
|||||||
menuSoundID = 23
|
menuSoundID = 23
|
||||||
menuNetID = 24
|
menuNetID = 24
|
||||||
menuMuteID = 25
|
menuMuteID = 25
|
||||||
|
|
||||||
|
menuWeatherID = 26
|
||||||
|
menuWeatherZipID = 27
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -101,7 +105,7 @@ func trayItemsAt(x, clientH int32) []trayItem {
|
|||||||
continue // this PC has no battery
|
continue // this PC has no battery
|
||||||
}
|
}
|
||||||
w = px(36)
|
w = px(36)
|
||||||
case trayVolume, trayWifi:
|
case trayVolume, trayWifi, trayWeather:
|
||||||
w = px(36)
|
w = px(36)
|
||||||
case trayChevron:
|
case trayChevron:
|
||||||
w = px(28)
|
w = px(28)
|
||||||
@@ -290,6 +294,8 @@ func drawTray(hdc uintptr, w, h int32) {
|
|||||||
drawNetwork(hdc, it.r)
|
drawNetwork(hdc, it.r)
|
||||||
case trayChevron:
|
case trayChevron:
|
||||||
drawChevron(hdc, it.r)
|
drawChevron(hdc, it.r)
|
||||||
|
case trayWeather:
|
||||||
|
drawWeatherIcon(hdc, it.r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -568,6 +574,8 @@ func trayClick(kind int) {
|
|||||||
tapChord(vkLWin, 'N') // notification center and calendar
|
tapChord(vkLWin, 'N') // notification center and calendar
|
||||||
case trayBattery, trayVolume, trayWifi:
|
case trayBattery, trayVolume, trayWifi:
|
||||||
tapChord(vkLWin, 'A') // quick settings
|
tapChord(vkLWin, 'A') // quick settings
|
||||||
|
case trayWeather:
|
||||||
|
weatherToggle()
|
||||||
case trayChevron:
|
case trayChevron:
|
||||||
// If the hidden icons are already showing, a second click closes them. Sending
|
// If the hidden icons are already showing, a second click closes them. Sending
|
||||||
// Win+B and Enter again would instead press Enter on the highlighted icon.
|
// Win+B and Enter again would instead press Enter on the highlighted icon.
|
||||||
@@ -650,6 +658,10 @@ func addTrayMenu(add func(flags uintptr, id uintptr, text string), sep func(), k
|
|||||||
add(mfGrayed, 0, networkText())
|
add(mfGrayed, 0, networkText())
|
||||||
add(0, menuNetID, "Network && internet settings")
|
add(0, menuNetID, "Network && internet settings")
|
||||||
sep()
|
sep()
|
||||||
|
case trayWeather:
|
||||||
|
add(0, menuWeatherID, "Show weather")
|
||||||
|
add(0, menuWeatherZipID, "Weather ZIP code...")
|
||||||
|
sep()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -664,6 +676,12 @@ func handleTrayCommand(cmd uintptr) bool {
|
|||||||
shellOpen("ms-settings:sound", "")
|
shellOpen("ms-settings:sound", "")
|
||||||
case menuNetID:
|
case menuNetID:
|
||||||
shellOpen("ms-settings:network", "")
|
shellOpen("ms-settings:network", "")
|
||||||
|
case menuWeatherID:
|
||||||
|
if !wxShown {
|
||||||
|
weatherToggle()
|
||||||
|
}
|
||||||
|
case menuWeatherZipID:
|
||||||
|
openSettings()
|
||||||
case menuAutostartID:
|
case menuAutostartID:
|
||||||
setAutostart(!autostartEnabled())
|
setAutostart(!autostartEnabled())
|
||||||
case menuMuteID:
|
case menuMuteID:
|
||||||
@@ -679,7 +697,7 @@ func handleTrayCommand(cmd uintptr) bool {
|
|||||||
// ---------- the right row can be rearranged too ----------
|
// ---------- the right row can be rearranged too ----------
|
||||||
|
|
||||||
// trayOrder is the left to right order of the items to the right of center.
|
// trayOrder is the left to right order of the items to the right of center.
|
||||||
var trayOrder = []int{trayClock, trayBattery, trayVolume, trayWifi, trayChevron}
|
var trayOrder = []int{trayClock, trayWeather, trayBattery, trayVolume, trayWifi, trayChevron}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dragTray int // the tray item the mouse went down on, 0 for none
|
dragTray int // the tray item the mouse went down on, 0 for none
|
||||||
@@ -698,12 +716,23 @@ func loadTrayOrder() {
|
|||||||
seen := map[int]bool{}
|
seen := map[int]bool{}
|
||||||
for _, p := range strings.Split(strings.TrimSpace(string(b)), ",") {
|
for _, p := range strings.Split(strings.TrimSpace(string(b)), ",") {
|
||||||
n, err := strconv.Atoi(strings.TrimSpace(p))
|
n, err := strconv.Atoi(strings.TrimSpace(p))
|
||||||
if err != nil || n < trayClock || n > trayChevron || seen[n] {
|
if err != nil || n < trayClock || n > trayWeather || seen[n] {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
seen[n] = true
|
seen[n] = true
|
||||||
order = append(order, n)
|
order = append(order, n)
|
||||||
}
|
}
|
||||||
|
// An order saved before the weather cloud existed: put the cloud next to the clock.
|
||||||
|
if !seen[trayWeather] {
|
||||||
|
withCloud := make([]int, 0, len(order)+1)
|
||||||
|
for _, k := range order {
|
||||||
|
withCloud = append(withCloud, k)
|
||||||
|
if k == trayClock {
|
||||||
|
withCloud = append(withCloud, trayWeather)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
order = withCloud
|
||||||
|
}
|
||||||
if len(order) == len(trayOrder) {
|
if len(order) == len(trayOrder) {
|
||||||
trayOrder = order
|
trayOrder = order
|
||||||
}
|
}
|
||||||
|
|||||||
+558
@@ -0,0 +1,558 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// Weather data for the flyout. The location comes from the ZIP code typed in
|
||||||
|
// the settings window: Open-Meteo's geocoder turns it into a place (with
|
||||||
|
// api.zippopotam.us as a backup), then Open-Meteo's forecast service gives the
|
||||||
|
// current conditions and the next days, in Fahrenheit and miles per hour. The
|
||||||
|
// moon phase needs no service at all; it is worked out from the date.
|
||||||
|
//
|
||||||
|
// Everything that touches the network runs on its own goroutine. The result is
|
||||||
|
// kept under a lock and the bar is told with a posted message (wmWxDone), so
|
||||||
|
// the bar itself never waits on the internet.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const wmWxDone = wmUser + 2
|
||||||
|
|
||||||
|
const (
|
||||||
|
wxFresh = 10 * time.Minute // how long a forecast counts as current
|
||||||
|
)
|
||||||
|
|
||||||
|
// Icon kinds, chosen from the WMO weather code Open-Meteo reports.
|
||||||
|
const (
|
||||||
|
wxSun = iota
|
||||||
|
wxPartly
|
||||||
|
wxCloudy
|
||||||
|
wxFog
|
||||||
|
wxRain
|
||||||
|
wxSnow
|
||||||
|
wxStorm
|
||||||
|
)
|
||||||
|
|
||||||
|
type wxNow struct {
|
||||||
|
temp, feels float64
|
||||||
|
humidity int
|
||||||
|
wind float64
|
||||||
|
code int
|
||||||
|
day bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type wxDay struct {
|
||||||
|
date time.Time
|
||||||
|
code int
|
||||||
|
hi, lo float64
|
||||||
|
rain int
|
||||||
|
uv float64
|
||||||
|
sunrise string
|
||||||
|
sunset string
|
||||||
|
}
|
||||||
|
|
||||||
|
type wxState struct {
|
||||||
|
zip string
|
||||||
|
place string
|
||||||
|
lat float64
|
||||||
|
lon float64
|
||||||
|
now wxNow
|
||||||
|
days []wxDay // today first
|
||||||
|
ok bool // now and days hold a good forecast for zip
|
||||||
|
loading bool
|
||||||
|
err string
|
||||||
|
fetched time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
wxMu sync.Mutex
|
||||||
|
wx wxState
|
||||||
|
wxGen int // bumped whenever the ZIP changes, so an old answer can be thrown away
|
||||||
|
|
||||||
|
wxZip string // the saved ZIP code; used on the bar's own thread only
|
||||||
|
wxClient = &http.Client{Timeout: 12 * time.Second}
|
||||||
|
)
|
||||||
|
|
||||||
|
// ---------- the saved ZIP code ----------
|
||||||
|
|
||||||
|
func zipFile() string { return filepath.Join(dataDir(), "zip.txt") }
|
||||||
|
func locCacheFile() string { return filepath.Join(dataDir(), "weatherloc.txt") }
|
||||||
|
|
||||||
|
func loadZip() {
|
||||||
|
b, err := os.ReadFile(zipFile())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if z := onlyDigits(string(b)); len(z) == 5 {
|
||||||
|
wxZip = z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveZip() {
|
||||||
|
_ = os.MkdirAll(dataDir(), 0o755)
|
||||||
|
_ = os.WriteFile(zipFile(), []byte(wxZip), 0o644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func onlyDigits(s string) string {
|
||||||
|
var sb strings.Builder
|
||||||
|
for _, r := range s {
|
||||||
|
if r >= '0' && r <= '9' {
|
||||||
|
sb.WriteRune(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherSetZip is called by the settings window when the ZIP code changes.
|
||||||
|
// An empty string clears it.
|
||||||
|
func weatherSetZip(z string) {
|
||||||
|
if z == wxZip {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wxZip = z
|
||||||
|
saveZip()
|
||||||
|
if z == "" {
|
||||||
|
_ = os.Remove(locCacheFile())
|
||||||
|
}
|
||||||
|
weatherRefresh(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// weatherRefresh starts a fetch if the forecast is missing or old (or force is
|
||||||
|
// set). Safe to call as often as you like.
|
||||||
|
func weatherRefresh(force bool) {
|
||||||
|
zip := wxZip
|
||||||
|
wxMu.Lock()
|
||||||
|
if len(zip) != 5 {
|
||||||
|
wx = wxState{}
|
||||||
|
wxGen++
|
||||||
|
wxMu.Unlock()
|
||||||
|
weatherNotify()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
same := wx.zip == zip
|
||||||
|
if same && wx.loading && !force {
|
||||||
|
wxMu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if same && wx.ok && time.Since(wx.fetched) < wxFresh && !force {
|
||||||
|
wxMu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !same {
|
||||||
|
wx = wxState{zip: zip}
|
||||||
|
}
|
||||||
|
wx.loading = true
|
||||||
|
wx.err = ""
|
||||||
|
wxGen++
|
||||||
|
gen := wxGen
|
||||||
|
wxMu.Unlock()
|
||||||
|
weatherNotify()
|
||||||
|
go wxFetch(zip, gen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func weatherNotify() {
|
||||||
|
if hwndMain != 0 {
|
||||||
|
pPostMessage.Call(hwndMain, wmWxDone, 0, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxSnapshot() wxState {
|
||||||
|
wxMu.Lock()
|
||||||
|
defer wxMu.Unlock()
|
||||||
|
s := wx
|
||||||
|
s.days = append([]wxDay(nil), wx.days...)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- fetching ----------
|
||||||
|
|
||||||
|
func wxFetch(zip string, gen int) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
wxFinish(gen, nil, "The weather lookup failed.")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
lat, lon, place, err := wxLocate(zip)
|
||||||
|
if err != nil {
|
||||||
|
wxFinish(gen, nil, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st := wxState{zip: zip, place: place, lat: lat, lon: lon}
|
||||||
|
if err := wxForecast(&st); err != nil {
|
||||||
|
wxFinish(gen, nil, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st.ok = true
|
||||||
|
st.fetched = time.Now()
|
||||||
|
wxFinish(gen, &st, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// wxFinish stores a result, unless the ZIP changed while it was being fetched.
|
||||||
|
func wxFinish(gen int, st *wxState, errText string) {
|
||||||
|
wxMu.Lock()
|
||||||
|
if gen != wxGen {
|
||||||
|
wxMu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if st != nil {
|
||||||
|
wx = *st
|
||||||
|
} else {
|
||||||
|
wx.loading = false
|
||||||
|
wx.err = errText
|
||||||
|
}
|
||||||
|
wxMu.Unlock()
|
||||||
|
weatherNotify()
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxGetJSON(u string, v any) (status int, err error) {
|
||||||
|
req, err := http.NewRequest("GET", u, nil)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", "MirrorBar weather flyout")
|
||||||
|
resp, err := wxClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return resp.StatusCode, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return resp.StatusCode, json.NewDecoder(io.LimitReader(resp.Body, 2<<20)).Decode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wxLocate turns a ZIP code into a place. The answer is kept in a small file so
|
||||||
|
// the lookup happens once per ZIP code, not every time the bar starts.
|
||||||
|
func wxLocate(zip string) (lat, lon float64, place string, err error) {
|
||||||
|
if b, e := os.ReadFile(locCacheFile()); e == nil {
|
||||||
|
p := strings.Split(strings.TrimSpace(string(b)), "\t")
|
||||||
|
if len(p) == 4 && p[0] == zip {
|
||||||
|
la, e1 := strconv.ParseFloat(p[1], 64)
|
||||||
|
lo, e2 := strconv.ParseFloat(p[2], 64)
|
||||||
|
if e1 == nil && e2 == nil {
|
||||||
|
return la, lo, p[3], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reached := false
|
||||||
|
|
||||||
|
// Open-Meteo's own geocoder lists postal codes with each place. Other countries
|
||||||
|
// can share a number (46750 is also a town in Spain), so only a US match counts.
|
||||||
|
var gr struct {
|
||||||
|
Results []struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Latitude float64 `json:"latitude"`
|
||||||
|
Longitude float64 `json:"longitude"`
|
||||||
|
CountryCode string `json:"country_code"`
|
||||||
|
Admin1 string `json:"admin1"`
|
||||||
|
Postcodes []string `json:"postcodes"`
|
||||||
|
} `json:"results"`
|
||||||
|
}
|
||||||
|
if st, e := wxGetJSON("https://geocoding-api.open-meteo.com/v1/search?name="+zip+"&count=10&language=en&format=json", &gr); e == nil || st != 0 {
|
||||||
|
reached = true
|
||||||
|
}
|
||||||
|
for _, r := range gr.Results {
|
||||||
|
if r.CountryCode != "US" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, pc := range r.Postcodes {
|
||||||
|
if pc == zip {
|
||||||
|
place = r.Name
|
||||||
|
if r.Admin1 != "" {
|
||||||
|
place += ", " + r.Admin1
|
||||||
|
}
|
||||||
|
wxSaveLoc(zip, r.Latitude, r.Longitude, place)
|
||||||
|
return r.Latitude, r.Longitude, place, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Backup: a ZIP code service, for codes the geocoder doesn't list.
|
||||||
|
var zr struct {
|
||||||
|
Places []struct {
|
||||||
|
Name string `json:"place name"`
|
||||||
|
Latitude string `json:"latitude"`
|
||||||
|
Longitude string `json:"longitude"`
|
||||||
|
State string `json:"state"`
|
||||||
|
} `json:"places"`
|
||||||
|
}
|
||||||
|
st, e := wxGetJSON("https://api.zippopotam.us/us/"+zip, &zr)
|
||||||
|
if e == nil || st != 0 {
|
||||||
|
reached = true
|
||||||
|
}
|
||||||
|
if e == nil && len(zr.Places) > 0 {
|
||||||
|
p := zr.Places[0]
|
||||||
|
la, e1 := strconv.ParseFloat(p.Latitude, 64)
|
||||||
|
lo, e2 := strconv.ParseFloat(p.Longitude, 64)
|
||||||
|
if e1 == nil && e2 == nil {
|
||||||
|
place = p.Name
|
||||||
|
if p.State != "" {
|
||||||
|
place += ", " + p.State
|
||||||
|
}
|
||||||
|
wxSaveLoc(zip, la, lo, place)
|
||||||
|
return la, lo, place, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reached {
|
||||||
|
return 0, 0, "", fmt.Errorf("Can't reach the weather service. Check your internet connection.")
|
||||||
|
}
|
||||||
|
return 0, 0, "", fmt.Errorf("ZIP code %s was not found.", zip)
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxSaveLoc(zip string, lat, lon float64, place string) {
|
||||||
|
_ = os.MkdirAll(dataDir(), 0o755)
|
||||||
|
s := zip + "\t" + strconv.FormatFloat(lat, 'f', 5, 64) + "\t" + strconv.FormatFloat(lon, 'f', 5, 64) + "\t" + place
|
||||||
|
_ = os.WriteFile(locCacheFile(), []byte(s), 0o644)
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxForecast(st *wxState) error {
|
||||||
|
u := fmt.Sprintf("https://api.open-meteo.com/v1/forecast?latitude=%.4f&longitude=%.4f"+
|
||||||
|
"¤t=temperature_2m,apparent_temperature,relative_humidity_2m,weather_code,wind_speed_10m,is_day"+
|
||||||
|
"&daily=weather_code,temperature_2m_max,temperature_2m_min,precipitation_probability_max,uv_index_max,sunrise,sunset"+
|
||||||
|
"&temperature_unit=fahrenheit&wind_speed_unit=mph&timezone=auto&forecast_days=6", st.lat, st.lon)
|
||||||
|
|
||||||
|
var fr struct {
|
||||||
|
Current struct {
|
||||||
|
Temp float64 `json:"temperature_2m"`
|
||||||
|
Feels float64 `json:"apparent_temperature"`
|
||||||
|
Humidity float64 `json:"relative_humidity_2m"`
|
||||||
|
Code int `json:"weather_code"`
|
||||||
|
Wind float64 `json:"wind_speed_10m"`
|
||||||
|
IsDay int `json:"is_day"`
|
||||||
|
} `json:"current"`
|
||||||
|
Daily struct {
|
||||||
|
Time []string `json:"time"`
|
||||||
|
Code []int `json:"weather_code"`
|
||||||
|
Max []float64 `json:"temperature_2m_max"`
|
||||||
|
Min []float64 `json:"temperature_2m_min"`
|
||||||
|
Rain []float64 `json:"precipitation_probability_max"`
|
||||||
|
UV []float64 `json:"uv_index_max"`
|
||||||
|
Sunrise []string `json:"sunrise"`
|
||||||
|
Sunset []string `json:"sunset"`
|
||||||
|
} `json:"daily"`
|
||||||
|
}
|
||||||
|
status, err := wxGetJSON(u, &fr)
|
||||||
|
if err != nil {
|
||||||
|
if status == 0 {
|
||||||
|
return fmt.Errorf("Can't reach the weather service. Check your internet connection.")
|
||||||
|
}
|
||||||
|
return fmt.Errorf("The weather service returned an error (%v).", err)
|
||||||
|
}
|
||||||
|
d := fr.Daily
|
||||||
|
n := len(d.Time)
|
||||||
|
if n < 2 || len(d.Code) < n || len(d.Max) < n || len(d.Min) < n {
|
||||||
|
return fmt.Errorf("The weather service sent a forecast MirrorBar couldn't read.")
|
||||||
|
}
|
||||||
|
|
||||||
|
st.now = wxNow{
|
||||||
|
temp: fr.Current.Temp,
|
||||||
|
feels: fr.Current.Feels,
|
||||||
|
humidity: int(math.Round(fr.Current.Humidity)),
|
||||||
|
wind: fr.Current.Wind,
|
||||||
|
code: fr.Current.Code,
|
||||||
|
day: fr.Current.IsDay != 0,
|
||||||
|
}
|
||||||
|
st.days = st.days[:0]
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
dt, err := time.Parse("2006-01-02", d.Time[i])
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("The weather service sent a forecast MirrorBar couldn't read.")
|
||||||
|
}
|
||||||
|
day := wxDay{date: dt, code: d.Code[i], hi: d.Max[i], lo: d.Min[i]}
|
||||||
|
if i < len(d.Rain) {
|
||||||
|
day.rain = int(math.Round(d.Rain[i]))
|
||||||
|
}
|
||||||
|
if i < len(d.UV) {
|
||||||
|
day.uv = d.UV[i]
|
||||||
|
}
|
||||||
|
if i < len(d.Sunrise) {
|
||||||
|
day.sunrise = wxClockText(d.Sunrise[i])
|
||||||
|
}
|
||||||
|
if i < len(d.Sunset) {
|
||||||
|
day.sunset = wxClockText(d.Sunset[i])
|
||||||
|
}
|
||||||
|
st.days = append(st.days, day)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// wxClockText turns "2026-11-07T07:19" into "7:19 AM".
|
||||||
|
func wxClockText(s string) string {
|
||||||
|
t, err := time.Parse("2006-01-02T15:04", s)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return t.Format("3:04 PM")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- what the codes mean ----------
|
||||||
|
|
||||||
|
// wxKind picks the picture for a WMO weather code.
|
||||||
|
func wxKind(code int) int {
|
||||||
|
switch {
|
||||||
|
case code == 0, code == 1:
|
||||||
|
return wxSun
|
||||||
|
case code == 2:
|
||||||
|
return wxPartly
|
||||||
|
case code == 3:
|
||||||
|
return wxCloudy
|
||||||
|
case code == 45, code == 48:
|
||||||
|
return wxFog
|
||||||
|
case code >= 51 && code <= 67, code >= 80 && code <= 82:
|
||||||
|
return wxRain
|
||||||
|
case code >= 71 && code <= 77, code == 85, code == 86:
|
||||||
|
return wxSnow
|
||||||
|
case code >= 95:
|
||||||
|
return wxStorm
|
||||||
|
}
|
||||||
|
return wxCloudy
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxDesc(code int) string {
|
||||||
|
switch code {
|
||||||
|
case 0:
|
||||||
|
return "Clear sky"
|
||||||
|
case 1:
|
||||||
|
return "Mostly clear"
|
||||||
|
case 2:
|
||||||
|
return "Partly cloudy"
|
||||||
|
case 3:
|
||||||
|
return "Overcast"
|
||||||
|
case 45, 48:
|
||||||
|
return "Fog"
|
||||||
|
case 51, 53, 55:
|
||||||
|
return "Drizzle"
|
||||||
|
case 56, 57:
|
||||||
|
return "Freezing drizzle"
|
||||||
|
case 61:
|
||||||
|
return "Light rain"
|
||||||
|
case 63:
|
||||||
|
return "Rain"
|
||||||
|
case 65:
|
||||||
|
return "Heavy rain"
|
||||||
|
case 66, 67:
|
||||||
|
return "Freezing rain"
|
||||||
|
case 71:
|
||||||
|
return "Light snow"
|
||||||
|
case 73:
|
||||||
|
return "Snow"
|
||||||
|
case 75:
|
||||||
|
return "Heavy snow"
|
||||||
|
case 77:
|
||||||
|
return "Snow grains"
|
||||||
|
case 80:
|
||||||
|
return "Light showers"
|
||||||
|
case 81:
|
||||||
|
return "Showers"
|
||||||
|
case 82:
|
||||||
|
return "Heavy showers"
|
||||||
|
case 85, 86:
|
||||||
|
return "Snow showers"
|
||||||
|
case 95:
|
||||||
|
return "Thunderstorm"
|
||||||
|
case 96, 99:
|
||||||
|
return "Thunderstorm with hail"
|
||||||
|
}
|
||||||
|
return "Cloudy"
|
||||||
|
}
|
||||||
|
|
||||||
|
func wxDeg(v float64) string { return strconv.Itoa(int(math.Round(v))) + "°" }
|
||||||
|
func wxF(v float64) string { return strconv.Itoa(int(math.Round(v))) + "°F" }
|
||||||
|
|
||||||
|
// ---------- the moon ----------
|
||||||
|
|
||||||
|
const synodicMonth = 29.530588853 // days from one new moon to the next
|
||||||
|
|
||||||
|
type moonInfo struct {
|
||||||
|
frac float64 // 0 is new, 0.5 is full, back to 1 at the next new moon
|
||||||
|
illum float64 // 0 to 1, how much of the face is lit
|
||||||
|
name string
|
||||||
|
fullIn int // whole days until the next full moon
|
||||||
|
tonight bool // it is full right now (within about half a day)
|
||||||
|
}
|
||||||
|
|
||||||
|
// moonElongation is how far the moon is ahead of the sun in the sky, in degrees
|
||||||
|
// (0 at new moon, 180 at full). It is the moon's mean position plus the main
|
||||||
|
// corrections for its uneven orbit (Meeus, Astronomical Algorithms, chapter 48),
|
||||||
|
// which keeps the phase within a fraction of a percent. Counting the average
|
||||||
|
// month from a known new moon would be up to 15 hours off.
|
||||||
|
func moonElongation(t time.Time) float64 {
|
||||||
|
jd := float64(t.Unix())/86400 + 2440587.5
|
||||||
|
T := (jd - 2451545.0) / 36525
|
||||||
|
T2, T3, T4 := T*T, T*T*T, T*T*T*T
|
||||||
|
D := 297.8501921 + 445267.1114034*T - 0.0018819*T2 + T3/545868 - T4/113065000
|
||||||
|
M := 357.5291092 + 35999.0502909*T - 0.0001536*T2 + T3/24490000
|
||||||
|
Mp := 134.9633964 + 477198.8675055*T + 0.0087414*T2 + T3/69699 - T4/14712000
|
||||||
|
r := math.Pi / 180
|
||||||
|
e := D + 6.289*math.Sin(Mp*r) - 2.100*math.Sin(M*r) + 1.274*math.Sin((2*D-Mp)*r) +
|
||||||
|
0.658*math.Sin(2*D*r) + 0.214*math.Sin(2*Mp*r) + 0.110*math.Sin(D*r)
|
||||||
|
e = math.Mod(e, 360)
|
||||||
|
if e < 0 {
|
||||||
|
e += 360
|
||||||
|
}
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
func moonAt(t time.Time) moonInfo {
|
||||||
|
e := moonElongation(t)
|
||||||
|
m := moonInfo{frac: e / 360}
|
||||||
|
m.illum = (1 - math.Cos(e*math.Pi/180)) / 2
|
||||||
|
|
||||||
|
// Eight named phases, each an eighth of the cycle centered on its moment.
|
||||||
|
switch f := m.frac * 16; {
|
||||||
|
case f < 1:
|
||||||
|
m.name = "New Moon"
|
||||||
|
case f < 3:
|
||||||
|
m.name = "Waxing Crescent"
|
||||||
|
case f < 5:
|
||||||
|
m.name = "First Quarter"
|
||||||
|
case f < 7:
|
||||||
|
m.name = "Waxing Gibbous"
|
||||||
|
case f < 9:
|
||||||
|
m.name = "Full Moon"
|
||||||
|
case f < 11:
|
||||||
|
m.name = "Waning Gibbous"
|
||||||
|
case f < 13:
|
||||||
|
m.name = "Last Quarter"
|
||||||
|
case f < 15:
|
||||||
|
m.name = "Waning Crescent"
|
||||||
|
default:
|
||||||
|
m.name = "New Moon"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hours from now to the nearest full moon (negative just after one).
|
||||||
|
m.tonight = math.Abs(e-180)/360*synodicMonth*24 < 14
|
||||||
|
|
||||||
|
// Walk forward an hour at a time to the moment the moon next passes 180 degrees.
|
||||||
|
prev := e
|
||||||
|
for h := 1; h <= 31*24; h++ {
|
||||||
|
cur := moonElongation(t.Add(time.Duration(h) * time.Hour))
|
||||||
|
if prev < 180 && cur >= 180 {
|
||||||
|
m.fullIn = int(math.Round(float64(h) / 24))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
prev = cur
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m moonInfo) fullText() string {
|
||||||
|
switch {
|
||||||
|
case m.tonight:
|
||||||
|
return "Full moon tonight"
|
||||||
|
case m.fullIn <= 1:
|
||||||
|
return "Full moon in 1 day"
|
||||||
|
}
|
||||||
|
return "Full moon in " + strconv.Itoa(m.fullIn) + " days"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user