Skip to content

Commit

Permalink
refactor: move the category popup styles to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jul 4, 2023
1 parent 9e0d4e4 commit 4186724
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 38 deletions.
51 changes: 13 additions & 38 deletions internal/model/tab/category/popup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,21 @@ const (
newCategoryField
)

// Popup is the category popup where a user can create a category.
type Popup struct {
defaultPopup popup.Default
colors colorscheme.Colorscheme
style popupStyle
textInput textinput.Model
focused focusedField
renderStyle lipgloss.Style
}

// NewPopup creates a new popup window in which the user can choose a new category.
func NewPopup(colors colorscheme.Colorscheme, bgRaw string, width, height int) Popup {
return Popup{
defaultPopup: popup.New(bgRaw, width, height),
colors: colors,
style: newPopupStyle(colors, width, height),
textInput: textinput.New(),
focused: allField,
renderStyle: lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFFFF")).
Width(width - 2).
Height(height - 2).
Border(lipgloss.NormalBorder()).
BorderForeground(colors.Color1),
}
}

Expand Down Expand Up @@ -106,56 +100,37 @@ func (p Popup) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return p, tea.Batch(cmds...)
}

// View renders the popup window.
func (p Popup) View() string {
headingStyle := lipgloss.NewStyle().
Margin(2, 2).
Width(p.defaultPopup.Width() - 2).
Align(lipgloss.Center).
Italic(true)

question := headingStyle.Render("Which category do you want to add?")

question := p.style.heading.Render("Choose a category")
choices := []string{"All", "Downloaded", "New Category"}
descriptions := []string{
"All the feeds from all the categories",
"Downloaded articles",
"",
}
choiceSectionStyle := lipgloss.NewStyle().
Padding(2).
Width(p.defaultPopup.Width() - 2).
Height(10)

choiceStyle := lipgloss.NewStyle().
PaddingLeft(2).
MarginBottom(1).
Border(lipgloss.RoundedBorder(), false, false, false, true)

selectedChoiceStyle := choiceStyle.Copy().
BorderForeground(p.colors.Color4)

renderedChoices := make([]string, len(choices))
if p.focused == allField {
renderedChoices[0] = selectedChoiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[0], descriptions[0]))
renderedChoices[0] = p.style.selectedChoice.Render(lipgloss.JoinVertical(lipgloss.Top, choices[0], descriptions[0]))
} else {
renderedChoices[0] = choiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[0], descriptions[0]))
renderedChoices[0] = p.style.choice.Render(lipgloss.JoinVertical(lipgloss.Top, choices[0], descriptions[0]))
}

if p.focused == downloadedField {
renderedChoices[1] = selectedChoiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[1], descriptions[1]))
renderedChoices[1] = p.style.selectedChoice.Render(lipgloss.JoinVertical(lipgloss.Top, choices[1], descriptions[1]))
} else {
renderedChoices[1] = choiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[1], descriptions[1]))
renderedChoices[1] = p.style.choice.Render(lipgloss.JoinVertical(lipgloss.Top, choices[1], descriptions[1]))
}

if p.focused == newCategoryField {
renderedChoices[2] = selectedChoiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], p.textInput.View()))
renderedChoices[2] = p.style.selectedChoice.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], p.textInput.View()))
} else {
renderedChoices[2] = choiceStyle.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], p.textInput.View()))
renderedChoices[2] = p.style.choice.Render(lipgloss.JoinVertical(lipgloss.Top, choices[2], p.textInput.View()))
}

toBox := choiceSectionStyle.Render(lipgloss.JoinVertical(lipgloss.Top, renderedChoices...))
toBox := p.style.choiceSection.Render(lipgloss.JoinVertical(lipgloss.Top, renderedChoices...))
popup := lipgloss.JoinVertical(lipgloss.Top, question, toBox)
return p.defaultPopup.Overlay(p.renderStyle.Render(popup))
return p.defaultPopup.Overlay(p.style.general.Render(popup))
}

// confirmCategory returns a tea.Cmd which relays the message to the browser.
Expand Down
52 changes: 52 additions & 0 deletions internal/model/tab/category/popup_style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package category

import (
"github.com/TypicalAM/goread/internal/colorscheme"
"github.com/charmbracelet/lipgloss"
)

// popupStyle is the style of the popup window.
type popupStyle struct {
general lipgloss.Style
heading lipgloss.Style
choiceSection lipgloss.Style
choice lipgloss.Style
selectedChoice lipgloss.Style
}

// newPopupStyle creates a new popup style.
func newPopupStyle(colors colorscheme.Colorscheme, width, height int) popupStyle {
heading := lipgloss.NewStyle().
Margin(2, 2).
Width(width - 2).
Align(lipgloss.Center).
Italic(true)

choiceSection := lipgloss.NewStyle().
Padding(2).
Width(width - 2).
Height(10)

choice := lipgloss.NewStyle().
PaddingLeft(2).
MarginBottom(1).
Border(lipgloss.RoundedBorder(), false, false, false, true)

selectedChoice := choice.Copy().
BorderForeground(colors.Color4)

general := lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFFFF")).
Width(width - 2).
Height(height - 2).
Border(lipgloss.NormalBorder()).
BorderForeground(colors.Color1)

return popupStyle{
heading: heading,
choiceSection: choiceSection,
choice: choice,
selectedChoice: selectedChoice,
general: general,
}
}

0 comments on commit 4186724

Please sign in to comment.