Keel v0.5.1 中文 GitHub

Avatar

Live example: a real Keel application compiled to WebAssembly. Interact with it here. The initial download is about 10 MB.Open full screen
Example sourceGo · Copyable

This is the complete source for the gallery example above.examples/components/avatar.go

Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.

package main

import (
	"bytes"
	"encoding/base64"
	"image"
	"image/color"
	"image/png"

	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
	"github.com/dyike/keel/ui/theme"
)

func init() {
	registerSection("avatar", "display", func() core.Widget {
		img := image.NewNRGBA(image.Rect(0, 0, 80, 40))
		for y := 0; y < 40; y++ {
			for x := 0; x < 80; x++ {
				img.SetNRGBA(x, y, color.NRGBA{R: uint8(x * 3), G: 140, B: uint8(y * 6), A: 255})
			}
		}
		var encoded bytes.Buffer
		if err := png.Encode(&encoded, img); err != nil {
			panic(err)
		}
		return el.Embed(&avatarGallery{
			photo:  kit.Avatar(demoText("Sample image", "示例图片")).Image(img),
			source: kit.Avatar(demoText("Async image", "异步图片")).Source("data:image/png;base64," + base64.StdEncoding.EncodeToString(encoded.Bytes())),
		})
	})
}

type avatarGallery struct{ photo, source *kit.AvatarView }

func (v *avatarGallery) Render(cx *el.Context) el.Element {
	return el.Div().Row().Wrap().Gap(16).Items(el.Center).Child(kit.Avatar(demoText("Alex Chen", "张三")).Status(kit.AvatarOnline).Size(32).Render(cx), kit.Avatar("Ada Lovelace").Status(kit.AvatarBusy).Render(cx), kit.Avatar("").Size(56).Render(cx), v.photo.Render(cx), v.source.Render(cx),
		kit.Avatar("Keel Team").Size(48).Rounded(theme.RadiusLg).Colors(theme.Primary, theme.OnColor).Render(cx),
		kit.Avatar("Bob").Size(48).Border(2, theme.Primary).Render(cx),
		kit.Avatar("").Size(48).Placeholder(kit.IconSettings).Rounded(theme.RadiusMd).Render(cx))
}

kit.Avatar("Alex Chen").Size(40).Status(kit.AvatarOnline) displays a circular avatar. Size accepts float32 dp. The recommended small, medium and large sizes are 24, 40 and 56. Image accepts a decoded image.Image, nil restore name fallback. Use core.Update to update after background loading is completed.

In Chinese, the first character is taken, in English, the first letters of the first two words are taken, and a question mark is displayed for an empty name. The background is picked from the current theme based on the name hash. Status supports AvatarOnline, AvatarBusy, and AvatarOffline, and empty values are hidden; the status point is located inside the avatar and does not change the layout.

Agent role avatar, the name is the person's name, the value is the state, and it is empty if there is no state. Pure display, no keyboard handling. Verification: go run ./examples/components -section avatar -theme dark, omit theme to see the light theme.

Source(url) loads avatars in the background, supporting data URL, local path, file URL, and HTTP(S) after introducing ui/netimage; PNG, JPEG, GIF first frame, WebP and SVG. The name display falls back during loading or on failure, and the diameter remains unchanged. The default request timeout is 15 seconds, the encoded data limit is 16 MiB, and the decoded image limit is 32 million pixels. HTTP requests in a browser environment are still subject to CORS restrictions.

avatar := kit.Avatar("Ada Lovelace").Source("https://example.com/ada.png")

If the same source is set repeatedly for the same instance, the request will not be re-requested. Loading() queries the loading state, ImageError() returns the latest loading error, and Retry() re-requests the current source. Switching sources, calling Image or Source("") will cancel the old request, and late results will not overwrite the new state. Source("") restores name fallback; loading completed is applied in the next frame via core.Update. When uninstalling the avatar, you can call Source("") to cancel the unfinished request.

No cross-instance image caching is performed; when authentication requests or unified caching are required, the application provides pixels through Image. Calling core.DecodeImage(ctx, source) directly reuses the same decoder, which should be used in a background thread and provide its own timeout.

Avatar group see AvatarGroup.

Appearance

Method Function
Rounded(dp) Rounded corners, the default is circle; rounded square corners are commonly used for team and application avatars, such as Rounded(theme.RadiusLg)
Colors(bg, fg) The background color and text color of the initial letter and placeholder icon; the zero value remains the default (select the background color by name, and use theme.Text for text)
Border(dp, c) Outer ring, such as stacked avatars separated by theme.Surface colored rings; 0 removed
Placeholder(icon) The icon displayed when there is no picture or name, the default is IconUser; IconNone displays nothing
Style(fn) Adjust the avatar frame after the default style in each frame to handle situations not covered by the above methods

The placeholder icon is displayed when there is no name, and the question mark is no longer displayed.