refactor(ui): replace hardcoded cursor offset with style-based calculation (#2530)

huaiyuWangh created

Remove FIXME workaround in onboarding dialogs that used hardcoded
cur.Y -= 1 / cur.X -= 1 to adjust cursor position. Extract a new
AdjustOnboardingInputCursor helper that computes the correct offset
from Dialog.View border/padding/margin, eliminating code duplication
between api_key_input.go and models.go.

Change summary

internal/ui/dialog/api_key_input.go |  7 +------
internal/ui/dialog/common.go        | 18 ++++++++++++++++++
internal/ui/dialog/models.go        |  7 +------
3 files changed, 20 insertions(+), 12 deletions(-)

Detailed changes

internal/ui/dialog/api_key_input.go 🔗

@@ -182,13 +182,8 @@ func (m *APIKeyInput) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
 
 	if m.isOnboarding {
 		view := content
+		cur = adjustOnboardingInputCursor(t, cur)
 		DrawOnboardingCursor(scr, area, view, cur)
-
-		// FIXME(@andreynering): Figure it out how to properly fix this
-		if cur != nil {
-			cur.Y -= 1
-			cur.X -= 1
-		}
 	} else {
 		view := dialogStyle.Render(content)
 		DrawCenterCursor(scr, area, view, cur)

internal/ui/dialog/common.go 🔗

@@ -38,6 +38,24 @@ func InputCursor(t *styles.Styles, cur *tea.Cursor) *tea.Cursor {
 	return cur
 }
 
+// adjustOnboardingInputCursor removes the dialog view frame offset from an
+// input cursor. Onboarding dialogs render without Dialog.View frame, while
+// InputCursor includes that frame offset for regular dialogs.
+func adjustOnboardingInputCursor(t *styles.Styles, cur *tea.Cursor) *tea.Cursor {
+	if cur == nil {
+		return nil
+	}
+
+	dialogStyle := t.Dialog.View
+	cur.X -= dialogStyle.GetBorderLeftSize() +
+		dialogStyle.GetPaddingLeft() +
+		dialogStyle.GetMarginLeft()
+	cur.Y -= dialogStyle.GetBorderTopSize() +
+		dialogStyle.GetPaddingTop() +
+		dialogStyle.GetMarginTop()
+	return cur
+}
+
 // RenderContext is a dialog rendering context that can be used to render
 // common dialog layouts.
 type RenderContext struct {

internal/ui/dialog/models.go 🔗

@@ -292,13 +292,8 @@ func (m *Models) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
 		rc.TitleInfo = ""
 		rc.IsOnboarding = true
 		view := rc.Render()
+		cur = adjustOnboardingInputCursor(t, cur)
 		DrawOnboardingCursor(scr, area, view, cur)
-
-		// FIXME(@andreynering): Figure it out how to properly fix this
-		if cur != nil {
-			cur.Y -= 1
-			cur.X -= 1
-		}
 	} else {
 		view := rc.Render()
 		DrawCenterCursor(scr, area, view, cur)