Просмотр исходного кода

feat(config): add theme.title.align field (#525)

Ayyan 1 год назад
Родитель
Сommit
c21ba4ed8d
3 измененных файлов с 94 добавлено и 63 удалено
  1. 67 0
      internal/config/border.go
  2. 23 60
      internal/config/theme.go
  3. 4 3
      internal/ui/util.go

+ 67 - 0
internal/config/border.go

@@ -0,0 +1,67 @@
+package config
+
+import "github.com/rivo/tview"
+
+type BorderPreset struct {
+	Horizontal  rune
+	Vertical    rune
+	TopLeft     rune
+	TopRight    rune
+	BottomLeft  rune
+	BottomRight rune
+}
+
+func (p *BorderPreset) UnmarshalTOML(v any) error {
+	switch v.(string) {
+	case "double":
+		*p = borderPresetDouble()
+	case "thick":
+		*p = borderPresetThick()
+	case "round":
+		*p = borderPresetRound()
+	case "hidden":
+		*p = BorderPreset{
+			Horizontal:  ' ',
+			Vertical:    ' ',
+			TopLeft:     ' ',
+			TopRight:    ' ',
+			BottomLeft:  ' ',
+			BottomRight: ' ',
+		}
+	}
+
+	return nil
+}
+
+func borderPresetDouble() BorderPreset {
+	return BorderPreset{
+		Horizontal:  tview.BoxDrawingsDoubleHorizontal,
+		Vertical:    tview.BoxDrawingsDoubleVertical,
+		TopLeft:     tview.BoxDrawingsDoubleDownAndRight,
+		TopRight:    tview.BoxDrawingsDoubleDownAndLeft,
+		BottomLeft:  tview.BoxDrawingsDoubleUpAndRight,
+		BottomRight: tview.BoxDrawingsDoubleUpAndLeft,
+	}
+}
+
+func borderPresetThick() BorderPreset {
+	return BorderPreset{
+		Horizontal:  tview.BoxDrawingsHeavyHorizontal,
+		Vertical:    tview.BoxDrawingsHeavyVertical,
+		TopLeft:     tview.BoxDrawingsHeavyDownAndRight,
+		TopRight:    tview.BoxDrawingsHeavyDownAndLeft,
+		BottomLeft:  tview.BoxDrawingsHeavyUpAndRight,
+		BottomRight: tview.BoxDrawingsHeavyUpAndLeft,
+	}
+}
+
+func borderPresetRound() BorderPreset {
+	return BorderPreset{
+		Horizontal:  tview.BoxDrawingsLightHorizontal,
+		Vertical:    tview.BoxDrawingsLightVertical,
+		TopLeft:     tview.BoxDrawingsLightArcDownAndRight,
+		TopRight:    tview.BoxDrawingsLightArcDownAndLeft,
+		BottomLeft:  tview.BoxDrawingsLightArcUpAndRight,
+		BottomRight: tview.BoxDrawingsLightArcUpAndLeft,
+	}
+}

+ 23 - 60
internal/config/theme.go

@@ -4,53 +4,16 @@ import (
 	"github.com/rivo/tview"
 )
 
-type BorderPreset struct {
-	Horizontal  rune
-	Vertical    rune
-	TopLeft     rune
-	TopRight    rune
-	BottomLeft  rune
-	BottomRight rune
-}
+type TitleAlign int
 
-func (p *BorderPreset) UnmarshalTOML(v any) error {
+func (ta *TitleAlign) UnmarshalTOML(v any) error {
 	switch v.(string) {
-	case "double":
-		*p = BorderPreset{
-			Horizontal:  tview.BoxDrawingsDoubleHorizontal,
-			Vertical:    tview.BoxDrawingsDoubleVertical,
-			TopLeft:     tview.BoxDrawingsDoubleDownAndRight,
-			TopRight:    tview.BoxDrawingsDoubleDownAndLeft,
-			BottomLeft:  tview.BoxDrawingsDoubleUpAndRight,
-			BottomRight: tview.BoxDrawingsDoubleUpAndLeft,
-		}
-	case "thick":
-		*p = BorderPreset{
-			Horizontal:  tview.BoxDrawingsHeavyHorizontal,
-			Vertical:    tview.BoxDrawingsHeavyVertical,
-			TopLeft:     tview.BoxDrawingsHeavyDownAndRight,
-			TopRight:    tview.BoxDrawingsHeavyDownAndLeft,
-			BottomLeft:  tview.BoxDrawingsHeavyUpAndRight,
-			BottomRight: tview.BoxDrawingsHeavyUpAndLeft,
-		}
-	case "round":
-		*p = BorderPreset{
-			Horizontal:  tview.BoxDrawingsLightHorizontal,
-			Vertical:    tview.BoxDrawingsLightVertical,
-			TopLeft:     tview.BoxDrawingsLightArcDownAndRight,
-			TopRight:    tview.BoxDrawingsLightArcDownAndLeft,
-			BottomLeft:  tview.BoxDrawingsLightArcUpAndRight,
-			BottomRight: tview.BoxDrawingsLightArcUpAndLeft,
-		}
-	case "hidden":
-		*p = BorderPreset{
-			Horizontal:  ' ',
-			Vertical:    ' ',
-			TopLeft:     ' ',
-			TopRight:    ' ',
-			BottomLeft:  ' ',
-			BottomRight: ' ',
-		}
+	case "left":
+		*ta = tview.AlignLeft
+	case "center":
+		*ta = tview.AlignCenter
+	case "right":
+		*ta = tview.AlignRight
 	}
 
 	return nil
@@ -67,12 +30,16 @@ type (
 		Preset BorderPreset `toml:"preset"`
 	}
 
+	TitleTheme struct {
+		Color       string     `toml:"color"`
+		ActiveColor string     `toml:"active_color"`
+		Align       TitleAlign `toml:"align"`
+	}
+
 	Theme struct {
 		BackgroundColor string `toml:"background_color"`
 
-		TitleColor       string `toml:"title_color"`
-		ActiveTitleColor string `toml:"active_title_color"`
-
+		Title        TitleTheme        `toml:"title"`
 		Border       BorderTheme       `toml:"border"`
 		GuildsTree   GuildsTreeTheme   `toml:"guilds_tree"`
 		MessagesText MessagesTextTheme `toml:"messages_text"`
@@ -100,26 +67,22 @@ type (
 
 func defaultTheme() Theme {
 	return Theme{
+		BackgroundColor: "default",
+
 		Border: BorderTheme{
 			Enabled: true,
 			Padding: [...]int{0, 0, 1, 1},
 
 			Color:       "default",
 			ActiveColor: "green",
-			Preset: BorderPreset{
-				Horizontal:  tview.Borders.Horizontal,
-				Vertical:    tview.Borders.Vertical,
-				TopLeft:     tview.Borders.TopLeft,
-				TopRight:    tview.Borders.TopRight,
-				BottomLeft:  tview.Borders.BottomLeft,
-				BottomRight: tview.Borders.BottomRight,
-			},
+			Preset:      borderPresetRound(),
 		},
 
-		BackgroundColor: "default",
-
-		TitleColor:       "default",
-		ActiveTitleColor: "green",
+		Title: TitleTheme{
+			Color:       "default",
+			ActiveColor: "green",
+			Align:       tview.AlignLeft,
+		},
 
 		GuildsTree: GuildsTreeTheme{
 			AutoExpandFolders:   true,

+ 4 - 3
internal/ui/util.go

@@ -8,19 +8,20 @@ import (
 
 func NewConfiguredBox(box *tview.Box, cfg *config.Theme) *tview.Box {
 	b := cfg.Border
+	t := cfg.Title
 	p := b.Padding
 	box.
 		SetBorder(cfg.Border.Enabled).
 		SetBorderColor(tcell.GetColor(b.Color)).
 		SetBorderPadding(p[0], p[1], p[2], p[3]).
-		SetTitleAlign(tview.AlignLeft).
+		SetTitleAlign(int(t.Align)).
 		SetFocusFunc(func() {
 			box.SetBorderColor(tcell.GetColor(b.ActiveColor))
-			box.SetTitleColor(tcell.GetColor(cfg.ActiveTitleColor))
+			box.SetTitleColor(tcell.GetColor(t.ActiveColor))
 		}).
 		SetBlurFunc(func() {
 			box.SetBorderColor(tcell.GetColor(b.Color))
-			box.SetTitleColor(tcell.GetColor(cfg.TitleColor))
+			box.SetTitleColor(tcell.GetColor(t.Color))
 		})
 	return box
 }