Skip to content

Commit

Permalink
minor bug fix; ui improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
zMoooooritz committed Oct 8, 2024
1 parent b509f3d commit fb93ee9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
22 changes: 18 additions & 4 deletions pkg/config/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ type Style struct {
ReaderStyle ansi.StyleConfig
}

func topDoubleBorder() lipgloss.Border {
border := lipgloss.DoubleBorder()
border.BottomRight = border.MiddleRight
border.BottomLeft = border.MiddleLeft
return border
}

func topRoundedBorder() lipgloss.Border {
border := lipgloss.RoundedBorder()
border.BottomRight = border.MiddleRight
border.BottomLeft = border.MiddleLeft
return border
}

func NewsStyle(t Theme) (s Style) {
primaryColor := lipgloss.Color(t.PrimaryColor)
shadedColor := lipgloss.Color(t.ShadedColor)
Expand All @@ -69,8 +83,8 @@ func NewsStyle(t Theme) (s Style) {
s.ActiveHighlightStyle = lipgloss.NewStyle().Foreground(highlightShadedColor).BorderForeground(highlightShadedColor).Bold(true)
s.HighlightStyle = lipgloss.NewStyle().Foreground(markerColor).BorderForeground(markerColor).Bold(true)
s.TitleActiveStyle = lipgloss.NewStyle().Background(highlightColor).Foreground(primaryColor)
s.ListActiveStyle = listBaseStyle.BorderForeground(highlightColor).BorderStyle(lipgloss.DoubleBorder())
s.ListHeaderActiveStyle = s.ActiveStyle.Padding(0, 1).Border(lipgloss.DoubleBorder(), false, false, true, false)
s.ListActiveStyle = listBaseStyle.BorderForeground(highlightColor).Border(lipgloss.DoubleBorder(), false, true, true, true)
s.ListHeaderActiveStyle = s.ActiveStyle.Padding(0, 1).Margin(0, 1).BorderStyle(topDoubleBorder())
s.ReaderTitleActiveStyle = func() lipgloss.Style {
b := lipgloss.DoubleBorder()
b.Right = "╠"
Expand All @@ -84,8 +98,8 @@ func NewsStyle(t Theme) (s Style) {

s.InactiveStyle = lipgloss.NewStyle().Foreground(primaryColor).BorderForeground(primaryColor)
s.TitleInactiveStyle = lipgloss.NewStyle().Foreground(primaryColor)
s.ListInactiveStyle = listBaseStyle.BorderForeground(primaryColor).BorderStyle(lipgloss.RoundedBorder())
s.ListHeaderInactiveStyle = s.InactiveStyle.Padding(0, 1).Border(lipgloss.RoundedBorder(), false, false, true, false)
s.ListInactiveStyle = listBaseStyle.BorderForeground(primaryColor).Border(lipgloss.RoundedBorder(), false, true, true, true)
s.ListHeaderInactiveStyle = s.InactiveStyle.Padding(0, 1).Margin(0, 1).BorderStyle(topRoundedBorder())
s.ReaderTitleInactiveStyle = func() lipgloss.Style {
b := lipgloss.RoundedBorder()
b.Right = "├"
Expand Down
14 changes: 14 additions & 0 deletions pkg/tui/base_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
type Selector interface {
PushCurrentArticle() tea.Cmd
SelectorType() SelectorType
SetVisible(bool)
IsVisible() bool
SetActive(bool)
IsActive() bool
SetFocused(bool)
Expand All @@ -33,6 +35,7 @@ type BaseSelector struct {
selectorType SelectorType
shared *SharedState
isActive bool
isVisible bool
isFocused bool
width int
height int
Expand All @@ -48,6 +51,7 @@ func NewSelector(selectorType SelectorType, shared *SharedState, isActive bool)
selectorType: selectorType,
isActive: isActive,
isFocused: isActive,
isVisible: true,
list: initList(shared.style, listKeymap),
selectedIndex: 0,
}
Expand Down Expand Up @@ -86,6 +90,14 @@ func (s BaseSelector) SelectorType() SelectorType {
return s.selectorType
}

func (s *BaseSelector) SetVisible(isVisible bool) {
s.isVisible = isVisible
}

func (s *BaseSelector) IsVisible() bool {
return s.isVisible
}

func (s *BaseSelector) SetActive(isActive bool) {
s.isActive = isActive
}
Expand Down Expand Up @@ -131,6 +143,8 @@ func (s BaseSelector) Update(msg tea.Msg) (BaseSelector, tea.Cmd) {
if s.isActive {
s.isFocused = true
}
case key.Matches(msg, s.shared.keymap.full):
s.isVisible = !s.isVisible
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/tui/home_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *HomeSelector) Update(msg tea.Msg) (Selector, tea.Cmd) {
}
s.rebuildList()
case tea.KeyMsg:
if s.isFocused {
if s.isFocused && s.isVisible {
s.list, cmd = s.list.Update(msg)
cmds = append(cmds, cmd)
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/tui/navigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ func (n *Navigator) selectSearchSelector() {
}

func (n *Navigator) gotoSelector(index int) {
n.selectors[n.activeSelectorIndex].SetVisible(false)
n.selectors[n.activeSelectorIndex].SetActive(false)
n.selectors[n.activeSelectorIndex].SetFocused(false)
n.activeSelectorIndex = index
n.selectors[n.activeSelectorIndex].SetVisible(true)
n.selectors[n.activeSelectorIndex].SetActive(true)
n.selectors[n.activeSelectorIndex].SetFocused(true)
}
Expand Down Expand Up @@ -101,7 +103,9 @@ func (n *Navigator) Update(msg tea.Msg) (*Navigator, tea.Cmd) {
case key.Matches(msg, n.shared.keymap.full):
n.isVisible = !n.isVisible
case key.Matches(msg, n.shared.keymap.search):
n.selectSearchSelector()
if n.isFocused && n.isVisible {
n.selectSearchSelector()
}
}
}
}
Expand All @@ -122,16 +126,16 @@ func (n Navigator) View() string {
}

headerView := n.headerView()
selectorView := n.selectorView([]string{nationalHeaderText, regionalHeaderText, searchHeaderText}, n.activeSelectorIndex)
tabView := n.tabView([]string{nationalHeaderText, regionalHeaderText, searchHeaderText}, n.activeSelectorIndex)

style := n.shared.style.ListInactiveStyle
if n.isFocused {
style = n.shared.style.ListActiveStyle
}

n.selectors[n.activeSelectorIndex].SetDims(n.width, n.height-lipgloss.Height(headerView)-lipgloss.Height(selectorView)-lipgloss.Height(style.Render(""))+1)
n.selectors[n.activeSelectorIndex].SetDims(n.width, n.height-lipgloss.Height(headerView)-lipgloss.Height(tabView)-lipgloss.Height(style.Render(""))+1)

return style.Render(lipgloss.JoinVertical(lipgloss.Left, headerView, selectorView, n.selectors[n.activeSelectorIndex].View()))
return lipgloss.JoinVertical(lipgloss.Left, headerView, style.Render(lipgloss.JoinVertical(lipgloss.Left, tabView, n.selectors[n.activeSelectorIndex].View())))
}

func (n Navigator) headerView() string {
Expand All @@ -140,11 +144,11 @@ func (n Navigator) headerView() string {
headerStyle = n.shared.style.ListHeaderActiveStyle
}

centeredHeader := lipgloss.PlaceHorizontal(n.width-2, lipgloss.Center, headerText)
centeredHeader := lipgloss.PlaceHorizontal(n.width, lipgloss.Center, headerText)
return headerStyle.Render(centeredHeader)
}

func (n Navigator) selectorView(names []string, activeIndex int) string {
func (n Navigator) tabView(names []string, activeIndex int) string {
cellWidth := (n.width - 2*len(names)) / len(names)
var widths []int
for i := 0; i < len(names)-1; i++ {
Expand Down
20 changes: 13 additions & 7 deletions pkg/tui/search_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ type SearchSelector struct {

func loadArticles(seachTerm string) tea.Cmd {
return func() tea.Msg {
articles, err := tagesschau.SearchArticles(seachTerm)
if err == nil {
return articles
searchResult, err := tagesschau.SearchArticles(seachTerm)
if err == nil && len(searchResult.Articles) > 0 {
return searchResult
}
return LoadingArticlesFailed{}
}
}

func NewSearchSelector(selector BaseSelector) *SearchSelector {
searchInput := textinput.New()
searchInput.Prompt = "> "
searchInput.Prompt = ""
searchInput.Placeholder = "Suche ..."
searchInput.PromptStyle = selector.shared.style.ItemSelectedTitle
searchInput.Cursor.Style = selector.shared.style.InactiveStyle
searchInput.Cursor.TextStyle = selector.shared.style.InactiveStyle
searchInput.TextStyle = selector.shared.style.InactiveStyle

selector.articles = noArticles
return &SearchSelector{
Expand Down Expand Up @@ -72,14 +76,16 @@ func (s *SearchSelector) Update(msg tea.Msg) (Selector, tea.Cmd) {
}
cmds = append(cmds, s.PushCurrentArticle())
case tea.KeyMsg:
if s.isFocused {
if s.isFocused && s.isVisible {
if s.shared.mode == NORMAL_MODE {
switch {
case key.Matches(msg, s.shared.keymap.search):
s.shared.mode = INSERT_MODE
s.search.Focus()
s.search.Reset()
return s, nil
bs, cmd := s.BaseSelector.Update(msg)
cmds = append(cmds, cmd)
return &SearchSelector{BaseSelector: bs, search: s.search}, tea.Batch(cmds...)
}
}
if s.shared.mode == NORMAL_MODE {
Expand All @@ -101,7 +107,7 @@ func (s *SearchSelector) Update(msg tea.Msg) (Selector, tea.Cmd) {
}
}

if s.isFocused {
if s.isFocused && s.isVisible {
s.search, cmd = s.search.Update(msg)
cmds = append(cmds, cmd)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (m Model) View() string {

navigatorWidthMultiplier := max(min(m.shared.config.Settings.NavigatorWidth, 0.8), 0.2)
navigatorWidth := int(float32(m.width) * navigatorWidthMultiplier)
m.navigator.SetDims(navigatorWidth, m.height-lipgloss.Height(help))
m.navigator.SetDims(navigatorWidth, m.height-m.helper.Height())
navigator := m.navigator.View()

m.viewManager.SetDims(m.width, m.height-m.helper.Height(), lipgloss.Width(navigator))
Expand Down

0 comments on commit fb93ee9

Please sign in to comment.