feat(gui): refine DPI-aware layout and typography in the VM guide
This commit is contained in:
1 parent
0afeac693c
commit
af39c861e8
4 files changed
+923
-401
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -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
|
||||
})
|
||||
@@ -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.
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user