diff --git a/activity_feed.go b/activity_feed.go new file mode 100644 index 0000000..c0406b1 --- /dev/null +++ b/activity_feed.go @@ -0,0 +1,137 @@ +package mujica + +import ( + "cmp" + "log/slog" + "time" + + "github.com/egoist/mygo/ui" + + "mujicaui/icons" + "mujicaui/theme" +) + +// Activity is one event of an ActivityFeed: Actor did Action to Object at Time. +type Activity struct { + ID string + Actor string + Action string + Object string + Time time.Time +} + +// ActivityFeedOptions configure an ActivityFeed. +type ActivityFeedOptions struct { + // Location decides which day an event falls on; nil is time.Local. + Location *time.Location + // Today fixes the day called Today; zero uses the clock. + Today Date +} + +// activityRow is a day header (item -1) or an event. +type activityRow struct { + day Date + item int +} + +// activityFeedCache keeps the grouped rows until the count, first or last event, or location changes. +type activityFeedCache struct { + n int + first, last string + loc *time.Location + rows []activityRow + scroll ui.ScrollState + sized bool +} + +// ActivityFeed lists events grouped by day, newest first as given, building only the rows in view. +func ActivityFeed(c *ui.Context, items []Activity, opts ActivityFeedOptions) *ui.Element { + loc := cmp.Or(opts.Location, time.Local) + for _, a := range items { + if a.ID == "" || a.Actor == "" || a.Action == "" || a.Time.IsZero() { + panic("mujica: ActivityFeed event needs an ID, Actor, Action and Time") + } + } + k := Tokens(c) + d := Density(c) + sc := ui.Scroll(c).MinHeight(0).Role(ui.RoleList).Label(msg(c, "activity_feed.label")) + if len(items) == 0 { + sc.Children(func() { + EmptyState(c, EmptyStateOptions{Title: msg(c, "activity_feed.empty"), Description: msg(c, "activity_feed.empty_hint"), Icon: icons.Must("clock")}) + }) + return sc + } + cache := *ui.Local(sc, "activity_feed", func() *activityFeedCache { return &activityFeedCache{} }) + sc.TrackScroll(&cache.scroll) + if cache.n != len(items) || cache.first != items[0].ID || cache.last != items[len(items)-1].ID || cache.loc != loc { + cache.n, cache.first, cache.last, cache.loc = len(items), items[0].ID, items[len(items)-1].ID, loc + cache.rows = cache.rows[:0] + var prev Date + for i, a := range items { + if day := DateOf(a.Time.In(loc)); day != prev || i == 0 { + cache.rows = append(cache.rows, activityRow{day: day, item: -1}) + prev = day + } + cache.rows = append(cache.rows, activityRow{item: i}) + } + slog.Debug("mujica: activity_feed grouped", "events", len(items), "rows", len(cache.rows)) + } + rowH := d.ControlHeight() + 2*d.Unit() + view := sc.Bounds() + if view.H == 0 && !cache.sized { + // Unmeasured: build again once the box has its height. + cache.sized = true + c.Invalidate() + } + first := max(0, int(cache.scroll.Y/rowH)-2) + last := min(len(cache.rows), int((cache.scroll.Y+max(view.H, rowH))/rowH)+3) + today := datetimeToday(c, opts.Today) + sc.Children(func() { + ui.Box(c).FillWidth().Height(rowH * float32(len(cache.rows))).Children(func() { + for r := first; r < last; r++ { + row := cache.rows[r] + box := ui.Row(c).Key(r).Absolute().Left(0).Right(0).Top(rowH*float32(r)).Height(rowH). + AlignItems(ui.Center).Gap(d.Unit()*3).Padding(0, d.Unit()*3) + if row.item < 0 { + activityFeedHeader(c, k, box, row.day, today) + continue + } + activityFeedEvent(c, k, box, items[row.item], loc) + } + }) + }) + return sc +} + +// activityFeedHeader is a day's title: Today, Yesterday or the date, over an ornament rule. +func activityFeedHeader(c *ui.Context, k theme.Tokens, box *ui.Element, day, today Date) { + label := datetimeDayLabel(c, day) + switch day { + case today: + label = msg(c, "activity_feed.today") + case today.AddDays(-1): + label = msg(c, "activity_feed.yesterday") + } + box.Role(ui.RoleText).AlignItems(ui.End).Padding(0, Density(c).Unit()*3, Density(c).Unit(), Density(c).Unit()*3) + box.Draw(func(p *ui.Painter, r ui.Rect) { p.Fill(ui.Rect{X: r.X, Y: r.Y + r.H - 1, W: r.W, H: 1}, k.Ornament, 0) }) + box.Children(func() { + ui.Text(c, label).Font(theme.SerifFont).FontSize(FontSize(c, theme.CaptionSize+1)).FontWeight(600).TextColor(k.Text).SingleLine() + }) +} + +// activityFeedEvent is one event: avatar, "Actor action Object", and the time. +func activityFeedEvent(c *ui.Context, k theme.Tokens, box *ui.Element, a Activity, loc *time.Location) { + when := a.Time.In(loc).Format("15:04") + box.Role(ui.RoleListItem).Label(a.Actor + " " + a.Action + " " + a.Object + ", " + when) + box.Children(func() { + Avatar(c, a.Actor, AvatarOptions{Size: 28}) + ui.Row(c).Grow(1).Shrink(1).MinWidth(0).Gap(4).Children(func() { + ui.Text(c, a.Actor).Bold().TextColor(k.Text).SingleLine().Shrink(0) + ui.Text(c, a.Action).TextColor(k.TextMuted).SingleLine().Shrink(0) + if a.Object != "" { + ui.Text(c, a.Object).TextColor(k.AccentText).SingleLine().Shrink(1).Ellipsis("…") + } + }) + ui.Text(c, when).TextColor(k.TextMuted).FontSize(FontSize(c, theme.CaptionSize)).FontFeatures("tnum").SingleLine() + }) +} diff --git a/activity_feed_test.go b/activity_feed_test.go new file mode 100644 index 0000000..2d408de --- /dev/null +++ b/activity_feed_test.go @@ -0,0 +1,92 @@ +package mujica + +import ( + "strconv" + "testing" + "time" + + "github.com/egoist/mygo/ui" +) + +func feedItems(n int) []Activity { + base := time.Date(2026, 10, 5, 18, 0, 0, 0, time.UTC) + out := make([]Activity, n) + for i := range out { + out[i] = Activity{ID: strconv.Itoa(i), Actor: "Mira", Action: "edited", Object: "Doc " + strconv.Itoa(i), Time: base.Add(-time.Duration(i) * 5 * time.Hour)} + } + return out +} + +func TestActivityFeedGroupsByDay(t *testing.T) { + items := feedItems(6) // 18:00, 13:00, 08:00, 03:00 on the 5th, then 22:00 and 17:00 on the 4th + tt := ui.NewTester(func(c *ui.Context) { + Use(c, Settings{}) + ActivityFeed(c, items, ActivityFeedOptions{Location: time.UTC, Today: Date{2026, 10, 5}}).Size(600, 500) + }, 640, 540) + tt.Frame() + for _, s := range []string{"Today", "Yesterday", "Mira edited Doc 0, 18:00", "Mira edited Doc 4, 22:00"} { + if _, ok := tt.Find(s); !ok { + t.Errorf("missing %q in %q", s, tt.Texts()) + } + } + today, _ := tt.Find("Today") + yest, _ := tt.Find("Yesterday") + doc3, _ := tt.Find("Mira edited Doc 3, 03:00") + doc4, _ := tt.Find("Mira edited Doc 4, 22:00") + if !(today.Y < doc3.Y && doc3.Y < yest.Y && yest.Y < doc4.Y) { + t.Fatalf("order: today %v, doc3 %v, yesterday %v, doc4 %v", today, doc3, yest, doc4) + } + // Another zone moves 03:00 UTC to the previous day. + tt2 := ui.NewTester(func(c *ui.Context) { + Use(c, Settings{Locale: ZhCN}) + ActivityFeed(c, items, ActivityFeedOptions{Location: time.FixedZone("W", -5*3600), Today: Date{2026, 10, 5}}).Size(600, 500) + }, 640, 540) + tt2.Frame() + if _, ok := tt2.Find("Mira edited Doc 3, 22:00"); !ok || !tt2.HasText("今天") || !tt2.HasText("昨天") { + t.Fatalf("zone texts %q", tt2.Texts()) + } +} + +func TestActivityFeedVirtualizesAndEmpty(t *testing.T) { + items := feedItems(100000) + tt := ui.NewTester(func(c *ui.Context) { + Use(c, Settings{}) + ActivityFeed(c, items, ActivityFeedOptions{Location: time.UTC, Today: Date{2026, 10, 5}}).Size(600, 400) + }, 640, 440) + tt.Frame() + if n := len(tt.Texts()); n == 0 || n > 120 { + t.Fatalf("built %d texts for 100000 events", n) + } + tt.Scroll(100, 100, 0, 2000000) + tt.Frame() + if _, ok := tt.Find("Mira edited Doc 0, 18:00"); ok { + t.Fatal("first event still built after scrolling far") + } + tt3 := ui.NewTester(func(c *ui.Context) { + Use(c, Settings{}) + ActivityFeed(c, nil, ActivityFeedOptions{}).Size(400, 300) + }, 440, 340) + if !tt3.HasText("No activity yet") { + t.Fatalf("empty: %q", tt3.Texts()) + } + defer func() { + if recover() == nil { + t.Fatal("event without ID accepted") + } + }() + ui.NewTester(func(c *ui.Context) { + Use(c, Settings{}) + ActivityFeed(c, []Activity{{Actor: "x"}}, ActivityFeedOptions{}) + }, 200, 100) +} + +func TestActivityFeedSnapshot(t *testing.T) { + items := feedItems(5) + items[1].Actor, items[1].Action, items[1].Object = "Lin", "commented on", "Quarterly report" + items[3].Actor, items[3].Action, items[3].Object = "Sakiko", "approved", "Merge request !42" + snapshot(t, "activity_feed", 520, 320, func(c *ui.Context) { + ui.Column(c).Padding(12).Children(func() { + ActivityFeed(c, items, ActivityFeedOptions{Location: time.UTC, Today: Date{2026, 10, 5}}).Size(496, 296) + }) + }) +} diff --git a/messages_essentials.go b/messages_essentials.go index 5c9e9d2..4c8abe1 100644 --- a/messages_essentials.go +++ b/messages_essentials.go @@ -36,6 +36,12 @@ func init() { "dock_panel.zone3": {"Bottom dock", "底部停靠区"}, "dock_panel.hint": {"Drag to an edge, or Shift+Alt+arrow, to dock", "拖到边缘,或按 Shift+Alt+方向键停靠"}, + "activity_feed.label": {"Activity", "活动"}, + "activity_feed.today": {"Today", "今天"}, + "activity_feed.yesterday": {"Yesterday", "昨天"}, + "activity_feed.empty": {"No activity yet", "暂无活动"}, + "activity_feed.empty_hint": {"Changes people make will show here.", "成员的操作会显示在这里。"}, + "qr_code.label": {"QR code", "二维码"}, "qr_code.too_long": {"Text too long for a QR code", "文字过长,无法生成二维码"}, }) diff --git a/resizable_panel_group_test.go b/resizable_panel_group_test.go index 7deb347..6c56463 100644 --- a/resizable_panel_group_test.go +++ b/resizable_panel_group_test.go @@ -106,7 +106,10 @@ func TestResizablePanelGroupRejects(t *testing.T) { } }() s := tc.sizes - ui.NewTester(func(c *ui.Context) { Use(c, Settings{}); ResizablePanelGroup(c, &s, tc.panels, ResizablePanelGroupOptions{}) }, 300, 200) + ui.NewTester(func(c *ui.Context) { + Use(c, Settings{}) + ResizablePanelGroup(c, &s, tc.panels, ResizablePanelGroupOptions{}) + }, 300, 200) }() } } diff --git a/testdata/snapshots/darwin/activity_feed-dark.png b/testdata/snapshots/darwin/activity_feed-dark.png new file mode 100644 index 0000000..b6a09e5 Binary files /dev/null and b/testdata/snapshots/darwin/activity_feed-dark.png differ diff --git a/testdata/snapshots/darwin/activity_feed-light.png b/testdata/snapshots/darwin/activity_feed-light.png new file mode 100644 index 0000000..9e25971 Binary files /dev/null and b/testdata/snapshots/darwin/activity_feed-light.png differ