Google Messages text flyout, part 1
This commit is contained in:
+146
-128
@@ -1,128 +1,146 @@
|
||||
package main
|
||||
|
||||
// The two optional features, weather and local news. Each can be turned off in
|
||||
// the settings window: its icon leaves the bar, its panel is closed, and nothing
|
||||
// is downloaded for it. Both are on until switched off.
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
showWeather = true
|
||||
showNews = true
|
||||
showSys = true
|
||||
)
|
||||
|
||||
func featuresFile() string { return filepath.Join(dataDir(), "features.txt") }
|
||||
|
||||
func loadFeatures() {
|
||||
b, err := os.ReadFile(featuresFile())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, ln := range strings.Split(string(b), "\n") {
|
||||
k, v, ok := strings.Cut(strings.TrimSpace(ln), "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
on := strings.TrimSpace(v) != "0"
|
||||
switch strings.TrimSpace(k) {
|
||||
case "weather":
|
||||
showWeather = on
|
||||
case "news":
|
||||
showNews = on
|
||||
case "sys":
|
||||
showSys = on
|
||||
case "panelfade":
|
||||
flyMatchBar = on
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func saveFeatures() {
|
||||
s := "weather=" + b01(showWeather) + "\nnews=" + b01(showNews) + "\nsys=" + b01(showSys) + "\npanelfade=" + b01(flyMatchBar) + "\n"
|
||||
_ = os.MkdirAll(dataDir(), 0o755)
|
||||
_ = os.WriteFile(featuresFile(), []byte(s), 0o644)
|
||||
}
|
||||
|
||||
func b01(b bool) string {
|
||||
if b {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
// setShowWeather is called by the settings window.
|
||||
func setShowWeather(on bool) {
|
||||
if showWeather == on {
|
||||
return
|
||||
}
|
||||
showWeather = on
|
||||
saveFeatures()
|
||||
if on {
|
||||
weatherRefresh(false)
|
||||
} else {
|
||||
weatherHide()
|
||||
}
|
||||
featuresChanged()
|
||||
}
|
||||
|
||||
// setShowNews is called by the settings window.
|
||||
func setShowNews(on bool) {
|
||||
if showNews == on {
|
||||
return
|
||||
}
|
||||
showNews = on
|
||||
saveFeatures()
|
||||
if on {
|
||||
newsRefresh(false)
|
||||
} else {
|
||||
newsHide()
|
||||
}
|
||||
featuresChanged()
|
||||
}
|
||||
|
||||
// setShowSys is called by the settings window.
|
||||
func setShowSys(on bool) {
|
||||
if showSys == on {
|
||||
return
|
||||
}
|
||||
showSys = on
|
||||
saveFeatures()
|
||||
if !on {
|
||||
sysHide()
|
||||
}
|
||||
featuresChanged()
|
||||
}
|
||||
|
||||
// setFlyMatchBar is called by the settings window: should the panels be as
|
||||
// see-through as the bar?
|
||||
func setFlyMatchBar(on bool) {
|
||||
if flyMatchBar == on {
|
||||
return
|
||||
}
|
||||
flyMatchBar = on
|
||||
saveFeatures()
|
||||
flyRefreshAll()
|
||||
}
|
||||
|
||||
func featuresChanged() {
|
||||
settingsZipStatusUpdate()
|
||||
redraw(hwndMain)
|
||||
}
|
||||
|
||||
// zipStatusText is the one line the settings window shows under the ZIP box.
|
||||
func zipStatusText() string {
|
||||
switch {
|
||||
case !showWeather && !showNews:
|
||||
return "Weather and news are both turned off."
|
||||
case wxZip == "":
|
||||
return "Type a 5 digit ZIP code to turn on weather and local news."
|
||||
case showWeather:
|
||||
return weatherStatusText()
|
||||
}
|
||||
return newsStatusText()
|
||||
}
|
||||
package main
|
||||
|
||||
// The two optional features, weather and local news. Each can be turned off in
|
||||
// the settings window: its icon leaves the bar, its panel is closed, and nothing
|
||||
// is downloaded for it. Both are on until switched off.
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
showWeather = true
|
||||
showNews = true
|
||||
showSys = true
|
||||
showGM = true // the Google Messages bubble
|
||||
)
|
||||
|
||||
func featuresFile() string { return filepath.Join(dataDir(), "features.txt") }
|
||||
|
||||
func loadFeatures() {
|
||||
b, err := os.ReadFile(featuresFile())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, ln := range strings.Split(string(b), "\n") {
|
||||
k, v, ok := strings.Cut(strings.TrimSpace(ln), "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
on := strings.TrimSpace(v) != "0"
|
||||
switch strings.TrimSpace(k) {
|
||||
case "weather":
|
||||
showWeather = on
|
||||
case "news":
|
||||
showNews = on
|
||||
case "sys":
|
||||
showSys = on
|
||||
case "messages":
|
||||
showGM = on
|
||||
case "panelfade":
|
||||
flyMatchBar = on
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func saveFeatures() {
|
||||
s := "weather=" + b01(showWeather) + "\nnews=" + b01(showNews) + "\nsys=" + b01(showSys) + "\nmessages=" + b01(showGM) + "\npanelfade=" + b01(flyMatchBar) + "\n"
|
||||
_ = os.MkdirAll(dataDir(), 0o755)
|
||||
_ = os.WriteFile(featuresFile(), []byte(s), 0o644)
|
||||
}
|
||||
|
||||
func b01(b bool) string {
|
||||
if b {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
// setShowWeather is called by the settings window.
|
||||
func setShowWeather(on bool) {
|
||||
if showWeather == on {
|
||||
return
|
||||
}
|
||||
showWeather = on
|
||||
saveFeatures()
|
||||
if on {
|
||||
weatherRefresh(false)
|
||||
} else {
|
||||
weatherHide()
|
||||
}
|
||||
featuresChanged()
|
||||
}
|
||||
|
||||
// setShowNews is called by the settings window.
|
||||
func setShowNews(on bool) {
|
||||
if showNews == on {
|
||||
return
|
||||
}
|
||||
showNews = on
|
||||
saveFeatures()
|
||||
if on {
|
||||
newsRefresh(false)
|
||||
} else {
|
||||
newsHide()
|
||||
}
|
||||
featuresChanged()
|
||||
}
|
||||
|
||||
// setShowSys is called by the settings window.
|
||||
func setShowSys(on bool) {
|
||||
if showSys == on {
|
||||
return
|
||||
}
|
||||
showSys = on
|
||||
saveFeatures()
|
||||
if !on {
|
||||
sysHide()
|
||||
}
|
||||
featuresChanged()
|
||||
}
|
||||
|
||||
// setShowGM is called by the settings window.
|
||||
func setShowGM(on bool) {
|
||||
if showGM == on {
|
||||
return
|
||||
}
|
||||
showGM = on
|
||||
saveFeatures()
|
||||
if on {
|
||||
gmAutoTried = false // let the background check start Messages again
|
||||
} else {
|
||||
gmClose()
|
||||
}
|
||||
featuresChanged()
|
||||
}
|
||||
|
||||
// setFlyMatchBar is called by the settings window: should the panels be as
|
||||
// see-through as the bar?
|
||||
func setFlyMatchBar(on bool) {
|
||||
if flyMatchBar == on {
|
||||
return
|
||||
}
|
||||
flyMatchBar = on
|
||||
saveFeatures()
|
||||
flyRefreshAll()
|
||||
}
|
||||
|
||||
func featuresChanged() {
|
||||
settingsZipStatusUpdate()
|
||||
redraw(hwndMain)
|
||||
}
|
||||
|
||||
// zipStatusText is the one line the settings window shows under the ZIP box.
|
||||
func zipStatusText() string {
|
||||
switch {
|
||||
case !showWeather && !showNews:
|
||||
return "Weather and news are both turned off."
|
||||
case wxZip == "":
|
||||
return "Type a 5 digit ZIP code to turn on weather and local news."
|
||||
case showWeather:
|
||||
return weatherStatusText()
|
||||
}
|
||||
return newsStatusText()
|
||||
}
|
||||
|
||||
+553
@@ -0,0 +1,553 @@
|
||||
package main
|
||||
|
||||
// The Google Messages flyout: a speech bubble on the bar that opens Messages for
|
||||
// web (messages.google.com) in a small window above the bar, so you can read and
|
||||
// answer texts without opening a browser tab.
|
||||
//
|
||||
// How it works: MirrorBar starts Microsoft Edge in "app" mode (no tabs, no address
|
||||
// bar) with its own private profile folder, so the sign-in from pairing your phone
|
||||
// is remembered and never touches your normal Edge. Edge is already on every
|
||||
// Windows 11 PC, so there is nothing to install. The window is kept just off the
|
||||
// edge of the screen while the flyout is closed; clicking the bubble slides it
|
||||
// above the bar, clicking anywhere else slides it away again. Because the page
|
||||
// keeps running, the unread number in its window title can be shown on the bubble.
|
||||
//
|
||||
// Messages for web only works while the phone is on and connected; that is how
|
||||
// Google built it, not something MirrorBar can change.
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
gmURL = "https://messages.google.com/web/conversations"
|
||||
|
||||
gmW = 780.0 // flyout size, in 96 dpi units
|
||||
gmH = 580.0
|
||||
|
||||
gmTimerID = 3 // on the main window: watches for clicks outside the flyout
|
||||
gmParkX = -32000
|
||||
gmParkY = -32000
|
||||
gmAutoDelay = 20 * time.Second // wait this long after start before opening Messages in the background
|
||||
gmLaunchMax = 45 * time.Second // give up looking for the window after this long
|
||||
|
||||
gmSwpNoSize = 0x0001
|
||||
gmSwpNoMove = 0x0002
|
||||
gmSwpNoZOrder = 0x0004
|
||||
gmSwpFrameChanged = 0x0020
|
||||
gmSwShowNoActive = 4
|
||||
gmWsExAppWindow = 0x00040000
|
||||
gmGaRootOwner = 3
|
||||
)
|
||||
|
||||
var (
|
||||
pIsWindow = user32.NewProc("IsWindow")
|
||||
pWindowFromPoint = user32.NewProc("WindowFromPoint")
|
||||
pGetAncestor = user32.NewProc("GetAncestor")
|
||||
pSetWindowLongPtr = user32.NewProc("SetWindowLongPtrW")
|
||||
pSetWindowPlace = user32.NewProc("SetWindowPlacement")
|
||||
)
|
||||
|
||||
var (
|
||||
gmHwnd uintptr
|
||||
gmPid uint32
|
||||
gmShown bool
|
||||
gmPrevBtn bool
|
||||
gmPrevFg uintptr
|
||||
gmShownAt time.Time
|
||||
gmLaunching bool
|
||||
gmLaunchedAt time.Time
|
||||
gmWantOpen bool // the bubble was clicked while the window was still starting
|
||||
gmAutoTried bool
|
||||
gmUnread int
|
||||
gmEnumCb uintptr
|
||||
gmFound uintptr
|
||||
gmLogged = map[string]bool{}
|
||||
gmLoggedN int
|
||||
)
|
||||
|
||||
type gmPlacementT struct {
|
||||
Length uint32
|
||||
Flags uint32
|
||||
ShowCmd uint32
|
||||
MinPos point
|
||||
MaxPos point
|
||||
Normal rect
|
||||
}
|
||||
|
||||
// ---------- the button on the bar ----------
|
||||
|
||||
func drawGMIcon(hdc uintptr, r rect) {
|
||||
if !layerFg {
|
||||
return
|
||||
}
|
||||
col := iconColor()
|
||||
cx, cy := center(r)
|
||||
e := px(1)
|
||||
l, t, rt, b := cx-px(6.5), cy-px(5.5), cx+px(6.5), cy+px(3)
|
||||
fillRaw(hdc, rect{l, t, rt, t + e}, col)
|
||||
fillRaw(hdc, rect{l, b - e, rt, b}, col)
|
||||
fillRaw(hdc, rect{l, t, l + e, b}, col)
|
||||
fillRaw(hdc, rect{rt - e, t, rt, b}, col)
|
||||
// the little tail at the bottom left
|
||||
polyline(hdc, []point{{cx - px(3.5), b}, {cx - px(3.5), cy + px(6)}, {cx - px(0.5), b}}, e, col)
|
||||
// two lines of "text" in the bubble
|
||||
fillRaw(hdc, rect{cx - px(4), cy - px(3), cx + px(4), cy - px(3) + e}, col)
|
||||
fillRaw(hdc, rect{cx - px(4), cy - px(0.5), cx + px(1.5), cy - px(0.5) + e}, col)
|
||||
|
||||
if gmUnread > 0 {
|
||||
br, bg, bb := styleColor(235, 70, 70)
|
||||
bx, by, rad := cx+px(6.5), cy-px(5.5), px(5)
|
||||
circleRaw(hdc, bx, by, rad, rgb(br, bg, bb))
|
||||
txt := strconv.Itoa(gmUnread)
|
||||
if gmUnread > 9 {
|
||||
txt = "9+"
|
||||
}
|
||||
f := makeFont(px(8), 700)
|
||||
old, _, _ := pSelectObject.Call(hdc, f)
|
||||
pSetBkMode.Call(hdc, transparent)
|
||||
pSetTextColor.Call(hdc, rgb(255, 255, 255))
|
||||
drawText(hdc, txt, rect{bx - rad, by - rad, bx + rad + 1, by + rad + 1})
|
||||
pSelectObject.Call(hdc, old)
|
||||
pDeleteObject.Call(f)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- finding and starting the browser window ----------
|
||||
|
||||
func gmProfileDir() string { return filepath.Join(dataDir(), "messages") }
|
||||
|
||||
func gmProfileExists() bool {
|
||||
_, err := os.Stat(filepath.Join(gmProfileDir(), "Local State"))
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// gmBrowserPath is Edge (always there on Windows 11), or Chrome if Edge is gone.
|
||||
func gmBrowserPath() string {
|
||||
type cand struct{ env, vendor, app, exe string }
|
||||
for _, c := range []cand{
|
||||
{"ProgramFiles(x86)", "Microsoft", "Edge", "msedge.exe"},
|
||||
{"ProgramFiles", "Microsoft", "Edge", "msedge.exe"},
|
||||
{"LOCALAPPDATA", "Microsoft", "Edge", "msedge.exe"},
|
||||
{"ProgramFiles", "Google", "Chrome", "chrome.exe"},
|
||||
{"ProgramFiles(x86)", "Google", "Chrome", "chrome.exe"},
|
||||
{"LOCALAPPDATA", "Google", "Chrome", "chrome.exe"},
|
||||
} {
|
||||
base := os.Getenv(c.env)
|
||||
if base == "" {
|
||||
continue
|
||||
}
|
||||
p := filepath.Join(base, c.vendor, c.app, "Application", c.exe)
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// gmLaunch starts the app window. Started in the background it opens minimized
|
||||
// and is never given focus, so it does not interrupt whatever is being typed.
|
||||
func gmLaunch(visible bool) bool {
|
||||
exe := gmBrowserPath()
|
||||
if exe == "" {
|
||||
return false
|
||||
}
|
||||
prof := gmProfileDir()
|
||||
if err := os.MkdirAll(prof, 0o755); err != nil {
|
||||
return false
|
||||
}
|
||||
args := []string{
|
||||
exe,
|
||||
"--app=" + gmURL,
|
||||
"--user-data-dir=" + prof,
|
||||
"--no-first-run",
|
||||
"--no-default-browser-check",
|
||||
// keep the page awake while its window is parked off the screen
|
||||
"--disable-features=CalculateNativeWinOcclusion",
|
||||
"--disable-renderer-backgrounding",
|
||||
"--disable-background-timer-throttling",
|
||||
"--disable-backgrounding-occluded-windows",
|
||||
}
|
||||
parts := make([]string, len(args))
|
||||
for i, a := range args {
|
||||
parts[i] = syscall.EscapeArg(a)
|
||||
}
|
||||
si := syscall.StartupInfo{Flags: 1} // STARTF_USESHOWWINDOW
|
||||
si.Cb = uint32(unsafe.Sizeof(si))
|
||||
si.ShowWindow = 7 // SW_SHOWMINNOACTIVE
|
||||
if visible {
|
||||
si.ShowWindow = 1 // SW_SHOWNORMAL
|
||||
}
|
||||
var pi syscall.ProcessInformation
|
||||
err := syscall.CreateProcess(u16(exe), u16(strings.Join(parts, " ")), nil, nil, false, 0, nil, nil, &si, &pi)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
gmPid = pi.ProcessId
|
||||
syscall.CloseHandle(pi.Process)
|
||||
syscall.CloseHandle(pi.Thread)
|
||||
gmLaunching = true
|
||||
gmLaunchedAt = time.Now()
|
||||
gmWantOpen = visible
|
||||
return true
|
||||
}
|
||||
|
||||
func gmWindowTitle(hwnd uintptr) string {
|
||||
var tb [512]uint16
|
||||
n, _, _ := pGetWindowText.Call(hwnd, uintptr(unsafe.Pointer(&tb[0])), 512)
|
||||
return syscall.UTF16ToString(tb[:n])
|
||||
}
|
||||
|
||||
// gmTitleMatch is true for the app window's title ("Messages for web", maybe
|
||||
// with a count in front) and false for ordinary browser windows.
|
||||
func gmTitleMatch(t string) bool {
|
||||
l := strings.ToLower(t)
|
||||
if strings.Contains(l, "microsoft edge") || strings.Contains(l, "google chrome") {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(l, "messages")
|
||||
}
|
||||
|
||||
func gmEnumProc(hwnd, lparam uintptr) uintptr {
|
||||
var cls [64]uint16
|
||||
n, _, _ := pGetClassName.Call(hwnd, uintptr(unsafe.Pointer(&cls[0])), 64)
|
||||
if syscall.UTF16ToString(cls[:n]) != "Chrome_WidgetWin_1" {
|
||||
return 1
|
||||
}
|
||||
if !gmTitleMatch(gmWindowTitle(hwnd)) {
|
||||
return 1
|
||||
}
|
||||
var pid uint32
|
||||
pGetWindowThreadProcessId.Call(hwnd, uintptr(unsafe.Pointer(&pid)))
|
||||
if gmPid != 0 && pid == gmPid {
|
||||
gmFound = hwnd
|
||||
return 0 // exactly the window we started
|
||||
}
|
||||
if gmFound == 0 {
|
||||
gmFound = hwnd
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
func gmFindWindow() uintptr {
|
||||
if gmEnumCb == 0 {
|
||||
gmEnumCb = syscall.NewCallback(gmEnumProc)
|
||||
}
|
||||
gmFound = 0
|
||||
pEnumWindows.Call(gmEnumCb, 0)
|
||||
return gmFound
|
||||
}
|
||||
|
||||
func gmWindowOK() bool {
|
||||
if gmHwnd == 0 {
|
||||
return false
|
||||
}
|
||||
ok, _, _ := pIsWindow.Call(gmHwnd)
|
||||
return ok != 0
|
||||
}
|
||||
|
||||
// gmAdopt takes charge of the window once it exists: it stays out of the app
|
||||
// list and Alt+Tab, and sits off screen until the bubble is clicked.
|
||||
func gmAdopt(h uintptr) {
|
||||
gmHwnd = h
|
||||
var pid uint32
|
||||
pGetWindowThreadProcessId.Call(h, uintptr(unsafe.Pointer(&pid)))
|
||||
if pid != 0 {
|
||||
gmPid = pid
|
||||
}
|
||||
ex, _, _ := pGetWindowLongPtr.Call(h, neg(-20))
|
||||
want := (ex | wsExToolWindow) &^ gmWsExAppWindow
|
||||
if want != ex {
|
||||
pSetWindowLongPtr.Call(h, neg(-20), want)
|
||||
pSetWindowPos.Call(h, 0, 0, 0, 0, 0, gmSwpNoMove|gmSwpNoSize|gmSwpNoZOrder|swpNoActivate|gmSwpFrameChanged)
|
||||
}
|
||||
gmLaunching = false
|
||||
if gmWantOpen {
|
||||
gmWantOpen = false
|
||||
gmShow()
|
||||
} else {
|
||||
gmPark()
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- moving the window ----------
|
||||
|
||||
func gmSize() (int32, int32) {
|
||||
sw, sh := screenSize()
|
||||
cw := min(px(gmW), sw-px(16))
|
||||
ch := min(px(gmH), sh-px(24))
|
||||
return cw, ch
|
||||
}
|
||||
|
||||
// gmMove puts the window at x, y, w, h. A minimized window is first brought back
|
||||
// to that spot without taking focus.
|
||||
func gmMove(x, y, w, h int32, topmost, activate bool) {
|
||||
if !gmWindowOK() {
|
||||
return
|
||||
}
|
||||
if z, _, _ := pIsZoomed.Call(gmHwnd); z != 0 {
|
||||
pShowWindow.Call(gmHwnd, swRestore)
|
||||
}
|
||||
if ic, _, _ := pIsIconic.Call(gmHwnd); ic != 0 {
|
||||
cmd := uint32(gmSwShowNoActive)
|
||||
if activate {
|
||||
cmd = swRestore
|
||||
}
|
||||
wp := gmPlacementT{ShowCmd: cmd, Normal: rect{x, y, x + w, y + h}}
|
||||
wp.Length = uint32(unsafe.Sizeof(wp))
|
||||
pSetWindowPlace.Call(gmHwnd, uintptr(unsafe.Pointer(&wp)))
|
||||
}
|
||||
after := ^uintptr(1) // HWND_NOTOPMOST
|
||||
if topmost {
|
||||
after = hwndTopmost
|
||||
}
|
||||
var flags uintptr
|
||||
if !activate {
|
||||
flags = swpNoActivate
|
||||
}
|
||||
pSetWindowPos.Call(gmHwnd, after, uintptr(x), uintptr(y), uintptr(w), uintptr(h), flags|swpShowWindow)
|
||||
}
|
||||
|
||||
func gmPark() {
|
||||
cw, ch := gmSize()
|
||||
gmMove(gmParkX, gmParkY, cw, ch, false, false)
|
||||
}
|
||||
|
||||
// gmPlace is where the flyout goes: above a horizontal bar, to the left of a
|
||||
// vertical one, centered on the bubble and always on the screen.
|
||||
func gmPlace() (x, y, w, h int32) {
|
||||
cw, ch := gmSize()
|
||||
ar := trayAnchor(trayGM)
|
||||
var wr rect
|
||||
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
|
||||
sw, sh := screenSize()
|
||||
space := px(8)
|
||||
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
|
||||
}
|
||||
}
|
||||
return clampI(x, 0, sw-cw), clampI(y, 0, sh-ch), cw, ch
|
||||
}
|
||||
|
||||
func gmFocus() {
|
||||
// This program just received the click, so Windows lets it hand the focus on.
|
||||
pSetForegroundWindow.Call(gmHwnd)
|
||||
}
|
||||
|
||||
// ---------- open, close, click ----------
|
||||
|
||||
func gmToggle() {
|
||||
if !showGM {
|
||||
return
|
||||
}
|
||||
if gmShown {
|
||||
gmHide()
|
||||
return
|
||||
}
|
||||
previewHide()
|
||||
weatherHide() // one panel at a time
|
||||
newsHide()
|
||||
sysHide()
|
||||
if gmWindowOK() {
|
||||
gmShow()
|
||||
return
|
||||
}
|
||||
gmHwnd = 0
|
||||
if gmLaunching {
|
||||
gmWantOpen = true // it is still starting; open it the moment it exists
|
||||
return
|
||||
}
|
||||
if !gmLaunch(true) {
|
||||
shellOpen(gmURL, "") // no Edge or Chrome found: use the default browser
|
||||
}
|
||||
}
|
||||
|
||||
func gmShow() {
|
||||
if !gmWindowOK() {
|
||||
return
|
||||
}
|
||||
fg, _, _ := pGetForegroundWindow.Call()
|
||||
if fg != gmHwnd {
|
||||
gmPrevFg = fg
|
||||
}
|
||||
gmShown = true
|
||||
gmPrevBtn = true // ignore a button that is still held from the click that opened this
|
||||
gmShownAt = time.Now()
|
||||
x, y, w, h := gmPlace()
|
||||
gmMove(x, y, w, h, true, true)
|
||||
gmFocus()
|
||||
pSetTimer.Call(hwndMain, gmTimerID, 80, 0)
|
||||
}
|
||||
|
||||
func gmHide() {
|
||||
if !gmShown {
|
||||
return
|
||||
}
|
||||
gmShown = false
|
||||
pKillTimer.Call(hwndMain, gmTimerID)
|
||||
if !gmWindowOK() {
|
||||
return
|
||||
}
|
||||
fg, _, _ := pGetForegroundWindow.Call()
|
||||
gmPark()
|
||||
if fg == gmHwnd && gmPrevFg != 0 && gmPrevFg != hwndMain && gmPrevFg != gmHwnd {
|
||||
if ok, _, _ := pIsWindow.Call(gmPrevFg); ok != 0 {
|
||||
pSetForegroundWindow.Call(gmPrevFg) // give the keyboard back to what you were doing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gmClose shuts the Messages window down (right-click, "Close Messages").
|
||||
func gmClose() {
|
||||
gmHide()
|
||||
if gmWindowOK() {
|
||||
pPostMessage.Call(gmHwnd, wmClose, 0, 0)
|
||||
}
|
||||
gmHwnd = 0
|
||||
gmLaunching = false
|
||||
gmWantOpen = false
|
||||
gmAutoTried = true // do not bring it back by itself
|
||||
gmUnread = 0
|
||||
}
|
||||
|
||||
func gmShutdown() {
|
||||
pKillTimer.Call(hwndMain, gmTimerID)
|
||||
if gmWindowOK() {
|
||||
pPostMessage.Call(gmHwnd, wmClose, 0, 0)
|
||||
}
|
||||
gmHwnd = 0
|
||||
}
|
||||
|
||||
// gmPoll runs every 80 ms while the flyout is open and closes it when a mouse
|
||||
// button goes down anywhere outside it (the bubble itself is left alone, so
|
||||
// clicking it can close the flyout).
|
||||
func gmPoll() {
|
||||
if !gmShown {
|
||||
return
|
||||
}
|
||||
if !gmWindowOK() {
|
||||
gmShown = false
|
||||
pKillTimer.Call(hwndMain, gmTimerID)
|
||||
return
|
||||
}
|
||||
l, _, _ := pGetAsyncKeyState.Call(vkLButton)
|
||||
r, _, _ := pGetAsyncKeyState.Call(vkRButton)
|
||||
down := (l|r)&0x8000 != 0
|
||||
if down && !gmPrevBtn && time.Since(gmShownAt) > 300*time.Millisecond {
|
||||
var cp point
|
||||
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
|
||||
var fr rect
|
||||
pGetWindowRect.Call(gmHwnd, uintptr(unsafe.Pointer(&fr)))
|
||||
inside := inRect(fr, cp)
|
||||
if !inside {
|
||||
// a dialog or menu that belongs to the Messages window counts as inside
|
||||
h, _, _ := pWindowFromPoint.Call(uintptr(uint32(cp.X)) | uintptr(uint32(cp.Y))<<32)
|
||||
if h != 0 {
|
||||
root, _, _ := pGetAncestor.Call(h, gmGaRootOwner)
|
||||
inside = root == gmHwnd
|
||||
}
|
||||
}
|
||||
if !inside && !inRect(trayAnchor(trayGM), cp) {
|
||||
gmHide()
|
||||
}
|
||||
}
|
||||
gmPrevBtn = down
|
||||
}
|
||||
|
||||
// ---------- the background check ----------
|
||||
|
||||
var gmCountRe = regexp.MustCompile(`[\(\[](\d{1,3})\+?[\)\]]`)
|
||||
|
||||
func gmParseUnread(title string) int {
|
||||
m := gmCountRe.FindStringSubmatch(title)
|
||||
if m == nil {
|
||||
return 0
|
||||
}
|
||||
n, _ := strconv.Atoi(m[1])
|
||||
return n
|
||||
}
|
||||
|
||||
// gmLogTitle keeps the first few different window titles in titles.txt next to
|
||||
// the Messages profile, so the unread badge can be tuned if Google words it
|
||||
// differently than expected. It stops after 25 so it never grows.
|
||||
func gmLogTitle(t string) {
|
||||
if t == "" || gmLogged[t] || gmLoggedN >= 25 {
|
||||
return
|
||||
}
|
||||
gmLogged[t] = true
|
||||
gmLoggedN++
|
||||
f, err := os.OpenFile(filepath.Join(dataDir(), "messages-titles.txt"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
if len(t) > 80 {
|
||||
t = t[:80]
|
||||
}
|
||||
_, _ = f.WriteString(time.Now().Format("2006-01-02 15:04:05") + "\t" + t + "\n")
|
||||
}
|
||||
|
||||
// gmTick runs from the bar's 250 ms timer. It starts Messages in the background
|
||||
// once you have paired a phone, notices when the window goes away, and reads
|
||||
// the unread count from the window title. Returns true when the bar needs a
|
||||
// repaint.
|
||||
func gmTick(tick int) (changed bool) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
changed = false // optional feature; never take the bar down with it
|
||||
}
|
||||
}()
|
||||
if !showGM {
|
||||
return false
|
||||
}
|
||||
if gmHwnd != 0 && !gmWindowOK() {
|
||||
gmHwnd = 0
|
||||
gmShown = false
|
||||
pKillTimer.Call(hwndMain, gmTimerID)
|
||||
changed = gmUnread != 0
|
||||
gmUnread = 0
|
||||
}
|
||||
if gmHwnd == 0 {
|
||||
switch {
|
||||
case gmLaunching:
|
||||
if h := gmFindWindow(); h != 0 {
|
||||
gmAdopt(h)
|
||||
} else if time.Since(gmLaunchedAt) > gmLaunchMax {
|
||||
gmLaunching = false
|
||||
gmWantOpen = false
|
||||
}
|
||||
case !gmAutoTried && time.Since(startedAt) > gmAutoDelay:
|
||||
gmAutoTried = true
|
||||
if h := gmFindWindow(); h != 0 {
|
||||
gmAdopt(h) // left over from an earlier run of MirrorBar
|
||||
} else if gmProfileExists() {
|
||||
gmLaunch(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
if gmHwnd != 0 && tick%8 == 0 {
|
||||
t := gmWindowTitle(gmHwnd)
|
||||
gmLogTitle(t)
|
||||
if n := gmParseUnread(t); n != gmUnread {
|
||||
gmUnread = n
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
return changed
|
||||
}
|
||||
+360
-359
@@ -1,359 +1,360 @@
|
||||
package main
|
||||
|
||||
// The local news flyout: a little newspaper on the bar that opens a panel above
|
||||
// it with a list of local headlines. Each headline is a link; clicking one opens
|
||||
// it in the default browser. The panel is its own window that never takes focus,
|
||||
// it goes away when you click the newspaper again or click anywhere else, and it
|
||||
// is painted at double size and shrunk down, the same way the weather panel is.
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
newsW = 380.0 // panel width, in 96 dpi units
|
||||
newsHead = 44.0 // header height
|
||||
newsRow = 58.0 // height of one headline
|
||||
newsFoot = 8.0
|
||||
newsMaxRows = 10
|
||||
|
||||
idcHandCursor = 32649
|
||||
)
|
||||
|
||||
var (
|
||||
newsHwnd uintptr
|
||||
newsShown bool
|
||||
newsPrevBtn bool
|
||||
newsHover = -1 // the headline under the mouse, -1 for none
|
||||
newsVisible int // how many headlines the panel is showing
|
||||
)
|
||||
|
||||
// ---------- the button on the bar ----------
|
||||
|
||||
// drawNewsIcon draws the little newspaper on the bar.
|
||||
func drawNewsIcon(hdc uintptr, r rect) {
|
||||
if !layerFg {
|
||||
return
|
||||
}
|
||||
col := iconColor()
|
||||
cx, cy := center(r)
|
||||
l, t, rt, b := cx-px(6), cy-px(5.5), cx+px(6), cy+px(5.5)
|
||||
e := px(1)
|
||||
fillRaw(hdc, rect{l, t, rt, t + e}, col)
|
||||
fillRaw(hdc, rect{l, b - e, rt, b}, col)
|
||||
fillRaw(hdc, rect{l, t, l + e, b}, col)
|
||||
fillRaw(hdc, rect{rt - e, t, rt, b}, col)
|
||||
fillRaw(hdc, rect{l + px(2.5), t + px(2.5), l + px(6), t + px(5.5)}, col) // the main headline
|
||||
fillRaw(hdc, rect{l + px(7.5), t + px(2.5), rt - px(2.5), t + px(3.5)}, col)
|
||||
fillRaw(hdc, rect{l + px(7.5), t + px(4.5), rt - px(2.5), t + px(5.5)}, col)
|
||||
fillRaw(hdc, rect{l + px(2.5), t + px(7), rt - px(2.5), t + px(8)}, col) // lines of text
|
||||
}
|
||||
|
||||
// ---------- the panel window ----------
|
||||
|
||||
func newsInit(inst uintptr) {
|
||||
cursor, _, _ := pLoadCursor.Call(0, idcArrow)
|
||||
className := u16("MirrorBarNews")
|
||||
wc := wndClassEx{
|
||||
WndProc: syscall.NewCallback(newsProc),
|
||||
Instance: inst,
|
||||
Cursor: cursor,
|
||||
ClassName: className,
|
||||
}
|
||||
wc.Size = uint32(unsafe.Sizeof(wc))
|
||||
pRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
|
||||
newsHwnd, _, _ = pCreateWindowEx.Call(
|
||||
wsExToolWindow|wsExTopmost|wsExNoActivate|wsExLayered,
|
||||
uintptr(unsafe.Pointer(className)),
|
||||
uintptr(unsafe.Pointer(u16(""))),
|
||||
wsPopup,
|
||||
0, 0, 10, 10,
|
||||
0, 0, inst, 0)
|
||||
}
|
||||
|
||||
func newsProc(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:
|
||||
flyPaint(hwnd, newsDraw)
|
||||
return 0
|
||||
case wmTimer:
|
||||
newsPoll()
|
||||
return 0
|
||||
case wmSetCursor:
|
||||
if newsHover >= 0 {
|
||||
h, _, _ := pLoadCursor.Call(0, idcHandCursor)
|
||||
pSetCursor.Call(h)
|
||||
return 1
|
||||
}
|
||||
case wmLButtonUp:
|
||||
newsClick(int32(int16(lparam&0xFFFF)), int32(int16(lparam>>16&0xFFFF)))
|
||||
return 0
|
||||
}
|
||||
r, _, _ := pDefWindowProc.Call(hwnd, m, wparam, lparam)
|
||||
return r
|
||||
}
|
||||
|
||||
// newsToggle is what clicking the newspaper does.
|
||||
func newsToggle() {
|
||||
if !showNews {
|
||||
return
|
||||
}
|
||||
if newsShown {
|
||||
newsHide()
|
||||
return
|
||||
}
|
||||
if newsHwnd == 0 {
|
||||
return
|
||||
}
|
||||
previewHide()
|
||||
weatherHide() // one panel at a time
|
||||
sysHide()
|
||||
newsRefresh(false)
|
||||
newsShown = true
|
||||
newsPrevBtn = true // ignore a button that is still held from the click that opened this
|
||||
newsHover = -1
|
||||
newsPlace()
|
||||
pSetTimer.Call(newsHwnd, wxPollTimerID, 80, 0)
|
||||
}
|
||||
|
||||
func newsHide() {
|
||||
if !newsShown {
|
||||
return
|
||||
}
|
||||
newsShown = false
|
||||
newsHover = -1
|
||||
pKillTimer.Call(newsHwnd, wxPollTimerID)
|
||||
pShowWindow.Call(newsHwnd, swHide)
|
||||
}
|
||||
|
||||
func newsShutdown() {
|
||||
newsHide()
|
||||
if newsHwnd != 0 {
|
||||
pDestroyWindow.Call(newsHwnd)
|
||||
newsHwnd = 0
|
||||
}
|
||||
}
|
||||
|
||||
// newsVisibleRows is how many headlines fit on the screen, up to ten.
|
||||
func newsVisibleRows(n int) int {
|
||||
_, sh := screenSize()
|
||||
avail := sh - px(90) - px(newsHead+newsFoot)
|
||||
m := int(avail / px(newsRow))
|
||||
m = max(3, min(m, newsMaxRows))
|
||||
return min(n, m)
|
||||
}
|
||||
|
||||
// newsPlace sizes the panel for what it has to show and puts it next to the
|
||||
// newspaper: above a horizontal bar, to the left of a vertical one.
|
||||
func newsPlace() {
|
||||
if newsHwnd == 0 {
|
||||
return
|
||||
}
|
||||
s := newsSnapshot()
|
||||
lh := wxShortH
|
||||
newsVisible = 0
|
||||
if wxZip != "" && len(s.items) > 0 {
|
||||
newsVisible = newsVisibleRows(len(s.items))
|
||||
lh = newsHead + float64(newsVisible)*newsRow + newsFoot
|
||||
}
|
||||
flyPlace(newsHwnd, trayAnchor(trayNews), px(newsW), px(lh))
|
||||
flyRefresh(newsHwnd)
|
||||
}
|
||||
|
||||
// newsDone runs when a download finishes.
|
||||
func newsDone() {
|
||||
if newsShown {
|
||||
newsPlace()
|
||||
}
|
||||
}
|
||||
|
||||
// newsPoll closes the panel when a mouse button goes down anywhere outside it
|
||||
// (the newspaper itself is left alone, so clicking it can close the panel) and
|
||||
// keeps track of which headline the mouse is over.
|
||||
func newsPoll() {
|
||||
l, _, _ := pGetAsyncKeyState.Call(vkLButton)
|
||||
r, _, _ := pGetAsyncKeyState.Call(vkRButton)
|
||||
down := (l|r)&0x8000 != 0
|
||||
var cp point
|
||||
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
|
||||
var fr rect
|
||||
pGetWindowRect.Call(newsHwnd, uintptr(unsafe.Pointer(&fr)))
|
||||
if down && !newsPrevBtn && !inRect(fr, cp) && !inRect(trayAnchor(trayNews), cp) {
|
||||
newsHide()
|
||||
newsPrevBtn = down
|
||||
return
|
||||
}
|
||||
newsPrevBtn = down
|
||||
|
||||
h := -1
|
||||
if inRect(fr, cp) {
|
||||
h = newsRowAt(cp.X-fr.Left, cp.Y-fr.Top)
|
||||
}
|
||||
if h != newsHover {
|
||||
newsHover = h
|
||||
flyRefresh(newsHwnd)
|
||||
}
|
||||
}
|
||||
|
||||
// newsRowAt is the headline under a spot in the panel (real pixels), or -1.
|
||||
func newsRowAt(x, y int32) int {
|
||||
ry := y - px(newsHead)
|
||||
if ry < 0 || x < 0 {
|
||||
return -1
|
||||
}
|
||||
i := int(ry / px(newsRow))
|
||||
if i >= newsVisible {
|
||||
return -1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// newsClick runs when the panel is clicked: a headline opens in the browser, the
|
||||
// header refreshes, and the messages in an empty panel are buttons.
|
||||
func newsClick(x, y int32) {
|
||||
s := newsSnapshot()
|
||||
switch {
|
||||
case wxZip == "":
|
||||
newsHide()
|
||||
openSettings()
|
||||
return
|
||||
case len(s.items) == 0:
|
||||
if !s.loading {
|
||||
newsRefresh(true)
|
||||
}
|
||||
return
|
||||
case y < px(newsHead):
|
||||
newsRefresh(true)
|
||||
return
|
||||
}
|
||||
if i := newsRowAt(x, y); i >= 0 && i < len(s.items) {
|
||||
link := s.items[i].link
|
||||
newsHide()
|
||||
shellOpen(link, "") // only web links were kept when the feed was read
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- painting ----------
|
||||
|
||||
func newsDraw(hdc uintptr, W, H int32) {
|
||||
u := px
|
||||
flyFrame(hdc, W, H) // outline in the tray icon color; the background is added when painting
|
||||
|
||||
pad := u(14)
|
||||
s := newsSnapshot()
|
||||
line := uintptr(dtLeft | dtVCenter | dtSingleLine | dtEndEllipsis)
|
||||
|
||||
title := "Local news"
|
||||
if s.place != "" && wxZip != "" {
|
||||
title = s.place + " news"
|
||||
}
|
||||
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 == "" || len(s.items) == 0 {
|
||||
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 headlines...", ""
|
||||
default:
|
||||
head, sub = "No news right now", s.err+" Click here to try again."
|
||||
}
|
||||
wxText(hdc, head, rect{pad, u(48), W - pad, u(74)}, fMed, colText, dtCenter|dtVCenter|dtSingleLine)
|
||||
if sub != "" {
|
||||
wxText(hdc, sub, rect{pad, u(78), W - pad, H - pad}, fText, colDim, dtCenter|dtWordBreak)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fillRaw(hdc, rect{pad, u(newsHead) - u(2), W - pad, u(newsHead) - u(1)}, colRule)
|
||||
now := time.Now()
|
||||
for i := 0; i < newsVisible && i < len(s.items); i++ {
|
||||
it := s.items[i]
|
||||
y := u(newsHead) + int32(i)*u(newsRow)
|
||||
head := colText
|
||||
if i == newsHover {
|
||||
fillRaw(hdc, rect{u(6), y, W - u(6), y + u(newsRow)}, rgb(40, 40, 48))
|
||||
head = colAccent
|
||||
}
|
||||
wxText(hdc, it.title, rect{pad, y + u(6), W - pad, y + u(42)}, fBold, head, dtLeft|dtWordBreak|dtEndEllipsis)
|
||||
meta := it.source
|
||||
if age := newsAge(it.when, now); age != "" {
|
||||
if meta != "" {
|
||||
meta += " · "
|
||||
}
|
||||
meta += age
|
||||
}
|
||||
wxText(hdc, meta, rect{pad, y + u(42), W - pad, y + u(56)}, fSmall, colDim, line)
|
||||
if i < newsVisible-1 && i != newsHover && i+1 != newsHover {
|
||||
fillRaw(hdc, rect{pad, y + u(newsRow) - u(1), W - pad, y + u(newsRow)}, colRule)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- shared by both panels ----------
|
||||
|
||||
// trayAnchor is where an icon on the bar sits on the screen.
|
||||
func trayAnchor(kind int) rect {
|
||||
var wr rect
|
||||
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
|
||||
w, h := clientSize(hwndMain)
|
||||
for _, it := range trayItems(w, h) {
|
||||
if it.kind == kind {
|
||||
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
|
||||
}
|
||||
|
||||
// flyPlace puts a panel of size cw by ch next to an icon: above a horizontal
|
||||
// bar, to the left of a vertical one, and always on the screen.
|
||||
func flyPlace(hwnd uintptr, ar rect, cw, ch int32) {
|
||||
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(hwnd, hwndTopmost, uintptr(x), uintptr(y), uintptr(cw), uintptr(ch), swpNoActivate|swpShowWindow)
|
||||
}
|
||||
package main
|
||||
|
||||
// The local news flyout: a little newspaper on the bar that opens a panel above
|
||||
// it with a list of local headlines. Each headline is a link; clicking one opens
|
||||
// it in the default browser. The panel is its own window that never takes focus,
|
||||
// it goes away when you click the newspaper again or click anywhere else, and it
|
||||
// is painted at double size and shrunk down, the same way the weather panel is.
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
newsW = 380.0 // panel width, in 96 dpi units
|
||||
newsHead = 44.0 // header height
|
||||
newsRow = 58.0 // height of one headline
|
||||
newsFoot = 8.0
|
||||
newsMaxRows = 10
|
||||
|
||||
idcHandCursor = 32649
|
||||
)
|
||||
|
||||
var (
|
||||
newsHwnd uintptr
|
||||
newsShown bool
|
||||
newsPrevBtn bool
|
||||
newsHover = -1 // the headline under the mouse, -1 for none
|
||||
newsVisible int // how many headlines the panel is showing
|
||||
)
|
||||
|
||||
// ---------- the button on the bar ----------
|
||||
|
||||
// drawNewsIcon draws the little newspaper on the bar.
|
||||
func drawNewsIcon(hdc uintptr, r rect) {
|
||||
if !layerFg {
|
||||
return
|
||||
}
|
||||
col := iconColor()
|
||||
cx, cy := center(r)
|
||||
l, t, rt, b := cx-px(6), cy-px(5.5), cx+px(6), cy+px(5.5)
|
||||
e := px(1)
|
||||
fillRaw(hdc, rect{l, t, rt, t + e}, col)
|
||||
fillRaw(hdc, rect{l, b - e, rt, b}, col)
|
||||
fillRaw(hdc, rect{l, t, l + e, b}, col)
|
||||
fillRaw(hdc, rect{rt - e, t, rt, b}, col)
|
||||
fillRaw(hdc, rect{l + px(2.5), t + px(2.5), l + px(6), t + px(5.5)}, col) // the main headline
|
||||
fillRaw(hdc, rect{l + px(7.5), t + px(2.5), rt - px(2.5), t + px(3.5)}, col)
|
||||
fillRaw(hdc, rect{l + px(7.5), t + px(4.5), rt - px(2.5), t + px(5.5)}, col)
|
||||
fillRaw(hdc, rect{l + px(2.5), t + px(7), rt - px(2.5), t + px(8)}, col) // lines of text
|
||||
}
|
||||
|
||||
// ---------- the panel window ----------
|
||||
|
||||
func newsInit(inst uintptr) {
|
||||
cursor, _, _ := pLoadCursor.Call(0, idcArrow)
|
||||
className := u16("MirrorBarNews")
|
||||
wc := wndClassEx{
|
||||
WndProc: syscall.NewCallback(newsProc),
|
||||
Instance: inst,
|
||||
Cursor: cursor,
|
||||
ClassName: className,
|
||||
}
|
||||
wc.Size = uint32(unsafe.Sizeof(wc))
|
||||
pRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc)))
|
||||
newsHwnd, _, _ = pCreateWindowEx.Call(
|
||||
wsExToolWindow|wsExTopmost|wsExNoActivate|wsExLayered,
|
||||
uintptr(unsafe.Pointer(className)),
|
||||
uintptr(unsafe.Pointer(u16(""))),
|
||||
wsPopup,
|
||||
0, 0, 10, 10,
|
||||
0, 0, inst, 0)
|
||||
}
|
||||
|
||||
func newsProc(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:
|
||||
flyPaint(hwnd, newsDraw)
|
||||
return 0
|
||||
case wmTimer:
|
||||
newsPoll()
|
||||
return 0
|
||||
case wmSetCursor:
|
||||
if newsHover >= 0 {
|
||||
h, _, _ := pLoadCursor.Call(0, idcHandCursor)
|
||||
pSetCursor.Call(h)
|
||||
return 1
|
||||
}
|
||||
case wmLButtonUp:
|
||||
newsClick(int32(int16(lparam&0xFFFF)), int32(int16(lparam>>16&0xFFFF)))
|
||||
return 0
|
||||
}
|
||||
r, _, _ := pDefWindowProc.Call(hwnd, m, wparam, lparam)
|
||||
return r
|
||||
}
|
||||
|
||||
// newsToggle is what clicking the newspaper does.
|
||||
func newsToggle() {
|
||||
if !showNews {
|
||||
return
|
||||
}
|
||||
if newsShown {
|
||||
newsHide()
|
||||
return
|
||||
}
|
||||
if newsHwnd == 0 {
|
||||
return
|
||||
}
|
||||
previewHide()
|
||||
weatherHide() // one panel at a time
|
||||
sysHide()
|
||||
gmHide()
|
||||
newsRefresh(false)
|
||||
newsShown = true
|
||||
newsPrevBtn = true // ignore a button that is still held from the click that opened this
|
||||
newsHover = -1
|
||||
newsPlace()
|
||||
pSetTimer.Call(newsHwnd, wxPollTimerID, 80, 0)
|
||||
}
|
||||
|
||||
func newsHide() {
|
||||
if !newsShown {
|
||||
return
|
||||
}
|
||||
newsShown = false
|
||||
newsHover = -1
|
||||
pKillTimer.Call(newsHwnd, wxPollTimerID)
|
||||
pShowWindow.Call(newsHwnd, swHide)
|
||||
}
|
||||
|
||||
func newsShutdown() {
|
||||
newsHide()
|
||||
if newsHwnd != 0 {
|
||||
pDestroyWindow.Call(newsHwnd)
|
||||
newsHwnd = 0
|
||||
}
|
||||
}
|
||||
|
||||
// newsVisibleRows is how many headlines fit on the screen, up to ten.
|
||||
func newsVisibleRows(n int) int {
|
||||
_, sh := screenSize()
|
||||
avail := sh - px(90) - px(newsHead+newsFoot)
|
||||
m := int(avail / px(newsRow))
|
||||
m = max(3, min(m, newsMaxRows))
|
||||
return min(n, m)
|
||||
}
|
||||
|
||||
// newsPlace sizes the panel for what it has to show and puts it next to the
|
||||
// newspaper: above a horizontal bar, to the left of a vertical one.
|
||||
func newsPlace() {
|
||||
if newsHwnd == 0 {
|
||||
return
|
||||
}
|
||||
s := newsSnapshot()
|
||||
lh := wxShortH
|
||||
newsVisible = 0
|
||||
if wxZip != "" && len(s.items) > 0 {
|
||||
newsVisible = newsVisibleRows(len(s.items))
|
||||
lh = newsHead + float64(newsVisible)*newsRow + newsFoot
|
||||
}
|
||||
flyPlace(newsHwnd, trayAnchor(trayNews), px(newsW), px(lh))
|
||||
flyRefresh(newsHwnd)
|
||||
}
|
||||
|
||||
// newsDone runs when a download finishes.
|
||||
func newsDone() {
|
||||
if newsShown {
|
||||
newsPlace()
|
||||
}
|
||||
}
|
||||
|
||||
// newsPoll closes the panel when a mouse button goes down anywhere outside it
|
||||
// (the newspaper itself is left alone, so clicking it can close the panel) and
|
||||
// keeps track of which headline the mouse is over.
|
||||
func newsPoll() {
|
||||
l, _, _ := pGetAsyncKeyState.Call(vkLButton)
|
||||
r, _, _ := pGetAsyncKeyState.Call(vkRButton)
|
||||
down := (l|r)&0x8000 != 0
|
||||
var cp point
|
||||
pGetCursorPos.Call(uintptr(unsafe.Pointer(&cp)))
|
||||
var fr rect
|
||||
pGetWindowRect.Call(newsHwnd, uintptr(unsafe.Pointer(&fr)))
|
||||
if down && !newsPrevBtn && !inRect(fr, cp) && !inRect(trayAnchor(trayNews), cp) {
|
||||
newsHide()
|
||||
newsPrevBtn = down
|
||||
return
|
||||
}
|
||||
newsPrevBtn = down
|
||||
|
||||
h := -1
|
||||
if inRect(fr, cp) {
|
||||
h = newsRowAt(cp.X-fr.Left, cp.Y-fr.Top)
|
||||
}
|
||||
if h != newsHover {
|
||||
newsHover = h
|
||||
flyRefresh(newsHwnd)
|
||||
}
|
||||
}
|
||||
|
||||
// newsRowAt is the headline under a spot in the panel (real pixels), or -1.
|
||||
func newsRowAt(x, y int32) int {
|
||||
ry := y - px(newsHead)
|
||||
if ry < 0 || x < 0 {
|
||||
return -1
|
||||
}
|
||||
i := int(ry / px(newsRow))
|
||||
if i >= newsVisible {
|
||||
return -1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// newsClick runs when the panel is clicked: a headline opens in the browser, the
|
||||
// header refreshes, and the messages in an empty panel are buttons.
|
||||
func newsClick(x, y int32) {
|
||||
s := newsSnapshot()
|
||||
switch {
|
||||
case wxZip == "":
|
||||
newsHide()
|
||||
openSettings()
|
||||
return
|
||||
case len(s.items) == 0:
|
||||
if !s.loading {
|
||||
newsRefresh(true)
|
||||
}
|
||||
return
|
||||
case y < px(newsHead):
|
||||
newsRefresh(true)
|
||||
return
|
||||
}
|
||||
if i := newsRowAt(x, y); i >= 0 && i < len(s.items) {
|
||||
link := s.items[i].link
|
||||
newsHide()
|
||||
shellOpen(link, "") // only web links were kept when the feed was read
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- painting ----------
|
||||
|
||||
func newsDraw(hdc uintptr, W, H int32) {
|
||||
u := px
|
||||
flyFrame(hdc, W, H) // outline in the tray icon color; the background is added when painting
|
||||
|
||||
pad := u(14)
|
||||
s := newsSnapshot()
|
||||
line := uintptr(dtLeft | dtVCenter | dtSingleLine | dtEndEllipsis)
|
||||
|
||||
title := "Local news"
|
||||
if s.place != "" && wxZip != "" {
|
||||
title = s.place + " news"
|
||||
}
|
||||
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 == "" || len(s.items) == 0 {
|
||||
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 headlines...", ""
|
||||
default:
|
||||
head, sub = "No news right now", s.err+" Click here to try again."
|
||||
}
|
||||
wxText(hdc, head, rect{pad, u(48), W - pad, u(74)}, fMed, colText, dtCenter|dtVCenter|dtSingleLine)
|
||||
if sub != "" {
|
||||
wxText(hdc, sub, rect{pad, u(78), W - pad, H - pad}, fText, colDim, dtCenter|dtWordBreak)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fillRaw(hdc, rect{pad, u(newsHead) - u(2), W - pad, u(newsHead) - u(1)}, colRule)
|
||||
now := time.Now()
|
||||
for i := 0; i < newsVisible && i < len(s.items); i++ {
|
||||
it := s.items[i]
|
||||
y := u(newsHead) + int32(i)*u(newsRow)
|
||||
head := colText
|
||||
if i == newsHover {
|
||||
fillRaw(hdc, rect{u(6), y, W - u(6), y + u(newsRow)}, rgb(40, 40, 48))
|
||||
head = colAccent
|
||||
}
|
||||
wxText(hdc, it.title, rect{pad, y + u(6), W - pad, y + u(42)}, fBold, head, dtLeft|dtWordBreak|dtEndEllipsis)
|
||||
meta := it.source
|
||||
if age := newsAge(it.when, now); age != "" {
|
||||
if meta != "" {
|
||||
meta += " · "
|
||||
}
|
||||
meta += age
|
||||
}
|
||||
wxText(hdc, meta, rect{pad, y + u(42), W - pad, y + u(56)}, fSmall, colDim, line)
|
||||
if i < newsVisible-1 && i != newsHover && i+1 != newsHover {
|
||||
fillRaw(hdc, rect{pad, y + u(newsRow) - u(1), W - pad, y + u(newsRow)}, colRule)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- shared by both panels ----------
|
||||
|
||||
// trayAnchor is where an icon on the bar sits on the screen.
|
||||
func trayAnchor(kind int) rect {
|
||||
var wr rect
|
||||
pGetWindowRect.Call(hwndMain, uintptr(unsafe.Pointer(&wr)))
|
||||
w, h := clientSize(hwndMain)
|
||||
for _, it := range trayItems(w, h) {
|
||||
if it.kind == kind {
|
||||
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
|
||||
}
|
||||
|
||||
// flyPlace puts a panel of size cw by ch next to an icon: above a horizontal
|
||||
// bar, to the left of a vertical one, and always on the screen.
|
||||
func flyPlace(hwnd uintptr, ar rect, cw, ch int32) {
|
||||
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(hwnd, hwndTopmost, uintptr(x), uintptr(y), uintptr(cw), uintptr(ch), swpNoActivate|swpShowWindow)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user