Files
MirrorBar/flyout.go
T

603 lines
18 KiB
Go

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
wxRadarH = 158.0 // extra height when the radar picture fits on the screen
wxShortH = 150.0 // panel height for the "add a ZIP code" and error messages
wxPollTimerID = 1
wxAnimTimerID = 2
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, fTiny uintptr
rdHold int // how many beats the newest radar frame has been held for
)
// ---------- 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:
if wparam == wxAnimTimerID {
radarAdvance()
return 0
}
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 || !showWeather {
return
}
previewHide()
newsHide() // one panel at a time
sysHide()
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)
pSetTimer.Call(wxHwnd, wxAnimTimerID, 450, 0)
radarEnsure(false)
}
func weatherHide() {
if !wxShown {
return
}
wxShown = false
pKillTimer.Call(wxHwnd, wxPollTimerID)
pKillTimer.Call(wxHwnd, wxAnimTimerID)
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 { return trayAnchor(trayWeather) }
// 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
if wxRadarFits() {
lh += wxRadarH
}
}
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() {
newsDone()
if wxShown {
weatherPlace()
radarEnsure(false)
}
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)
fTiny = makeFont(px(9), 400)
wxFontsMade = true
}
func wxPaint(hwnd uintptr) { flyPaint(hwnd, wxDraw) }
// flyPaint paints a panel at double size into a memory bitmap with the given
// drawing function, then shrinks it onto the window.
func flyPaint(hwnd uintptr, draw func(hdc uintptr, W, H int32)) {
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()
draw(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)
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}, iconColor()) // the outline follows the tray icon color
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))
// Radar, when the panel is tall enough to have room for it. Everything below it
// is drawn the same way, just moved down.
if H >= u(wxFullH+wxRadarH-1) {
radarDraw(hdc, rect{pad, u(226), W - pad, u(226) + u(150)})
rule(u(382))
pSetViewportOrgEx.Call(hdc, 0, uintptr(u(wxRadarH)), 0)
defer pSetViewportOrgEx.Call(hdc, 0, 0, 0)
}
// 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)
}
}