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

114 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package ui
import (
"image"
"github.com/egoist/mygo/internal/scene"
)
// A Canvas is a bitmap that a painter draws, on the GPU where the window
// draws there: render to texture, as SkSurface, LÖVE's Canvas and raylib's
// RenderTexture2D. A view draws what seldom changes into one once, as a
// chart's grid or a map's ground, and shows it every frame for the price
// of an image; fades or tints a whole layer at once; or draws at a
// resolution of its own and shows it scaled, as pixel art scales up
// crisp (game.IntegerFit, in the game plugin).
//
// It keeps what was last drawn into it until Draw draws it anew, and
// shows anywhere a bitmap does, through Bitmap.
type Canvas struct {
img *scene.Image
bm *Bitmap
// part is the bitmap of the canvas's top left corner that region
// returned last.
part *Bitmap
// cache tells, for the layer of an element kept by Cache, whether
// what it paints changed.
cache *layerCache
}
// NewCanvas returns a canvas of w×h pixels, transparent until drawn.
func NewCanvas(w, h int) *Canvas {
img := scene.NewCanvas(max(w, 1), max(h, 1))
return &Canvas{img: img, bm: &Bitmap{img: img, w: img.W, h: img.H}}
}
// Bitmap returns the bitmap the canvas shows as, for Image, Painter.Image
// and Painter.ImageRegion; make it Pixelated to scale pixel art up crisp.
func (cv *Canvas) Bitmap() *Bitmap { return cv.bm }
// Size returns the canvas's size in pixels, the units it is drawn in.
func (cv *Canvas) Size() (w, h int) { return cv.img.W, cv.img.H }
// Draw draws the canvas anew, cleared to transparent, with what fn paints
// with cp in the canvas's pixels, during the frame p paints: a Draw
// callback's painter. What fn paints is what any painter paints: shapes,
// paths, text, images, parts of them, other canvases, under transforms and
// clips. Drawn, the canvas shows it from this frame on.
func (cv *Canvas) Draw(p *Painter, fn func(cp *Painter)) { cv.draw(p, 1, fn) }
// draw is Draw with scale pixels a DIP, the units fn paints in.
func (cv *Canvas) draw(p *Painter, scale float32, fn func(cp *Painter)) {
cv.drawPart(p, scale, cv.img.W, cv.img.H, fn)
}
// drawPart is draw into the canvas's top left w×h pixels, as an effect's
// layer larger than its content: fn paints nothing beyond them, and
// backdrops end at their edge (scene.Scene.Drawn), as in a canvas of that
// size.
func (cv *Canvas) drawPart(p *Painter, scale float32, w, h int, fn func(cp *Painter)) {
cv.paint(p, scale, w, h, fn)
cv.drawn(p)
}
// paint paints fn into the canvas's scene, as drawPart does, leaving the
// canvas as it shows.
func (cv *Canvas) paint(p *Painter, scale float32, w, h int, fn func(cp *Painter)) {
s := cv.img.Scene
s.Reset(cv.img.W, cv.img.H, scene.Color{})
if w != cv.img.W || h != cv.img.H {
s.Drawn = image.Pt(w, h)
}
s.Scale = scale
parent := p.s
s.MaskAtlas, s.ColorAtlas, s.Text = parent.MaskAtlas, parent.ColorAtlas, parent.Text
part := Rect{W: float32(w) / scale, H: float32(h) / scale}
cp := &Painter{rt: p.rt, s: s, scale: scale, opacity: 1, clip: part, theme: p.theme}
if s.Drawn != (image.Point{}) {
cp.Clip(part, 0, func() { fn(cp) })
} else {
fn(cp)
}
}
// drawn has the canvas show what its scene holds from the frame p paints.
func (cv *Canvas) drawn(p *Painter) {
cv.img.Changed()
// The frame's renderer draws it, before the frame: canvases drawn in
// this one first, as they finish first.
top := &p.rt.scene
top.Canvases = append(top.Canvases, cv.img)
}
// resize makes the canvas w×h pixels, transparent until drawn, keeping
// its identity in renderers' texture caches: they replace its texture in
// place, rather than keep the old one until the canvas is collected.
func (cv *Canvas) resize(w, h int) {
cv.img.Resize(max(w, 1), max(h, 1))
cv.bm = &Bitmap{img: cv.img, w: cv.img.W, h: cv.img.H}
cv.part = nil
}
// region returns the canvas's top left w×h pixels as a bitmap of that
// size, sharing the canvas's texture: what reads it, as an effect's
// shader, finds it transparent beyond them.
func (cv *Canvas) region(w, h int) *Bitmap {
if w == cv.img.W && h == cv.img.H {
return cv.bm
}
if p := cv.part; p == nil || p.w != w || p.h != h {
cv.part = &Bitmap{img: cv.img, w: w, h: h}
}
return cv.part
}