Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyaoMa committed Sep 14, 2022
1 parent a66f58f commit c7bcede
Show file tree
Hide file tree
Showing 16 changed files with 676 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# My App (my-app)

My App is a continuously updated service collection.
My App is a continuously updated personal service collection.

Prepare and install environment for development?

Expand Down
10 changes: 5 additions & 5 deletions backend/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import (

var (
once sync.Once
instance *Application
instance *app
)

type Application struct {
type app struct {
wlc *WailsLifeCycle
}

func App() *Application {
func App() *app {
once.Do(func() {
instance = &Application{
instance = &app{
wlc: &WailsLifeCycle{},
}
})
return instance
}

func (a *Application) WailsLifeCycle() *WailsLifeCycle {
func (a *app) WailsLifeCycle() *WailsLifeCycle {
return a.wlc
}
8 changes: 7 additions & 1 deletion backend/app/wails.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package app
import (
"context"
"log"
"my-app/backend/i18n"
"my-app/backend/tray"
"my-app/backend/tray/menus"
)

// Wailsapp Life Cycle
Expand All @@ -12,7 +15,10 @@ type WailsLifeCycle struct {

func (wlc *WailsLifeCycle) Startup(ctx context.Context) {
wlc.ctx = ctx

tray.Tray().
SetWailsContext(ctx).
ChangeTheme(menus.ColorThemeSystem).
ChangeLanguage(i18n.I18n().GetCurrentLanguage())
log.Println("WAILS START UP")
}

Expand Down
33 changes: 22 additions & 11 deletions backend/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,48 @@ var (
)

type i18n struct {
localeMap localeMap
localeMap map[string]Locale
availableLanguages []string
currentLocale string
currentLanguage string
}

func I18n() *i18n {
once.Do(func() {
localeMap, availableLanguages := load()

currentLocale := ""
currentLanguage := ""
if len(availableLanguages) > 0 {
currentLocale = availableLanguages[0]
currentLanguage = availableLanguages[0]
}

instance = &i18n{
localeMap: localeMap,
availableLanguages: availableLanguages,
currentLocale: currentLocale,
currentLanguage: currentLanguage,
}
})
return instance
}

func (i *i18n) Change(lang string) (ok bool) {
func (i *i18n) Change(lang string) *i18n {
if slices.Contains(i.availableLanguages, lang) {
i.currentLocale = lang
return true
i.currentLanguage = lang
}
return false
return i
}

func (i *i18n) Locale() (l locale) {
return i.localeMap[i.currentLocale]
func (i *i18n) Get(lang string) (l Locale) {
return i.localeMap[lang]
}

func (i *i18n) GetCurrentLanguage() string {
return i.currentLanguage
}

func (i *i18n) GetLangText(lang string) string {
return i.localeMap[lang].Lang.Text
}

func (i *i18n) Locale() (l Locale) {
return i.Get(i.currentLanguage)
}
36 changes: 19 additions & 17 deletions backend/i18n/locales.go → backend/i18n/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,56 @@ package i18n
import (
"embed"
"encoding/json"
"path/filepath"
)

//go:embed locales
var locales embed.FS

const (
dirLocales = "locales"
En = "en"
Zh = "zh"
)

type locale struct {
type Locale struct {
Lang struct {
Code string `json:"code"`
Text string `json:"text"`
} `json:"lang"`
AppName string `json:"appname"`
OpenWindow string `json:"open_window"`
Quit string `json:"quit"`
DisplayLanguage string `json:"display_language"`
ColorTheme struct {
Title string `json:"title"`
Light string `json:"light"`
Dark string `json:"dark"`
Title string `json:"title"`
System string `json:"system"`
Light string `json:"light"`
Dark string `json:"dark"`
} `json:"color_theme"`
ApiService struct {
Swagger string `json:"swagger"`
Start string `json:"start"`
Stop string `json:"stop"`
Swagger string `json:"swagger"`
} `json:"api_service"`
Quit string `json:"quit"`
QuitDialog struct {
Message string `json:"message"`
DefaultButton string `json:"default_button"`
CancelButton string `json:"cancel_button"`
} `json:"quit_dialog"`
}

type localeMap map[string]locale

func load() (tm localeMap, availableLanguages []string) {
tm = make(localeMap)
func load() (localeMap map[string]Locale, availableLanguages []string) {
dirLocales := "locales"
var al []string
lm := make(map[string]Locale)
files, _ := locales.ReadDir(dirLocales)
for _, f := range files {
if !f.IsDir() { // load only locale JSON file
t := locale{}
data, _ := locales.ReadFile(filepath.Join(dirLocales, f.Name()))
t := Locale{}
data, _ := locales.ReadFile(dirLocales + "/" + f.Name()) // embed use slash as separator
json.Unmarshal(data, &t)
tm[t.Lang.Code] = t
availableLanguages = append(availableLanguages, t.Lang.Code)
lm[t.Lang.Code] = t
al = append(al, t.Lang.Code)
}
}
return
return lm, al
}
6 changes: 4 additions & 2 deletions backend/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
"text": "English"
},
"appname": "My Application",
"open_window": "Open My Application",
"quit": "Quit",
"display_language": "Display Language",
"color_theme": {
"title": "Color Theme",
"system": "System Default",
"light": "Light",
"dark": "Dark"
},
"api_service": {
"swagger": "Open API docs",
"start": "Enable API service",
"stop": "Disable API service",
"swagger": "Open API docs"
"stop": "Disable API service"
},
"quit_dialog": {
"message": "Are you sure to quit?",
Expand Down
10 changes: 6 additions & 4 deletions backend/i18n/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
"text": "简体中文"
},
"appname": "我的应用程序",
"open_window": "打开 我的应用程序",
"quit": "退出",
"display_language": "选择 显示语言",
"color_theme": {
"title": "选择 颜色主题",
"light": "默认浅色",
"dark": "默认深色"
"system": "跟随系统",
"light": "浅色",
"dark": "深色"
},
"api_service": {
"swagger": "打开 API 文档",
"start": "开启 API 服务",
"stop": "关闭 API 服务",
"swagger": "打开 API 文档"
"stop": "关闭 API 服务"
},
"quit_dialog": {
"message": "确认退出 我的应用程序 吗?",
Expand Down
120 changes: 120 additions & 0 deletions backend/tray/menus/api_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package menus

import (
"my-app/backend/i18n"

"github.com/getlantern/systray"
)

type ApiServiceListener struct {
OnOpenSwagger func()
OnStart func() (ok bool)
OnStop func() (ok bool)
}

type ApiService struct {
isWatched bool
chanStop chan struct{}
isEnabled bool
swagger *systray.MenuItem
start *systray.MenuItem
stop *systray.MenuItem
}

func NewApiService() *ApiService {
return &ApiService{
chanStop: make(chan struct{}, 1),
swagger: systray.AddMenuItem("", ""),
start: systray.AddMenuItem("", ""),
stop: systray.AddMenuItem("", ""),
}
}

func (as *ApiService) SetIconSwagger(templateIconBytes []byte, regularIconBytes []byte) *ApiService {
as.swagger.SetTemplateIcon(templateIconBytes, regularIconBytes)
return as
}

func (as *ApiService) SetIconStart(templateIconBytes []byte, regularIconBytes []byte) *ApiService {
as.start.SetTemplateIcon(templateIconBytes, regularIconBytes)
return as
}

func (as *ApiService) SetIconStop(templateIconBytes []byte, regularIconBytes []byte) *ApiService {
as.stop.SetTemplateIcon(templateIconBytes, regularIconBytes)
return as
}

func (as *ApiService) SetLocale(locale i18n.Locale) *ApiService {
as.start.SetTitle(locale.ApiService.Start)
as.start.SetTooltip(locale.ApiService.Start)
as.stop.SetTitle(locale.ApiService.Stop)
as.stop.SetTooltip(locale.ApiService.Stop)
as.swagger.SetTitle(locale.ApiService.Swagger)
as.swagger.SetTooltip(locale.ApiService.Swagger)
if as.isEnabled {
as.start.Hide()
as.stop.Show()
as.swagger.Show()
} else {
as.start.Show()
as.stop.Hide()
as.swagger.Hide()
}
return as
}

func (as *ApiService) Watch(listener ApiServiceListener) *ApiService {
if as.isWatched {
return as
}

as.isWatched = true
go func() {
for {
select {
case <-as.start.ClickedCh:
if listener.OnStart() {
as.start.Hide()
as.stop.Show()
as.swagger.Show()
as.isEnabled = true
}
case <-as.stop.ClickedCh:
if listener.OnStop() {
as.start.Show()
as.stop.Hide()
as.swagger.Hide()
as.isEnabled = false
}
case <-as.swagger.ClickedCh:
listener.OnOpenSwagger()
case <-as.chanStop:
return
}
}
}()
return as
}

func (as *ApiService) StopWatch() *ApiService {
if as.isWatched {
as.chanStop <- struct{}{}
}
return as
}

func (as *ApiService) ClickOpenSwagger() *ApiService {
as.swagger.ClickedCh <- struct{}{}
return as
}

func (as *ApiService) ClickStart() *ApiService {
as.start.ClickedCh <- struct{}{}
return as
}

func (as *ApiService) ClickStop() *ApiService {
as.stop.ClickedCh <- struct{}{}
return as
}
Loading

0 comments on commit c7bcede

Please sign in to comment.