Files
MujicaUI/chat/drag_drop_overlay.go
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

102 lines
3.1 KiB
Go

package chat
import (
"fmt"
"log/slog"
"path/filepath"
"strings"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// DragDropOverlayOptions configure a DragDropOverlay.
type DragDropOverlayOptions struct {
// Hint replaces the built-in "Drop files to attach".
Hint string
// Accept checks each dropped path; nil accepts all.
Accept func(path string) bool
}
// DropResult carries the overlay and dropped files.
type DropResult struct {
Element ui.Element
accepted []string
rejected []string
}
// Accepted returns accepted paths; Rejected returns refused paths.
func (r DropResult) Accepted() []string { return r.accepted }
func (r DropResult) Rejected() []string { return r.rejected }
// DragDropOverlay covers content during external file dragging.
func DragDropOverlay(c *ui.Context, opts DragDropOverlayOptions, content func()) DropResult {
if content == nil {
panic("mujica: DragDropOverlay needs content")
}
k := core.Tokens(c)
u := core.Density(c).Unit()
var r DropResult
r.Element = ui.Box(c).MinHeight(0).MinWidth(0)
disabled := r.Element.IsDisabled()
rejected := ui.Local(r.Element, "rejected", func() []string { return nil })
over := !disabled && r.Element.FileDragOver()
if files := r.Element.DroppedFiles(); len(files) > 0 && !disabled {
r.accepted, r.rejected = dragDropSplit(files, opts.Accept)
*rejected = r.rejected
slog.Debug("mujica: drag_drop_overlay drop", "accepted", len(r.accepted), "rejected", len(r.rejected))
}
if over {
*rejected = nil
}
hint := opts.Hint
if hint == "" {
hint = core.Msg(c, "drag_drop_overlay.hint")
}
r.Element.Children(func() {
content()
if len(*rejected) > 0 && !over {
names := make([]string, len(*rejected))
for i, p := range *rejected {
names[i] = filepath.Base(p)
}
ui.Text(c, fmt.Sprintf(core.Msg(c, "drag_drop_overlay.rejected"), strings.Join(names, ", "))).
TextColor(k.Danger).FontSize(core.FontSize(c, theme.CaptionSize)).Role(ui.RoleStatus).Padding(u, u*2)
}
if over {
dragDropMask(c, hint)
}
})
return r
}
// dragDropMask covers its parent with the drop hint.
func dragDropMask(c *ui.Context, hint string) {
k := core.Tokens(c)
u := core.Density(c).Unit()
mask := ui.Column(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Center().Gap(u * 2).
Background(k.Background.Alpha(0.92)).Radius(theme.CardRadius).PassThrough().Role(ui.RoleStatus).Label(hint)
mask.Opacity(core.OverlayEnter(c, mask))
mask.Draw(func(p *ui.Painter, b ui.Rect) { internal.Frame(p, b, u*2, 5, k.Ornament) })
mask.Children(func() {
ui.Icon(c, icons.Must("upload")).Size(24, 24).TextColor(k.AccentText)
ui.Text(c, hint).Font(theme.SerifFont).FontSize(core.FontSize(c, theme.H3Size)).TextColor(k.Text)
})
}
// dragDropSplit sorts dropped paths by accept, nil accepting all.
func dragDropSplit(files []string, accept func(string) bool) (ok, no []string) {
for _, f := range files {
if accept == nil || accept(f) {
ok = append(ok, f)
} else {
no = append(no, f)
}
}
return ok, no
}