From 6fbc9043d8b56a9b9440cb097f71583a09f87189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Mon, 2 Jan 2023 16:45:42 -0500 Subject: [PATCH] Fixed resizing --- ui/cmd/cmd.go | 3 ++ ui/ui.go | 20 ++++++---- ui/windowmanager/windowmanager.go | 64 +++++++++++++++++++++++++------ ui/windows/postshow/postshow.go | 23 +++++++++-- 4 files changed, 86 insertions(+), 24 deletions(-) diff --git a/ui/cmd/cmd.go b/ui/cmd/cmd.go index 55b4167..6ba3e17 100644 --- a/ui/cmd/cmd.go +++ b/ui/cmd/cmd.go @@ -6,6 +6,7 @@ type CallType int8 const ( WinOpen CallType = iota + WinClose WinFocus WinBlur WinRefreshData @@ -15,6 +16,8 @@ const ( ViewBlur ViewRefreshData ViewFreshData + + MsgError ) type Arg struct { diff --git a/ui/ui.go b/ui/ui.go index 3029527..376a5d7 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -53,7 +53,7 @@ func NewModel(c *ctx.Ctx) Model { m := Model{ keymap: DefaultKeyMap, currentView: 0, - wm: windowmanager.New(), + wm: windowmanager.New(c), ctx: c, } @@ -80,11 +80,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch { case key.Matches(msg, m.keymap.Close): m.ctx.Logger.Debug("close received") - if !m.wm.CloseFocused() { + closed, ccmds := m.wm.CloseFocused() + if !closed { m.ctx.Logger.Debug("CloseFocused() was false, quitting") return m, tea.Quit } - return m, nil + return m, tea.Batch(ccmds...) + default: + if m.wm.GetNumberOpen() > 0 { + cmd := m.wm.Update(m.wm.Focused(), msg) + return m, cmd + } } case tea.WindowSizeMsg: @@ -94,10 +100,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.views[i] = v cmds = append(cmds, cmd) } - ccmds := m.wm.UpdateAll(tea.WindowSizeMsg{ - Width: m.ctx.Content[0], - Height: m.ctx.Content[1], - }) + m.ctx.Logger.Debugf("resizing all: %v\n", m.ctx.Content) + ccmds := m.wm.ResizeAll(m.ctx.Content[0], m.ctx.Content[1]) cmds = append(cmds, ccmds...) case cmd.Command: @@ -109,7 +113,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { ccmds = m.wm.Open( msg.Target, postshow.NewModel(m.ctx), - [4]int{3, 2, 10, 6}, + [4]int{3, 2, 9, 10}, &msg, ) m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds) diff --git a/ui/windowmanager/windowmanager.go b/ui/windowmanager/windowmanager.go index aebee2d..52aadb2 100644 --- a/ui/windowmanager/windowmanager.go +++ b/ui/windowmanager/windowmanager.go @@ -3,6 +3,7 @@ package windowmanager import ( tea "github.com/charmbracelet/bubbletea" "github.com/mrusme/gobbs/ui/cmd" + "github.com/mrusme/gobbs/ui/ctx" "github.com/mrusme/gobbs/ui/helpers" "github.com/mrusme/gobbs/ui/windows" ) @@ -14,11 +15,13 @@ type StackItem struct { } type WM struct { + ctx *ctx.Ctx stack []StackItem } -func New() *WM { +func New(c *ctx.Ctx) *WM { wm := new(WM) + wm.ctx = c return wm } @@ -40,35 +43,40 @@ func (wm *WM) Open(id string, win windows.Window, xywh [4]int, command *cmd.Comm wm.stack = append(wm.stack, *item) - // tcmds = append(tcmds, wm.Update(id, *command)) - // tcmds = append(tcmds, wm.Update(id, tea.WindowSizeMsg{ - // Width: item.XYWH[2], - // Height: item.XYWH[3], - // })) + tcmds = append(tcmds, wm.Update(id, *command)) + wm.ctx.Logger.Debugf("content: %v\n", wm.ctx.Content) + tcmds = append(tcmds, wm.Resize(id, wm.ctx.Content[0], wm.ctx.Content[1])...) // tcmds = append(tcmds, wm.Update(id, *cmd.New( // cmd.WinRefreshData, // id, // ))) - // fcmds := wm.Focus(id) - // tcmds = append(tcmds, fcmds...) + fcmds := wm.Focus(id) + tcmds = append(tcmds, fcmds...) return tcmds } -func (wm *WM) CloseFocused() bool { +func (wm *WM) CloseFocused() (bool, []tea.Cmd) { return wm.Close(wm.Focused()) } -func (wm *WM) Close(id string) bool { +func (wm *WM) Close(id string) (bool, []tea.Cmd) { + var tcmds []tea.Cmd for i := len(wm.stack) - 1; i >= 0; i-- { if wm.stack[i].ID == id { wm.stack = append(wm.stack[:i], wm.stack[i+1:]...) - return true + tcmds = append(tcmds, cmd.New(cmd.WinClose, id).Tea()) + wm.ctx.Loading = false + + if wm.GetNumberOpen() == 0 { + tcmds = append(tcmds, cmd.New(cmd.ViewFocus, "*").Tea()) + } + return true, tcmds } } - return false + return false, tcmds } func (wm *WM) Focus(id string) []tea.Cmd { @@ -138,6 +146,38 @@ func (wm *WM) UpdateAll(msg tea.Msg) []tea.Cmd { return tcmds } +func (wm *WM) Resize(id string, w int, h int) []tea.Cmd { + var tcmd tea.Cmd + var tcmds []tea.Cmd + + for i := 0; i < len(wm.stack); i++ { + if wm.stack[i].ID == id { + wm.stack[i].Win, tcmd = wm.stack[i].Win.Update(tea.WindowSizeMsg{ + Width: w - wm.stack[i].XYWH[2], + Height: h - wm.stack[i].XYWH[3], + }) + tcmds = append(tcmds, tcmd) + } + } + + return tcmds +} + +func (wm *WM) ResizeAll(w int, h int) []tea.Cmd { + var tcmd tea.Cmd + var tcmds []tea.Cmd + + for i := 0; i < len(wm.stack); i++ { + wm.stack[i].Win, tcmd = wm.stack[i].Win.Update(tea.WindowSizeMsg{ + Width: w - wm.stack[i].XYWH[2], + Height: h - wm.stack[i].XYWH[3], + }) + tcmds = append(tcmds, tcmd) + } + + return tcmds +} + func (wm *WM) View(view string) string { var v string = view diff --git a/ui/windows/postshow/postshow.go b/ui/windows/postshow/postshow.go index b3c204c..bc96018 100644 --- a/ui/windows/postshow/postshow.go +++ b/ui/windows/postshow/postshow.go @@ -63,6 +63,7 @@ var DefaultKeyMap = KeyMap{ type Model struct { ctx *ctx.Ctx keymap KeyMap + wh [2]int focused bool viewport viewport.Model @@ -93,6 +94,7 @@ func NewModel(c *ctx.Ctx) Model { m := Model{ ctx: c, keymap: DefaultKeyMap, + wh: [2]int{0,0}, buffer: "", replyIDs: []string{}, @@ -144,9 +146,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case tea.WindowSizeMsg: - m.ctx.Logger.Debug("received WindowSizeMsg") - viewportWidth := m.ctx.Content[0] - 9 - viewportHeight := m.ctx.Content[1] - 10 + m.wh[0] = msg.Width + m.wh[1] = msg.Height + m.ctx.Logger.Debugf("received WindowSizeMsg: %v\n", m.wh) + viewportWidth := m.wh[0] + viewportHeight := m.wh[1] viewportStyle.Width(viewportWidth) viewportStyle.Height(viewportHeight) @@ -210,6 +214,12 @@ func (m *Model) loadPost(p *post.Post) tea.Cmd { m.ctx.Logger.Debug("------ EXECUTED -----") if err := m.a.LoadPost(p); err != nil { m.ctx.Logger.Error(err) + c := cmd.New( + cmd.MsgError, + "*", + cmd.Arg{Name: "error", Value: err}, + ) + return *c } c := cmd.New( @@ -344,6 +354,11 @@ func (m *Model) renderReplies( m.allReplies = append(m.allReplies, &(*replies)[ri]) idx := len(m.replyIDs) - 1 + replyIdPadding := (m.viewport.Width-len(author)-len(inReplyTo)-28) + if replyIdPadding < 0 { + replyIdPadding = 0 + } + out += fmt.Sprintf( "\n\n %s %s%s%s\n%s", m.ctx.Theme.Reply.Author.Render( @@ -352,7 +367,7 @@ func (m *Model) renderReplies( lipgloss.NewStyle(). Foreground(m.ctx.Theme.Reply.Author.GetBackground()). Render(fmt.Sprintf("writes in reply to %s:", inReplyTo)), - strings.Repeat(" ", (m.viewport.Width-len(author)-len(inReplyTo)-28)), + strings.Repeat(" ", replyIdPadding), lipgloss.NewStyle(). Foreground(lipgloss.Color("#777777")). Render(fmt.Sprintf("#%d", idx)),