1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2025-02-02 15:07:59 -05:00

Fixed resizing

This commit is contained in:
マリウス 2023-01-02 16:45:42 -05:00
parent 666f3b596d
commit 6fbc9043d8
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
4 changed files with 86 additions and 24 deletions

View File

@ -6,6 +6,7 @@ type CallType int8
const ( const (
WinOpen CallType = iota WinOpen CallType = iota
WinClose
WinFocus WinFocus
WinBlur WinBlur
WinRefreshData WinRefreshData
@ -15,6 +16,8 @@ const (
ViewBlur ViewBlur
ViewRefreshData ViewRefreshData
ViewFreshData ViewFreshData
MsgError
) )
type Arg struct { type Arg struct {

View File

@ -53,7 +53,7 @@ func NewModel(c *ctx.Ctx) Model {
m := Model{ m := Model{
keymap: DefaultKeyMap, keymap: DefaultKeyMap,
currentView: 0, currentView: 0,
wm: windowmanager.New(), wm: windowmanager.New(c),
ctx: c, ctx: c,
} }
@ -80,11 +80,17 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch { switch {
case key.Matches(msg, m.keymap.Close): case key.Matches(msg, m.keymap.Close):
m.ctx.Logger.Debug("close received") 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") m.ctx.Logger.Debug("CloseFocused() was false, quitting")
return m, tea.Quit 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: case tea.WindowSizeMsg:
@ -94,10 +100,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.views[i] = v m.views[i] = v
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
} }
ccmds := m.wm.UpdateAll(tea.WindowSizeMsg{ m.ctx.Logger.Debugf("resizing all: %v\n", m.ctx.Content)
Width: m.ctx.Content[0], ccmds := m.wm.ResizeAll(m.ctx.Content[0], m.ctx.Content[1])
Height: m.ctx.Content[1],
})
cmds = append(cmds, ccmds...) cmds = append(cmds, ccmds...)
case cmd.Command: case cmd.Command:
@ -109,7 +113,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
ccmds = m.wm.Open( ccmds = m.wm.Open(
msg.Target, msg.Target,
postshow.NewModel(m.ctx), postshow.NewModel(m.ctx),
[4]int{3, 2, 10, 6}, [4]int{3, 2, 9, 10},
&msg, &msg,
) )
m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds) m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds)

View File

@ -3,6 +3,7 @@ package windowmanager
import ( import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/ui/cmd" "github.com/mrusme/gobbs/ui/cmd"
"github.com/mrusme/gobbs/ui/ctx"
"github.com/mrusme/gobbs/ui/helpers" "github.com/mrusme/gobbs/ui/helpers"
"github.com/mrusme/gobbs/ui/windows" "github.com/mrusme/gobbs/ui/windows"
) )
@ -14,11 +15,13 @@ type StackItem struct {
} }
type WM struct { type WM struct {
ctx *ctx.Ctx
stack []StackItem stack []StackItem
} }
func New() *WM { func New(c *ctx.Ctx) *WM {
wm := new(WM) wm := new(WM)
wm.ctx = c
return wm 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) wm.stack = append(wm.stack, *item)
// tcmds = append(tcmds, wm.Update(id, *command)) tcmds = append(tcmds, wm.Update(id, *command))
// tcmds = append(tcmds, wm.Update(id, tea.WindowSizeMsg{ wm.ctx.Logger.Debugf("content: %v\n", wm.ctx.Content)
// Width: item.XYWH[2], tcmds = append(tcmds, wm.Resize(id, wm.ctx.Content[0], wm.ctx.Content[1])...)
// Height: item.XYWH[3],
// }))
// tcmds = append(tcmds, wm.Update(id, *cmd.New( // tcmds = append(tcmds, wm.Update(id, *cmd.New(
// cmd.WinRefreshData, // cmd.WinRefreshData,
// id, // id,
// ))) // )))
// fcmds := wm.Focus(id) fcmds := wm.Focus(id)
// tcmds = append(tcmds, fcmds...) tcmds = append(tcmds, fcmds...)
return tcmds return tcmds
} }
func (wm *WM) CloseFocused() bool { func (wm *WM) CloseFocused() (bool, []tea.Cmd) {
return wm.Close(wm.Focused()) 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-- { for i := len(wm.stack) - 1; i >= 0; i-- {
if wm.stack[i].ID == id { if wm.stack[i].ID == id {
wm.stack = append(wm.stack[:i], wm.stack[i+1:]...) 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 { func (wm *WM) Focus(id string) []tea.Cmd {
@ -138,6 +146,38 @@ func (wm *WM) UpdateAll(msg tea.Msg) []tea.Cmd {
return tcmds 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 { func (wm *WM) View(view string) string {
var v string = view var v string = view

View File

@ -63,6 +63,7 @@ var DefaultKeyMap = KeyMap{
type Model struct { type Model struct {
ctx *ctx.Ctx ctx *ctx.Ctx
keymap KeyMap keymap KeyMap
wh [2]int
focused bool focused bool
viewport viewport.Model viewport viewport.Model
@ -93,6 +94,7 @@ func NewModel(c *ctx.Ctx) Model {
m := Model{ m := Model{
ctx: c, ctx: c,
keymap: DefaultKeyMap, keymap: DefaultKeyMap,
wh: [2]int{0,0},
buffer: "", buffer: "",
replyIDs: []string{}, replyIDs: []string{},
@ -144,9 +146,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
case tea.WindowSizeMsg: case tea.WindowSizeMsg:
m.ctx.Logger.Debug("received WindowSizeMsg") m.wh[0] = msg.Width
viewportWidth := m.ctx.Content[0] - 9 m.wh[1] = msg.Height
viewportHeight := m.ctx.Content[1] - 10 m.ctx.Logger.Debugf("received WindowSizeMsg: %v\n", m.wh)
viewportWidth := m.wh[0]
viewportHeight := m.wh[1]
viewportStyle.Width(viewportWidth) viewportStyle.Width(viewportWidth)
viewportStyle.Height(viewportHeight) viewportStyle.Height(viewportHeight)
@ -210,6 +214,12 @@ func (m *Model) loadPost(p *post.Post) tea.Cmd {
m.ctx.Logger.Debug("------ EXECUTED -----") m.ctx.Logger.Debug("------ EXECUTED -----")
if err := m.a.LoadPost(p); err != nil { if err := m.a.LoadPost(p); err != nil {
m.ctx.Logger.Error(err) m.ctx.Logger.Error(err)
c := cmd.New(
cmd.MsgError,
"*",
cmd.Arg{Name: "error", Value: err},
)
return *c
} }
c := cmd.New( c := cmd.New(
@ -344,6 +354,11 @@ func (m *Model) renderReplies(
m.allReplies = append(m.allReplies, &(*replies)[ri]) m.allReplies = append(m.allReplies, &(*replies)[ri])
idx := len(m.replyIDs) - 1 idx := len(m.replyIDs) - 1
replyIdPadding := (m.viewport.Width-len(author)-len(inReplyTo)-28)
if replyIdPadding < 0 {
replyIdPadding = 0
}
out += fmt.Sprintf( out += fmt.Sprintf(
"\n\n %s %s%s%s\n%s", "\n\n %s %s%s%s\n%s",
m.ctx.Theme.Reply.Author.Render( m.ctx.Theme.Reply.Author.Render(
@ -352,7 +367,7 @@ func (m *Model) renderReplies(
lipgloss.NewStyle(). lipgloss.NewStyle().
Foreground(m.ctx.Theme.Reply.Author.GetBackground()). Foreground(m.ctx.Theme.Reply.Author.GetBackground()).
Render(fmt.Sprintf("writes in reply to %s:", inReplyTo)), Render(fmt.Sprintf("writes in reply to %s:", inReplyTo)),
strings.Repeat(" ", (m.viewport.Width-len(author)-len(inReplyTo)-28)), strings.Repeat(" ", replyIdPadding),
lipgloss.NewStyle(). lipgloss.NewStyle().
Foreground(lipgloss.Color("#777777")). Foreground(lipgloss.Color("#777777")).
Render(fmt.Sprintf("#%d", idx)), Render(fmt.Sprintf("#%d", idx)),