Skip to content

Commit

Permalink
refactor(theme): centralize theme management and streamline settings …
Browse files Browse the repository at this point in the history
…component
  • Loading branch information
AlbinoGeek committed Jun 16, 2024
1 parent ca3400b commit 1212dab
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 96 deletions.
42 changes: 36 additions & 6 deletions cmd/flower-tui/App.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ type App struct {
controls *controlsFooterComponent

// State
config *cfg.Config
gamePath string
sourcePath string
config *cfg.Config
theme map[string]string
}

func (app *App) Init(reactea.NoProps) tea.Cmd {
Expand All @@ -43,6 +42,13 @@ func (app *App) Init(reactea.NoProps) tea.Cmd {
app.controls = &controlsFooterComponent{}
app.controls.Init(&controlsFooterProps{})

app.theme = make(map[string]string)
app.theme["Border"] = ANSIBorder
app.theme["Primary"] = ANSIPrimary
app.theme["Secondary"] = ANSISecondary
app.theme["Disabled"] = ANSIDisabled
app.theme["Error"] = ANSIError

// Router
return app.mainRouter.Init(map[string]router.RouteInitializer{
"default": func(router.Params) (reactea.SomeComponent, tea.Cmd) {
Expand All @@ -51,7 +57,32 @@ func (app *App) Init(reactea.NoProps) tea.Cmd {
},
"settings": func(router.Params) (reactea.SomeComponent, tea.Cmd) {
component := &settingsComponent{}
return component, component.Init(&settingsProps{})
return component, component.Init(&settingsProps{
getThemeColor: func(key string) string {
return app.theme[key]
},
setThemeColor: func(key, value string) {
app.theme[key] = value

switch key {
case "Border":
ANSIBorder = value
ColorBorder = lipgloss.NewStyle().BorderForeground(lipgloss.Color(ANSIBorder))
case "Primary":
ANSIPrimary = value
ColorPrimary = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSIPrimary))
case "Secondary":
ANSISecondary = value
ColorSecondary = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSISecondary))
case "Disabled":
ANSIDisabled = value
ColorDisabled = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSIDisabled))
case "Error":
ANSIError = value
ColorError = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSIError))
}
},
})
},
})
}
Expand Down Expand Up @@ -79,10 +110,9 @@ func (app *App) Render(outerWidth, outerHeight int) string {
lipgloss.Left,

// Main content
lipgloss.NewStyle().
ColorBorder.
Width(innerWidth).
Height(innerHeight).
BorderForeground(ColorPrimaryMain).
Border(lipgloss.DoubleBorder(), true).
Render(app.mainRouter.Render(innerWidth, innerHeight)),

Expand Down
18 changes: 10 additions & 8 deletions cmd/flower-tui/Theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
)

var (
ColorPrimaryMain = lipgloss.Color("176")
ColorPrimaryDark = lipgloss.Color("96")
ColorDisabled = lipgloss.Color("243")
ANSIBorder = "103"
ANSIPrimary = "140"
ANSISecondary = "97"
ANSIDisabled = "243"
ANSIError = "160"

TextDark = lipgloss.NewStyle().Foreground(ColorPrimaryDark)
TextDisabled = lipgloss.NewStyle().Foreground(ColorDisabled)
TextMain = lipgloss.NewStyle().Foreground(ColorPrimaryMain)

TextError = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
ColorBorder = lipgloss.NewStyle().BorderForeground(lipgloss.Color(ANSIBorder))
ColorPrimary = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSIPrimary))
ColorSecondary = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSISecondary))
ColorDisabled = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSIDisabled))
ColorError = lipgloss.NewStyle().Foreground(lipgloss.Color(ANSIError))
)
8 changes: 4 additions & 4 deletions cmd/flower-tui/controlsFooter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ func (c *controlsFooterComponent) Init(props *controlsFooterProps) tea.Cmd {
}

func (c *controlsFooterComponent) Render(width, height int) string {
dot := TextDisabled.Render(" • ")
dot := ColorDisabled.Render(" • ")

return lipgloss.JoinVertical(
lipgloss.Top,
lipgloss.JoinHorizontal(
lipgloss.Left, " ",
TextMain.Render("Ctrl+C"), " ", TextDark.Render("Quit"),
ColorPrimary.Render("Ctrl+C"), " ", ColorSecondary.Render("Quit"),
dot,
TextMain.Render("↑↓"), " ", TextDark.Render("Navigate"),
ColorPrimary.Render("↑↓"), " ", ColorSecondary.Render("Navigate"),
dot,
TextMain.Render("Enter"), " ", TextDark.Render("Select Highlighted Option"),
ColorPrimary.Render("Enter"), " ", ColorSecondary.Render("Select Highlighted Option"),
),
)
}
Loading

0 comments on commit 1212dab

Please sign in to comment.