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

Add customizable theme support

rigormorrtiss 4 лет назад
Родитель
Сommit
639a48447d
7 измененных файлов с 85 добавлено и 19 удалено
  1. 6 7
      discordo.go
  2. 7 2
      ui/dropdowns.go
  3. 5 3
      ui/inputfields.go
  4. 6 4
      ui/lists.go
  5. 5 1
      ui/textviews.go
  6. 2 2
      util/discord.go
  7. 54 0
      util/theme.go

+ 6 - 7
discordo.go

@@ -21,21 +21,19 @@ var (
 	mainFlex          *tview.Flex
 
 	loginVia       string
+	theme          *util.Theme
 	session        *discordgo.Session
 	currentGuild   *discordgo.Guild
 	currentChannel *discordgo.Channel
 )
 
 func main() {
-	tview.Styles.PrimitiveBackgroundColor = tcell.GetColor("#1C1E26")
-
 	tview.Borders.HorizontalFocus = tview.Borders.Horizontal
 	tview.Borders.VerticalFocus = tview.Borders.Vertical
 	tview.Borders.TopLeftFocus = tview.Borders.TopLeft
 	tview.Borders.TopRightFocus = tview.Borders.TopRight
 	tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
 	tview.Borders.BottomRightFocus = tview.Borders.BottomRight
-
 	tview.Borders.Horizontal = ' '
 	tview.Borders.Vertical = ' '
 	tview.Borders.TopLeft = ' '
@@ -43,10 +41,11 @@ func main() {
 	tview.Borders.BottomLeft = ' '
 	tview.Borders.BottomRight = ' '
 
+	theme = util.NewTheme()
 	loginModal = ui.NewLoginModal(onLoginModalDone)
-	guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected)
-	channelsList = ui.NewChannelsList(onChannelsListSelected)
-	messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged)
+	guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected, theme)
+	channelsList = ui.NewChannelsList(onChannelsListSelected, theme)
+	messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged, theme)
 	mainFlex = ui.NewMainFlex(guildsDropDown, channelsList, messagesTextView)
 	app = ui.NewApp()
 
