Files
8bitMyGoUI/files/filepreview.go
T
ZacharyZhang-NY 552b073e81 Add files components 406-420; Tree opens to a known choice
Fifteen file components: FileExplorer, FileGrid, FileIcon, PathBar,
MemoryCard, DiskUsage, UploadQueue, Trash, RecentFiles, FilePreview,
ArchiveViewer, FileDetails, SharePanel, SyncStatus and QuotaMeter.

FileIcon draws six kinds as hand-placed 12x12 pages and a folder. The
browsers build on data.Tree, List, GridView and Table; ArchiveViewer
owns its sort and keeps the caller's index on its entry. UploadQueue
keys rows by name so focus stays on its row. SharePanel never logs the
link. SyncStatus turns its arrows on Busy steps, so syncing moves under
Reduce Motion.

data.Tree now opens the closed branches above a chosen path it knows,
so a caller's starting path survives the first frame.
2026-10-12 09:27:26 -04:00

64 lines
2.1 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 files
import (
"image"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/display"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
// Preview is what a file shows: a picture, else text.
type Preview struct {
Picture image.Image
Text string
}
type FilePreviewOptions struct {
// W and H are dots; zeros mean 160 and 120.
W, H int
}
type previewKey struct{}
// FilePreview shows a picture, else text, in a sunken pane.
func FilePreview(c *ui.Context, name string, pv Preview, o FilePreviewOptions) *ui.Element {
k := pixel.K(c)
w, h := pixel.Or("FilePreview W", o.W, 160), pixel.Or("FilePreview H", o.H, 120)
return ui.Column(c).Gap(k.D(2)).AlignItems(ui.Start).Role(ui.RoleGroup).Label(name).Children(func() {
// The group says the name; the title only shows it.
ui.Box(c).Size(k.D(pixel.Measure(name)), k.D(pixel.LineHeight)).Draw(func(p *ui.Painter, r ui.Rect) {
k.Canvas(p, r).Text(0, 0, name, pixel.Faint, 1)
})
layout.Panel(c, layout.PanelOptions{Style: pixel.Sunken, Fill: pixel.Well, Padding: 2}, func() {
pane := ui.Column(c).Size(k.D(w), k.D(h)).AlignItems(ui.Center).Justify(ui.Center)
pane.Children(func() {
switch {
case pv.Picture != nil:
pw, ph := fit(pv.Picture.Bounds(), w, h)
display.Picture(c, pv.Picture, display.PictureOptions{W: pw, H: ph, Label: k.Msg("preview")})
case pv.Text != "":
s := ui.Local(pane, previewKey{}, func() ui.ScrollState { return ui.ScrollState{} })
layout.ScrollArea(c, s, layout.ScrollAreaOptions{Label: k.Msg("preview")}, func() {
pixel.Text(c, pv.Text, pixel.TextOptions{Wrap: true})
}).Element.Size(k.D(w), k.D(h))
default:
pixel.Text(c, k.Msg("no_preview"), pixel.TextOptions{Slot: pixel.Faint})
}
})
})
})
}
// fit scales b into w×h: whole steps up, any down.
func fit(b image.Rectangle, w, h int) (int, int) {
bw, bh := max(1, b.Dx()), max(1, b.Dy())
s := min(float64(w)/float64(bw), float64(h)/float64(bh))
if s >= 1 {
s = float64(int(s))
}
return max(1, int(float64(bw)*s)), max(1, int(float64(bh)*s))
}