Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,17 @@ type dashboardGeometry struct {
}

func (m Model) dashboardLayout() dashboardGeometry {
if m.geometry == nil {
return m.computeDashboardLayout()
}
if m.geometry.baseDashboard == nil {
layout := m.computeDashboardLayout()
m.geometry.baseDashboard = &layout
}
return *m.geometry.baseDashboard
}

func (m Model) computeDashboardLayout() dashboardGeometry {
width := m.width
if width == 0 {
width = 80
Expand Down
3 changes: 2 additions & 1 deletion internal/ui/monitor_approval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TestMonitorApprovalRenderedTargets(t *testing.T) {
}
g := m.dashboardLayout()
shown := m.monitorApprovalControls(g.contentWidth, g.meterHeight)
hit := m.monitorContextHitTester()
for i, option := range m.monitorSessionData[0].preview.ApprovalOptions {
if option.Kind == "" {
continue
Expand All @@ -135,7 +136,7 @@ func TestMonitorApprovalRenderedTargets(t *testing.T) {
found = true
x := lipgloss.Width(line[:pos])
for dx := 0; dx < lipgloss.Width(label); dx++ {
if got := m.monitorContextAt(x+dx, y); got != action {
if got := hit(x+dx, y); got != action {
t.Fatalf("%s miss at %d,%d got %q\n%s", action, x+dx, y, got, ansi.Strip(out))
}
}
Expand Down
13 changes: 12 additions & 1 deletion internal/ui/monitor_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,17 @@ func (m Model) updateMonitorContextKey(key string) (Model, tea.Cmd, bool) {
}

func (m Model) monitorContextAt(x, y int) string {
return m.monitorContextHitTester()(x, y)
}

// Reuse geometry only while probing one unchanged screen. Live input starts
// a new pass so a resize, telemetry update or approval expiry cannot reuse it.
func (m Model) monitorContextHitTester() func(x, y int) string {
m.geometry = &monitorGeometryCache{}
return m.monitorContextAtInPass
}

func (m Model) monitorContextAtInPass(x, y int) string {
if m.meterView != viewMonitor || m.loading && len(m.snapshot.Meters()) == 0 {
return ""
}
Expand Down Expand Up @@ -449,7 +459,8 @@ func (m Model) monitorContextAt(x, y int) string {
return hit
}
}
if hit := monitorButtonAt(m.monitorApprovalButtons(g.contentWidth, g.meterHeight), g.meterHeight, x, y); hit != "" {
approvalButtons := m.geometry.approvalButtons.at(g.contentWidth, g.meterHeight, m.monitorApprovalButtons)
if hit := monitorButtonAt(approvalButtons, g.meterHeight, x, y); hit != "" {
return hit
}
if r, ok := contextActionRect(g.contentWidth, 0, monitorDismissLabel); ok && r.contains(x, y) {
Expand Down
7 changes: 7 additions & 0 deletions internal/ui/monitor_detail_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func (m Model) layoutDetailControls(width, height int) detailControlLayout {
}

func (m Model) layoutDetailControlsBase(width, height int) detailControlLayout {
if m.geometry != nil {
return m.geometry.detailControls.at(width, height, m.computeDetailControlsBase)
}
return m.computeDetailControlsBase(width, height)
}

func (m Model) computeDetailControlsBase(width, height int) detailControlLayout {
if s, ok := m.contextDetailSession(); ok {
if buttons := m.profileButtons(width, height, s); len(buttons) > 0 {
return detailControlLayout{kind: "profile", rows: buttons[len(buttons)-1].y + 1}
Expand Down
23 changes: 23 additions & 0 deletions internal/ui/monitor_efficiency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

tea "charm.land/bubbletea/v2"
"github.com/charmbracelet/x/ansi"
"github.com/merefield/codexometer/internal/codex"
"github.com/merefield/codexometer/internal/i18n"
Expand Down Expand Up @@ -70,6 +71,28 @@ func TestMonitorGeometryPassMatchesUncached(t *testing.T) {
}
}

func TestMonitorHitTestPassDoesNotKeepExpiredApprovalClickable(t *testing.T) {
m := approvalTestModel()
label := m.approvalButtonLabel("accept", false, 0)
x, y := renderedTextStart(t, m, label)
hit := m.monitorContextHitTester()
if hit(x, y) != "decision:0" || hit(x+1, y) != "decision:0" {
t.Fatal("active approval did not cover its rendered button")
}

// Approval state may change independently of the UI model when the
// app-server consumes a request. Live clicks must start a new pass.
provider := m.fetcher.(*approvalTestClient)
provider.token = "replacement"
if got := m.monitorContextAt(x, y); got == "decision:0" {
t.Fatal("fresh hit test retained the obsolete approval")
}
updated, cmd := m.Update(tea.MouseClickMsg{X: x, Y: y, Button: tea.MouseLeft})
if updated.(Model).monitorApprovalBusy || cmd != nil || m.geometry != nil {
t.Fatal("live click used stale approval geometry or leaked the read-only cache")
}
}

func TestMonitorEditorConfigurationStableAndResponsive(t *testing.T) {
e := newMonitorEditor()
e.SetValue(strings.Repeat("wrapped text ", 30))
Expand Down
5 changes: 3 additions & 2 deletions internal/ui/monitor_prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,15 @@ func TestMonitorPromptWrapsAndGrowsUpward(t *testing.T) {
if lipgloss.Width(out) > m.width || lipgloss.Height(out) > m.height {
t.Fatalf("overflow %v\n%s", size, ansi.Strip(out))
}
hit := m.monitorContextHitTester()
for row := y; row < y+rows-1; row++ {
for x := 4; x < g.contentWidth; x++ {
if got := m.monitorContextAt(x, g.meterY+row); got != "prompt" {
if got := hit(x, g.meterY+row); got != "prompt" {
t.Fatalf("wrapped input miss %v %d,%d: %s", size, x, row, got)
}
}
}
if m.monitorContextAt(4, g.meterY+y+rows-1) != "prompt" {
if hit(4, g.meterY+y+rows-1) != "prompt" {
t.Fatal("bottom hint line should focus the composer")
}
if m.monitorPrompt.input.Value() != value {
Expand Down
22 changes: 20 additions & 2 deletions internal/ui/monitor_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,26 @@ func (m Model) renderMonitorAttention(width, rows int, buttons []monitorNavigati
// Overview attention fits between the summary and session rows; retain at least
// one useful row. The full detail budget additionally protects controls/text.
type monitorGeometryCache struct {
areas map[[2]int]monitorGeometry
dashboard *dashboardGeometry
areas map[[2]int]monitorGeometry
baseDashboard *dashboardGeometry
dashboard *dashboardGeometry
detailControls monitorSizedCache[detailControlLayout]
approvalButtons monitorSizedCache[[]monitorApprovalButton]
}

type monitorSizedCache[T any] map[[2]int]T

func (c *monitorSizedCache[T]) at(width, height int, compute func(int, int) T) T {
key := [2]int{width, height}
if v, ok := (*c)[key]; ok {
return v
}
v := compute(width, height)
if *c == nil {
*c = make(monitorSizedCache[T])
}
(*c)[key] = v
return v
}

func (m Model) monitorArea(width, height int) monitorGeometry {
Expand Down
Loading