@@ -177,7 +176,7 @@ func onChannelsListSelected(i int, mainText string, secondaryText string, _ rune
 	messagesTextView.Clear()
 
 	if messageInputField == nil {
-		messageInputField = ui.NewMessageInputField(onMessageInputFieldDone)
+		messageInputField = ui.NewMessageInputField(onMessageInputFieldDone, theme)
 		mainFlex.AddItem(messageInputField, 3, 1, false)
 	}
 

+ 7 - 2
ui/dropdowns.go

@@ -1,15 +1,20 @@
 package ui
 
 import (
+	"github.com/gdamore/tcell/v2"
+	"github.com/rigormorrtiss/discordo/util"
 	"github.com/rivo/tview"
 )
 
-func NewGuildsDropDown(onGuildsDropDownSelected func(text string, index int)) (guildsDropDown *tview.DropDown) {
+func NewGuildsDropDown(onGuildsDropDownSelected func(text string, index int), theme *util.Theme) (guildsDropDown *tview.DropDown) {
 	guildsDropDown = tview.NewDropDown().
 		SetLabel("Guild: ").
 		SetSelectedFunc(onGuildsDropDownSelected)
 	guildsDropDown.
-		SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
+		SetLabelColor(tcell.GetColor(theme.DropDownForeground)).
+		SetFieldBackgroundColor(tcell.GetColor(theme.DropDownBackground)).
+		SetFieldTextColor(tcell.GetColor(theme.DropDownForeground)).
+		SetBackgroundColor(tcell.GetColor(theme.DropDownBackground)).
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1)
 

+ 5 - 3
ui/inputfields.go

@@ -2,17 +2,19 @@ package ui
 
 import (
 	"github.com/gdamore/tcell/v2"
+	"github.com/rigormorrtiss/discordo/util"
 	"github.com/rivo/tview"
 )
 
-func NewMessageInputField(onMessageInputFieldDone func(key tcell.Key)) (messageInputField *tview.InputField) {
+func NewMessageInputField(onMessageInputFieldDone func(key tcell.Key), theme *util.Theme) (messageInputField *tview.InputField) {
 	messageInputField = tview.NewInputField().
 		SetPlaceholder("Message...").
 		SetFieldWidth(0).
 		SetDoneFunc(onMessageInputFieldDone)
 	messageInputField.
-		SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
-		SetPlaceholderTextColor(tcell.ColorDarkGray).
+		SetFieldBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
+		SetPlaceholderTextColor(tcell.GetColor(theme.InputFieldPlaceholderForeground)).
+		SetBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1)
 

+ 6 - 4
ui/lists.go

@@ -2,17 +2,19 @@ package ui
 
 import (
 	"github.com/gdamore/tcell/v2"
+	"github.com/rigormorrtiss/discordo/util"
 	"github.com/rivo/tview"
 )
 
-func NewChannelsList(onChannelsListSelected func(i int, mainText string, secondaryText string, _ rune)) (channelsList *tview.List) {
+func NewChannelsList(onChannelsListSelected func(i int, mainText string, secondaryText string, _ rune), theme *util.Theme) (channelsList *tview.List) {
 	channelsList = tview.NewList().
 		ShowSecondaryText(false).
-		SetMainTextColor(tcell.GetColor("#6C6F93")).
-		SetSelectedTextColor(tcell.ColorWhite).
-		SetSelectedBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
 		SetSelectedFunc(onChannelsListSelected)
 	channelsList.
+		SetMainTextColor(tcell.GetColor(theme.ListMainTextForeground)).
+		SetSelectedTextColor(tcell.GetColor(theme.ListSelectedForeground)).
+		SetSelectedBackgroundColor(tcell.GetColor(theme.ListBackground)).
+		SetBackgroundColor(tcell.GetColor(theme.ListBackground)).
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1)
 

+ 5 - 1
ui/textviews.go

@@ -1,10 +1,12 @@
 package ui
 
 import (
+	"github.com/gdamore/tcell/v2"
+	"github.com/rigormorrtiss/discordo/util"
 	"github.com/rivo/tview"
 )
 
-func NewMessagesTextView(onMessagesTextViewChanged func()) (messagesTextView *tview.TextView) {
+func NewMessagesTextView(onMessagesTextViewChanged func(), theme *util.Theme) (messagesTextView *tview.TextView) {
 	messagesTextView = tview.NewTextView().
 		SetDynamicColors(true).
 		SetWrap(true).
@@ -13,6 +15,8 @@ func NewMessagesTextView(onMessagesTextViewChanged func()) (messagesTextView *tv
 		ScrollToEnd().
 		SetChangedFunc(onMessagesTextViewChanged)
 	messagesTextView.
+		SetTextColor(tcell.GetColor(theme.TextViewForeground)).
+		SetBackgroundColor(tcell.GetColor(theme.TextViewBackground)).
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1)
 

+ 2 - 2
util/discord.go

@@ -14,7 +14,7 @@ func WriteMessage(messagesTextView *tview.TextView, session *discordgo.Session,
 	var content strings.Builder
 
 	if session.State.User.ID == message.Author.ID {
-		content.WriteString("[#29D398::b]")
+		content.WriteString("[#26BBD9::b]")
 		content.WriteString(message.Author.Username)
 		content.WriteString("[-:-:-] ")
 	} else {
@@ -25,7 +25,7 @@ func WriteMessage(messagesTextView *tview.TextView, session *discordgo.Session,
 
 	// If the author of the message is a bot account, add "BOT" beside the username of the author.
 	if message.Author.Bot {
-		content.WriteString("[#26BBD9]BOT[-:-:-] ")
+		content.WriteString("[#29D398]BOT[-:-:-] ")
 	}
 
 	if message.Content != "" {

+ 54 - 0
util/theme.go

@@ -0,0 +1,54 @@
+package util
+
+import (
+	"encoding/json"
+	"os"
+)
+
+type Theme struct {
+	DropDownBackground              string `json:"dropdown.background"`
+	DropDownForeground              string `json:"dropdown.foreground"`
+	InputFieldBackground            string `json:"inputField.background"`
+	InputFieldForeground            string `json:"inputField.foreground"`
+	InputFieldPlaceholderForeground string `json:"inputField.placeholderTextForeground"`
+	ListBackground                  string `json:"list.background"`
+	ListMainTextForeground          string `json:"list.mainTextForeground"`
+	ListSelectedForeground          string `json:"list.selectedTextForeground"`
+	TextViewBackground              string `json:"textview.background"`
+	TextViewForeground              string `json:"textview.foreground"`
+}
+
+func NewTheme() *Theme {
+	var theme Theme
+	theme.TextViewBackground = "#2E3440"
+	theme.TextViewForeground = "#D8DEE9"
+	theme.ListBackground = "#2E3440"
+	theme.ListMainTextForeground = "#4C566A"
+	theme.ListSelectedForeground = "#ECEFF4"
+	theme.InputFieldBackground = "#3B4252"
+	theme.InputFieldForeground = "#D8DEE9"
+	theme.InputFieldPlaceholderForeground = "#D8DEE9"
+	theme.DropDownBackground = "#3B4252"
+	theme.DropDownForeground = "#D8DEE9"
+
+	userHomeDir, err := os.UserHomeDir()
+	if err != nil {
+		panic(err)
+	}
+
+	themeFilePath := userHomeDir + "/.config/discordo/theme.json"
+	if _, err := os.Stat(themeFilePath); os.IsNotExist(err) {
+		return &theme
+	}
+
+	data, err := os.ReadFile(themeFilePath)
+	if err != nil {
+		panic(err)
+	}
+
+	if err = json.Unmarshal(data, &theme); err != nil {
+		panic(err)
+	}
+
+	return &theme
+}