fix(ui): scroll to the properly select model

Kieran Klukas created

Change summary

internal/ui/dialog/models.go      |  6 +++++-
internal/ui/dialog/models_list.go | 15 ++++++---------
2 files changed, 11 insertions(+), 10 deletions(-)

Detailed changes

internal/ui/dialog/models.go 🔗

@@ -496,7 +496,11 @@ func (m *Models) setProviderItems() error {
 	// Set model groups in the list.
 	m.list.SetGroups(groups...)
 	m.list.SetSelectedItem(selectedItemID)
-	m.list.ScrollToTop()
+	if selectedItemID != "" {
+		m.list.ScrollToSelected()
+	} else {
+		m.list.ScrollToTop()
+	}
 
 	// Update placeholder based on model type
 	if !m.isOnboarding {

internal/ui/dialog/models_list.go 🔗

@@ -85,18 +85,15 @@ func (f *ModelsList) SetSelected(index int) {
 // SetSelectedItem sets the selected item in the list by item ID.
 func (f *ModelsList) SetSelectedItem(itemID string) {
 	if itemID == "" {
-		f.SetSelected(0)
 		return
 	}
 
-	count := 0
-	for _, g := range f.groups {
-		for _, item := range g.Items {
-			if item.ID() == itemID {
-				f.SetSelected(count)
-				return
-			}
-			count++
+	// Walk the selectable model items using the same helpers that
+	// keyboard navigation uses, so we stay in sync with the flat
+	// list layout.
+	for ok := f.SelectFirst(); ok; ok = f.SelectNext() {
+		if mi, is := f.SelectedItem().(*ModelItem); is && mi.ID() == itemID {
+			return
 		}
 	}
 }