Skip to content

Commit 681b8e6

Browse files
committed
fix(topbar/footer): subscriptions + lifecycle
1 parent 4a60264 commit 681b8e6

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

lua/opencode/api.lua

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,14 +352,10 @@ end
352352

353353
function M.agent_plan()
354354
state.current_mode = 'plan'
355-
-- TODO: topbar subscribe to current_mode
356-
require('opencode.ui.topbar').render()
357355
end
358356

359357
function M.agent_build()
360358
state.current_mode = 'build'
361-
-- TODO: topbar subscribe to current_mode
362-
require('opencode.ui.topbar').render()
363359
end
364360

365361
function M.select_agent()
@@ -371,9 +367,7 @@ function M.select_agent()
371367
return
372368
end
373369

374-
-- TODO: topbar subscribe to current_mode
375370
state.current_mode = selection
376-
require('opencode.ui.topbar').render()
377371
end)
378372
end
379373

@@ -390,8 +384,6 @@ function M.switch_mode()
390384
local next_index = (current_index % #modes) + 1
391385

392386
state.current_mode = modes[next_index]
393-
-- TODO: topbar subscribe to current_mode
394-
require('opencode.ui.topbar').render()
395387
end
396388

397389
function M.with_header(lines, show_welcome)

lua/opencode/ui/footer.lua

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ local icons = require('opencode.ui.icons')
55
local output_window = require('opencode.ui.output_window')
66
local snapshot = require('opencode.snapshot')
77
local config_file = require('opencode.config_file')
8+
89
local M = {}
910

10-
function M.render(windows)
11+
function M.render()
12+
local windows = state.windows
1113
if not output_window.mounted(windows) or not M.mounted(windows) then
1214
return
1315
end
@@ -43,6 +45,7 @@ function M.render(windows)
4345
append_to_footer(restore_point_text)
4446
end
4547

48+
---@diagnostic disable-next-line: need-check-nil
4649
local win_width = vim.api.nvim_win_get_width(windows.output_win)
4750
local footer_text = table.concat(segments, ' | ') .. ' '
4851
footer_text = string.rep(' ', win_width - #footer_text) .. footer_text
@@ -67,24 +70,36 @@ function M._build_footer_win_config(windows)
6770
}
6871
end
6972

73+
local function on_change(_, _, _)
74+
M.render()
75+
end
76+
77+
local function on_job_count_changed(_, new, old)
78+
if new == 0 or old == 0 then
79+
M.render()
80+
end
81+
end
82+
7083
function M.setup(windows)
7184
windows.footer_win = vim.api.nvim_open_win(windows.footer_buf, false, M._build_footer_win_config(windows))
7285
vim.api.nvim_set_option_value('winhl', 'Normal:OpenCodeHint', { win = windows.footer_win })
7386

7487
-- for stats changes
75-
state.subscribe('current_model', function(_, _, _)
76-
M.render(windows)
77-
end)
88+
state.subscribe('current_model', on_change)
89+
-- to show C-c message
90+
state.subscribe('job_count', on_job_count_changed)
91+
state.subscribe('restore_points', on_change)
92+
end
7893

79-
state.subscribe('job_count', function(_, new, old)
80-
if new == 0 or old == 0 then
81-
M.render(windows)
82-
end
83-
end)
94+
function M.close()
95+
if state.windows then
96+
pcall(vim.api.nvim_win_close, state.windows.footer_win, true)
97+
pcall(vim.api.nvim_buf_delete, state.windows.footer_buf, { force = true })
98+
end
8499

85-
state.subscribe('restore_points', function(_, _, _)
86-
M.render(windows)
87-
end)
100+
state.unsubscribe('current_model', on_change)
101+
state.unsubscribe('job_count', on_job_count_changed)
102+
state.unsubscribe('restore_points', on_change)
88103
end
89104

90105
function M.mounted(windows)
@@ -101,7 +116,7 @@ function M.update_window(windows)
101116
end
102117

103118
vim.api.nvim_win_set_config(windows.footer_win, M._build_footer_win_config(windows))
104-
M.render(windows)
119+
M.render()
105120
end
106121

107122
---@return integer
@@ -137,13 +152,4 @@ function M.set_content(lines)
137152
vim.api.nvim_set_option_value('modifiable', false, { buf = windows.footer_buf })
138153
end
139154

140-
function M.close()
141-
if not state.windows then
142-
return
143-
end
144-
145-
pcall(vim.api.nvim_win_close, state.windows.footer_win, true)
146-
pcall(vim.api.nvim_buf_delete, state.windows.footer_buf, { force = true })
147-
end
148-
149155
return M

lua/opencode/ui/loading_animation.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@ M.render = vim.schedule_wrap(function(windows)
3030
if not windows or not windows.output_buf or not windows.footer_buf then
3131
return false
3232
end
33-
if not vim.api.nvim_buf_is_valid(windows.output_buf) or not vim.api.nvim_buf_is_valid(windows.footer_buf) then
34-
return false
35-
end
3633

37-
local buffer_line_count = vim.api.nvim_buf_line_count(windows.output_buf)
38-
if buffer_line_count <= 0 then
34+
if not vim.api.nvim_buf_is_valid(windows.output_buf) or not vim.api.nvim_buf_is_valid(windows.footer_buf) then
3935
return false
4036
end
4137

lua/opencode/ui/output_renderer.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ M.render = vim.schedule_wrap(function(windows, force)
4343
vim.schedule(function()
4444
-- M.render_markdown()
4545
M.handle_auto_scroll(windows)
46-
require('opencode.ui.topbar').render()
4746
end)
4847

4948
pcall(function()
5049
vim.schedule(function()
5150
require('opencode.ui.mention').highlight_all_mentions(windows.output_buf)
5251
require('opencode.ui.contextual_actions').setup_contextual_actions()
53-
require('opencode.ui.footer').render(windows)
5452
end)
5553
end)
5654
end)

lua/opencode/ui/topbar.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,15 @@ function M.render()
103103
end)
104104
end
105105

106+
local function on_mode_changed(_, _, _)
107+
M.render()
108+
end
109+
106110
function M.setup()
111+
state.subscribe('current_mode', on_mode_changed)
107112
M.render()
108113
end
109114

115+
function M.close() end
116+
state.unsubscribe('current_mode', on_mode_changed)
110117
return M

lua/opencode/ui/ui.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function M.close_windows(windows)
3535
M.return_to_last_code_win()
3636
end
3737

38+
topbar.close()
3839
output_renderer.teardown()
3940
streaming_renderer.teardown()
4041

0 commit comments

Comments
 (0)