Files
openchamber-android-native/third_party/mygo/menu_icon_svg.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

72 lines
2.4 KiB
Go

package mygo
import (
"bytes"
"errors"
"image"
"image/color"
"image/png"
"math"
"github.com/egoist/mygo/internal/platform"
)
// SVGImage is a vector image that NewMenuIconSVG draws: a *ui.SVG, which
// package ui parses (ui.ParseSVG).
type SVGImage interface {
// Size returns the size the image says it has.
Size() (w, h float32)
// Image draws it to an image of w by h pixels, with current for
// currentColor.
Image(w, h int, current color.Color) *image.RGBA
}
// menuIconSVGPixels is the size, in pixels, of the longer side of the image
// NewMenuIconSVG draws an SVG to: four times the box of a menu's icons
// (platform.MenuIconSize), sharp on displays of up to four pixels a point,
// which the backends scale down from, as they do a PNG image.
const menuIconSVGPixels = 4 * platform.MenuIconSize
// NewMenuIconSVG makes an icon for menu items of an SVG, as NewMenuIcon
// does of a PNG image: the *ui.SVG an app shows with ui.Icon, drawn by
// native UI's renderer, with the shapes and paint it draws (see ui.SVG).
// It draws it once, to an image of 64 pixels on its longer side, keeping
// its aspect, which menus fit in their icons' box of 16 by 16 points.
//
// With template, only the shapes' coverage counts, whatever their colors,
// as for ui.Icon: the menu shows them in the color of its text, as
// NewMenuIcon's template images, or in an item's ui.MenuItem.IconColor in
// the menus package ui draws. Without it, the SVG shows in its own colors,
// with black for currentColor. It returns an error for a nil SVG or one
// without a size.
//
// //go:embed icons/trash.svg
// var trashSVG []byte
//
// var trash = ui.MustParseSVG(trashSVG) // also for ui.Icon(c, trash)
//
// trashIcon, err := mygo.NewMenuIconSVG(trash, true)
func NewMenuIconSVG(s SVGImage, template bool) (*MenuIcon, error) {
if s == nil {
return nil, errors.New("mygo: NewMenuIconSVG of a nil SVG")
}
w, h := s.Size()
if !(w > 0 && h > 0) || math.IsInf(float64(w), 0) || math.IsInf(float64(h), 0) {
return nil, errors.New("mygo: the SVG has no size")
}
pw, ph := menuIconSVGPixels, menuIconSVGPixels
if w >= h {
ph = max(1, int(math.Round(float64(h*menuIconSVGPixels/w))))
} else {
pw = max(1, int(math.Round(float64(w*menuIconSVGPixels/h))))
}
var b bytes.Buffer
if err := png.Encode(&b, s.Image(pw, ph, color.Black)); err != nil {
return nil, err
}
linkMenuIcons()
menuMu.Lock()
defer menuMu.Unlock()
return &MenuIcon{menuIcon(b.Bytes(), template)}, nil
}