From af39c861e83aa33a3faef5935beb809623667fbd Mon Sep 17 00:00:00 2001 From: YXC0915 Date: Mon, 30 Nov 2026 00:21:15 +0800 Subject: [PATCH] feat(gui): refine DPI-aware layout and typography in the VM guide --- internal/installguide/appearance_windows.go | 978 ++++++++++++++---- internal/installguide/dialog_windows.go | 167 +-- internal/installguide/overlay_windows.go | 13 +- internal/installguide/overlay_windows_test.go | 166 ++- 4 files changed, 923 insertions(+), 401 deletions(-) diff --git a/internal/installguide/appearance_windows.go b/internal/installguide/appearance_windows.go index 450428f..2588e88 100644 --- a/internal/installguide/appearance_windows.go +++ b/internal/installguide/appearance_windows.go @@ -2,8 +2,10 @@ package installguide import ( "math" + "strings" "sync" "syscall" + "unicode/utf16" "unsafe" "golang.org/x/sys/windows/registry" @@ -15,17 +17,18 @@ var ( comctl32 = syscall.NewLazyDLL("comctl32.dll") kernel32 = syscall.NewLazyDLL("kernel32.dll") - selectGDI = gdi.NewProc("SelectObject") - stockObject = gdi.NewProc("GetStockObject") - deleteGDI = gdi.NewProc("DeleteObject") - brushColor = gdi.NewProc("SetDCBrushColor") - textColor = gdi.NewProc("SetTextColor") - bkMode = gdi.NewProc("SetBkMode") - setDIBitsToDevice = gdi.NewProc("SetDIBitsToDevice") - getObjectW = gdi.NewProc("GetObjectW") - createFontIndirectW = gdi.NewProc("CreateFontIndirectW") - createRoundRectRgn = gdi.NewProc("CreateRoundRectRgn") - gdiFlush = gdi.NewProc("GdiFlush") + selectGDI = gdi.NewProc("SelectObject") + stockObject = gdi.NewProc("GetStockObject") + deleteGDI = gdi.NewProc("DeleteObject") + brushColor = gdi.NewProc("SetDCBrushColor") + textColor = gdi.NewProc("SetTextColor") + bkMode = gdi.NewProc("SetBkMode") + setDIBitsToDevice = gdi.NewProc("SetDIBitsToDevice") + createFontIndirectW = gdi.NewProc("CreateFontIndirectW") + createRoundRectRgn = gdi.NewProc("CreateRoundRectRgn") + enumFontFamiliesExW = gdi.NewProc("EnumFontFamiliesExW") + getTextExtentExPointW = gdi.NewProc("GetTextExtentExPointW") + gdiFlush = gdi.NewProc("GdiFlush") drawText = user32.NewProc("DrawTextW") sendMessage = user32.NewProc("SendMessageW") @@ -40,13 +43,13 @@ var ( trackMouseEvent = user32.NewProc("TrackMouseEvent") loadCursorW = user32.NewProc("LoadCursorW") setCursor = user32.NewProc("SetCursor") - mapDialogRect = user32.NewProc("MapDialogRect") setLayeredAttr = user32.NewProc("SetLayeredWindowAttributes") setWindowRgn = user32.NewProc("SetWindowRgn") beginPaint = user32.NewProc("BeginPaint") endPaint = user32.NewProc("EndPaint") getDC = user32.NewProc("GetDC") releaseDC = user32.NewProc("ReleaseDC") + getDpiForWindow = user32.NewProc("GetDpiForWindow") dwmSetWindowAttr = dwmapi.NewProc("DwmSetWindowAttribute") setWindowSubclass = comctl32.NewProc("SetWindowSubclass") removeSubclass = comctl32.NewProc("RemoveWindowSubclass") @@ -54,10 +57,7 @@ var ( rtlMoveMemory = kernel32.NewProc("RtlMoveMemory") ) -const ( - panelAlpha = 242 // ~95% opacity, matching --surface-2 rgba(..., 0.94) - linkPadDLU = 4 // symmetric horizontal padding for secondary text link hover pill -) +const panelAlpha = 244 // ~95.7% opacity, matching --surface-2 rgba(..., 0.94) type rgb struct{ r, g, b uint8 } @@ -80,10 +80,13 @@ func blendRGB(bg, fg rgb, alpha float64) rgb { } } -// palette mirrors frontend/src/styles/tokens.css (Light & Dark). +// palette mirrors frontend/src/styles/tokens.css (Light & Dark), pre-composited +// over --surface-2 so native controls match the main Reims-Go window verbatim. type palette struct { surface rgb cardBorder rgb + topHighlight rgb + divider rgb text rgb text2 rgb text3 rgb @@ -93,11 +96,11 @@ type palette struct { accentHighlight rgb accentLink rgb accentSoft rgb - accentLinkHover rgb btnFill rgb btnHover rgb btnPress rgb btnBorder rgb + btnHighlight rgb ghostHover rgb ghostPress rgb codeFill rgb @@ -113,25 +116,27 @@ type palette struct { var lightPalette = palette{ surface: rgb{255, 255, 255}, // --surface-2 - cardBorder: rgb{214, 220, 229}, // ~rgba(15,23,42,0.14) + cardBorder: rgb{224, 228, 234}, // --border (~rgba(15,23,42,0.11)) + topHighlight: rgb{255, 255, 255}, // --highlight-top + divider: rgb{235, 237, 240}, // --border (rgba(15,23,42,0.08)) text: rgb{29, 29, 31}, // --text #1d1d1f text2: rgb{66, 66, 71}, // --text-2 #424247 text3: rgb{110, 110, 115}, // --text-3 #6e6e73 accent: rgb{0, 122, 255}, // --accent #007aff accentHover: rgb{0, 107, 230}, // --accent-hover #006be6 accentPress: rgb{0, 94, 196}, - accentHighlight: rgb{56, 151, 255}, // inset top highlight + accentHighlight: rgb{56, 151, 255}, // inset 0 1px 0 rgba(255,255,255,0.22) accentLink: rgb{0, 102, 214}, // --accent-soft-text #0066d6 - accentSoft: rgb{229, 242, 255}, // --accent-soft - accentLinkHover: rgb{236, 245, 255}, + accentSoft: rgb{227, 240, 255}, // --accent-soft btnFill: rgb{255, 255, 255}, // --surface-card-elevated - btnHover: rgb{243, 245, 248}, // --surface-hover - btnPress: rgb{232, 236, 242}, // --surface-active - btnBorder: rgb{207, 213, 223}, // --border-strong - ghostHover: rgb{241, 243, 246}, - ghostPress: rgb{229, 232, 238}, + btnHover: rgb{244, 245, 247}, // --surface-hover + btnPress: rgb{235, 237, 240}, // --surface-active + btnBorder: rgb{218, 221, 227}, // --border-strong + btnHighlight: rgb{255, 255, 255}, + ghostHover: rgb{243, 245, 247}, + ghostPress: rgb{234, 236, 240}, codeFill: rgb{245, 246, 248}, // --surface-1 #f5f6f8 - codeBorder: rgb{226, 229, 236}, // --border + codeBorder: rgb{235, 237, 240}, // --border codeText: rgb{44, 44, 48}, // --text-code #2c2c30 okSoft: rgb{227, 249, 231}, // --ok-soft okText: rgb{24, 140, 44}, // --ok-text #188c2c @@ -142,32 +147,34 @@ var lightPalette = palette{ } var darkPalette = palette{ - surface: rgb{42, 43, 50}, // --surface-2 #2a2b32 - cardBorder: rgb{68, 70, 80}, // ~rgba(255,255,255,0.12) + surface: rgb{36, 37, 43}, // --surface-sidebar / floating dark card #24252b + cardBorder: rgb{56, 57, 65}, // --border rgba(255,255,255,0.09) + topHighlight: rgb{54, 55, 63}, // --highlight-top rgba(255,255,255,0.08) + divider: rgb{52, 53, 61}, // --border rgba(255,255,255,0.08) text: rgb{245, 245, 247}, // --text #f5f5f7 text2: rgb{194, 194, 202}, // --text-2 #c2c2ca text3: rgb{146, 146, 156}, // --text-3 #92929c accent: rgb{10, 132, 255}, // --accent #0a84ff accentHover: rgb{36, 146, 255}, // --accent-hover #2492ff - accentPress: rgb{0, 110, 224}, - accentHighlight: rgb{66, 160, 255}, + accentPress: rgb{0, 112, 224}, + accentHighlight: rgb{64, 159, 255}, // inset 0 1px 0 rgba(255,255,255,0.22) accentLink: rgb{100, 176, 255}, // --accent-soft-text #64b0ff - accentSoft: rgb{34, 58, 88}, // --accent-soft - accentLinkHover: rgb{42, 60, 84}, - btnFill: rgb{54, 56, 66}, // --surface-card-elevated - btnHover: rgb{65, 67, 79}, // --surface-hover - btnPress: rgb{75, 77, 90}, // --surface-active - btnBorder: rgb{83, 86, 100}, // --border-strong - ghostHover: rgb{56, 58, 68}, - ghostPress: rgb{67, 69, 81}, - codeFill: rgb{30, 31, 37}, // --surface-input - codeBorder: rgb{62, 64, 74}, // --border + accentSoft: rgb{30, 55, 84}, // --accent-soft + btnFill: rgb{47, 48, 55}, // --surface-card-elevated #2f3037 + btnHover: rgb{57, 58, 66}, // --surface-hover + btnPress: rgb{66, 68, 77}, // --surface-active + btnBorder: rgb{67, 68, 76}, // --border-strong #43444c + btnHighlight: rgb{59, 60, 68}, // inset 0 1px 0 rgba(255,255,255,0.06) + ghostHover: rgb{51, 52, 60}, // --surface-hover + ghostPress: rgb{61, 62, 71}, // --surface-active + codeFill: rgb{27, 28, 33}, // --surface-0 / --surface-input #1b1c21 + codeBorder: rgb{53, 54, 62}, // --border codeText: rgb{212, 214, 224}, // --text-code #d4d6e0 - okSoft: rgb{35, 65, 49}, // --ok-soft + okSoft: rgb{37, 63, 49}, // --ok-soft okText: rgb{48, 209, 88}, // --ok-text #30d158 - warnSoft: rgb{69, 62, 36}, // --warn-soft + warnSoft: rgb{68, 63, 38}, // --warn-soft warnText: rgb{255, 214, 10}, // --warn-text #ffd60a - dangerSoft: rgb{73, 40, 43}, // --danger-soft + dangerSoft: rgb{68, 41, 46}, // --danger-soft dangerText: rgb{255, 139, 131}, // --danger-text #ff8b83 } @@ -203,31 +210,67 @@ const ( dialogDark = 1 << iota dialogCompact dialogDWM - dialogToneShift = 3 - - // DWLP_USER is the pointer-sized data slot following DWLP_DLGPROC. - dialogUser = 2 * unsafe.Sizeof(uintptr(0)) -) - -var ( - getWindowLongPtr = user32.NewProc("GetWindowLongPtrW") - setWindowLongPtr = user32.NewProc("SetWindowLongPtrW") - hoveredControls sync.Map + dialogZh + dialogConfirm + dialogToneShift = 5 ) type dialogMeta struct { - dark, compact, dwm bool - stageTone tone + dark, compact, dwm, zh, isConfirm bool + stageTone tone +} + +type panelState struct { + meta dialogMeta + dpi int32 + dividerY int32 + extraStacked bool + bodyLines []string + bodyLineH int32 +} + +var ( + panelStatesMu sync.RWMutex + panelStates = map[uintptr]*panelState{} + hoveredControls sync.Map +) + +func getPanelState(hwnd uintptr) *panelState { + panelStatesMu.RLock() + st := panelStates[hwnd] + panelStatesMu.RUnlock() + return st } func getDialogMeta(hwnd uintptr) dialogMeta { - flags, _, _ := getWindowLongPtr.Call(hwnd, dialogUser) - return dialogMeta{ - dark: flags&dialogDark != 0, - compact: flags&dialogCompact != 0, - dwm: flags&dialogDWM != 0, - stageTone: tone(flags >> dialogToneShift), + if st := getPanelState(hwnd); st != nil { + return st.meta } + return dialogMeta{} +} + +func cleanupPanelState(hwnd uintptr) { + panelStatesMu.Lock() + delete(panelStates, hwnd) + panelStatesMu.Unlock() +} + +// DPI scaling helpers (CSS px -> physical window pixels). +func windowDPI(hwnd uintptr) int32 { + if hwnd != 0 && getDpiForWindow.Find() == nil { + if d, _, _ := getDpiForWindow.Call(hwnd); d >= 96 { + return int32(d) + } + } + return 96 +} + +func dp(dpi, px int32) int32 { + return (px*dpi + 48) / 96 +} + +func dpF(dpi int32, px float64) float64 { + return px * float64(dpi) / 96.0 } type drawItem struct { @@ -269,10 +312,128 @@ type bitmapInfoHeader struct { type logFontW struct { height, width, escapement, orientation, weight int32 - attributes [8]byte + italic, underline, strikeOut, charSet byte + outPrecision, clipPrecision, quality, pitchFam byte face [32]uint16 } +type gdiSize struct{ cx, cy int32 } + +type fontKey struct { + dpi int32 + zh bool +} + +type fontSet struct { + title uintptr + body uintptr + btn uintptr + btnBold uintptr + badge uintptr + small uintptr + smallMed uintptr + code uintptr +} + +var ( + fontProbeOnce sync.Once + fontProbeHit bool + fontEnumProc = syscall.NewCallback(func(lpelfe, lpntme, fontType, lparam uintptr) uintptr { + fontProbeHit = true + return 0 + }) + zhSansFace = "Microsoft YaHei UI" + enSansFace = "Segoe UI" + monoFace = "Consolas" + + fontCacheMu sync.Mutex + fontCache = map[fontKey]fontSet{} +) + +func probeFontFamilies() { + fontProbeOnce.Do(func() { + dc, _, _ := getDC.Call(0) + if dc == 0 { + return + } + defer releaseDC.Call(0, dc) + has := func(name string) bool { + var lf logFontW + lf.charSet = 1 // DEFAULT_CHARSET + setFace(&lf, name) + fontProbeHit = false + enumFontFamiliesExW.Call(dc, uintptr(unsafe.Pointer(&lf)), fontEnumProc, 0, 0) + return fontProbeHit + } + // Match frontend/src/styles/tokens.css font priority order. + for _, f := range []string{ + "PingFang SC Medium", "PingFang SC", "HarmonyOS Sans SC", "MiSans", + "Noto Sans SC Medium", "Noto Sans SC", "Source Han Sans CN Medium", "Source Han Sans CN", + "Microsoft YaHei UI", + } { + if has(f) { + zhSansFace = f + break + } + } + for _, f := range []string{"Segoe UI Variable Text", "Segoe UI"} { + if has(f) { + enSansFace = f + break + } + } + for _, f := range []string{"Cascadia Mono", "JetBrains Mono", "Consolas"} { + if has(f) { + monoFace = f + break + } + } + }) +} + +func makeFont(face string, px float64, dpi, weight int32) uintptr { + var lf logFontW + lf.height = -max(1, int32(math.Round(px*float64(dpi)/96.0))) + lf.weight = weight + lf.charSet = 1 // DEFAULT_CHARSET + lf.quality = 5 // CLEARTYPE_QUALITY + setFace(&lf, face) + h, _, _ := createFontIndirectW.Call(uintptr(unsafe.Pointer(&lf))) + return h +} + +func getFonts(dpi int32, zh bool) fontSet { + probeFontFamilies() + key := fontKey{dpi: dpi, zh: zh} + fontCacheMu.Lock() + defer fontCacheMu.Unlock() + if fs, ok := fontCache[key]; ok { + return fs + } + face := enSansFace + medWeight := int32(500) + if zh { + face = zhSansFace + // In GDI, Microsoft YaHei UI jumps straight from Regular (400) to Bold (700) + // at weight 500+; keep normal/secondary labels crisp at 400 and strong at 600. + if face == "Microsoft YaHei UI" { + medWeight = 400 + } + } + fs := fontSet{ + title: makeFont(face, 15, dpi, 600), + body: makeFont(face, 13, dpi, 400), + btn: makeFont(face, 13, dpi, medWeight), + btnBold: makeFont(face, 13, dpi, 600), + badge: makeFont(face, 11.5, dpi, 600), + small: makeFont(face, 12, dpi, 400), + smallMed: makeFont(face, 12, dpi, medWeight), + code: makeFont(monoFace, 12, dpi, 400), + } + fontCache[key] = fs + return fs +} + var btnSubclassProc uintptr func init() { @@ -290,13 +451,11 @@ func init() { if _, wasHovered := hoveredControls.LoadAndDelete(hwnd); wasHovered { invalidateRect.Call(hwnd, 0, 0) } - case 0x0020: // WM_SETCURSOR - if id == idExtra || id == idJump || ref != 0 { - cur, _, _ := loadCursorW.Call(0, 32649) // IDC_HAND - if cur != 0 { - setCursor.Call(cur) - return 1 - } + case 0x0020: // WM_SETCURSOR: pointer cursor on all interactive buttons, matching Reims-Go. + cur, _, _ := loadCursorW.Call(0, 32649) // IDC_HAND + if cur != 0 { + setCursor.Call(cur) + return 1 } case 0x0082: // WM_NCDESTROY hoveredControls.Delete(hwnd) @@ -312,72 +471,403 @@ func stylePanel(hwnd, flags uintptr) { // never shows an unpainted black/white frame during step transitions. setLayeredAttr.Call(hwnd, 0, 0, 2) // LWA_ALPHA - dark := flags&dialogDark != 0 - pal := paletteFor(dark) + meta := dialogMeta{ + dark: flags&dialogDark != 0, + compact: flags&dialogCompact != 0, + zh: flags&dialogZh != 0, + isConfirm: flags&dialogConfirm != 0, + stageTone: tone(flags >> dialogToneShift), + } + pal := paletteFor(meta.dark) - // Prefer Windows 11 DWM smooth rounded corners + native drop shadow + dark hint. + // Prefer Windows 11 DWM smooth rounded corners + native drop shadow + single 1px border. var darkMode int32 - if dark { + if meta.dark { darkMode = 1 } dwmSetWindowAttr.Call(hwnd, 20, uintptr(unsafe.Pointer(&darkMode)), unsafe.Sizeof(darkMode)) // DWMWA_USE_IMMERSIVE_DARK_MODE - corner := uint32(2) // DWMWCP_ROUND (8 DIPs, matching panelRadius) + corner := uint32(2) // DWMWCP_ROUND (8 DIPs) hr, _, _ := dwmSetWindowAttr.Call(hwnd, 33, uintptr(unsafe.Pointer(&corner)), unsafe.Sizeof(corner)) // DWMWA_WINDOW_CORNER_PREFERENCE if hr == 0 { - flags |= dialogDWM + meta.dwm = true border := uint32(pal.cardBorder.colorref()) dwmSetWindowAttr.Call(hwnd, 34, uintptr(unsafe.Pointer(&border)), unsafe.Sizeof(border)) // DWMWA_BORDER_COLOR } - setWindowLongPtr.Call(hwnd, dialogUser, flags) - if hr != 0 { - roundPanel(hwnd) - } - // Shrink link buttons to hug their text symmetrically so hover pills are - // centered around the label and empty space to the right is not clickable. - for _, id := range []uintptr{idExtra, idJump} { - if ctrl, _, _ := getDlgItem.Call(hwnd, id); ctrl != 0 { - fitLinkControl(hwnd, ctrl) - } + panelStatesMu.Lock() + panelStates[hwnd] = &panelState{meta: meta, dpi: windowDPI(hwnd)} + panelStatesMu.Unlock() + + layoutPanel(hwnd) + if !meta.dwm { + roundPanel(hwnd) } for _, id := range []uintptr{idFold, idLeft, idRight, idExtra, idJump} { if ctrl, _, _ := getDlgItem.Call(hwnd, id); ctrl != 0 { - setWindowSubclass.Call(ctrl, btnSubclassProc, id, flags&dialogCompact) + setWindowSubclass.Call(ctrl, btnSubclassProc, id, 0) } } } -func fitLinkControl(hwnd, ctrl uintptr) { - var caption [256]uint16 - n, _, _ := getWindowTextW.Call(ctrl, uintptr(unsafe.Pointer(&caption[0])), uintptr(len(caption))) +func controlText(ctrl uintptr) string { + if ctrl == 0 { + return "" + } + var buf [512]uint16 + n, _, _ := getWindowTextW.Call(ctrl, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf))) if n == 0 { + return "" + } + return syscall.UTF16ToString(buf[:n]) +} + +func measureLineWidth(dc, font uintptr, text string) int32 { + if text == "" { + return 0 + } + old, _, _ := selectGDI.Call(dc, font) + defer selectGDI.Call(dc, old) + u16 := utf16.Encode([]rune(text)) + if len(u16) == 0 { + return 0 + } + var sz gdiSize + var fit int32 + getTextExtentExPointW.Call( + dc, + uintptr(unsafe.Pointer(&u16[0])), + uintptr(len(u16)), + 0x7fffffff, + uintptr(unsafe.Pointer(&fit)), + 0, + uintptr(unsafe.Pointer(&sz)), + ) + return sz.cx +} + +func isASCIIWord(u uint16) bool { + return (u >= 'a' && u <= 'z') || (u >= 'A' && u <= 'Z') || (u >= '0' && u <= '9') || u == '_' || u == '\'' || u == '’' +} + +func isClosingPunct(u uint16) bool { + switch u { + case '.', ',', ';', ':', '?', '!', ')', ']', '}', + '。', ',', '、', ';', ':', '?', '!', ')', '」', '』', '》', '”', '…': + return true + } + return false +} + +func isOpeningPunct(u uint16) bool { + switch u { + case '(', '[', '{', '(', '「', '『', '《', '“': + return true + } + return false +} + +// wrapLines splits text into visual lines fitting within maxW pixels using GDI +// font metrics, respecting Latin word boundaries and CJK punctuation rules. +func wrapLines(dc, font uintptr, text string, maxW int32, maxLines int) []string { + if text == "" || maxW <= 0 { + return nil + } + old, _, _ := selectGDI.Call(dc, font) + defer selectGDI.Call(dc, old) + + var out []string + for _, para := range strings.Split(text, "\n") { + u16 := utf16.Encode([]rune(strings.TrimSpace(para))) + if len(u16) == 0 { + continue + } + for len(u16) > 0 { + var fit int32 + var sz gdiSize + getTextExtentExPointW.Call( + dc, + uintptr(unsafe.Pointer(&u16[0])), + uintptr(len(u16)), + uintptr(maxW), + uintptr(unsafe.Pointer(&fit)), + 0, + uintptr(unsafe.Pointer(&sz)), + ) + if int(fit) >= len(u16) { + out = append(out, string(utf16.Decode(u16))) + break + } + cut := int(fit) + if cut <= 0 { + cut = 1 + } + // Never split a UTF-16 surrogate pair. + if cut < len(u16) && u16[cut-1] >= 0xD800 && u16[cut-1] <= 0xDBFF { + if cut > 1 { + cut-- + } else { + cut++ + } + } + if cut < len(u16) && u16[cut] != ' ' { + // Avoid breaking inside an ASCII/Latin word, or splitting an ASCII token when + // pulling closing punctuation onto the next line (e.g. "1129」。"). + if (isASCIIWord(u16[cut-1]) && isASCIIWord(u16[cut])) || + (isClosingPunct(u16[cut]) && isASCIIWord(u16[cut-1])) { + for i := cut - 1; i > 0; i-- { + c := u16[i] + if c == ' ' { + cut = i + break + } + if c == '/' || c == '\\' || c == '-' || c == '·' || c == '›' || c > 0x7f { + cut = i + 1 + break + } + } + } else if cut > 1 && isClosingPunct(u16[cut]) && !isOpeningPunct(u16[cut-1]) { + // Avoid orphan closing punctuation at the start of the next line. + cut-- + } else if cut > 1 && isOpeningPunct(u16[cut-1]) { + // Avoid dangling opening punctuation at the end of the current line. + cut-- + } + } + line := strings.TrimRight(string(utf16.Decode(u16[:cut])), " ") + out = append(out, line) + u16 = u16[cut:] + for len(u16) > 0 && u16[0] == ' ' { + u16 = u16[1:] + } + if maxLines > 0 && len(out) >= maxLines && len(u16) > 0 { + out[len(out)-1] = strings.TrimRight(out[len(out)-1], " ") + "…" + return out + } + } + } + return out +} + +func moveControl(ctrl uintptr, x, y, w, h int32) { + if ctrl == 0 { return } - var avail rect - getClientRect.Call(ctrl, uintptr(unsafe.Pointer(&avail))) - if avail.right <= 0 || avail.bottom <= 0 { + // SWP_NOZORDER | SWP_NOACTIVATE + setWindowPos.Call(ctrl, 0, uintptr(x), uintptr(y), uintptr(w), uintptr(h), 0x14) +} + +func setCtrlFont(ctrl, font uintptr) { + if ctrl != 0 && font != 0 { + sendMessage.Call(ctrl, 0x0030, font, 0) // WM_SETFONT + } +} + +// layoutPanel computes exact DPI-scaled pixel geometry for every control on +// Reims-Go's 4px spacing grid (--sp-1..--sp-4, 31px .btn, 20px .badge, 32px .cmd-line). +func layoutPanel(hwnd uintptr) { + st := getPanelState(hwnd) + if st == nil { return } - dc, _, _ := getDC.Call(ctrl) + dpi := windowDPI(hwnd) + st.dpi = dpi + st.dividerY = 0 + st.extraStacked = false + st.bodyLines = nil + + fonts := getFonts(dpi, st.meta.zh) + dc, _, _ := getDC.Call(hwnd) if dc == 0 { return } - defer releaseDC.Call(ctrl, dc) - font, _, _ := sendMessage.Call(ctrl, 0x0031, 0, 0) // WM_GETFONT - if font != 0 { - old, _, _ := selectGDI.Call(dc, font) - defer selectGDI.Call(dc, old) + defer releaseDC.Call(hwnd, dc) + + if st.meta.compact { + fold, _, _ := getDlgItem.Call(hwnd, idFold) + setCtrlFont(fold, fonts.btn) + textW := measureLineWidth(dc, fonts.btn, controlText(fold)) + w := dp(dpi, 11) + dp(dpi, 7) + dp(dpi, 7) + textW + dp(dpi, 12) + h := dp(dpi, 32) + moveControl(fold, 0, 0, w, h) + setWindowPos.Call(hwnd, 0, 0, 0, uintptr(w), uintptr(h), 0x16) // SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE + return } - var measured rect - drawText.Call(dc, uintptr(unsafe.Pointer(&caption[0])), n, uintptr(unsafe.Pointer(&measured)), 0x400|0x820) - padX := dialogPixels(hwnd, linkPadDLU) - wantW := min(avail.right, (measured.right-measured.left)+padX*2) - if wantW > 0 && wantW < avail.right { - // SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE - setWindowPos.Call(ctrl, 0, 0, 0, uintptr(wantW), uintptr(avail.bottom), 0x16) + + cardW := dp(dpi, 340) + if !st.meta.zh { + cardW = dp(dpi, 376) } + padX := dp(dpi, 16) + innerW := cardW - padX*2 + y := dp(dpi, 14) + + stageCtrl, _, _ := getDlgItem.Call(hwnd, idStage) + foldCtrl, _, _ := getDlgItem.Call(hwnd, idFold) + titleCtrl, _, _ := getDlgItem.Call(hwnd, idTitle) + bodyCtrl, _, _ := getDlgItem.Call(hwnd, idBody) + codeCtrl, _, _ := getDlgItem.Call(hwnd, idCode) + hintCtrl, _, _ := getDlgItem.Call(hwnd, idInputHint) + leftCtrl, _, _ := getDlgItem.Call(hwnd, idLeft) + extraCtrl, _, _ := getDlgItem.Call(hwnd, idExtra) + rightCtrl, _, _ := getDlgItem.Call(hwnd, idRight) + jumpCtrl, _, _ := getDlgItem.Call(hwnd, idJump) + + // 1. Meta row: 20px .badge pill on left, quiet ghost "收起" optically flush right. + if stageCtrl != 0 || foldCtrl != 0 { + rowH := dp(dpi, 22) + maxStageW := innerW + if foldCtrl != 0 { + setCtrlFont(foldCtrl, fonts.smallMed) + foldPadX := dp(dpi, 8) + foldW := measureLineWidth(dc, fonts.smallMed, controlText(foldCtrl)) + foldPadX*2 + foldH := dp(dpi, 24) + // Extend the invisible hover hit box by +foldPadX on the right so the text + // aligns optically flush with cardW - padX (matching code box & primary button). + foldX := cardW - padX - foldW + foldPadX + foldY := y + (rowH-foldH)/2 + moveControl(foldCtrl, foldX, foldY, foldW, foldH) + maxStageW = max(dp(dpi, 40), (foldX-dp(dpi, 8))-padX) + } + if stageCtrl != 0 { + setCtrlFont(stageCtrl, fonts.badge) + pillPadX := dp(dpi, 8) + pillH := dp(dpi, 20) + pillW := min(maxStageW, measureLineWidth(dc, fonts.badge, controlText(stageCtrl))+pillPadX*2) + moveControl(stageCtrl, padX, y+(rowH-pillH)/2, pillW, pillH) + } + y += rowH + dp(dpi, 10) + } + + // 2. Title (15px semibold). + if titleCtrl != 0 { + setCtrlFont(titleCtrl, fonts.title) + titleH := dp(dpi, 22) + moveControl(titleCtrl, padX, y, innerW, titleH) + y += titleH + dp(dpi, 4) + } + + // 3. Body (13px, 20px line-height = 1.54x, measured to exact line count). + if bodyCtrl != 0 { + setCtrlFont(bodyCtrl, fonts.body) + lineH := dp(dpi, 20) + lines := wrapLines(dc, fonts.body, controlText(bodyCtrl), innerW, 6) + st.bodyLines = lines + st.bodyLineH = lineH + bodyH := max(lineH, int32(len(lines))*lineH) + moveControl(bodyCtrl, padX, y, innerW, bodyH) + y += bodyH + } + + // 4. Monospace command / filename box (.cmd-line: 32px tall, r=8px). + if codeCtrl != 0 { + setCtrlFont(codeCtrl, fonts.code) + y += dp(dpi, 12) + codeH := dp(dpi, 32) + moveControl(codeCtrl, padX, y, innerW, codeH) + y += codeH + } + + // 5. Input release hint (12px muted). + if hintCtrl != 0 { + setCtrlFont(hintCtrl, fonts.small) + y += dp(dpi, 8) + hintH := dp(dpi, 18) + moveControl(hintCtrl, padX, y, innerW, hintH) + y += hintH + } + + // 6. Footer action buttons (31px tall .btn / .btn-primary / .btn-ghost, 8px gap). + if leftCtrl != 0 || extraCtrl != 0 || rightCtrl != 0 { + btnH := dp(dpi, 31) + btnPadX := dp(dpi, 13) + ghostPadX := dp(dpi, 10) + gap := dp(dpi, 8) + + var leftW, rightW, extraW int32 + if leftCtrl != 0 { + setCtrlFont(leftCtrl, fonts.btn) + leftW = max(dp(dpi, 60), measureLineWidth(dc, fonts.btn, controlText(leftCtrl))+btnPadX*2) + } + if rightCtrl != 0 { + setCtrlFont(rightCtrl, fonts.btnBold) + rightW = max(dp(dpi, 64), measureLineWidth(dc, fonts.btnBold, controlText(rightCtrl))+btnPadX*2) + } + if extraCtrl != 0 { + setCtrlFont(extraCtrl, fonts.btn) + extraW = measureLineWidth(dc, fonts.btn, controlText(extraCtrl)) + ghostPadX*2 + } + + // Keep auxiliary action inline on the action row whenever it fits cleanly; + // only stack above the buttons if a long localized string needs more room. + needInline := rightW + if leftCtrl != 0 { + needInline += leftW + dp(dpi, 12) + } + if extraCtrl != 0 { + needInline += extraW + gap + } + if extraCtrl != 0 && needInline > innerW { + st.extraStacked = true + setCtrlFont(extraCtrl, fonts.smallMed) + stackPadX := dp(dpi, 8) + stackW := min(innerW+stackPadX*2, measureLineWidth(dc, fonts.smallMed, controlText(extraCtrl))+stackPadX*2) + stackH := dp(dpi, 26) + y += dp(dpi, 10) + moveControl(extraCtrl, padX-stackPadX, y, stackW, stackH) + y += stackH + dp(dpi, 10) + } else { + y += dp(dpi, 16) + } + + rightX := cardW - padX - rightW + if rightCtrl != 0 { + moveControl(rightCtrl, rightX, y, rightW, btnH) + } + if st.meta.isConfirm { + if leftCtrl != 0 { + moveControl(leftCtrl, rightX-gap-leftW, y, leftW, btnH) + } + } else { + if leftCtrl != 0 { + if rightCtrl == 0 { + moveControl(leftCtrl, cardW-padX-leftW, y, leftW, btnH) + } else { + moveControl(leftCtrl, padX, y, leftW, btnH) + } + } + if extraCtrl != 0 && !st.extraStacked { + if leftCtrl != 0 { + moveControl(extraCtrl, rightX-gap-extraW, y, extraW, btnH) + } else { + // No Back button: align auxiliary ghost button text optically flush left. + moveControl(extraCtrl, padX-ghostPadX, y, extraW, btnH) + } + } + } + if leftCtrl != 0 || rightCtrl != 0 || (extraCtrl != 0 && !st.extraStacked) { + y += btnH + } + } + + // 7. Full-width escape-hatch footer strip separated by a 1px --border hairline. + if jumpCtrl != 0 { + setCtrlFont(jumpCtrl, fonts.smallMed) + y += dp(dpi, 14) + st.dividerY = y + y++ // 1px hairline divider + stripPadX := dp(dpi, 8) + stripPadY := dp(dpi, 4) + jumpH := dp(dpi, 30) + moveControl(jumpCtrl, stripPadX, y+stripPadY, cardW-stripPadX*2, jumpH) + y += jumpH + stripPadY*2 + } else { + y += dp(dpi, 16) + } + + // SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE + setWindowPos.Call(hwnd, 0, 0, 0, uintptr(cardW), uintptr(y), 0x16) } // revealPanel synchronously repaints the dialog and every child control before @@ -389,19 +879,9 @@ func revealPanel(hwnd uintptr) { setLayeredAttr.Call(hwnd, 0, panelAlpha, 2) // LWA_ALPHA } -// Derive pixel sizes from the dialog's font-scaled layout (works across DPIs). -func dialogPixels(hwnd uintptr, units int32) int32 { - r := rect{right: units} - mapDialogRect.Call(hwnd, uintptr(unsafe.Pointer(&r))) - return max(1, r.right) -} - -func panelRadius(hwnd uintptr) int32 { - return max(6, dialogPixels(hwnd, 5)) // ~8 DIPs, matching Win11 DWMWCP_ROUND -} - func roundPanel(hwnd uintptr) { - if getDialogMeta(hwnd).dwm { + st := getPanelState(hwnd) + if st == nil || st.meta.dwm { return } var r rect @@ -409,10 +889,9 @@ func roundPanel(hwnd uintptr) { if r.right <= 0 || r.bottom <= 0 { return } - diameter := uintptr(panelRadius(hwnd) * 2) + diameter := uintptr(dp(st.dpi, 8) * 2) region, _, _ := createRoundRectRgn.Call(0, 0, uintptr(r.right+1), uintptr(r.bottom+1), diameter, diameter) if region != 0 { - // SetWindowRgn takes ownership only on success. if ok, _, _ := setWindowRgn.Call(hwnd, region, 1); ok == 0 { deleteGDI.Call(region) } @@ -420,36 +899,54 @@ func roundPanel(hwnd uintptr) { } func panelBrush(hwnd, dc, ctrl uintptr) uintptr { - meta := getDialogMeta(hwnd) - pal := paletteFor(meta.dark) - fg := pal.text2 - if ctrl != 0 { - if id, _, _ := getDlgCtrlID.Call(ctrl); id == idInputHint { - fg = pal.text3 - } - } - textColor.Call(dc, fg.colorref()) + pal := paletteFor(getDialogMeta(hwnd).dark) + textColor.Call(dc, pal.text2.colorref()) bkMode.Call(dc, 1) // TRANSPARENT brushColor.Call(dc, pal.surface.colorref()) brush, _, _ := stockObject.Call(18) // DC_BRUSH return brush } -// paintDialogDC draws the fill and crisp 1px border for the rounded popup. +// paintDialogDC paints the card background, subtle top highlight (--highlight-top), +// and optional 1px footer hairline (--border). On Win11 DWM already renders the +// single outer border + rounded clip + drop shadow, avoiding any double border. func paintDialogDC(hwnd, dc uintptr) { - meta := getDialogMeta(hwnd) - pal := paletteFor(meta.dark) + st := getPanelState(hwnd) + if st == nil { + return + } + pal := paletteFor(st.meta.dark) var r rect getClientRect.Call(hwnd, uintptr(unsafe.Pointer(&r))) - if meta.compact { - // The compact button covers the full client area and paints its own border. + brush, _, _ := stockObject.Call(18) // DC_BRUSH + + if st.meta.compact { brushColor.Call(dc, pal.surface.colorref()) - brush, _, _ := stockObject.Call(18) fillRect.Call(dc, uintptr(unsafe.Pointer(&r)), brush) return } - radius := float64(panelRadius(hwnd)) - paintRoundedBox(dc, r, radius, pal.surface, pal.surface, pal.cardBorder, true, rgb{}, false) + + if st.meta.dwm { + brushColor.Call(dc, pal.surface.colorref()) + fillRect.Call(dc, uintptr(unsafe.Pointer(&r)), brush) + if st.meta.dark && r.bottom > 0 { + topLine := rect{left: 0, top: 0, right: r.right, bottom: 1} + brushColor.Call(dc, pal.topHighlight.colorref()) + fillRect.Call(dc, uintptr(unsafe.Pointer(&topLine)), brush) + } + } else { + paintRoundedBox(dc, r, dpF(st.dpi, 8), pal.surface, pal.surface, pal.cardBorder, true, pal.topHighlight, st.meta.dark) + } + + if st.dividerY > 0 && st.dividerY < r.bottom { + inset := int32(0) + if !st.meta.dwm { + inset = 1 + } + sep := rect{left: inset, top: st.dividerY, right: r.right - inset, bottom: st.dividerY + 1} + brushColor.Call(dc, pal.divider.colorref()) + fillRect.Call(dc, uintptr(unsafe.Pointer(&sep)), brush) + } } func paintDialogFrame(hwnd uintptr) { @@ -479,8 +976,14 @@ func stageColors(pal palette, tn tone) (rgb, rgb) { // native click notifications). Only painting is custom, matching tokens.css. func paintItem(it *drawItem) { parent, _, _ := getParent.Call(it.control) - meta := getDialogMeta(parent) + st := getPanelState(parent) + if st == nil { + return + } + meta := st.meta + dpi := st.dpi pal := paletteFor(meta.dark) + fonts := getFonts(dpi, meta.zh) dc := it.dc saved, _, _ := gdi.NewProc("SaveDC").Call(dc) @@ -492,61 +995,36 @@ func paintItem(it *drawItem) { r := it.area h := r.bottom - r.top - font, _, _ := sendMessage.Call(it.control, 0x0031, 0, 0) // WM_GETFONT - if font != 0 { - selectGDI.Call(dc, font) - } + pressed := it.state&1 != 0 + _, hovered := hoveredControls.Load(it.control) + btnR := dpF(dpi, 8) // --r-md + smallR := dpF(dpi, 6) // --r-sm - var caption [256]uint16 - n, _, _ := getWindowTextW.Call(it.control, uintptr(unsafe.Pointer(&caption[0])), uintptr(len(caption))) - if n == 0 { + if it.id == idBody { + selectGDI.Call(dc, fonts.body) + textColor.Call(dc, pal.text2.colorref()) + lineH := st.bodyLineH + if lineH <= 0 { + lineH = dp(dpi, 20) + } + for i, line := range st.bodyLines { + if line == "" { + continue + } + u16 := utf16.Encode([]rune(line)) + if len(u16) == 0 { + continue + } + lr := rect{left: r.left, top: r.top + int32(i)*lineH, right: r.right, bottom: r.top + int32(i+1)*lineH} + drawText.Call(dc, uintptr(unsafe.Pointer(&u16[0])), uintptr(len(u16)), uintptr(unsafe.Pointer(&lr)), 0x8824) // DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX|DT_END_ELLIPSIS + } return } - pressed := it.state&1 != 0 - _, hovered := hoveredControls.Load(it.control) - btnRadius := float64(panelRadius(parent)) // ~8px (--r-md) - - // Select custom font if needed (title, stage badge, primary button, code pill). - var ownedFont uintptr - if font != 0 { - var lf logFontW - if got, _, _ := getObjectW.Call(font, unsafe.Sizeof(lf), uintptr(unsafe.Pointer(&lf))); got != 0 { - makeFont := false - switch it.id { - case idTitle: - lf.weight = 600 - lf.height = lf.height * 6 / 5 - makeFont = true - case idStage: - lf.weight = 600 - lf.height = lf.height * 11 / 12 - makeFont = true - case idRight: - lf.weight = 600 - makeFont = true - case idLeft: - lf.weight = 500 - makeFont = true - case idCode: - lf.weight = 400 - lf.height = lf.height * 11 / 12 - setFace(&lf, "Consolas") - makeFont = true - case idFold: - if meta.compact { - lf.weight = 500 - makeFont = true - } - } - if makeFont { - ownedFont, _, _ = createFontIndirectW.Call(uintptr(unsafe.Pointer(&lf))) - } - } - } - if ownedFont != 0 { - prev, _, _ := selectGDI.Call(dc, ownedFont) - defer func() { selectGDI.Call(dc, prev); deleteGDI.Call(ownedFont) }() + var caption [512]uint16 + n, _, _ := getWindowTextW.Call(it.control, uintptr(unsafe.Pointer(&caption[0])), uintptr(len(caption))) + if n == 0 { + return } flags := uintptr(0x825) // DT_SINGLELINE | DT_VCENTER | DT_CENTER | DT_NOPREFIX @@ -555,84 +1033,123 @@ func paintItem(it *drawItem) { switch it.id { case idStage: + selectGDI.Call(dc, fonts.badge) pillFill, pillFg := stageColors(pal, meta.stageTone) + paintRoundedBox(dc, r, float64(h)/2, pal.surface, pillFill, rgb{}, false, rgb{}, false) fg = pillFg - var measured rect - drawText.Call(dc, uintptr(unsafe.Pointer(&caption[0])), n, uintptr(unsafe.Pointer(&measured)), 0x400|0x820) - padX := max(7, dialogPixels(parent, 5)) - pillW := min(r.right-r.left, (measured.right-measured.left)+padX*2) - pillH := min(h, max(measured.bottom-measured.top+6, dialogPixels(parent, 12))) - pillY := r.top + (h-pillH)/2 - pillRect := rect{left: r.left, top: pillY, right: r.left + pillW, bottom: pillY + pillH} - paintRoundedBox(dc, pillRect, float64(pillH)/2, pal.surface, pillFill, rgb{}, false, rgb{}, false) - textRect = rect{left: pillRect.left + padX, top: pillRect.top, right: pillRect.right - padX, bottom: pillRect.bottom} flags = 0x825 case idTitle: - flags = 0x824 | 0x8000 // left | vcenter | singleline | noprefix | DT_END_ELLIPSIS + selectGDI.Call(dc, fonts.title) fg = pal.text + flags = 0x8824 // left | vcenter | singleline | noprefix | end_ellipsis + case idInputHint: + selectGDI.Call(dc, fonts.small) + fg = pal.text3 + flags = 0x8824 case idCode: - paintRoundedBox(dc, r, btnRadius, pal.surface, pal.codeFill, pal.codeBorder, true, rgb{}, false) - padX := max(8, dialogPixels(parent, 6)) + selectGDI.Call(dc, fonts.code) + paintRoundedBox(dc, r, btnR, pal.surface, pal.codeFill, pal.codeBorder, true, rgb{}, false) + padX := dp(dpi, 12) textRect = rect{left: r.left + padX, top: r.top, right: r.right - padX, bottom: r.bottom} - flags = 0x824 | 0x8000 // left | vcenter | singleline | noprefix | DT_END_ELLIPSIS fg = pal.codeText + flags = 0x8824 case idRight: + selectGDI.Call(dc, fonts.btnBold) fill := pal.accent if pressed { fill = pal.accentPress } else if hovered { fill = pal.accentHover } - paintRoundedBox(dc, r, btnRadius, pal.surface, fill, rgb{}, false, pal.accentHighlight, !pressed) + paintRoundedBox(dc, r, btnR, pal.surface, fill, rgb{}, false, pal.accentHighlight, !pressed) fg = rgb{255, 255, 255} case idLeft: + selectGDI.Call(dc, fonts.btn) fill := pal.btnFill if pressed { fill = pal.btnPress } else if hovered { fill = pal.btnHover } - paintRoundedBox(dc, r, btnRadius, pal.surface, fill, pal.btnBorder, true, rgb{}, false) + paintRoundedBox(dc, r, btnR, pal.surface, fill, pal.btnBorder, true, pal.btnHighlight, !pressed) fg = pal.text + case idExtra: + radius := btnR + if st.extraStacked { + selectGDI.Call(dc, fonts.smallMed) + radius = smallR + } else { + selectGDI.Call(dc, fonts.btn) + } + fg = pal.text2 + if pressed { + paintRoundedBox(dc, r, radius, pal.surface, pal.ghostPress, rgb{}, false, rgb{}, false) + fg = pal.text + } else if hovered { + paintRoundedBox(dc, r, radius, pal.surface, pal.ghostHover, rgb{}, false, rgb{}, false) + fg = pal.text + } case idFold: if meta.compact { + selectGDI.Call(dc, fonts.btn) fill := pal.surface if pressed { fill = pal.btnPress } else if hovered { fill = pal.btnHover } - paintRoundedBox(dc, r, btnRadius, pal.surface, fill, pal.cardBorder, true, rgb{}, false) - // Accent status dot on the left of the compact chip. - dotD := max(7, dialogPixels(parent, 4)) - dotX := r.left + max(10, dialogPixels(parent, 6)) + if meta.dwm { + brushColor.Call(dc, fill.colorref()) + fillRect.Call(dc, uintptr(unsafe.Pointer(&r)), brush) + if meta.dark && !pressed && r.bottom > 0 { + topLine := rect{left: 0, top: 0, right: r.right, bottom: 1} + brushColor.Call(dc, pal.topHighlight.colorref()) + fillRect.Call(dc, uintptr(unsafe.Pointer(&topLine)), brush) + } + } else { + paintRoundedBox(dc, r, btnR, pal.surface, fill, pal.cardBorder, true, pal.topHighlight, meta.dark && !pressed) + } + dotD := dp(dpi, 7) + dotX := r.left + dp(dpi, 11) dotY := r.top + (h-dotD)/2 paintRoundedBox(dc, rect{left: dotX, top: dotY, right: dotX + dotD, bottom: dotY + dotD}, float64(dotD)/2, fill, pal.accent, rgb{}, false, rgb{}, false) - textRect = rect{left: dotX + dotD + max(6, dialogPixels(parent, 4)), top: r.top, right: r.right - 8, bottom: r.bottom} + textRect = rect{left: dotX + dotD + dp(dpi, 7), top: r.top, right: r.right - dp(dpi, 10), bottom: r.bottom} flags = 0x824 fg = pal.text } else { + selectGDI.Call(dc, fonts.smallMed) fg = pal.text3 if pressed { - paintRoundedBox(dc, r, max(5, btnRadius-2), pal.surface, pal.ghostPress, rgb{}, false, rgb{}, false) + paintRoundedBox(dc, r, smallR, pal.surface, pal.ghostPress, rgb{}, false, rgb{}, false) fg = pal.text } else if hovered { - paintRoundedBox(dc, r, max(5, btnRadius-2), pal.surface, pal.ghostHover, rgb{}, false, rgb{}, false) + paintRoundedBox(dc, r, smallR, pal.surface, pal.ghostHover, rgb{}, false, rgb{}, false) fg = pal.text } } - case idExtra, idJump: - fg = pal.accentLink - if pressed || hovered { - fill := pal.accentLinkHover - if pressed { - fill = pal.accentSoft - } - paintRoundedBox(dc, r, max(5, btnRadius-2), pal.surface, fill, rgb{}, false, rgb{}, false) + case idJump: + selectGDI.Call(dc, fonts.smallMed) + if pressed { + paintRoundedBox(dc, r, smallR, pal.surface, pal.ghostPress, rgb{}, false, rgb{}, false) + } else if hovered { + paintRoundedBox(dc, r, smallR, pal.surface, pal.ghostHover, rgb{}, false, rgb{}, false) } - padX := dialogPixels(parent, linkPadDLU) - textRect = rect{left: r.left + padX, top: r.top, right: r.right - padX, bottom: r.bottom} - flags = 0x825 | 0x8000 // center | vcenter | singleline | noprefix | DT_END_ELLIPSIS + padX := dp(dpi, 8) + chevW := dp(dpi, 14) + textRect = rect{left: r.left + padX, top: r.top, right: r.right - padX - chevW, bottom: r.bottom} + fg = pal.accentLink + flags = 0x8824 + // Draw subtle trailing chevron aligned optically with cardW - 16px. + chevCol := pal.text3 + if hovered || pressed { + chevCol = pal.accentLink + } + selectGDI.Call(dc, fonts.title) + textColor.Call(dc, chevCol.colorref()) + chevRect := rect{left: r.right - padX - chevW, top: r.top - dp(dpi, 1), right: r.right - padX, bottom: r.bottom} + chev := [1]uint16{'›'} + drawText.Call(dc, uintptr(unsafe.Pointer(&chev[0])), 1, uintptr(unsafe.Pointer(&chevRect)), 0x826) // DT_RIGHT|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX + selectGDI.Call(dc, fonts.smallMed) } textColor.Call(dc, fg.colorref()) @@ -678,6 +1195,10 @@ func paintRoundedBox(dc uintptr, r rect, radius float64, bg, fill, border rgb, h bx := hw - radius by := hh - radius borderW := 1.0 + hiRow := 0 + if hasBorder { + hiRow = 1 + } // Precompute corner bounding margin to fast-fill interior spans. marginX := int(math.Ceil(radius)) + 2 @@ -689,7 +1210,6 @@ func paintRoundedBox(dc uintptr, r rect, radius float64, bg, fill, border rgb, h inFlatY := y >= marginY && y < h-marginY for x := 0; x < w; x++ { if inFlatY && x >= marginX && x < w-marginX { - // Fast path: deep interior pixel. i := rowOff + x*4 buf[i] = fill.b buf[i+1] = fill.g @@ -699,18 +1219,20 @@ func paintRoundedBox(dc uintptr, r rect, radius float64, bg, fill, border rgb, h px := math.Abs(float64(x)+0.5-hw) - bx qx := math.Max(px, 0) qy := math.Max(py, 0) - // Signed distance to outer edge (negative inside, positive outside). d := math.Hypot(qx, qy) + math.Min(math.Max(px, py), 0) - radius outerCov := clamp01(0.5 - d) col := bg if outerCov > 0 { inner := fill - if hasHighlight && y == 1 && d <= -1.0 { - inner = hi + if hasHighlight && y == hiRow { + if !hasBorder && d <= -0.25 { + inner = hi + } else if hasBorder && d <= -1.25 { + inner = hi + } } if hasBorder { - // Inner fill starts borderW pixels inside the outer edge. innerCov := clamp01(0.5 - (d + borderW)) inner = blendRGB(border, inner, innerCov) } diff --git a/internal/installguide/dialog_windows.go b/internal/installguide/dialog_windows.go index 896d23a..bc7dfbc 100644 --- a/internal/installguide/dialog_windows.go +++ b/internal/installguide/dialog_windows.go @@ -22,39 +22,30 @@ const ( wmAction = 0x8001 ) -// A standard Win32 DLGTEMPLATE in dialog units so Windows scales layout and -// controls to the monitor DPI while supporting plain `go build`. +// dialogTemplate emits a minimal Win32 DLGTEMPLATE containing real native +// controls (for Tab navigation, Enter/Esc, and accessibility). Exact DPI-scaled +// pixel geometry and typography matching tokens.css are applied in layoutPanel +// during WM_INITDIALOG. func dialogTemplate(p page, c content, locale i18n.Locale) []uint16 { const ( button = 0x80 label = 0x82 ) - zh := locale == i18n.ZhCN - width := uint16(248) - if zh { - width = 228 - } - if p == compact { - width = 82 - if zh { - width = 62 - } - } // WS_POPUP | WS_CLIPCHILDREN | DS_SETFONT, WS_EX_TOOLWINDOW | WS_EX_LAYERED. - data := []uint16{0x40, 0x8200, 0x80, 8, 0, 0, 0, width, 20, 0, 0} + data := []uint16{0x40, 0x8200, 0x80, 8, 0, 0, 0, 180, 40, 0, 0} addString := func(s string) { data = append(data, utf16.Encode([]rune(s))...) data = append(data, 0) } addString(c.title) data = append(data, 9) - if zh { + if locale == i18n.ZhCN { addString("Microsoft YaHei UI") } else { addString("Segoe UI") } - item := func(class, id, x, y, w, h uint16, caption string) { + item := func(class, id uint16, caption string) { if len(data)%2 != 0 { data = append(data, 0) } @@ -63,148 +54,62 @@ func dialogTemplate(p page, c content, locale i18n.Locale) []uint16 { if class == button { style |= 1 // WS_TABSTOP controlStyle = 0x0b // BS_OWNERDRAW - } else if id == idTitle || id == idStage || id == idCode { + } else { controlStyle = 0x0d // SS_OWNERDRAW } - data = append(data, controlStyle, style, 0, 0, x, y, w, h, id, 0xffff, class) + data = append(data, controlStyle, style, 0, 0, 0, 0, 10, 10, id, 0xffff, class) addString(caption) data = append(data, 0) data[4]++ } if p == compact { - item(button, idFold, 0, 0, width, 20, c.right) + item(button, idFold, c.right) return data } - const padX uint16 = 12 - innerW := width - padX*2 - y := uint16(10) - - // Top meta row: stage/step pill on the left, quiet Hide button on the right. - if c.stage != "" || c.fold != "" { - stageW := innerW - if c.fold != "" { - stageW -= 38 - item(button, idFold, width-padX-34, y, 34, 15, c.fold) - } - if c.stage != "" { - item(label, idStage, padX, y, stageW, 15, c.stage) - } - y += 19 + if c.stage != "" { + item(label, idStage, c.stage) + } + if c.fold != "" { + item(button, idFold, c.fold) + } + item(label, idTitle, c.title) + if c.body != "" { + item(label, idBody, c.body) } - - // Title - item(label, idTitle, padX, y, innerW, 16, c.title) - y += 20 - - // Body: size tightly to the actual text so cards stay compact and balanced. - bodyH := estimateBodyHeight(p, c.body, zh) - item(label, idBody, padX, y, innerW, bodyH, c.body) - y += bodyH + 5 - - // Monospace command / filename box if c.code != "" { - item(label, idCode, padX, y, innerW, 19, c.code) - y += 24 + item(label, idCode, c.code) } - - // Release-keys footnote if c.inputHint != "" { - item(label, idInputHint, padX, y, innerW, 13, c.inputHint) - y += 17 + item(label, idInputHint, c.inputHint) + } + if c.left != "" { + item(button, idLeft, c.left) } - - // Secondary text actions: inset left by linkPadDLU so unhovered text aligns - // flush with padX while hover pills sit symmetrically around the label. if c.extra != "" { - item(button, idExtra, padX-linkPadDLU, y, innerW+linkPadDLU*2, 16, c.extra) - y += 18 + item(button, idExtra, c.extra) + } + if c.right != "" { + item(button, idRight, c.right) } if c.jump != "" { - item(button, idJump, padX-linkPadDLU, y, innerW+linkPadDLU*2, 16, c.jump) - y += 18 + item(button, idJump, c.jump) } - - // Footer actions - if c.left != "" || c.right != "" { - y += 6 - const btnH uint16 = 19 - leftW := uint16(52) - if zh { - leftW = 48 - } - rightW := primaryButtonWidth(c.right, zh) - rightX := width - padX - rightW - leftX := padX - if c.isConfirm { - // Standard macOS / Reims-Go confirmation order: Cancel left of Confirm. - leftX = rightX - 6 - leftW - } else if c.right == "" { - leftX = width - padX - leftW - } - if c.left != "" { - item(button, idLeft, leftX, y, leftW, btnH, c.left) - } - if c.right != "" { - item(button, idRight, rightX, y, rightW, btnH, c.right) - } - y += btnH + 11 - } else { - y += 8 - } - - data[8] = y return data } -func estimateBodyHeight(p page, body string, zh bool) uint16 { - if p == failed { - return 54 - } - // Estimate visual width in half-width units (CJK = 2, ASCII = 1). - units := 0 - for _, r := range body { - if r > 0x7f { - units += 2 - } else { - units++ - } - } - unitsPerLine := 56 - if zh { - unitsPerLine = 48 - } - lines := max(1, (units+unitsPerLine-1)/unitsPerLine) - return uint16(lines*10 + 3) -} - -func primaryButtonWidth(label string, zh bool) uint16 { - runes := len([]rune(label)) - if zh { - if runes <= 3 { - return 54 - } - if runes <= 5 { - return 72 - } - return 88 - } - if runes <= 8 { - return 64 - } - if runes <= 14 { - return 88 - } - return 108 -} - var dialogProc = syscall.NewCallback(func(hwnd, msg, wp, lp uintptr) uintptr { switch msg { case 0x0110: // WM_INITDIALOG stylePanel(hwnd, lp) - case 0x0005: // WM_SIZE (including DPI changes) + case 0x0005: // WM_SIZE roundPanel(hwnd) + case 0x02e0: // WM_DPICHANGED + layoutPanel(hwnd) + roundPanel(hwnd) + invalidateRect.Call(hwnd, 0, 0) + return 1 case 0x000f: // WM_PAINT paintDialogFrame(hwnd) return 1 @@ -233,6 +138,8 @@ var dialogProc = syscall.NewCallback(func(hwnd, msg, wp, lp uintptr) uintptr { case 0x0010: // WM_CLOSE: leave a way to reopen the guide. postMessage.Call(hwnd, wmAction, 2, 0) return 1 + case 0x0082: // WM_NCDESTROY + cleanupPanelState(hwnd) } return 0 }) diff --git a/internal/installguide/overlay_windows.go b/internal/installguide/overlay_windows.go index 19a2ebd..644380e 100644 --- a/internal/installguide/overlay_windows.go +++ b/internal/installguide/overlay_windows.go @@ -133,8 +133,11 @@ func (w *panel) targetOrigin(hwnd uintptr) (int32, int32, bool) { getWindowRect.Call(hwnd, uintptr(unsafe.Pointer(&bounds))) width := bounds.right - bounds.left height := bounds.bottom - bounds.top - x := origin.x + max(0, client.right-width-12) - y := origin.y + min(42, max(0, client.bottom-height)) + dpi := windowDPI(w.owner) + marginX := dp(dpi, 16) + offsetY := dp(dpi, 40) + x := origin.x + max(0, client.right-width-marginX) + y := origin.y + min(offsetY, max(0, client.bottom-height)) return x, y, true } @@ -313,6 +316,12 @@ func (w *panel) sync(fg uintptr, g Guide, p page, failure string) error { if p == compact { flags |= dialogCompact } + if loc == i18n.ZhCN { + flags |= dialogZh + } + if c.isConfirm { + flags |= dialogConfirm + } hwnd, _, err := createDialog.Call(0, uintptr(unsafe.Pointer(&template[0])), w.owner, dialogProc, flags) if hwnd == 0 { // The renderer can replace its window while toggling fullscreen. diff --git a/internal/installguide/overlay_windows_test.go b/internal/installguide/overlay_windows_test.go index d5ccf8e..e4996b5 100644 --- a/internal/installguide/overlay_windows_test.go +++ b/internal/installguide/overlay_windows_test.go @@ -142,7 +142,8 @@ func TestOwnedGuide(t *testing.T) { t.Fatal("double click repeated the action") } - // Verify body and control labels fit cleanly across locales, themes, kinds and pages. + // Verify body, spacing, alignment rails, non-overlap and control labels across + // every locale, kind and page. for _, loc := range []i18n.Locale{i18n.ZhCN, i18n.EnUS} { g.Locale = loc g.Theme = ThemeLight @@ -158,70 +159,150 @@ func TestOwnedGuide(t *testing.T) { t.Fatal(err) } c := g.content(p, detail) + st := getPanelState(w.hwnd) + if st == nil { + t.Fatalf("missing panelState for page %s", p) + } + dpi := st.dpi if p == compact { if ctrl, _, _ := getItem.Call(w.hwnd, idFold); ctrl != 0 { - checkSingleLineFits(t, ctrl, c.right, 500, 1, 1, 22) + checkSingleLineFits(t, ctrl, c.right, dp(dpi, 25)) } } else { + checkCardGeometry(t, w.hwnd, st, p, c) if body, _, _ := getItem.Call(w.hwnd, idBody); body != 0 { - checkTextFits(t, body, c.body) + checkBodyFits(t, body, st, c.body) } if title, _, _ := getItem.Call(w.hwnd, idTitle); title != 0 { - checkSingleLineFits(t, title, c.title, 600, 6, 5, 0) + checkSingleLineFits(t, title, c.title, 0) } if right, _, _ := getItem.Call(w.hwnd, idRight); right != 0 && c.right != "" { - checkSingleLineFits(t, right, c.right, 600, 1, 1, 12) + checkSingleLineFits(t, right, c.right, dp(dpi, 20)) } if left, _, _ := getItem.Call(w.hwnd, idLeft); left != 0 && c.left != "" { - checkSingleLineFits(t, left, c.left, 500, 1, 1, 12) + checkSingleLineFits(t, left, c.left, dp(dpi, 20)) + } + if extra, _, _ := getItem.Call(w.hwnd, idExtra); extra != 0 && c.extra != "" { + checkSingleLineFits(t, extra, c.extra, dp(dpi, 12)) + } + if fold, _, _ := getItem.Call(w.hwnd, idFold); fold != 0 && c.fold != "" { + checkSingleLineFits(t, fold, c.fold, dp(dpi, 12)) + } + if jump, _, _ := getItem.Call(w.hwnd, idJump); jump != 0 && c.jump != "" { + checkSingleLineFits(t, jump, c.jump, dp(dpi, 24)) } if hint, _, _ := getItem.Call(w.hwnd, idInputHint); hint != 0 && c.inputHint != "" { - checkSingleLineFits(t, hint, c.inputHint, 400, 1, 1, 0) + checkSingleLineFits(t, hint, c.inputHint, 0) } } w.close() } } } -} - -func checkTextFits(t *testing.T, label uintptr, text string) { - t.Helper() - dc, _, _ := user32.NewProc("GetDC").Call(label) - defer user32.NewProc("ReleaseDC").Call(label, dc) - font, _, _ := sendMessage.Call(label, 0x0031, 0, 0) // WM_GETFONT - old, _, _ := selectGDI.Call(dc, font) - defer selectGDI.Call(dc, old) - var available rect - getClientRect.Call(label, uintptr(unsafe.Pointer(&available))) - needed := available - s, _ := syscall.UTF16FromString(text) - drawText.Call(dc, uintptr(unsafe.Pointer(&s[0])), uintptr(len(s)-1), uintptr(unsafe.Pointer(&needed)), 0x410) // DT_CALCRECT | DT_WORDBREAK - if needed.bottom > available.bottom || needed.right > available.right { - t.Errorf("text needs %+v, have %+v: %s", needed, available, text) + panelStatesMu.RLock() + leaked := len(panelStates) + panelStatesMu.RUnlock() + if leaked != 0 { + t.Fatalf("leaked %d panelStates after close", leaked) } } -func checkSingleLineFits(t *testing.T, ctrl uintptr, text string, weight, num, den, padX int32) { +func childRectInParent(parent, child uintptr) rect { + var r rect + getWindowRect.Call(child, uintptr(unsafe.Pointer(&r))) + var p rect + getWindowRect.Call(parent, uintptr(unsafe.Pointer(&p))) + return rect{left: r.left - p.left, top: r.top - p.top, right: r.right - p.left, bottom: r.bottom - p.top} +} + +func checkCardGeometry(t *testing.T, hwnd uintptr, st *panelState, p page, c content) { t.Helper() - dc, _, _ := user32.NewProc("GetDC").Call(ctrl) - defer user32.NewProc("ReleaseDC").Call(ctrl, dc) - font, _, _ := sendMessage.Call(ctrl, 0x0031, 0, 0) - useFont := font - var custom uintptr - if font != 0 { - var lf logFontW - if n, _, _ := getObjectW.Call(font, unsafe.Sizeof(lf), uintptr(unsafe.Pointer(&lf))); n != 0 { - lf.weight = weight - lf.height = lf.height * num / den - if f, _, _ := createFontIndirectW.Call(uintptr(unsafe.Pointer(&lf))); f != 0 { - custom = f - useFont = f - defer deleteGDI.Call(custom) + var card rect + getClientRect.Call(hwnd, uintptr(unsafe.Pointer(&card))) + dpi := st.dpi + padX := dp(dpi, 16) + rightRail := card.right - padX + + type itemBox struct { + id uintptr + r rect + } + var boxes []itemBox + ids := []uintptr{idStage, idFold, idTitle, idBody, idCode, idInputHint, idLeft, idExtra, idRight, idJump} + for _, id := range ids { + ctrl, _, _ := getDlgItem.Call(hwnd, id) + if ctrl == 0 { + continue + } + r := childRectInParent(hwnd, ctrl) + if r.left < 0 || r.top < 0 || r.right > card.right || r.bottom > card.bottom { + t.Errorf("page=%s ctrl=%d out of card %+v: %+v", p, id, card, r) + } + switch id { + case idStage, idTitle, idBody, idCode, idInputHint: + if r.left != padX { + t.Errorf("page=%s ctrl=%d left=%d want padX=%d", p, id, r.left, padX) + } + case idFold: + // Fold text right edge aligns optically with rightRail (box extends +dp(8) for symmetric hover pill). + if r.right-dp(dpi, 8) != rightRail { + t.Errorf("page=%s fold text right=%d want rightRail=%d", p, r.right-dp(dpi, 8), rightRail) + } + case idRight: + if r.right != rightRail { + t.Errorf("page=%s primary right=%d want rightRail=%d", p, r.right, rightRail) + } + if r.bottom-r.top != dp(dpi, 31) { + t.Errorf("page=%s primary height=%d want %d", p, r.bottom-r.top, dp(dpi, 31)) + } + case idLeft: + if r.bottom-r.top != dp(dpi, 31) { + t.Errorf("page=%s secondary height=%d want %d", p, r.bottom-r.top, dp(dpi, 31)) + } + if !c.isConfirm && c.right != "" && r.left != padX { + t.Errorf("page=%s left button x=%d want padX=%d", p, r.left, padX) + } + case idJump: + if r.left+dp(dpi, 8) != padX || r.right-dp(dpi, 8) != rightRail { + t.Errorf("page=%s jump text rails (%d,%d) want (%d,%d)", p, r.left+dp(dpi, 8), r.right-dp(dpi, 8), padX, rightRail) + } + } + boxes = append(boxes, itemBox{id: id, r: r}) + } + for i := 0; i < len(boxes); i++ { + for j := i + 1; j < len(boxes); j++ { + a, b := boxes[i].r, boxes[j].r + if a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top { + t.Errorf("page=%s controls %d (%+v) and %d (%+v) overlap", p, boxes[i].id, a, boxes[j].id, b) } } } - old, _, _ := selectGDI.Call(dc, useFont) +} + +func checkBodyFits(t *testing.T, label uintptr, st *panelState, text string) { + t.Helper() + var available rect + getClientRect.Call(label, uintptr(unsafe.Pointer(&available))) + wantH := max(st.bodyLineH, int32(len(st.bodyLines))*st.bodyLineH) + if available.bottom != wantH { + t.Errorf("body height=%d want exact %d (%d lines)", available.bottom, wantH, len(st.bodyLines)) + } + dc, _, _ := user32.NewProc("GetDC").Call(label) + defer user32.NewProc("ReleaseDC").Call(label, dc) + font, _, _ := sendMessage.Call(label, 0x0031, 0, 0) // WM_GETFONT + for _, line := range st.bodyLines { + if w := measureLineWidth(dc, font, line); w > available.right { + t.Errorf("wrapped body line %q width %d exceeds %d (full=%q)", line, w, available.right, text) + } + } +} + +func checkSingleLineFits(t *testing.T, ctrl uintptr, text string, padX int32) { + t.Helper() + dc, _, _ := user32.NewProc("GetDC").Call(ctrl) + defer user32.NewProc("ReleaseDC").Call(ctrl, dc) + font, _, _ := sendMessage.Call(ctrl, 0x0031, 0, 0) // WM_GETFONT + old, _, _ := selectGDI.Call(dc, font) defer selectGDI.Call(dc, old) var available rect getClientRect.Call(ctrl, uintptr(unsafe.Pointer(&available))) @@ -308,15 +389,18 @@ func TestWinEventFollowsOwner(t *testing.T) { var initial rect getWindowRect.Call(w.hwnd, uintptr(unsafe.Pointer(&initial))) overlayW := initial.right - initial.left + dpi := windowDPI(owner) + marginX := dp(dpi, 16) + offsetY := dp(dpi, 40) // 1. Move owner window: overlay should follow via EVENT_OBJECT_LOCATIONCHANGE with no timer. newX, newY := offX+240, offY+130 setWindowPos.Call(owner, 0, uintptr(newX), uintptr(newY), 0, 0, 0x0015) // SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE - waitForOrigin(newX+1280-overlayW-12, newY+42, "after move") + waitForOrigin(newX+1280-overlayW-marginX, newY+offsetY, "after move") // 2. Resize owner window: overlay should stay pinned to the new top-right corner. setWindowPos.Call(owner, 0, 0, 0, 960, 600, 0x0016) // SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE - waitForOrigin(newX+960-overlayW-12, newY+42, "after resize") + waitForOrigin(newX+960-overlayW-marginX, newY+offsetY, "after resize") // 3. Disabling owner (e.g., guest modal dialog) causes followOwner to hide the overlay. enableWindow := user32.NewProc("EnableWindow")