Skip to content

Commit

Permalink
chore: clean up active pane mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Dec 22, 2024
1 parent 41a5ce2 commit 134e53e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 69 deletions.
9 changes: 0 additions & 9 deletions internal/tui/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,3 @@ type FilterCloseMsg struct{}

// FilterKeyMsg is a key entered by the user into the filter widget
type FilterKeyMsg tea.KeyMsg

// FocusExplorerMsg switches the focus to the explorer pane.
type FocusExplorerMsg struct{}

// FocusPaneMsg is sent to a model when it focused
type FocusPaneMsg struct{}

// UnfocusPaneMsg is sent to a model when it unfocused
type UnfocusPaneMsg struct{}
88 changes: 41 additions & 47 deletions internal/tui/pane_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ type PaneManager struct {
makers map[Kind]Maker
// cache of previously made models
cache *Cache
// the position of the currently active pane
active Position
// the position of the currently focused pane
focused Position
// panes tracks currently visible panes
panes map[Position]pane
// total width and height of the terminal space available to panes.
Expand Down Expand Up @@ -82,7 +82,7 @@ func (p *PaneManager) Update(msg tea.Msg) tea.Cmd {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Common.Back):
if p.active != TopRightPane {
if p.focused != TopRightPane {
// History is only maintained for the top right pane.
break
}
Expand Down Expand Up @@ -110,20 +110,20 @@ func (p *PaneManager) Update(msg tea.Msg) tea.Cmd {
p.updateTopRightHeight(1)
p.updateChildSizes()
case key.Matches(msg, keys.Navigation.SwitchPane):
p.cycleActivePane(false)
p.cycleFocusedPane(false)
case key.Matches(msg, keys.Navigation.SwitchPaneBack):
p.cycleActivePane(true)
p.cycleFocusedPane(true)
case key.Matches(msg, keys.Global.ClosePane):
cmds = append(cmds, p.closeActivePane())
cmds = append(cmds, p.closeFocusedPane())
case key.Matches(msg, keys.Navigation.LeftPane):
cmds = append(cmds, p.focusPane(LeftPane))
p.focusPane(LeftPane)
case key.Matches(msg, keys.Navigation.TopRightPane):
cmds = append(cmds, p.focusPane(TopRightPane))
p.focusPane(TopRightPane)
case key.Matches(msg, keys.Navigation.BottomRightPane):
cmds = append(cmds, p.focusPane(BottomRightPane))
p.focusPane(BottomRightPane)
default:
// Send remaining keys to active pane
cmds = append(cmds, p.updateModel(p.active, msg))
// Send remaining keys to focused pane
cmds = append(cmds, p.updateModel(p.focused, msg))
}
case tea.WindowSizeMsg:
p.width = msg.Width
Expand Down Expand Up @@ -157,41 +157,42 @@ func (p *PaneManager) Update(msg tea.Msg) tea.Cmd {
return tea.Batch(cmds...)
}

// ActiveModel retrieves the model of the active pane.
func (p *PaneManager) ActiveModel() ChildModel {
return p.panes[p.active].model
// FocusedModel retrieves the model of the focused pane.
func (p *PaneManager) FocusedModel() ChildModel {
return p.panes[p.focused].model
}

// cycleActivePane makes the next pane the active pane. If last is true then the
// previous pane is made the active pane.
func (p *PaneManager) cycleActivePane(last bool) tea.Cmd {
// cycleFocusedPane makes the next pane the focused pane. If last is true then the
// previous pane is made the focused pane.
func (p *PaneManager) cycleFocusedPane(last bool) {
positions := maps.Keys(p.panes)
slices.Sort(positions)
var activeIndex int
var focusedIndex int
for i, pos := range positions {
if pos == p.active {
activeIndex = i
if pos == p.focused {
focusedIndex = i
}
}
var newActiveIndex int
var newFocusedIndex int
if last {
newActiveIndex = activeIndex - 1
if newActiveIndex < 0 {
newActiveIndex = len(positions) + newActiveIndex
newFocusedIndex = focusedIndex - 1
if newFocusedIndex < 0 {
newFocusedIndex = len(positions) + newFocusedIndex
}
} else {
newActiveIndex = (activeIndex + 1) % len(positions)
newFocusedIndex = (focusedIndex + 1) % len(positions)
}
return p.focusPane(positions[newActiveIndex])
p.focusPane(positions[newFocusedIndex])
}

func (p *PaneManager) closeActivePane() tea.Cmd {
func (p *PaneManager) closeFocusedPane() tea.Cmd {
if len(p.panes) == 1 {
return ReportError(errors.New("cannot close last pane"))
}
delete(p.panes, p.active)
delete(p.panes, p.focused)
p.updateChildSizes()
return p.cycleActivePane(false)
p.cycleFocusedPane(false)
return nil
}

func (p *PaneManager) updateLeftWidth(delta int) {
Expand All @@ -210,7 +211,7 @@ func (p *PaneManager) updateTopRightHeight(delta int) {
// There is no horizontal split to adjust
return
}
switch p.active {
switch p.focused {
case BottomRightPane:
delta = -delta
}
Expand All @@ -234,7 +235,7 @@ func (m *PaneManager) setPane(msg NavigationMsg) (cmd tea.Cmd) {
if pane, ok := m.panes[msg.Position]; ok && pane.page == msg.Page {
// Pane is already showing requested page, so just bring it into focus.
if !msg.DisableFocus {
return m.focusPane(msg.Position)
m.focusPane(msg.Position)
}
return nil
}
Expand Down Expand Up @@ -265,24 +266,17 @@ func (m *PaneManager) setPane(msg NavigationMsg) (cmd tea.Cmd) {
}
m.updateChildSizes()
if !msg.DisableFocus {
focus := m.focusPane(msg.Position)
cmd = tea.Batch(focus, cmd)
m.focusPane(msg.Position)
}
return cmd
}

func (m *PaneManager) focusPane(position Position) tea.Cmd {
func (m *PaneManager) focusPane(position Position) {
if _, ok := m.panes[position]; !ok {
// There is no pane to focus at requested position
return nil
}
var cmds []tea.Cmd
if previous, ok := m.panes[m.active]; ok {
cmds = append(cmds, previous.model.Update(UnfocusPaneMsg{}))
return
}
m.active = position
cmds = append(cmds, m.panes[m.active].model.Update(FocusPaneMsg{}))
return tea.Batch(cmds...)
m.focused = position
}

func (m *PaneManager) paneWidth(position Position) int {
Expand Down Expand Up @@ -332,7 +326,7 @@ func (m *PaneManager) renderPane(position Position) string {
return ""
}
model := m.panes[position].model
isActive := position == m.active
isFocused := position == m.focused
renderedPane := lipgloss.NewStyle().
Width(m.paneWidth(position) - 2). // -2 for border
Height(m.paneHeight(position) - 2). // -2 for border
Expand All @@ -345,7 +339,7 @@ func (m *PaneManager) renderPane(position Position) string {
}); ok {
borderTexts = textInBorder.BorderText()
}
if !isActive {
if !isFocused {
switch position {
case LeftPane:
borderTexts[TopRightBorder] = keys.Navigation.LeftPane.Keys()[0]
Expand All @@ -355,15 +349,15 @@ func (m *PaneManager) renderPane(position Position) string {
borderTexts[TopRightBorder] = keys.Navigation.BottomRightPane.Keys()[0]
}
}
return borderize(renderedPane, isActive, borderTexts)
return borderize(renderedPane, isFocused, borderTexts)
}

func (m *PaneManager) HelpBindings() (bindings []key.Binding) {
if m.active == TopRightPane {
if m.focused == TopRightPane {
// Only the top right pane has the ability to "go back"
bindings = append(bindings, keys.Common.Back)
}
if model, ok := m.ActiveModel().(ModelHelpBindings); ok {
if model, ok := m.FocusedModel().(ModelHelpBindings); ok {
bindings = append(bindings, model.HelpBindings()...)
}
return bindings
Expand Down
5 changes: 0 additions & 5 deletions internal/tui/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type Model[V resource.Resource] struct {
cols []Column
rows []Row[V]
rowRenderer RowRenderer[V]
focus bool
rendered map[resource.ID]RenderedRow

border lipgloss.Border
Expand Down Expand Up @@ -242,10 +241,6 @@ func (m Model[V]) Update(msg tea.Msg) (Model[V], tea.Cmd) {
// Filter table items
m.setRows(maps.Values(m.items)...)
return m, cmd
case tui.FocusPaneMsg:
m.focus = true
case tui.UnfocusPaneMsg:
m.focus = false
default:
// Send any other messages to the filter if it is focused.
if m.filter.Focused() {
Expand Down
2 changes: 0 additions & 2 deletions internal/tui/task/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
"Retry task?",
m.CreateTasksWithSpecs(m.task.Spec),
)
case key.Matches(msg, keys.Navigation.SwitchPane):
return tui.CmdHandler(tui.FocusExplorerMsg{})
default:
cmd := m.common.Update(msg)
cmds = append(cmds, cmd)
Expand Down
12 changes: 6 additions & 6 deletions internal/tui/top/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// switch back to normal mode, blur the filter widget, and let
// the key handler below handle the quit action.
m.mode = normalMode
cmd = m.ActiveModel().Update(tui.FilterBlurMsg{})
cmd = m.FocusedModel().Update(tui.FilterBlurMsg{})
return m, cmd
case key.Matches(msg, keys.Filter.Blur):
// Switch back to normal mode, and send message to current model
// to blur the filter widget
m.mode = normalMode
cmd = m.ActiveModel().Update(tui.FilterBlurMsg{})
cmd = m.FocusedModel().Update(tui.FilterBlurMsg{})
return m, cmd
case key.Matches(msg, keys.Filter.Close):
// Switch back to normal mode, and send message to current model
// to close the filter widget
m.mode = normalMode
cmd = m.ActiveModel().Update(tui.FilterCloseMsg{})
cmd = m.FocusedModel().Update(tui.FilterCloseMsg{})
return m, cmd
default:
// Wrap key message in a filter key message and send to current
// model.
cmd = m.ActiveModel().Update(tui.FilterKeyMsg(msg))
cmd = m.FocusedModel().Update(tui.FilterKeyMsg(msg))
return m, cmd
}
}
Expand All @@ -252,7 +252,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.Global.Filter):
// '/' enables filter mode if the current model indicates it
// supports it, which it does so by sending back a non-nil command.
if cmd = m.ActiveModel().Update(tui.FilterFocusReqMsg{}); cmd != nil {
if cmd = m.FocusedModel().Update(tui.FilterFocusReqMsg{}); cmd != nil {
m.mode = filterMode
}
return m, cmd
Expand Down Expand Up @@ -303,7 +303,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.mode == promptMode {
cmd = m.prompt.HandleBlink(msg)
} else {
cmd = m.ActiveModel().Update(msg)
cmd = m.FocusedModel().Update(msg)
}
return m, cmd
default:
Expand Down

0 comments on commit 134e53e

Please sign in to comment.