Files
openchamber-android-native/third_party/mygo/internal/darwin/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

81 lines
2.8 KiB
Go

//go:build darwin && !ios
package darwin
import (
"fmt"
"path/filepath"
)
// FileIcon returns the icon Finder shows for a file, a folder or an
// application (NSWorkspace iconForFile:), drawn at size by size pixels
// from the representation that fits it best. A symbolic link gets the
// icon of what it points to, without the arrow Finder adds to links.
func (shell) FileIcon(path string, size int) ([]byte, error) {
real, err := filepath.EvalSymlinks(path)
if err != nil {
return nil, err
}
path = real
var png []byte
withPool(func() {
if img := send(workspace(), "iconForFile:", uintptr(nsString(path))); img != 0 {
png = pngOfImage(img, size)
}
})
if png == nil {
return nil, fmt.Errorf("mygo: no icon for %q", path)
}
return png, nil
}
// TypeIcon returns the icon Finder shows for files with the extension ext
// (NSWorkspace iconForContentType:, or iconForFileType: before macOS 11),
// drawn as FileIcon draws its icon.
func (shell) TypeIcon(ext string, size int) ([]byte, error) {
var png []byte
withPool(func() {
var img id
if hasClass("UTType") {
if t := send(class("UTType"), "typeWithFilenameExtension:", uintptr(nsString(ext))); t != 0 {
img = send(workspace(), "iconForContentType:", uintptr(t))
}
}
if img == 0 {
img = send(workspace(), "iconForFileType:", uintptr(nsString(ext)))
}
if img != 0 {
png = pngOfImage(img, size)
}
})
if png == nil {
return nil, fmt.Errorf("mygo: no icon for files of type %q", ext)
}
return png, nil
}
// pngOfImage draws an NSImage into an sRGB bitmap of size by size pixels,
// one pixel to the point, and encodes it as PNG. It must run in a pool.
func pngOfImage(img id, size int) []byte {
rep := autorelease(msgInitBitmap(send(class("NSBitmapImageRep"), "alloc"),
sel("initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bytesPerRow:bitsPerPixel:"),
0, size, size, 8, 4, true, false, nsString("NSCalibratedRGBColorSpace"), 0, 0))
if rep == 0 {
return nil
}
rep = send(rep, "bitmapImageRepByRetaggingWithColorSpace:", uintptr(send(class("NSColorSpace"), "sRGBColorSpace")))
ctx := send(class("NSGraphicsContext"), "graphicsContextWithBitmapImageRep:", uintptr(rep))
if ctx == 0 {
return nil
}
gc := class("NSGraphicsContext")
send(gc, "saveGraphicsState")
send(gc, "setCurrentContext:", uintptr(ctx))
send(ctx, "setImageInterpolation:", 3) // NSImageInterpolationHigh
const sourceOver = 2 // NSCompositingOperationSourceOver
dst := NSRect{Size: NSSize{float64(size), float64(size)}}
msgDrawImage(img, sel("drawInRect:fromRect:operation:fraction:"), dst, NSRect{}, sourceOver, 1)
send(gc, "restoreGraphicsState")
return goBytes(send(rep, "representationUsingType:properties:", 4, uintptr(send(class("NSDictionary"), "dictionary")))) // NSBitmapImageFileTypePNG
}