package main // Pinned apps and the saved icon order. // // A pinned app that has no open window still gets a button (a "launcher", // which is an appWin with hwnd 0). Clicking it starts the program. When the // program opens a window, the window takes over that very spot, and when its // last window closes the launcher comes back in the same place. // // The order of the icons is remembered by program, so it comes back after a // restart no matter which windows happen to be open. import ( "os" "path/filepath" "strings" "unsafe" ) var ( pSHGetFileInfo = shell32.NewProc("SHGetFileInfoW") pinned []string // full paths of pinned programs orderKeys []string // programs in icon order, nearest the center first (lower case) winExe = map[uintptr]string{} exeIcons = map[string]uintptr{} ) const ( menuPinID = 40 menuUnpinID = 41 menuNotifyID = 42 menuLaunchID = 43 shgfiIcon = 0x100 shgfiSmallIcon = 0x1 vkN = 0x4E ) type shFileInfo struct { Icon uintptr IIcon int32 Attrs uint32 Display [260]uint16 Type [80]uint16 } func pinnedFile() string { return filepath.Join(dataDir(), "pinned.txt") } func orderFile() string { return filepath.Join(dataDir(), "order.txt") } func readLines(path string) []string { b, err := os.ReadFile(path) if err != nil { return nil } var out []string for _, l := range strings.Split(string(b), "\n") { if l = strings.TrimSpace(l); l != "" { out = append(out, l) } } return out } func writeLines(path string, lines []string) { _ = os.MkdirAll(dataDir(), 0o755) _ = os.WriteFile(path, []byte(strings.Join(lines, "\n")+"\n"), 0o644) } func loadPinned() { pinned = readLines(pinnedFile()) } func savePinned() { writeLines(pinnedFile(), pinned) } func loadOrderKeys() { orderKeys = readLines(orderFile()) } func isPinned(exe string) bool { if exe == "" { return false } for _, p := range pinned { if strings.EqualFold(p, exe) { return true } } return false } func pinApp(exe string) { if exe == "" || isPinned(exe) { return } pinned = append(pinned, exe) savePinned() saveOrder() } func unpinApp(exe string) { var keep []string for _, p := range pinned { if !strings.EqualFold(p, exe) { keep = append(keep, p) } } pinned = keep savePinned() saveOrder() } // canPin is false for Store and other packaged apps: their programs can't be // started again from a plain path. func canPin(exe string) bool { return exe != "" && !strings.Contains(strings.ToLower(exe), `\windowsapps\`) } // ---------- order ---------- func orderRank(exe string) int { if exe == "" { return -1 } for i, k := range orderKeys { if k == strings.ToLower(exe) { return i } } return -1 } // saveOrder remembers the current icon order. Programs that aren't open right // now keep their place relative to their old neighbors. func saveOrder() { var cur []string have := map[string]bool{} for _, a := range apps { k := strings.ToLower(a.exe) if k == "" || have[k] { continue } have[k] = true cur = append(cur, k) } res := cur for i, k := range orderKeys { if have[k] { continue } pos := 0 for j := i - 1; j >= 0; j-- { if p := indexOf(res, orderKeys[j]); p >= 0 { pos = p + 1 break } } res = append(res[:pos], append([]string{k}, res[pos:]...)...) have[k] = true } if len(res) > 200 { res = res[:200] } orderKeys = res writeLines(orderFile(), orderKeys) } func indexOf(list []string, s string) int { for i, v := range list { if v == s { return i } } return -1 } func insertAt(list []appWin, i int, a appWin) []appWin { list = append(list, appWin{}) copy(list[i+1:], list[i:]) list[i] = a return list } // addApp puts a new button into the list: into the spot of a waiting launcher // for the same program, else beside the program's other windows, else where // the saved order says, else at the far end. func addApp(list []appWin, f appWin) []appWin { if f.exe != "" { if f.hwnd != 0 { for i, a := range list { if a.hwnd == 0 && strings.EqualFold(a.exe, f.exe) { list[i] = f return list } } } last := -1 for i, a := range list { if a.hwnd != 0 && strings.EqualFold(a.exe, f.exe) { last = i } } if last >= 0 { return insertAt(list, last+1, f) } if r := orderRank(f.exe); r >= 0 { for i, a := range list { if ra := orderRank(a.exe); ra < 0 || ra > r { return insertAt(list, i, f) } } } } return append(list, f) } // ---------- launchers and icons ---------- func launcher(exe string) appWin { name := filepath.Base(exe) name = strings.TrimSuffix(name, filepath.Ext(name)) return appWin{title: name, exe: exe, icon: exeIcon(exe)} } // exeIcon is the small icon of a program file, fetched once. func exeIcon(path string) uintptr { key := strings.ToLower(path) if ic, ok := exeIcons[key]; ok { return ic } var fi shFileInfo r, _, _ := pSHGetFileInfo.Call(uintptr(unsafe.Pointer(u16(path))), 0, uintptr(unsafe.Pointer(&fi)), unsafe.Sizeof(fi), shgfiIcon|shgfiSmallIcon) var ic uintptr if r != 0 { ic = fi.Icon } exeIcons[key] = ic return ic } // refreshApps re-reads the open windows and merges them with the pinned // programs, keeping every button where it was. Returns true if anything changed. func refreshApps(refreshIcons bool) bool { enumFound = enumFound[:0] pEnumWindows.Call(enumCb, 0) present := make(map[uintptr]appWin, len(enumFound)) live := map[string]int{} // open windows per program for _, f := range enumFound { if _, ok := winExe[f.hwnd]; !ok { winExe[f.hwnd] = windowExePath(f.hwnd) } f.exe = winExe[f.hwnd] present[f.hwnd] = f if f.exe != "" { live[strings.ToLower(f.exe)]++ } } for h := range winExe { if _, ok := present[h]; !ok { delete(winExe, h) } } var next []appWin for _, a := range apps { if a.hwnd == 0 { next = append(next, a) // a waiting launcher; sorted out below continue } if f, ok := present[a.hwnd]; ok { a.title, a.exe = f.title, f.exe next = append(next, a) delete(present, a.hwnd) } else if isPinned(a.exe) && live[strings.ToLower(a.exe)] == 0 { // The last window of a pinned program closed: leave a launcher in its place. next = append(next, launcher(a.exe)) } } for _, f := range enumFound { if _, ok := present[f.hwnd]; ok { next = addApp(next, f) } } // Launchers for pinned programs that have neither a window nor a launcher yet. have := map[string]bool{} for _, a := range next { if a.exe != "" { have[strings.ToLower(a.exe)] = true } } for _, p := range pinned { if !have[strings.ToLower(p)] { next = addApp(next, launcher(p)) have[strings.ToLower(p)] = true } } // Drop launchers that are no longer needed. var tidy []appWin seen := map[string]bool{} for _, a := range next { if a.hwnd == 0 { k := strings.ToLower(a.exe) if !isPinned(a.exe) || live[k] > 0 || seen[k] { continue } seen[k] = true } tidy = append(tidy, a) } next = tidy for i := range next { if next[i].hwnd == 0 { if next[i].icon == 0 { next[i].icon = exeIcon(next[i].exe) } continue } if next[i].icon == 0 || refreshIcons { if ic := fetchIcon(next[i].hwnd); ic != 0 { next[i].icon = ic } } } // Titles change constantly (browser tabs, documents) but nothing on the // bar shows them, so only a change in which buttons exist, or their icons, // counts as a change. changed := len(next) != len(apps) if !changed { for i := range next { if next[i].hwnd != apps[i].hwnd || next[i].icon != apps[i].icon { changed = true break } } } apps = next return changed } // ---------- notification center ---------- // openNotifications presses Win+N, which opens the notification center and calendar. func openNotifications() { pKeybdEvent.Call(vkLWin, 0, 0, 0) pKeybdEvent.Call(vkN, 0, 0, 0) pKeybdEvent.Call(vkN, 0, keyUp, 0) pKeybdEvent.Call(vkLWin, 0, keyUp, 0) }