From 0579deced08c3cc6b50061a02e243422ccfd53c5 Mon Sep 17 00:00:00 2001 From: Cam Sweeney Date: Sat, 15 Jun 2024 20:16:44 -0700 Subject: [PATCH] support rending img after setting size (#6) --- ui/cast_details.go | 59 ++++++++++++++++++++++++++-------------------- ui/cast_item.go | 7 +++--- ui/feed.go | 3 ++- ui/image.go | 42 ++++++++++++++++----------------- ui/profile.go | 7 +++--- ui/sidebar.go | 3 ++- 6 files changed, 66 insertions(+), 55 deletions(-) diff --git a/ui/cast_details.go b/ui/cast_details.go index 7a37eca..980b2d1 100644 --- a/ui/cast_details.go +++ b/ui/cast_details.go @@ -35,7 +35,7 @@ func NewCastView(app *App, cast *api.Cast) *CastView { c := &CastView{ app: app, cast: cast, - pfp: NewImage(false, true, special), + pfp: NewImage(true, true, special), img: NewImage(true, true, special), replies: NewRepliesView(app), vp: &vp, @@ -43,6 +43,7 @@ func NewCastView(app *App, cast *api.Cast) *CastView { pubReply: NewPublishInput(app), hasImg: false, } + c.pfp.SetSize(4, 4) return c } @@ -58,16 +59,17 @@ func (m *CastView) Clear() { func (m *CastView) SetCast(cast *api.Cast) tea.Cmd { m.Clear() m.cast = cast + m.pfp.SetURL(m.cast.Author.PfpURL, false) cmds := []tea.Cmd{ m.replies.SetOpHash(m.cast.Hash), - m.pfp.SetURL(m.cast.Author.PfpURL, false), - m.pfp.SetSize(4, 4), m.pubReply.SetContext(m.cast.Hash, m.cast.ParentURL, m.cast.Author.FID), + m.pfp.Render(), } m.hasImg = false if len(m.cast.Embeds) > 0 { m.hasImg = true - cmds = append(cmds, m.img.SetURL(m.cast.Embeds[0].URL, true)) + m.img.SetURL(m.cast.Embeds[0].URL, true) + cmds = append(cmds, m.resize(), m.img.Render()) } return tea.Sequence(cmds...) } @@ -83,36 +85,41 @@ func min(a, b int) int { return b } -func (m *CastView) Update(msg tea.Msg) (tea.Model, tea.Cmd) { - switch msg := msg.(type) { - case tea.WindowSizeMsg: - cmds := []tea.Cmd{} - - fx, fy := style.GetFrameSize() - w := min(msg.Width-fx, int(float64(GetWidth())*0.75)) - h := min(msg.Height-fy, GetHeight()-4) +func (m *CastView) resize() tea.Cmd { + cmds := []tea.Cmd{} + fx, fy := style.GetFrameSize() + w := min(m.w-fx, int(float64(GetWidth())*0.75)) + h := min(m.h-fy, GetHeight()-4) - m.w, m.h = w, h + m.header.Height = min(10, int(float64(h)*0.2)) - m.header.Height = min(10, int(float64(h)*0.2)) + hHeight := lipgloss.Height(m.header.View()) - hHeight := lipgloss.Height(m.header.View()) + cy := h - hHeight - cy := h - hHeight + m.vp.Width = w - fx + m.vp.Height = int(float64(cy) * 0.5) //- fy - m.vp.Width = w - fx - m.vp.Height = int(float64(cy) * 0.5) //- fy + if m.hasImg { + q := int(float64(cy) * 0.25) + m.vp.Height = q + m.img.SetSize(m.vp.Width/2, q) - m.img.SetSize(0, 0) + cmds = append(cmds, m.img.Render()) + } else { + m.img.Clear() + } + m.replies.SetSize(w, int(float64(cy)*0.5)) - if m.hasImg { - m.img.SetSize(4, 4) - m.vp.Height = int(float64(cy) * 0.25) - } - m.replies.SetSize(w, int(float64(cy)*0.5)) + m.pubReply.SetSize(w, h) + return tea.Batch(cmds...) +} - m.pubReply.SetSize(m.w, m.h) - return m, tea.Batch(cmds...) +func (m *CastView) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.WindowSizeMsg: + m.w, m.h = msg.Width, msg.Height + return m, m.resize() case *ctxInfoMsg: _, cmd := m.pubReply.Update(msg) diff --git a/ui/cast_item.go b/ui/cast_item.go index 72d05a0..1c61dc0 100644 --- a/ui/cast_item.go +++ b/ui/cast_item.go @@ -134,16 +134,17 @@ func NewCastFeedItem(app *App, cast *api.Cast, compact bool) (*CastFeedItem, tea pfp: NewImage(true, true, special), compact: compact, } + c.pfp.SetURL(cast.Author.PfpURL, false) cmds := []tea.Cmd{ - c.pfp.SetURL(cast.Author.PfpURL, false), + c.pfp.Render(), getCastChannelCmd(app.client, cast), } if c.compact { - cmds = append(cmds, c.pfp.SetSize(2, 1)) + c.pfp.SetSize(2, 1) } else { - cmds = append(cmds, c.pfp.SetSize(4, 4)) + c.pfp.SetSize(4, 4) } return c, tea.Batch(cmds...) } diff --git a/ui/feed.go b/ui/feed.go index dd80f56..a1162dd 100644 --- a/ui/feed.go +++ b/ui/feed.go @@ -443,7 +443,8 @@ func (m *FeedView) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return m, nil } m.SetDescription(channelDescription(msg.channel, m.headerImg)) - return m, m.headerImg.SetURL(msg.channel.ImageURL, false) + m.headerImg.SetURL(msg.channel.ImageURL, false) + return m, m.headerImg.Render() case reactMsg: current := m.getCurrentItem() diff --git a/ui/image.go b/ui/image.go index aed1900..c98ad05 100644 --- a/ui/image.go +++ b/ui/image.go @@ -218,11 +218,12 @@ func convertImageToString(width int, ib []byte) (string, error) { // ImageModel represents the properties of a code bubble. type ImageModel struct { - Viewport viewport.Model + Viewport *viewport.Model BorderColor lipgloss.AdaptiveColor Active bool Borderless bool URL string + isEmbed bool ImageString string } @@ -242,7 +243,7 @@ func NewImage(active, borderless bool, borderColor lipgloss.AdaptiveColor) *Imag BorderForeground(borderColor) return &ImageModel{ - Viewport: viewPort, + Viewport: &viewPort, Active: active, Borderless: borderless, BorderColor: borderColor, @@ -258,27 +259,31 @@ func (m *ImageModel) Clear() { m.URL = "" m.ImageString = "" m.Viewport.SetContent("") + m.Viewport.Width = 0 + m.Viewport.Height = 0 } -func (m *ImageModel) SetURL(url string, embed bool) tea.Cmd { - m.URL = url - return getImageCmd(m.Viewport.Width, url, embed) +func (m *ImageModel) Render() tea.Cmd { + if m.Viewport.Width == 0 { + return nil + } + if m.URL == "" { + return nil + } + return getImageCmd(m.Viewport.Width, m.URL, m.isEmbed) } -// SetFileName sets current file to highlight, this -// returns a cmd which will highlight the text. -// func (m *ImageModel) SetFileName(filename string) tea.Cmd { -// m.FileName = filename -// return convertImageToStringCmd(m.Viewport.Width, filename) -// } +func (m *ImageModel) SetURL(url string, embed bool) { + m.URL = url + m.isEmbed = embed +} // SetBorderColor sets the current color of the border. func (m *ImageModel) SetBorderColor(color lipgloss.AdaptiveColor) { m.BorderColor = color } -// SetSize sets the size of the bubble. -func (m *ImageModel) SetSize(w, h int) tea.Cmd { +func (m *ImageModel) SetSize(w, h int) { m.Viewport.Width = w m.Viewport.Height = h @@ -293,12 +298,6 @@ func (m *ImageModel) SetSize(w, h int) tea.Cmd { PaddingRight(padding). Border(border). BorderForeground(m.BorderColor) - - if m.ImageString != "" { - return getImageCmd(w, m.URL, false) - } - - return nil } // SetIsActive sets if the bubble is currently active @@ -339,10 +338,11 @@ func (m *ImageModel) Update(msg tea.Msg) (*ImageModel, tea.Cmd) { case convertImageToStringMsg: if msg.url == m.URL && msg.str != "" { m.ImageString = NewStyle(). - // Width(m.Viewport.Width). - // Height(m.Viewport.Height). + Width(m.Viewport.Width). + Height(m.Viewport.Height). Render(msg.str) m.Viewport.SetContent(m.ImageString) + m.SetSize(m.Viewport.Width, m.Viewport.Height) } case downloadError: if msg.url == m.URL { diff --git a/ui/profile.go b/ui/profile.go index 5ec07c9..1dd8101 100644 --- a/ui/profile.go +++ b/ui/profile.go @@ -23,7 +23,7 @@ func UserBio(user *api.User) string { NewStyle().Render(" followers"), ) - style := NewStyle().BorderStyle(lipgloss.ThickBorder()).BorderBottom(true).Padding(2) + style := NewStyle().BorderStyle(lipgloss.RoundedBorder()).BorderBottom(true).Padding(2) return style.Render(lipgloss.JoinVertical(lipgloss.Top, NewStyle().MarginTop(0).MarginBottom(0).Padding(0).Render(user.Profile.Bio.Text), @@ -122,9 +122,10 @@ func (m *Profile) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case ProfileMsg: if msg.user != nil { m.user = msg.user + m.pfp.SetURL(m.user.PfpURL, false) + m.pfp.SetSize(4, 4) return m, tea.Batch( - m.pfp.SetURL(m.user.PfpURL, false), - m.pfp.SetSize(4, 4), + m.pfp.Render(), navNameCmd(fmt.Sprintf("profile: @%s", m.user.Username)), ) } diff --git a/ui/sidebar.go b/ui/sidebar.go index 1176202..ddba019 100644 --- a/ui/sidebar.go +++ b/ui/sidebar.go @@ -181,7 +181,8 @@ func (m *Sidebar) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case *currentAccountMsg: m.account = msg.account - return m, m.pfp.SetURL(m.account.PfpURL, false) + m.pfp.SetURL(m.account.PfpURL, false) + return m, m.pfp.Render() }