Files
openchamber-android-native/third_party/mygo/internal/windows/darkmenu.go
T

271 lines
8.1 KiB
Go
Raw Normal View History

//go:build windows && (amd64 || arm64)
package windows
import (
"sync"
"syscall"
"unsafe"
)
// Win32 menus have no dark appearance of their own. Popup menus follow the
// preferred app mode of uxtheme, which Windows exports by ordinal only
// (since Windows 10 1903, build 18362). The menu bar follows nothing: the
// window draws it, and the line below it, when the system asks through the
// messages UAH ("user API hook") menus send to their window. On Windows 11
// the window draws the bar in either appearance, in Windows 11's style
// (menudraw.go): taller, its items wider, with rounded backplates.
const (
wmNCPaint = 0x0085
wmNCActivate = 0x0086
wmUAHDrawMenu = 0x0091
wmUAHDrawMenuItem = 0x0092
wmUAHMeasureItem = 0x0094
objidMenu = 0xFFFFFFFD // OBJID_MENU
dtHidePrefix = 0x00100000
rdwFrame = 0x0400
odsSelected = 0x0001
odsGrayed = 0x0002
odsDisabled = 0x0004
odsHotLight = 0x0040
odsNoAccel = 0x0100
// SetPreferredAppMode's modes.
appModeForceDark = 2
appModeForceLight = 3
)
var (
procGetMenuBarInfo = user32.NewProc("GetMenuBarInfo")
procGetWindowDC = user32.NewProc("GetWindowDC")
procGetCurrentObject = gdi32.NewProc("GetCurrentObject")
)
// The colors of a dark menu bar, those of the dark title bar above it and,
// for hovered and open items, of the caption buttons drawn in its place
// (white at 10% and 20% over it).
const (
darkMenuBar = 0x202020 // COLORREF, 0x00BBGGRR
darkMenuHot = 0x363636
darkMenuOpen = 0x4D4D4D
darkMenuText = 0xFFFFFF
darkMenuDisabled = 0x6D6D6D
)
// uxtheme's preferred app mode, and the flush that makes menus created
// before it follow.
var appMode = sync.OnceValues(func() (setPreferredAppMode, flushMenuThemes uintptr) {
var v osVersionInfo
v.Size = uint32(unsafe.Sizeof(v))
procRtlGetVersion.Call(uintptr(unsafe.Pointer(&v)))
if v.Major < 10 || v.Major == 10 && v.Build < 18362 {
return 0, 0 // ordinal 135 was AllowDarkModeForApp before
}
const loadLibrarySearchSystem32 = 0x800
h, _, _ := procLoadLibraryExW.Call(uintptr(unsafe.Pointer(u16("uxtheme.dll"))), 0, loadLibrarySearchSystem32)
if h == 0 {
return 0, 0
}
set, _, _ := procGetProcAddress.Call(h, 135)
flush, _, _ := procGetProcAddress.Call(h, 136)
return set, flush
})
// applyMenuTheme gives the popup menus of the app, those of its trays
// included, the appearance.
func (b *Backend) applyMenuTheme() {
set, flush := appMode()
if set == 0 {
return
}
mode := uintptr(appModeForceLight)
if b.isDark() {
mode = appModeForceDark
}
syscall.SyscallN(set, mode)
if flush != 0 {
syscall.SyscallN(flush)
}
}
type drawItemStruct struct {
CtlType, CtlID, ItemID, ItemAction, ItemState uint32
HwndItem, DC uintptr
Item rect
ItemData uintptr
}
type uahMenu struct {
Menu, DC uintptr
Flags uint32
}
type uahDrawMenuItem struct {
Draw drawItemStruct
Menu uahMenu
Position int32
// UAHMENUITEMMETRICS and UAHMENUPOPUPMETRICS follow.
}
type menuBarInfo struct {
Size uint32
Bar rect
Menu, Hwnd uintptr
FocusedBits int32
}
// drawsMenuBar reports whether the window draws its menu bar: on Windows
// 11 always, on Windows 10 when dark. A contrast theme keeps the system's
// colors, as it does for popup menus.
func (w *window) drawsMenuBar() bool {
return w.menuShown() && !highContrastOn() && (fluentMenus() || w.b.isDark())
}
// menuBarColors returns the colors of the bar: its background, the
// backplates of an item hovered and of one open, and the text of an item
// and of a disabled one. Windows 11's backplates are WinUI's, over the
// title bar's colors.
func (b *Backend) menuBarColors() (back, hot, open, text, disabled uint32) {
switch dark := b.isDark(); {
case !fluentMenus():
return darkMenuBar, darkMenuHot, darkMenuOpen, darkMenuText, darkMenuDisabled
case dark:
return darkMenuBar, 0x2D2D2D, 0x333333, darkMenuText, darkMenuDisabled
}
return 0xFFFFFF, 0xF0F0F0, 0xEAEAEA, 0x1A1A1A, 0x9F9F9F
}
// menuBarRect is the menu bar in window coordinates, which the DCs of
// these messages use.
func (w *window) menuBarRect() (rect, bool) {
mbi := menuBarInfo{Size: uint32(unsafe.Sizeof(menuBarInfo{}))}
if ok, _, _ := procGetMenuBarInfo.Call(w.hwnd, objidMenu, 0, uintptr(unsafe.Pointer(&mbi))); ok == 0 {
return rect{}, false
}
var wr rect
procGetWindowRect.Call(w.hwnd, uintptr(unsafe.Pointer(&wr)))
r := mbi.Bar
r.Left, r.Right = r.Left-wr.Left, r.Right-wr.Left
r.Top, r.Bottom = r.Top-wr.Top, r.Bottom-wr.Top
return r, true
}
func fillColor(dc uintptr, r *rect, color uint32) {
brush, _, _ := procCreateSolidBrush.Call(uintptr(color))
procFillRect.Call(dc, uintptr(unsafe.Pointer(r)), brush)
procDeleteObject.Call(brush)
}
// drawMenuBar paints the background of the bar.
func (w *window) drawMenuBar(lp uintptr) {
um := (*uahMenu)(native(lp))
back, _, _, _, _ := w.b.menuBarColors()
if r, ok := w.menuBarRect(); ok {
fillColor(um.DC, &r, back)
}
}
// measureMenuBarItem makes an item of the bar, measured by Windows, as
// tall as Windows 11's bar and wider, for its backplate.
func (w *window) measureMenuBarItem(lp uintptr) {
mis := (*measureItemStruct)(native(lp)) // UAHMEASUREMENUITEM starts with it
dpi := dpiOf(w.hwnd)
mis.ItemWidth += uint32(toPx(menuBarExtra, dpi))
mis.ItemHeight = max(mis.ItemHeight, uint32(toPx(menuBarHeight, dpi)))
}
// drawMenuBarItem paints an item of the bar: its backplate and its label.
// On Windows 11 the backplate is inset, with rounded corners.
func (w *window) drawMenuBarItem(lp uintptr) {
d := (*uahDrawMenuItem)(native(lp))
dc, r, state := d.Draw.DC, d.Draw.Item, d.Draw.ItemState
back, hot, open, fore, disabled := w.b.menuBarColors()
plate := back
switch {
case state&(odsGrayed|odsDisabled) != 0:
fore = disabled
case state&odsSelected != 0:
plate = open
case state&odsHotLight != 0:
plate = hot
}
buf := make([]uint16, 256)
n, _, _ := procGetMenuStringW.Call(d.Menu.Menu, uintptr(d.Position), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), mfByPosition)
flags := uintptr(dtCenter | dtVCenter | dtSingleLine)
if state&odsNoAccel != 0 {
flags |= dtHidePrefix
}
label := func(dc uintptr, r rect) {
procSetBkMode.Call(dc, 1) // TRANSPARENT
procSetTextColor.Call(dc, uintptr(fore))
procDrawTextW.Call(dc, uintptr(unsafe.Pointer(&buf[0])), n, uintptr(unsafe.Pointer(&r)), flags)
}
if !fluentMenus() {
fillColor(dc, &r, plate)
label(dc, r)
return
}
const objFont = 6
font, _, _ := procGetCurrentObject.Call(dc, objFont)
dpi := dpiOf(w.hwnd)
x, y := toPx(menuBarInsetX, dpi), toPx(menuBarInsetY, dpi)
box := &rect{x, y, r.Right - r.Left - x, r.Bottom - r.Top - y}
if plate == back {
box = nil
}
paintItem(dc, r, itemFill{back: back, plate: plate, alpha: 1}, box, float64(toPx(menuRadius, dpi)), func(img *bitmap, r rect) {
old, _, _ := procSelectObject.Call(img.dc, font)
label(img.dc, r)
procSelectObject.Call(img.dc, old)
})
}
// coverMenuLine paints over the light line the frame draws below the menu
// bar, after the frame is drawn.
func (w *window) coverMenuLine() {
bar, ok := w.menuBarRect()
if !ok {
return
}
line := rect{bar.Left, bar.Bottom, bar.Right, bar.Bottom + 1}
dc, _, _ := procGetWindowDC.Call(w.hwnd)
if dc == 0 {
return
}
fillColor(dc, &line, darkMenuBar)
procReleaseDC.Call(w.hwnd, dc)
}
// menuBarMessage draws the menu bar. It handles the messages only while
// the window draws the bar (drawsMenuBar), and the frame's only while dark.
func (w *window) menuBarMessage(m uint32, wp, lp uintptr) (uintptr, bool) {
switch m {
case wmUAHDrawMenu, wmUAHDrawMenuItem, wmUAHMeasureItem, wmNCPaint, wmNCActivate:
default:
return 0, false
}
if !w.drawsMenuBar() {
return 0, false
}
switch m {
case wmUAHDrawMenu:
w.drawMenuBar(lp)
case wmUAHDrawMenuItem:
w.drawMenuBarItem(lp)
case wmUAHMeasureItem:
r, _, _ := procDefWindowProcW.Call(w.hwnd, uintptr(m), wp, lp)
if fluentMenus() {
w.measureMenuBarItem(lp)
}
return r, true
default: // the frame, then the line over it
r, _, _ := procDefWindowProcW.Call(w.hwnd, uintptr(m), wp, lp)
if w.b.isDark() {
w.coverMenuLine()
}
return r, true
}
return 0, true
}