Files
openchamber-android-native/third_party/mygo/internal/windows/icon.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

111 lines
3.8 KiB
Go

//go:build windows && (amd64 || arm64)
package windows
import (
"bytes"
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"unsafe"
)
var (
procGetObjectW = gdi32.NewProc("GetObjectW")
iidIShellItemImageFactory = guid("bcc18b79-ba16-442f-80c4-8a59c30c463b")
)
// dibSection is DIBSECTION, which GetObject fills for a DIB section.
type dibSection struct {
bmType, bmWidth, bmHeight, bmWidthBytes int32
bmPlanes, bmBitsPixel uint16
bmBits uintptr
biSize uint32
biWidth, biHeight int32
// The rest of BITMAPINFOHEADER, dsBitfields, dshSection and dsOffset.
rest [7]uint32
_ [3]uint32
_ uintptr
_ uint32
}
// FileIcon returns the icon Explorer shows for a file, a folder or an
// application, or for a shell item such as shell:AppsFolder\<AppUserModelID>
// (IShellItemImageFactory, without thumbnails), as Windows draws it for
// size by size pixels.
func (shell) FileIcon(path string, size int) ([]byte, error) {
var factory uintptr
if r, _, _ := procSHCreateItemFromParsingName.Call(uintptr(unsafe.Pointer(u16(path))), 0, uintptr(unsafe.Pointer(&iidIShellItemImageFactory)), uintptr(unsafe.Pointer(&factory))); failed(r) || factory == 0 {
return nil, hresultError(fmt.Sprintf("finding %q", path), r)
}
defer release(factory)
return factoryIcon(factory, size, path)
}
// TypeIcon returns the icon Explorer shows for files with the extension
// ext, that of a simple ID list of such a name, which needs no file, drawn
// as FileIcon draws its icon.
func (shell) TypeIcon(ext string, size int) ([]byte, error) {
path := filepath.Join(os.TempDir(), "file."+ext)
pidl, _, _ := procSHSimpleIDListFromPath.Call(uintptr(unsafe.Pointer(u16(path))))
if pidl == 0 {
return nil, fmt.Errorf("mygo: no ID list for %q", path)
}
defer procCoTaskMemFree.Call(pidl)
var factory uintptr
if r, _, _ := procSHCreateItemFromIDList.Call(pidl, uintptr(unsafe.Pointer(&iidIShellItemImageFactory)), uintptr(unsafe.Pointer(&factory))); failed(r) || factory == 0 {
return nil, hresultError(fmt.Sprintf("finding %q", path), r)
}
defer release(factory)
return factoryIcon(factory, size, path)
}
// factoryIcon draws the icon of an IShellItemImageFactory, without
// thumbnails, for size by size pixels, as a PNG.
func factoryIcon(factory uintptr, size int, path string) ([]byte, error) {
const iconOnly = 0x4 // SIIGBF_ICONONLY
var hbm uintptr
sz := uintptr(uint32(size)) | uintptr(uint32(size))<<32 // SIZE, by value
if r := comCall(factory, 3, sz, iconOnly, uintptr(unsafe.Pointer(&hbm))); failed(r) || hbm == 0 { // GetImage
return nil, hresultError(fmt.Sprintf("the icon of %q", path), r)
}
defer procDeleteObject.Call(hbm)
img, err := imageOfDIB(hbm)
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// imageOfDIB copies a 32-bit DIB section of the shell, whose pixels are
// BGRA with straight alpha (TestFileIconAlpha), as Go's NRGBA images are.
func imageOfDIB(hbm uintptr) (*image.NRGBA, error) {
var ds dibSection
if r, _, _ := procGetObjectW.Call(hbm, unsafe.Sizeof(ds), uintptr(unsafe.Pointer(&ds))); r == 0 || ds.bmBits == 0 || ds.bmBitsPixel != 32 {
return nil, fmt.Errorf("mygo: the icon is not a 32-bit DIB section (%d bits)", ds.bmBitsPixel)
}
w, h := int(ds.bmWidth), int(ds.bmHeight)
stride := int(ds.bmWidthBytes)
src := unsafe.Slice((*byte)(native(ds.bmBits)), stride*h)
img := image.NewNRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
row := y
if ds.biHeight > 0 { // bottom-up
row = h - 1 - y
}
s := src[row*stride:]
d := img.Pix[y*img.Stride:]
for x := 0; x < w; x++ {
d[4*x], d[4*x+1], d[4*x+2], d[4*x+3] = s[4*x+2], s[4*x+1], s[4*x], s[4*x+3]
}
}
return img, nil
}