Skip to content

Commit

Permalink
support rending img after setting size (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
treethought committed Jun 16, 2024
1 parent c738131 commit 0579dec
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 55 deletions.
59 changes: 33 additions & 26 deletions ui/cast_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ 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,
header: &hp,
pubReply: NewPublishInput(app),
hasImg: false,
}
c.pfp.SetSize(4, 4)
return c
}

Expand All @@ -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...)
}
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions ui/cast_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
3 changes: 2 additions & 1 deletion ui/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
42 changes: 21 additions & 21 deletions ui/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions ui/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)),
)
}
Expand Down
3 changes: 2 additions & 1 deletion ui/sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

}

Expand Down

0 comments on commit 0579dec

Please sign in to comment.