MirrorBar+ part 2: tray, weather, radar, local news data
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
package main
|
||||
|
||||
// Local news state and downloading. The place comes from the same ZIP code as
|
||||
// the weather. Like the weather, the download runs on its own goroutine and the
|
||||
// bar is told with a posted message when it finishes.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const newsFresh = 10 * time.Minute
|
||||
|
||||
type newsState struct {
|
||||
zip string
|
||||
place string
|
||||
items []newsItem
|
||||
ok bool
|
||||
loading bool
|
||||
err string
|
||||
fetched time.Time
|
||||
}
|
||||
|
||||
var (
|
||||
newsMu sync.Mutex
|
||||
news newsState
|
||||
newsGen int
|
||||
)
|
||||
|
||||
func newsSnapshot() newsState {
|
||||
newsMu.Lock()
|
||||
defer newsMu.Unlock()
|
||||
s := news
|
||||
s.items = append([]newsItem(nil), news.items...)
|
||||
return s
|
||||
}
|
||||
|
||||
// newsRefresh starts a download if there are no headlines for this ZIP code yet
|
||||
// or they are old (or force is set). Safe to call as often as you like.
|
||||
func newsRefresh(force bool) {
|
||||
if !showNews {
|
||||
return
|
||||
}
|
||||
zip := wxZip
|
||||
newsMu.Lock()
|
||||
if len(zip) != 5 {
|
||||
news = newsState{}
|
||||
newsGen++
|
||||
newsMu.Unlock()
|
||||
weatherNotify()
|
||||
return
|
||||
}
|
||||
same := news.zip == zip
|
||||
switch {
|
||||
case same && news.loading && !force:
|
||||
newsMu.Unlock()
|
||||
return
|
||||
case same && news.ok && time.Since(news.fetched) < newsFresh && !force:
|
||||
newsMu.Unlock()
|
||||
return
|
||||
case same && !news.ok && news.err != "" && time.Since(news.fetched) < 30*time.Second && !force:
|
||||
newsMu.Unlock() // it just failed; don't hammer the service
|
||||
return
|
||||
}
|
||||
if !same {
|
||||
news = newsState{zip: zip}
|
||||
}
|
||||
news.loading = true
|
||||
news.err = ""
|
||||
newsGen++
|
||||
gen := newsGen
|
||||
newsMu.Unlock()
|
||||
weatherNotify()
|
||||
go newsFetch(zip, gen)
|
||||
}
|
||||
|
||||
func newsFetch(zip string, gen int) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
newsFinish(gen, zip, "", nil, "The news lookup failed.")
|
||||
}
|
||||
}()
|
||||
_, _, place, err := wxLocate(zip)
|
||||
if err != nil {
|
||||
newsFinish(gen, zip, "", nil, err.Error())
|
||||
return
|
||||
}
|
||||
req, err := http.NewRequest("GET", newsFeedURL(place), nil)
|
||||
if err != nil {
|
||||
newsFinish(gen, zip, place, nil, "The news lookup failed.")
|
||||
return
|
||||
}
|
||||
req.Header.Set("User-Agent", "MirrorBar news flyout")
|
||||
resp, err := wxClient.Do(req)
|
||||
if err != nil {
|
||||
newsFinish(gen, zip, place, nil, "Can't reach the news service. Check your internet connection.")
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
newsFinish(gen, zip, place, nil, fmt.Sprintf("The news service said HTTP %d.", resp.StatusCode))
|
||||
return
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
if err != nil {
|
||||
newsFinish(gen, zip, place, nil, "The news download was cut off.")
|
||||
return
|
||||
}
|
||||
items, err := newsParse(data)
|
||||
if err != nil {
|
||||
newsFinish(gen, zip, place, nil, err.Error())
|
||||
return
|
||||
}
|
||||
if len(items) == 0 {
|
||||
newsFinish(gen, zip, place, nil, "No local headlines found for "+place+".")
|
||||
return
|
||||
}
|
||||
newsFinish(gen, zip, place, items, "")
|
||||
}
|
||||
|
||||
// newsFinish stores a result, unless the ZIP code changed while it was loading.
|
||||
func newsFinish(gen int, zip, place string, items []newsItem, errText string) {
|
||||
newsMu.Lock()
|
||||
if gen != newsGen {
|
||||
newsMu.Unlock()
|
||||
return
|
||||
}
|
||||
news.loading = false
|
||||
news.fetched = time.Now()
|
||||
if place != "" {
|
||||
news.place = place
|
||||
}
|
||||
if items != nil {
|
||||
news.items, news.ok, news.err = items, true, ""
|
||||
} else {
|
||||
news.err = errText // keep older headlines on screen if there are any
|
||||
}
|
||||
newsMu.Unlock()
|
||||
weatherNotify()
|
||||
}
|
||||
|
||||
// newsStatusText is the line the settings window shows when only news is on.
|
||||
func newsStatusText() string {
|
||||
s := newsSnapshot()
|
||||
switch {
|
||||
case s.ok && s.place != "":
|
||||
return "Showing local news for " + s.place
|
||||
case s.loading:
|
||||
return "Looking up " + wxZip + "..."
|
||||
case s.err != "":
|
||||
return s.err
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
package main
|
||||
|
||||
// Radar for the weather flyout: a small map centered on the ZIP code with the
|
||||
// last hour or so of rain radar over it, played as a loop.
|
||||
//
|
||||
// Radar pictures come from RainViewer (rainviewer.com, free, no account): a list
|
||||
// of the latest frames, then one picture tile per frame. The map under them is
|
||||
// Esri's dark gray map (gray base plus a layer of place names). Both are lists
|
||||
// of square tiles on the same world grid, so a window around the ZIP code is
|
||||
// cut out of the tiles and each radar frame is laid over the map.
|
||||
//
|
||||
// Nothing here runs until the panel is opened, and it all happens on a
|
||||
// background goroutine; the finished frames are plain pixel arrays that the
|
||||
// panel draws straight to the screen.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
radarW = 520 // pixels in a finished frame; 520 by 250 matches the panel's 312 by 150 picture
|
||||
radarH = 250
|
||||
radarFrames = 8 // the newest eight frames, 10 minutes apart
|
||||
radarFresh = 5 * time.Minute
|
||||
|
||||
// One world pixel grid serves everything: 65536 pixels around the globe. That is
|
||||
// RainViewer's 512 pixel tiles at zoom 7 and Esri's 256 pixel tiles at zoom 8.
|
||||
radarWorld = 65536.0
|
||||
radarTileRV = 512
|
||||
radarZoomRV = 7
|
||||
radarTileMap = 256
|
||||
radarZoomMap = 8
|
||||
)
|
||||
|
||||
type radarState struct {
|
||||
key string // the place these frames are for
|
||||
frames [][]byte
|
||||
times []time.Time
|
||||
loading bool
|
||||
err string
|
||||
fetched time.Time
|
||||
}
|
||||
|
||||
var (
|
||||
rdMu sync.Mutex
|
||||
rd radarState
|
||||
rdGen int
|
||||
rdIdx int // which frame is on screen; touched only by the panel's own thread
|
||||
)
|
||||
|
||||
func radarSnapshot() radarState {
|
||||
rdMu.Lock()
|
||||
defer rdMu.Unlock()
|
||||
return rd
|
||||
}
|
||||
|
||||
// radarEnsure starts a radar download if there is none for this place yet, or
|
||||
// the one we have is old. Called when the panel opens and when a forecast lands.
|
||||
func radarEnsure(force bool) {
|
||||
s := wxSnapshot()
|
||||
if !s.ok {
|
||||
return
|
||||
}
|
||||
key := fmt.Sprintf("%.3f,%.3f", s.lat, s.lon)
|
||||
rdMu.Lock()
|
||||
same := rd.key == key
|
||||
switch {
|
||||
case same && rd.loading && !force:
|
||||
rdMu.Unlock()
|
||||
return
|
||||
case same && len(rd.frames) > 0 && time.Since(rd.fetched) < radarFresh && !force:
|
||||
rdMu.Unlock()
|
||||
return
|
||||
case same && rd.err != "" && len(rd.frames) == 0 && time.Since(rd.fetched) < time.Minute && !force:
|
||||
rdMu.Unlock() // it just failed; don't hammer the service
|
||||
return
|
||||
}
|
||||
if !same {
|
||||
rd = radarState{key: key}
|
||||
}
|
||||
rd.loading = true
|
||||
rd.err = ""
|
||||
rdGen++
|
||||
gen := rdGen
|
||||
rdMu.Unlock()
|
||||
go radarFetch(key, s.lat, s.lon, gen)
|
||||
}
|
||||
|
||||
func radarFinish(gen int, key string, frames [][]byte, times []time.Time, errText string) {
|
||||
rdMu.Lock()
|
||||
if gen != rdGen || rd.key != key {
|
||||
rdMu.Unlock()
|
||||
return
|
||||
}
|
||||
rd.loading = false
|
||||
rd.fetched = time.Now()
|
||||
if frames != nil {
|
||||
rd.frames, rd.times, rd.err = frames, times, ""
|
||||
} else {
|
||||
rd.err = errText
|
||||
}
|
||||
rdMu.Unlock()
|
||||
weatherNotify()
|
||||
}
|
||||
|
||||
// worldPixel is where a place falls on the 65536 pixel world grid (Web Mercator).
|
||||
func worldPixel(lat, lon float64) (x, y float64) {
|
||||
x = (lon + 180) / 360 * radarWorld
|
||||
r := lat * math.Pi / 180
|
||||
y = (1 - math.Log(math.Tan(r)+1/math.Cos(r))/math.Pi) / 2 * radarWorld
|
||||
return
|
||||
}
|
||||
|
||||
type tileJob struct {
|
||||
url string
|
||||
img image.Image
|
||||
err error
|
||||
}
|
||||
|
||||
// fetchTiles downloads a set of tile pictures, a few at a time.
|
||||
func fetchTiles(jobs []*tileJob) {
|
||||
var wg sync.WaitGroup
|
||||
sem := make(chan struct{}, 6)
|
||||
for _, j := range jobs {
|
||||
wg.Add(1)
|
||||
sem <- struct{}{}
|
||||
go func(j *tileJob) {
|
||||
defer wg.Done()
|
||||
defer func() { <-sem }()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
j.err = fmt.Errorf("tile failed")
|
||||
}
|
||||
}()
|
||||
b, err := wxGetBytes(j.url)
|
||||
if err != nil {
|
||||
j.err = err
|
||||
return
|
||||
}
|
||||
j.img, _, j.err = image.Decode(bytes.NewReader(b))
|
||||
}(j)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func radarFetch(key string, lat, lon float64, gen int) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
radarFinish(gen, key, nil, nil, "The radar lookup failed.")
|
||||
}
|
||||
}()
|
||||
|
||||
// 1. The list of radar frames.
|
||||
var idx struct {
|
||||
Host string `json:"host"`
|
||||
Radar struct {
|
||||
Past []struct {
|
||||
Time int64 `json:"time"`
|
||||
Path string `json:"path"`
|
||||
} `json:"past"`
|
||||
} `json:"radar"`
|
||||
}
|
||||
if _, err := wxGetJSON("https://api.rainviewer.com/public/weather-maps.json", &idx); err != nil || idx.Host == "" || len(idx.Radar.Past) == 0 {
|
||||
radarFinish(gen, key, nil, nil, "Radar isn't available right now.")
|
||||
return
|
||||
}
|
||||
past := idx.Radar.Past
|
||||
if len(past) > radarFrames {
|
||||
past = past[len(past)-radarFrames:]
|
||||
}
|
||||
|
||||
// 2. Which tiles cover a window centered on the place.
|
||||
cx, cy := worldPixel(lat, lon)
|
||||
x0 := int(math.Floor(cx - radarW/2))
|
||||
y0 := int(math.Floor(cy - radarH/2))
|
||||
span := func(a, b int, size int) (lo, hi int) {
|
||||
lo = int(math.Floor(float64(a) / float64(size)))
|
||||
hi = int(math.Floor(float64(b) / float64(size)))
|
||||
return
|
||||
}
|
||||
|
||||
// 3. The map: gray base plus place names, from tiles 256 pixels wide at zoom 8.
|
||||
base := image.NewRGBA(image.Rect(0, 0, radarW, radarH))
|
||||
draw.Draw(base, base.Bounds(), image.NewUniform(color.RGBA{34, 34, 38, 255}), image.Point{}, draw.Src)
|
||||
mx0, mx1 := span(x0, x0+radarW-1, radarTileMap)
|
||||
my0, my1 := span(y0, y0+radarH-1, radarTileMap)
|
||||
type placed struct {
|
||||
job *tileJob
|
||||
ox, oy int
|
||||
}
|
||||
var mapTiles []placed
|
||||
var jobs []*tileJob
|
||||
for _, layer := range []string{"World_Dark_Gray_Base", "World_Dark_Gray_Reference"} {
|
||||
for ty := my0; ty <= my1; ty++ {
|
||||
for tx := mx0; tx <= mx1; tx++ {
|
||||
j := &tileJob{url: fmt.Sprintf("https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/%s/MapServer/tile/%d/%d/%d",
|
||||
layer, radarZoomMap, ty, tx)}
|
||||
mapTiles = append(mapTiles, placed{j, tx*radarTileMap - x0, ty*radarTileMap - y0})
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Every radar frame's tiles, 512 pixels wide at zoom 7.
|
||||
rx0, rx1 := span(x0, x0+radarW-1, radarTileRV)
|
||||
ry0, ry1 := span(y0, y0+radarH-1, radarTileRV)
|
||||
type frameTiles struct {
|
||||
t time.Time
|
||||
tiles []placed
|
||||
}
|
||||
frames := make([]frameTiles, len(past))
|
||||
for i, p := range past {
|
||||
frames[i].t = time.Unix(p.Time, 0)
|
||||
for ty := ry0; ty <= ry1; ty++ {
|
||||
for tx := rx0; tx <= rx1; tx++ {
|
||||
j := &tileJob{url: fmt.Sprintf("%s%s/%d/%d/%d/%d/2/1_1.png", idx.Host, p.Path, radarTileRV, radarZoomRV, tx, ty)}
|
||||
frames[i].tiles = append(frames[i].tiles, placed{j, tx*radarTileRV - x0, ty*radarTileRV - y0})
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
}
|
||||
}
|
||||
fetchTiles(jobs)
|
||||
|
||||
// The map must have come through; a missing radar frame is just skipped.
|
||||
for _, m := range mapTiles {
|
||||
if m.job.err != nil || m.job.img == nil {
|
||||
radarFinish(gen, key, nil, nil, "The map for radar didn't load.")
|
||||
return
|
||||
}
|
||||
b := m.job.img.Bounds()
|
||||
draw.Draw(base, image.Rect(m.ox, m.oy, m.ox+b.Dx(), m.oy+b.Dy()), m.job.img, b.Min, draw.Over)
|
||||
}
|
||||
|
||||
alpha := &image.Uniform{color.Alpha{A: 215}}
|
||||
var out [][]byte
|
||||
var times []time.Time
|
||||
for _, f := range frames {
|
||||
layer := image.NewRGBA(base.Bounds())
|
||||
good := true
|
||||
for _, t := range f.tiles {
|
||||
if t.job.err != nil || t.job.img == nil {
|
||||
good = false
|
||||
break
|
||||
}
|
||||
b := t.job.img.Bounds()
|
||||
draw.Draw(layer, image.Rect(t.ox, t.oy, t.ox+b.Dx(), t.oy+b.Dy()), t.job.img, b.Min, draw.Over)
|
||||
}
|
||||
if !good {
|
||||
continue
|
||||
}
|
||||
comp := image.NewRGBA(base.Bounds())
|
||||
copy(comp.Pix, base.Pix)
|
||||
draw.DrawMask(comp, comp.Bounds(), layer, image.Point{}, alpha, image.Point{}, draw.Over)
|
||||
|
||||
// Windows wants blue, green, red order.
|
||||
bgra := make([]byte, len(comp.Pix))
|
||||
for i := 0; i+3 < len(comp.Pix); i += 4 {
|
||||
bgra[i], bgra[i+1], bgra[i+2], bgra[i+3] = comp.Pix[i+2], comp.Pix[i+1], comp.Pix[i], 255
|
||||
}
|
||||
out = append(out, bgra)
|
||||
times = append(times, f.t)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
radarFinish(gen, key, nil, nil, "Radar isn't available right now.")
|
||||
return
|
||||
}
|
||||
radarFinish(gen, key, out, times, "")
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
// Drawing the radar picture inside the weather panel, and stepping the loop.
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
pStretchDIBits = gdi32.NewProc("StretchDIBits")
|
||||
pSetViewportOrgEx = gdi32.NewProc("SetViewportOrgEx")
|
||||
)
|
||||
|
||||
// wxRadarFits reports whether the panel with its radar picture fits on the
|
||||
// screen, with some room left for the bar. On a small or heavily scaled screen
|
||||
// the radar is left out and the rest of the panel stays as it was.
|
||||
func wxRadarFits() bool {
|
||||
_, sh := screenSize()
|
||||
return px(wxFullH+wxRadarH) <= sh-px(90)
|
||||
}
|
||||
|
||||
// radarAdvance moves the loop to its next frame. The newest frame is held for a
|
||||
// few beats before it starts over.
|
||||
func radarAdvance() {
|
||||
if !wxShown || !wxRadarFits() {
|
||||
return
|
||||
}
|
||||
rs := radarSnapshot()
|
||||
n := len(rs.frames)
|
||||
if n < 2 {
|
||||
return
|
||||
}
|
||||
rdIdx %= n
|
||||
if rdIdx == n-1 && rdHold < 3 {
|
||||
rdHold++
|
||||
return
|
||||
}
|
||||
rdHold = 0
|
||||
rdIdx = (rdIdx + 1) % n
|
||||
rc := rect{px(14), px(226), px(14 + 312), px(226 + 150)}
|
||||
pInvalidateRect.Call(wxHwnd, uintptr(unsafe.Pointer(&rc)), 0)
|
||||
}
|
||||
|
||||
func radarBox(hdc uintptr, r rect) {
|
||||
t := px(1)
|
||||
fillRaw(hdc, rect{r.Left, r.Top, r.Right, r.Top + t}, colRule)
|
||||
fillRaw(hdc, rect{r.Left, r.Bottom - t, r.Right, r.Bottom}, colRule)
|
||||
fillRaw(hdc, rect{r.Left, r.Top, r.Left + t, r.Bottom}, colRule)
|
||||
fillRaw(hdc, rect{r.Right - t, r.Top, r.Right, r.Bottom}, colRule)
|
||||
}
|
||||
|
||||
// radarDraw paints the radar picture (or a short message while there isn't one)
|
||||
// into the rectangle r. The panel is painted at double size, so px() here is
|
||||
// already the double size distance.
|
||||
func radarDraw(hdc uintptr, r rect) {
|
||||
rs := radarSnapshot()
|
||||
w, h := r.Right-r.Left, r.Bottom-r.Top
|
||||
|
||||
if len(rs.frames) == 0 {
|
||||
fillRaw(hdc, r, rgb(22, 22, 27))
|
||||
msg := "Loading radar..."
|
||||
if rs.err != "" {
|
||||
msg = rs.err
|
||||
}
|
||||
wxText(hdc, msg, r, fText, colDim, dtCenter|dtVCenter|dtSingleLine)
|
||||
radarBox(hdc, r)
|
||||
return
|
||||
}
|
||||
|
||||
i := rdIdx % len(rs.frames)
|
||||
bmi := bitmapInfoHeader{Width: radarW, Height: -radarH, Planes: 1, BitCount: 32}
|
||||
bmi.Size = uint32(unsafe.Sizeof(bmi))
|
||||
pSetStretchBltMode.Call(hdc, halftone)
|
||||
pSetBrushOrgEx.Call(hdc, 0, 0, 0)
|
||||
pStretchDIBits.Call(hdc,
|
||||
uintptr(r.Left), uintptr(r.Top), uintptr(w), uintptr(h),
|
||||
0, 0, radarW, radarH,
|
||||
uintptr(unsafe.Pointer(&rs.frames[i][0])), uintptr(unsafe.Pointer(&bmi)), 0, srcCopy)
|
||||
|
||||
// You are here.
|
||||
cx, cy := center(r)
|
||||
circleRaw(hdc, cx, cy, px(5), rgb(245, 245, 245))
|
||||
circleRaw(hdc, cx, cy, px(3), rgb(235, 70, 70))
|
||||
|
||||
// When this frame is from.
|
||||
back := rgb(22, 22, 27)
|
||||
label := "Radar"
|
||||
if i < len(rs.times) {
|
||||
label += " " + rs.times[i].Format("3:04 PM")
|
||||
}
|
||||
fillRaw(hdc, rect{r.Left, r.Top, r.Left + px(104), r.Top + px(19)}, back)
|
||||
wxText(hdc, label, rect{r.Left + px(6), r.Top, r.Left + px(104), r.Top + px(19)}, fSmall, colText, dtLeft|dtVCenter|dtSingleLine)
|
||||
|
||||
// Where the pictures come from.
|
||||
fillRaw(hdc, rect{r.Right - px(92), r.Bottom - px(14), r.Right, r.Bottom}, back)
|
||||
wxText(hdc, "RainViewer, Esri", rect{r.Right - px(92), r.Bottom - px(14), r.Right - px(4), r.Bottom}, fTiny, colDim, dtRight|dtVCenter|dtSingleLine)
|
||||
|
||||
radarBox(hdc, r)
|
||||
}
|
||||
@@ -21,6 +21,8 @@ const (
|
||||
trayWifi
|
||||
trayChevron
|
||||
trayWeather // added after the others so saved orders from older versions still mean the same thing
|
||||
trayNews
|
||||
traySys
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -45,6 +47,10 @@ const (
|
||||
|
||||
menuWeatherID = 26
|
||||
menuWeatherZipID = 27
|
||||
menuNewsID = 28
|
||||
menuNewsZipID = 29
|
||||
menuSysID = 34
|
||||
menuSysCopyID = 35
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -105,7 +111,22 @@ func trayItemsAt(x, clientH int32) []trayItem {
|
||||
continue // this PC has no battery
|
||||
}
|
||||
w = px(36)
|
||||
case trayVolume, trayWifi, trayWeather:
|
||||
case trayVolume, trayWifi:
|
||||
w = px(36)
|
||||
case trayWeather:
|
||||
if !showWeather {
|
||||
continue
|
||||
}
|
||||
w = px(36)
|
||||
case trayNews:
|
||||
if !showNews {
|
||||
continue
|
||||
}
|
||||
w = px(36)
|
||||
case traySys:
|
||||
if !showSys {
|
||||
continue
|
||||
}
|
||||
w = px(36)
|
||||
case trayChevron:
|
||||
w = px(28)
|
||||
@@ -296,6 +317,10 @@ func drawTray(hdc uintptr, w, h int32) {
|
||||
drawChevron(hdc, it.r)
|
||||
case trayWeather:
|
||||
drawWeatherIcon(hdc, it.r)
|
||||
case trayNews:
|
||||
drawNewsIcon(hdc, it.r)
|
||||
case traySys:
|
||||
drawSysIcon(hdc, it.r)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -576,6 +601,10 @@ func trayClick(kind int) {
|
||||
tapChord(vkLWin, 'A') // quick settings
|
||||
case trayWeather:
|
||||
weatherToggle()
|
||||
case trayNews:
|
||||
newsToggle()
|
||||
case traySys:
|
||||
sysToggle()
|
||||
case trayChevron:
|
||||
// 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.
|
||||
@@ -662,6 +691,14 @@ func addTrayMenu(add func(flags uintptr, id uintptr, text string), sep func(), k
|
||||
add(0, menuWeatherID, "Show weather")
|
||||
add(0, menuWeatherZipID, "Weather ZIP code...")
|
||||
sep()
|
||||
case trayNews:
|
||||
add(0, menuNewsID, "Show local news")
|
||||
add(0, menuNewsZipID, "News ZIP code...")
|
||||
sep()
|
||||
case traySys:
|
||||
add(0, menuSysID, "Show system stats")
|
||||
add(0, menuSysCopyID, "Copy system stats")
|
||||
sep()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -680,7 +717,20 @@ func handleTrayCommand(cmd uintptr) bool {
|
||||
if !wxShown {
|
||||
weatherToggle()
|
||||
}
|
||||
case menuWeatherZipID:
|
||||
case menuNewsID:
|
||||
if !newsShown {
|
||||
newsToggle()
|
||||
}
|
||||
case menuSysID:
|
||||
if !sysShown {
|
||||
sysToggle()
|
||||
}
|
||||
case menuSysCopyID:
|
||||
if clipboardSet(sysText(sysGather())) {
|
||||
sysCopied = "Copied everything"
|
||||
sysCopiedAt = time.Now()
|
||||
}
|
||||
case menuWeatherZipID, menuNewsZipID:
|
||||
openSettings()
|
||||
case menuAutostartID:
|
||||
setAutostart(!autostartEnabled())
|
||||
@@ -697,7 +747,7 @@ func handleTrayCommand(cmd uintptr) bool {
|
||||
// ---------- the right row can be rearranged too ----------
|
||||
|
||||
// trayOrder is the left to right order of the items to the right of center.
|
||||
var trayOrder = []int{trayClock, trayWeather, trayBattery, trayVolume, trayWifi, trayChevron}
|
||||
var trayOrder = []int{trayClock, trayWeather, trayNews, traySys, trayBattery, trayVolume, trayWifi, trayChevron}
|
||||
|
||||
var (
|
||||
dragTray int // the tray item the mouse went down on, 0 for none
|
||||
@@ -716,22 +766,33 @@ func loadTrayOrder() {
|
||||
seen := map[int]bool{}
|
||||
for _, p := range strings.Split(strings.TrimSpace(string(b)), ",") {
|
||||
n, err := strconv.Atoi(strings.TrimSpace(p))
|
||||
if err != nil || n < trayClock || n > trayWeather || seen[n] {
|
||||
if err != nil || n < trayClock || n > traySys || seen[n] {
|
||||
return
|
||||
}
|
||||
seen[n] = true
|
||||
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)
|
||||
// An order saved before the weather cloud or the newspaper existed: put the new
|
||||
// icon next to the clock (the newspaper goes after the cloud).
|
||||
insertAfter := func(after, kind int) {
|
||||
out := make([]int, 0, len(order)+1)
|
||||
for _, k := range order {
|
||||
withCloud = append(withCloud, k)
|
||||
if k == trayClock {
|
||||
withCloud = append(withCloud, trayWeather)
|
||||
out = append(out, k)
|
||||
if k == after {
|
||||
out = append(out, kind)
|
||||
}
|
||||
}
|
||||
order = withCloud
|
||||
order = out
|
||||
seen[kind] = true
|
||||
}
|
||||
if !seen[trayWeather] {
|
||||
insertAfter(trayClock, trayWeather)
|
||||
}
|
||||
if !seen[trayNews] {
|
||||
insertAfter(trayWeather, trayNews)
|
||||
}
|
||||
if !seen[traySys] {
|
||||
insertAfter(trayNews, traySys)
|
||||
}
|
||||
if len(order) == len(trayOrder) {
|
||||
trayOrder = order
|
||||
|
||||
+22
@@ -123,11 +123,15 @@ func weatherSetZip(z string) {
|
||||
_ = os.Remove(locCacheFile())
|
||||
}
|
||||
weatherRefresh(true)
|
||||
newsRefresh(true) // the local news follows the same ZIP code
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if !showWeather {
|
||||
return
|
||||
}
|
||||
zip := wxZip
|
||||
wxMu.Lock()
|
||||
if len(zip) != 5 {
|
||||
@@ -230,6 +234,24 @@ func wxGetJSON(u string, v any) (status int, err error) {
|
||||
return resp.StatusCode, json.NewDecoder(io.LimitReader(resp.Body, 2<<20)).Decode(v)
|
||||
}
|
||||
|
||||
// wxGetBytes downloads one file (a picture tile), up to 4 MB.
|
||||
func wxGetBytes(u string) ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", "MirrorBar weather flyout")
|
||||
resp, err := wxClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
return io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user