Tui fix focus
This commit is contained in:
@@ -31,8 +31,8 @@ func NewTextInput(placeholder string, defaultValue string) *TextInput {
|
||||
ti := textinput.New()
|
||||
ti.Placeholder = placeholder
|
||||
ti.SetValue(defaultValue)
|
||||
ti.Focus()
|
||||
return &TextInput{Model: ti, focused: true}
|
||||
ti.Blur()
|
||||
return &TextInput{Model: ti, focused: false}
|
||||
}
|
||||
|
||||
func (t *TextInput) Focus() tea.Cmd {
|
||||
@@ -135,9 +135,14 @@ func NewFocusManager(loop bool) *FocusManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Register 注册可聚焦组件(指定标识和切换顺序)
|
||||
/*
|
||||
Register 注册可聚焦组件(指定标识和切换顺序)
|
||||
ID 组件的唯一标识,用于后续切换和获取焦点
|
||||
例如 "form1.ip_input"、"form1.next_btn"
|
||||
*/
|
||||
func (fm *FocusManager) Register(id string, comp Focusable) {
|
||||
// 防御性检查:避免 components 为空导致 panic
|
||||
|
||||
// 防御性检查:避免 components 未初始化为nil导致 panic
|
||||
if fm.components == nil {
|
||||
fm.components = make(map[string]Focusable)
|
||||
}
|
||||
@@ -147,6 +152,8 @@ func (fm *FocusManager) Register(id string, comp Focusable) {
|
||||
return
|
||||
}
|
||||
|
||||
// id : accept_btn, form1.reject_btn
|
||||
// comp: 接受协议按钮, 拒绝协议按钮
|
||||
fm.components[id] = comp
|
||||
fm.order = append(fm.order, id)
|
||||
|
||||
@@ -211,6 +218,7 @@ func (fm *FocusManager) Prev() tea.Cmd {
|
||||
return nil
|
||||
}
|
||||
|
||||
//fm.components[fm.currentFocusID].Blur()
|
||||
fm.components[fm.currentFocusID].Blur()
|
||||
prevID := fm.order[prevIdx]
|
||||
fm.currentFocusID = prevID
|
||||
@@ -259,23 +267,22 @@ func (m *model) initPageFocus(page PageType) {
|
||||
|
||||
m.focusManager = NewFocusManager(true)
|
||||
|
||||
// 获取当前页面的组件
|
||||
pageComps, exists := m.pageComponents[page]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
// 按 [业务逻辑顺序] 注册组件 (决定Tab切换的顺序)
|
||||
var componentOrder []string
|
||||
var defaultFocusID string
|
||||
|
||||
// 按页面类型定义不同的注册顺序
|
||||
switch page {
|
||||
case PageAgreement:
|
||||
componentOrder = []string{"accept_btn", "reject_btn"}
|
||||
defaultFocusID = "accept_btn"
|
||||
case PageData:
|
||||
componentOrder = []string{
|
||||
"Homepage_input",
|
||||
"Hostname_input",
|
||||
"ClusterName_input",
|
||||
"Country_input",
|
||||
"Region_input",
|
||||
"Timezone_input",
|
||||
@@ -284,23 +291,28 @@ func (m *model) initPageFocus(page PageType) {
|
||||
"next_btn",
|
||||
"prev_btn",
|
||||
}
|
||||
defaultFocusID = "next_btn"
|
||||
case PagePublicNetwork:
|
||||
componentOrder = []string{
|
||||
"PublicInterface_input",
|
||||
"PublicIPAddress_input",
|
||||
"PublicNetmask_input",
|
||||
"PublicGateway_input",
|
||||
"PublicMTU_input",
|
||||
"next_btn",
|
||||
"prev_btn",
|
||||
}
|
||||
defaultFocusID = "next_btn"
|
||||
case PageInternalNetwork:
|
||||
componentOrder = []string{
|
||||
"InternalInterface_input",
|
||||
"InternalIPAddress_input",
|
||||
"InternalNetmask_input",
|
||||
"PrivateInterface_input",
|
||||
"PrivateIPAddress_input",
|
||||
"PrivateNetmask_input",
|
||||
"PrivateMTU_input",
|
||||
"next_btn",
|
||||
"prev_btn",
|
||||
}
|
||||
defaultFocusID = "next_btn"
|
||||
case PageDNS:
|
||||
componentOrder = []string{
|
||||
"Pri_DNS_input",
|
||||
@@ -308,14 +320,25 @@ func (m *model) initPageFocus(page PageType) {
|
||||
"next_btn",
|
||||
"prev_btn",
|
||||
}
|
||||
defaultFocusID = "next_btn"
|
||||
case PageSummary:
|
||||
componentOrder = []string{"confirm_btn", "cancel_btn"}
|
||||
defaultFocusID = "confirm_btn"
|
||||
}
|
||||
|
||||
// 注册组件到焦点管理器(按顺序)
|
||||
for _, compID := range componentOrder {
|
||||
if comp, exists := pageComps[compID]; exists {
|
||||
m.focusManager.Register(compID, comp)
|
||||
}
|
||||
}
|
||||
|
||||
if defaultFocusID != "" {
|
||||
if currentComp, exists := m.focusManager.GetCurrent(); exists {
|
||||
currentComp.Blur()
|
||||
}
|
||||
if targetComp, exists := pageComps[defaultFocusID]; exists {
|
||||
m.focusManager.currentFocusID = defaultFocusID
|
||||
targetComp.Focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user