Keel v0.5.1 中文 GitHub

AttachmentGroup

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/attachment_group.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 (
	"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("attachment_group", "chat", func() core.Widget {
		first := kit.Attachment(demoText("report.pdf", "报告.pdf"), 2048)
		second := kit.Attachment(demoText("image.png", "图片.png"), 4096)
		pending := kit.Attachment(demoText("pending-upload.zip", "待上传.zip"), 8192)
		pending.SetStatus(kit.AttachmentStatusPending)
		processing := kit.Attachment(demoText("processing.mp4", "处理中.mp4"), 16000)
		processing.SetStatus(kit.AttachmentStatusProcessing)
		processing.OnCancel(func() {}).OnRetry(func() {})
		group := kit.AttachmentGroup(first, second, pending, processing).Name(demoText("Attachments", "附件")).Gap(12)
		second.OnRemove(func() { group.SetItems(first, pending, processing) })
		sizes := []*kit.AttachmentView{
			kit.Attachment("XSmall.pdf", 1024).Size(kit.AttachmentSizeXSmall),
			kit.Attachment("Small.pdf", 1024).Size(kit.AttachmentSizeSmall),
			kit.Attachment("Medium.pdf", 1024).Size(kit.AttachmentSizeMedium),
			kit.Attachment("Large.pdf", 1024).Size(kit.AttachmentSizeLarge),
		}
		return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
			group.EdgeFade(theme.Bg)
			paging := el.Div().Wrap().Gap(8).Child(
				kit.Button(demoText("Previous screen", "前一屏"), func() { offset, width, _ := group.ScrollState(cx); group.ScrollTo(offset - width) }).Render(cx),
				kit.Button(demoText("Next screen", "后一屏"), func() { offset, width, _ := group.ScrollState(cx); group.ScrollTo(offset + width) }).Render(cx),
			)
			box := el.Div().P(24).Gap(12).Child(el.Text(demoText("Scroll horizontally; remove buttons update the attachment group", "横向滚动查看;移除按钮更新附件组")), paging, group.Render(cx))
			for _, a := range sizes {
				box.Child(a.Render(cx))
			}
			return box
		}))
	})
}

Arrange attachments horizontally and roll when out of container. Each item retains its own upload status and operation callbacks.

files := kit.AttachmentGroup(report, photo).Name("Attachments").Gap(12)
photo.OnRemove(func() { files.SetItems(report) })

The default spacing is SpaceSm, items are top aligned and width is not compressed. SetItems and Items isolate slice modifications and ignore nil entries; element instances are still shared, and the same instance should not be rendered repeatedly. SetDisabled prohibits operations within the group and does not modify the attachment's own settings.

Agent: The group role is group, and the name is set by Name; internal attachments and operations retain their own semantics.

Run go run ./examples/components -section attachment_group and remove the attachment after scrolling sideways. See Attachment for media and upload operations.

ScrollTo(dp) requests an absolute horizontal offset, which can be called before the first display; negative values return to the starting point, NaN/Inf are ignored. The request is limited to the valid range according to the current content after actual drawing, and is retained during the hiding period. Program scrolling is available even if in-group operations are disabled and does not trigger attachment selection or opening; background calls should be scheduled to the UI thread via core.Update.

ScrollState(cx) Returns the horizontal offset, visual width, and content width in dp; zero before first drawing. The Previous/Next buttons in the example use the current offset to add or subtract the visual width. The interface reads the status of the current root, and the same group of instances should only be rendered in one location.

next := kit.Button("Next screen", func() {
    offset, width, _ := files.ScrollState(cx)
    files.ScrollTo(offset + width)
})

EdgeFade(color) Draws a 24dp fade on the side with hidden content, the color should match the background around the group; does not show when the content is fully visible, hides the left/right fade when reaching the start/end point respectively. Narrow containers can take up to half the width on each side, leaving the bottom scroll bar uncovered. Fade only draws, does not add hit areas, and does not block attachment operations. ClearEdgeFade() is closed and not enabled by default; when the theme changes, the current theme background color can be re-introduced in Render.