🎨 Optimize preset theme code

This commit is contained in:
2025-10-20 21:58:37 +08:00
parent 9a15df01ee
commit aa8139884b
26 changed files with 245 additions and 465 deletions

View File

@@ -109,8 +109,9 @@ type EditingConfig struct {
// AppearanceConfig 外观设置配置
type AppearanceConfig struct {
Language LanguageType `json:"language"` // 界面语言
SystemTheme SystemThemeType `json:"systemTheme"` // 系统界面主题
Language LanguageType `json:"language"` // 界面语言
SystemTheme SystemThemeType `json:"systemTheme"` // 系统界面主题
CurrentTheme string `json:"currentTheme"` // 当前选择的预设主题名称
}
// UpdatesConfig 更新设置配置
@@ -179,8 +180,9 @@ func NewDefaultAppConfig() *AppConfig {
AutoSaveDelay: 2000,
},
Appearance: AppearanceConfig{
Language: LangEnUS,
SystemTheme: SystemThemeAuto,
Language: LangEnUS,
SystemTheme: SystemThemeAuto,
CurrentTheme: "default-dark", // 默认使用 default-dark 主题
},
Updates: UpdatesConfig{
Version: version.Version,

View File

@@ -219,7 +219,7 @@ func NewDraculaTheme() *ThemeColorConfig {
Dark: true,
Background: "#282A36",
BackgroundSecondary: "#282A36",
BackgroundSecondary: "#323543FF",
Surface: "#282A36",
DropdownBackground: "#282A36",
DropdownBorder: "#191A21",
@@ -266,7 +266,7 @@ func NewAuraTheme() *ThemeColorConfig {
Dark: true,
Background: "#21202e",
BackgroundSecondary: "#21202e",
BackgroundSecondary: "#2B2A3BFF",
Surface: "#21202e",
DropdownBackground: "#21202e",
DropdownBorder: "#3b334b",
@@ -313,7 +313,7 @@ func NewGitHubDarkTheme() *ThemeColorConfig {
Dark: true,
Background: "#24292e",
BackgroundSecondary: "#24292e",
BackgroundSecondary: "#2E343BFF",
Surface: "#24292e",
DropdownBackground: "#24292e",
DropdownBorder: "#1b1f23",
@@ -360,7 +360,7 @@ func NewMaterialDarkTheme() *ThemeColorConfig {
Dark: true,
Background: "#263238",
BackgroundSecondary: "#263238",
BackgroundSecondary: "#2D3E46FF",
Surface: "#263238",
DropdownBackground: "#263238",
DropdownBorder: "#FFFFFF10",
@@ -407,7 +407,7 @@ func NewOneDarkTheme() *ThemeColorConfig {
Dark: true,
Background: "#282c34",
BackgroundSecondary: "#2c313a",
BackgroundSecondary: "#313949FF",
Surface: "#353a42",
DropdownBackground: "#21252b",
DropdownBorder: "#7d8799",
@@ -454,7 +454,7 @@ func NewSolarizedDarkTheme() *ThemeColorConfig {
Dark: true,
Background: "#002B36",
BackgroundSecondary: "#002B36",
BackgroundSecondary: "#003643FF",
Surface: "#002B36",
DropdownBackground: "#002B36",
DropdownBorder: "#2AA19899",
@@ -501,7 +501,7 @@ func NewTokyoNightTheme() *ThemeColorConfig {
Dark: true,
Background: "#1a1b26",
BackgroundSecondary: "#1a1b26",
BackgroundSecondary: "#272839FF",
Surface: "#1a1b26",
DropdownBackground: "#1a1b26",
DropdownBorder: "#787c99",
@@ -548,7 +548,7 @@ func NewTokyoNightStormTheme() *ThemeColorConfig {
Dark: true,
Background: "#24283b",
BackgroundSecondary: "#24283b",
BackgroundSecondary: "#2B3151FF",
Surface: "#24283b",
DropdownBackground: "#24283b",
DropdownBorder: "#7982a9",
@@ -597,7 +597,7 @@ func NewGitHubLightTheme() *ThemeColorConfig {
Dark: false,
Background: "#fff",
BackgroundSecondary: "#fff",
BackgroundSecondary: "#f1faf1",
Surface: "#fff",
DropdownBackground: "#fff",
DropdownBorder: "#e1e4e8",
@@ -644,7 +644,7 @@ func NewMaterialLightTheme() *ThemeColorConfig {
Dark: false,
Background: "#FAFAFA",
BackgroundSecondary: "#FAFAFA",
BackgroundSecondary: "#f1faf1",
Surface: "#FAFAFA",
DropdownBackground: "#FAFAFA",
DropdownBorder: "#00000010",
@@ -691,7 +691,7 @@ func NewSolarizedLightTheme() *ThemeColorConfig {
Dark: false,
Background: "#FDF6E3",
BackgroundSecondary: "#FDF6E3",
BackgroundSecondary: "#FFEEBCD4",
Surface: "#FDF6E3",
DropdownBackground: "#FDF6E3",
DropdownBorder: "#D3AF86",
@@ -738,7 +738,7 @@ func NewTokyoNightDayTheme() *ThemeColorConfig {
Dark: false,
Background: "#e1e2e7",
BackgroundSecondary: "#e1e2e7",
BackgroundSecondary: "#D2D8EFFF",
Surface: "#e1e2e7",
DropdownBackground: "#e1e2e7",
DropdownBorder: "#6a6f8e",

View File

@@ -150,18 +150,35 @@ func (ts *ThemeService) initializeDefaultThemes() error {
return nil
}
// GetThemeByID 根据ID获取主题
func (ts *ThemeService) GetThemeByID(id int) (*models.Theme, error) {
query := `
SELECT id, name, type, colors, is_default, created_at, updated_at
FROM themes
WHERE id = ?
LIMIT 1
`
// GetThemeByID 根据ID或名称获取主题
// 如果 id > 0按ID查询如果 id = 0按名称查询
func (ts *ThemeService) GetThemeByIdOrName(id int, name ...string) (*models.Theme, error) {
var query string
var args []interface{}
if id > 0 {
query = `
SELECT id, name, type, colors, is_default, created_at, updated_at
FROM themes
WHERE id = ?
LIMIT 1
`
args = []interface{}{id}
} else if len(name) > 0 && name[0] != "" {
query = `
SELECT id, name, type, colors, is_default, created_at, updated_at
FROM themes
WHERE name = ?
LIMIT 1
`
args = []interface{}{name[0]}
} else {
return nil, fmt.Errorf("either id or name must be provided")
}
theme := &models.Theme{}
db := ts.getDB()
err := db.QueryRow(query, id).Scan(
err := db.QueryRow(query, args...).Scan(
&theme.ID,
&theme.Name,
&theme.Type,
@@ -173,86 +190,17 @@ func (ts *ThemeService) GetThemeByID(id int) (*models.Theme, error) {
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("theme not found with id: %d", id)
if id > 0 {
return nil, fmt.Errorf("theme not found with id: %d", id)
}
return nil, fmt.Errorf("theme not found with name: %s", name[0])
}
return nil, fmt.Errorf("failed to get theme by id: %w", err)
return nil, fmt.Errorf("failed to get theme: %w", err)
}
return theme, nil
}
// GetThemeByType 根据类型获取默认主题
func (ts *ThemeService) GetThemeByType(themeType models.ThemeType) (*models.Theme, error) {
query := `
SELECT id, name, type, colors, is_default, created_at, updated_at
FROM themes
WHERE type = ? AND is_default = 1
LIMIT 1
`
theme := &models.Theme{}
db := ts.getDB()
err := db.QueryRow(query, themeType).Scan(
&theme.ID,
&theme.Name,
&theme.Type,
&theme.Colors,
&theme.IsDefault,
&theme.CreatedAt,
&theme.UpdatedAt,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("no default theme found for type: %s", themeType)
}
return nil, fmt.Errorf("failed to get theme by type: %w", err)
}
return theme, nil
}
// GetThemesByType 根据类型获取所有主题
func (ts *ThemeService) GetThemesByType(themeType models.ThemeType) ([]*models.Theme, error) {
query := `
SELECT id, name, type, colors, is_default, created_at, updated_at
FROM themes
WHERE type = ?
ORDER BY is_default DESC, name ASC
`
db := ts.getDB()
rows, err := db.Query(query, themeType)
if err != nil {
return nil, fmt.Errorf("failed to query themes by type: %w", err)
}
defer rows.Close()
var themes []*models.Theme
for rows.Next() {
theme := &models.Theme{}
err := rows.Scan(
&theme.ID,
&theme.Name,
&theme.Type,
&theme.Colors,
&theme.IsDefault,
&theme.CreatedAt,
&theme.UpdatedAt,
)
if err != nil {
return nil, fmt.Errorf("failed to scan theme: %w", err)
}
themes = append(themes, theme)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to iterate themes: %w", err)
}
return themes, nil
}
// UpdateTheme 更新主题
func (ts *ThemeService) UpdateTheme(id int, colors models.ThemeColorConfig) error {
query := `
@@ -280,9 +228,9 @@ func (ts *ThemeService) UpdateTheme(id int, colors models.ThemeColorConfig) erro
}
// ResetTheme 重置主题为预设配置
func (ts *ThemeService) ResetTheme(id int) error {
func (ts *ThemeService) ResetTheme(id int, name ...string) error {
// 先获取主题信息
theme, err := ts.GetThemeByID(id)
theme, err := ts.GetThemeByIdOrName(id, name...)
if err != nil {
return err
}
@@ -331,37 +279,6 @@ func (ts *ThemeService) ResetTheme(id int) error {
return ts.UpdateTheme(id, *presetConfig)
}
// CreateTheme 创建新主题
func (ts *ThemeService) CreateTheme(theme *models.Theme) (*models.Theme, error) {
query := `
INSERT INTO themes (name, type, colors, is_default, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
`
db := ts.getDB()
result, err := db.Exec(
query,
theme.Name,
theme.Type,
theme.Colors,
theme.IsDefault,
theme.CreatedAt,
theme.UpdatedAt,
)
if err != nil {
return nil, fmt.Errorf("failed to create theme: %w", err)
}
id, err := result.LastInsertId()
if err != nil {
return nil, fmt.Errorf("failed to get theme ID: %w", err)
}
theme.ID = int(id)
return theme, nil
}
// GetAllThemes 获取所有主题
func (ts *ThemeService) GetAllThemes() ([]*models.Theme, error) {
query := `