Split into category packages; module github.com/ZacharyZhang-NY/MujicaUI
This commit is contained in:
1822 files changed
+24568
-21314
No files matched your search
@@ -0,0 +1,101 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/core"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/icons"
|
||||
"github.com/ZacharyZhang-NY/MujicaUI/internal"
|
||||
"github.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
|
||||
}
|
||||
Reference in new issue
Block a user