diff --git a/README.md b/README.md index 48f5e17..05e0374 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ The real taskbar is hidden and a see-through pill takes its place. The pill is a - Rest the mouse on an app icon for a moment and a live preview of that window pops up beside the bar (minimized windows show their icon instead). Click the preview to switch to that window. - Right-click an app icon and choose "Pin to MirrorBar" to keep it on the bar. A pinned app that isn't running shows as a launcher: click it to start the program, and when its window opens it takes that spot. Pinning works for normal programs; Store apps can't be pinned. - "Open notification center" is in the right-click menu. +- Drag files from Explorer onto an app icon and that program opens them, running or pinned. +- "MirrorBar settings..." in the right-click menu opens one window for background opacity, the three colors, icon style, vertical mode, start with Windows, and resetting the position and width. Changes apply as you make them. - Smooth, outline-free icons and text, even when the pill is see-through. - Pick the color of the Windows logo, the clock, and the battery/volume/wifi/arrow icons from the right-click menu (white, blue, sky blue, green, yellow, orange, red, pink, purple, black). - "Icon style" in the right-click menu: colorized or grayscale. It applies to the app icons and to the logo, clock and tray icons. diff --git a/drop.go b/drop.go new file mode 100644 index 0000000..d93210d --- /dev/null +++ b/drop.go @@ -0,0 +1,68 @@ +package main + +// Dropping files on an app icon: drag a document (or several) from Explorer +// onto an icon on the bar and that program opens them, like the real taskbar +// does. Works for running programs and for pinned ones that are closed. + +import ( + "strings" + "syscall" + "unsafe" +) + +const wmDropFiles = 0x0233 + +var ( + pDragAcceptFiles = shell32.NewProc("DragAcceptFiles") + pDragQueryFile = shell32.NewProc("DragQueryFileW") + pDragQueryPoint = shell32.NewProc("DragQueryPoint") + pDragFinish = shell32.NewProc("DragFinish") +) + +func enableFileDrop(hwnd uintptr) { pDragAcceptFiles.Call(hwnd, 1) } + +// dropFiles handles WM_DROPFILES: work out which icon the files landed on and +// start that program with the file names. +func dropFiles(hwnd, hdrop uintptr) { + defer pDragFinish.Call(hdrop) + + var pt point + pDragQueryPoint.Call(hdrop, uintptr(unsafe.Pointer(&pt))) + x := pt.X + if vertical { + x = pt.Y + } + idx := appIndexAt(hwnd, x) + if idx < 0 || idx >= len(apps) { + return // dropped on the Start button, the clock or empty bar + } + exe := apps[idx].exe + if exe == "" && apps[idx].hwnd != 0 { + exe = windowExePath(apps[idx].hwnd) + } + if exe == "" { + return // Store apps can't be started with a file this way + } + + count, _, _ := pDragQueryFile.Call(hdrop, 0xFFFFFFFF, 0, 0) + var params []string + total := 0 + for i := uintptr(0); i < count; i++ { + n, _, _ := pDragQueryFile.Call(hdrop, i, 0, 0) + if n == 0 { + continue + } + buf := make([]uint16, n+1) + pDragQueryFile.Call(hdrop, i, uintptr(unsafe.Pointer(&buf[0])), n+1) + p := `"` + syscall.UTF16ToString(buf) + `"` + if total+len(p) > 30000 { // a command line can't be longer than about 32,000 characters + break + } + total += len(p) + 1 + params = append(params, p) + } + if len(params) == 0 { + return + } + shellOpen(exe, strings.Join(params, " ")) +} diff --git a/main.go b/main.go index 1f1f0a3..f9cc1a5 100644 --- a/main.go +++ b/main.go @@ -859,6 +859,7 @@ func showMenu(hwnd uintptr, appIdx int, trayKind int) { addTrayMenu(add, sep, trayKind) } + add(0, menuSettingsID, "MirrorBar settings...") add(0, menuNotifyID, "Open notification center") sep() for i, l := range alphaLevels { @@ -934,6 +935,8 @@ func showMenu(hwnd uintptr, appIdx int, trayKind int) { unpinApp(exe) refreshApps(false) redraw(hwnd) + case cmd == menuSettingsID: + openSettings() case cmd == menuNotifyID: openNotifications() case cmd == menuFileLocID && exe != "": @@ -950,6 +953,7 @@ func showMenu(hwnd uintptr, appIdx int, trayKind int) { saveAlpha() redraw(hwnd) } + settingsSync() } func wndProc(hwnd, m, wparam, lparam uintptr) (result uintptr) { @@ -1120,6 +1124,9 @@ func wndProc(hwnd, m, wparam, lparam uintptr) (result uintptr) { showMenu(hwnd, appIndexAt(hwnd, x), trayAt(hwnd, x)) previewBlock(false) return 0 + case wmDropFiles: + dropFiles(hwnd, wparam) + return 0 case wmMouseWheel: // Scrolling over the volume icon changes the volume. The wheel // position arrives in screen coordinates, so make it relative to the bar. @@ -1290,6 +1297,7 @@ func main() { } hwndMain = hwnd previewInit(inst) + enableFileDrop(hwnd) refreshAutostartPath() // Hotkey: Ctrl+Alt+Q exits and restores. @@ -1324,6 +1332,11 @@ func main() { if int32(r) <= 0 { break } + if hwndSettings != 0 { + if handled, _, _ := pIsDialogMessage.Call(hwndSettings, uintptr(unsafe.Pointer(&m))); handled != 0 { + continue + } + } pTranslateMessage.Call(uintptr(unsafe.Pointer(&m))) pDispatchMessage.Call(uintptr(unsafe.Pointer(&m))) } diff --git a/settings.go b/settings.go new file mode 100644 index 0000000..4d4855f --- /dev/null +++ b/settings.go @@ -0,0 +1,286 @@ +package main + +// The settings window: everything the right-click menu can change, in one +// place. It opens from "MirrorBar settings..." in the menu. Changes apply the +// moment you make them, and the window stays in step if the menu is used too. + +import ( + "strconv" + "syscall" + "unsafe" +) + +var ( + pSendMessage = user32.NewProc("SendMessageW") + pSetWindowText = user32.NewProc("SetWindowTextW") + pIsDialogMessage = user32.NewProc("IsDialogMessageW") + pAdjustWindowRectEx = user32.NewProc("AdjustWindowRectEx") + pInitCommonControls = syscall.NewLazyDLL("comctl32.dll").NewProc("InitCommonControlsEx") +) + +const ( + menuSettingsID = 45 + + wmCommand = 0x0111 + wmHScroll = 0x0114 + wmSetFont = 0x0030 + + wsChild = 0x40000000 + wsTabStop = 0x00010000 + wsVScroll = 0x00200000 + wsWindowFrame = 0x00C80000 // caption, system menu, border: a plain window that can't be resized + bsPushButton = 0x0000 + bsDefPushButton = 0x0001 + bsAutoCheckBox = 0x0003 + cbsDropDownList = 0x0003 + tbsNoTicks = 0x0010 + + bmGetCheck = 0x00F0 + bmSetCheck = 0x00F1 + cbAddString = 0x0143 + cbGetCurSel = 0x0147 + cbSetCurSel = 0x014E + tbmGetPos = 0x0400 + tbmSetPos = 0x0405 + tbmSetRange = 0x0406 + + cbnSelChange = 1 + tbEndTrack = 8 + colorBtnFace = 15 + + idAlpha = 200 + idAlphaValue = 201 + idLogo = 202 + idClock = 203 + idTrayIcons = 204 + idStyle = 205 + idVertical = 206 + idAutostart = 207 + idResetPos = 208 + idResetWidth = 209 + idClose = 210 +) + +var ( + hwndSettings uintptr + settingsClass bool + settingsFont uintptr + + ctlAlpha, ctlAlphaValue, ctlLogo, ctlClock, ctlTrayIcons, ctlStyle uintptr + ctlVertical, ctlAutostart uintptr +) + +func boolWP(b bool) uintptr { + if b { + return 1 + } + return 0 +} + +// makeControl creates one child control; sizes are in 96 dpi units. +func makeControl(parent, inst uintptr, class, text string, style uintptr, x, y, w, h int, id uintptr) uintptr { + hw, _, _ := pCreateWindowEx.Call(0, + uintptr(unsafe.Pointer(u16(class))), uintptr(unsafe.Pointer(u16(text))), + style|wsChild|wsVisible, + uintptr(px(float64(x))), uintptr(px(float64(y))), uintptr(px(float64(w))), uintptr(px(float64(h))), + parent, id, inst, 0) + if hw != 0 && settingsFont != 0 { + pSendMessage.Call(hw, wmSetFont, settingsFont, 1) + } + return hw +} + +func openSettings() { + if hwndSettings != 0 { + pShowWindow.Call(hwndSettings, swRestore) + pSetForegroundWindow.Call(hwndSettings) + settingsSync() + return + } + inst, _, _ := pGetModuleHandle.Call(0) + className := u16("MirrorBarSettings") + if !settingsClass { + cursor, _, _ := pLoadCursor.Call(0, idcArrow) + wc := wndClassEx{ + WndProc: syscall.NewCallback(settingsProc), + Instance: inst, + Cursor: cursor, + Background: colorBtnFace + 1, + ClassName: className, + } + wc.Size = uint32(unsafe.Sizeof(wc)) + pRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc))) + icc := struct{ Size, Classes uint32 }{8, 0x4} // ICC_BAR_CLASSES: the slider + pInitCommonControls.Call(uintptr(unsafe.Pointer(&icc))) + settingsClass = true + } + if settingsFont == 0 { + settingsFont = makeFont(px(13), 400) + } + + r := rect{0, 0, px(400), px(330)} + pAdjustWindowRectEx.Call(uintptr(unsafe.Pointer(&r)), wsWindowFrame, 0, 0) + ow, oh := r.Right-r.Left, r.Bottom-r.Top + sw, sh := screenSize() + hw, _, _ := pCreateWindowEx.Call(0, + uintptr(unsafe.Pointer(className)), uintptr(unsafe.Pointer(u16("MirrorBar settings"))), + wsWindowFrame, + uintptr(max(0, (sw-ow)/2)), uintptr(max(0, (sh-oh)/2)), uintptr(ow), uintptr(oh), + 0, 0, inst, 0) + if hw == 0 { + return + } + + label := func(text string, y int) { + makeControl(hw, inst, "STATIC", text, 0, 16, y+4, 150, 20, 0) + } + combo := func(y int, id uintptr) uintptr { + return makeControl(hw, inst, "COMBOBOX", "", cbsDropDownList|wsVScroll|wsTabStop, 170, y, 214, 200, id) + } + + label("Background opacity", 16) + ctlAlpha = makeControl(hw, inst, "msctls_trackbar32", "", tbsNoTicks|wsTabStop, 166, 14, 170, 28, idAlpha) + ctlAlphaValue = makeControl(hw, inst, "STATIC", "", 0, 344, 20, 44, 20, idAlphaValue) + pSendMessage.Call(ctlAlpha, tbmSetRange, 1, uintptr(3|255<<16)) + + label("Windows logo color", 60) + ctlLogo = combo(60, idLogo) + label("Clock color", 96) + ctlClock = combo(96, idClock) + label("Tray icon color", 132) + ctlTrayIcons = combo(132, idTrayIcons) + label("Icon style", 168) + ctlStyle = combo(168, idStyle) + for _, c := range []uintptr{ctlLogo, ctlClock, ctlTrayIcons} { + for _, p := range palette { + pSendMessage.Call(c, cbAddString, 0, uintptr(unsafe.Pointer(u16(p.name)))) + } + } + for _, n := range styleNames { + pSendMessage.Call(ctlStyle, cbAddString, 0, uintptr(unsafe.Pointer(u16(n)))) + } + + ctlVertical = makeControl(hw, inst, "BUTTON", "Stand the bar up at the right edge (vertical)", bsAutoCheckBox|wsTabStop, 16, 214, 368, 22, idVertical) + ctlAutostart = makeControl(hw, inst, "BUTTON", "Start MirrorBar when I sign in", bsAutoCheckBox|wsTabStop, 16, 242, 368, 22, idAutostart) + + makeControl(hw, inst, "BUTTON", "Reset position", bsPushButton|wsTabStop, 16, 284, 116, 28, idResetPos) + makeControl(hw, inst, "BUTTON", "Reset bar width", bsPushButton|wsTabStop, 140, 284, 116, 28, idResetWidth) + makeControl(hw, inst, "BUTTON", "Close", bsDefPushButton|wsTabStop, 292, 284, 92, 28, idClose) + + hwndSettings = hw + settingsSync() + pShowWindow.Call(hw, 1) + pSetForegroundWindow.Call(hw) +} + +// settingsSync makes every control show the current setting. +func settingsSync() { + if hwndSettings == 0 { + return + } + pSendMessage.Call(ctlAlpha, tbmSetPos, 1, uintptr(bgAlpha)) + pSetWindowText.Call(ctlAlphaValue, uintptr(unsafe.Pointer(u16(strconv.Itoa(bgAlpha*100/255)+"%")))) + pSendMessage.Call(ctlLogo, cbSetCurSel, uintptr(logoIdx), 0) + pSendMessage.Call(ctlClock, cbSetCurSel, uintptr(clockIdx), 0) + pSendMessage.Call(ctlTrayIcons, cbSetCurSel, uintptr(iconIdx), 0) + pSendMessage.Call(ctlStyle, cbSetCurSel, uintptr(iconMode), 0) + pSendMessage.Call(ctlVertical, bmSetCheck, boolWP(vertical), 0) + pSendMessage.Call(ctlAutostart, bmSetCheck, boolWP(autostartEnabled()), 0) +} + +func comboChoice(c uintptr) int { + r, _, _ := pSendMessage.Call(c, cbGetCurSel, 0, 0) + return int(int32(r)) +} + +func checked(c uintptr) bool { + r, _, _ := pSendMessage.Call(c, bmGetCheck, 0, 0) + return r == 1 +} + +func settingsCommand(id, note uintptr) { + switch id { + case 1, 2: // Enter and Esc + pDestroyWindow.Call(hwndSettings) + case idClose: + pDestroyWindow.Call(hwndSettings) + case idLogo, idClock, idTrayIcons: + if note != cbnSelChange { + return + } + switch id { + case idLogo: + if i := comboChoice(ctlLogo); i >= 0 { + logoIdx = i + } + case idClock: + if i := comboChoice(ctlClock); i >= 0 { + clockIdx = i + } + case idTrayIcons: + if i := comboChoice(ctlTrayIcons); i >= 0 { + iconIdx = i + } + } + saveColors() + redraw(hwndMain) + case idStyle: + if note != cbnSelChange { + return + } + if i := comboChoice(ctlStyle); i >= 0 && i < len(styleNames) { + iconMode = i + saveIconMode() + styleCacheClear() + redraw(hwndMain) + } + case idVertical: + vertical = checked(ctlVertical) + saveOrient() + resetPos(hwndMain) + case idAutostart: + setAutostart(checked(ctlAutostart)) + settingsSync() + case idResetPos: + resetPos(hwndMain) + case idResetWidth: + leftPref = 0 + saveLeftPref() + redraw(hwndMain) + } +} + +func settingsProc(hwnd, m, wparam, lparam uintptr) (result uintptr) { + defer func() { + if r := recover(); r != nil { + result = 0 + } + }() + switch uint32(m) { + case wmCommand: + settingsCommand(wparam&0xFFFF, wparam>>16&0xFFFF) + return 0 + case wmHScroll: + if lparam == ctlAlpha && ctlAlpha != 0 { + pos, _, _ := pSendMessage.Call(ctlAlpha, tbmGetPos, 0, 0) + n := int(pos) + if n >= 3 && n <= 255 && n != bgAlpha { + bgAlpha = n + pSetWindowText.Call(ctlAlphaValue, uintptr(unsafe.Pointer(u16(strconv.Itoa(bgAlpha*100/255)+"%")))) + redraw(hwndMain) + } + if wparam&0xFFFF == tbEndTrack { + saveAlpha() + } + } + return 0 + case wmClose: + pDestroyWindow.Call(hwnd) + return 0 + case wmDestroy: + hwndSettings = 0 + return 0 + } + r, _, _ := pDefWindowProc.Call(hwnd, m, wparam, lparam) + return r +}