Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyaoMa committed Sep 13, 2022
1 parent c053e16 commit a66f58f
Show file tree
Hide file tree
Showing 19 changed files with 424 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dist-ssr
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/

# Go workspace file
go.work
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Prepare and install environment for development?

## Design Pattern Summary

| Pattern | Role | Packages |
| :-------- | :--------------------------------------------------------------------------------------- | :------- |
| Singleton | Maintains read-only state for the backend included connections to database, logger, etc. | app |
| Pattern | Role | Packages |
| :-------- | :------------------------------------------- | :-------------- |
| Singleton | Maintains read-only state and global objects | app, i18n, tray |
28 changes: 0 additions & 28 deletions app/app.go

This file was deleted.

38 changes: 0 additions & 38 deletions app/wails.go

This file was deleted.

27 changes: 27 additions & 0 deletions backend/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app

import (
"sync"
)

var (
once sync.Once
instance *Application
)

type Application struct {
wlc *WailsLifeCycle
}

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

func (a *Application) WailsLifeCycle() *WailsLifeCycle {
return a.wlc
}
38 changes: 38 additions & 0 deletions backend/app/wails.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package app

import (
"context"
"log"
)

// Wailsapp Life Cycle
type WailsLifeCycle struct {
ctx context.Context
}

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

log.Println("WAILS START UP")
}

func (wlc *WailsLifeCycle) DomReady(ctx context.Context) {
log.Println("WAILS DOM READY")
}

func (wlc *WailsLifeCycle) BeforeClose(ctx context.Context) (prevent bool) {
log.Println("WAILS BEFORE CLOSE")
return false
}

func (wlc *WailsLifeCycle) Shutdown(ctx context.Context) {
log.Println("WAILS SHUTDOWN")
}

func (wlc *WailsLifeCycle) Suspend() {
log.Println("WAILS SUSPEND")
}

func (wlc *WailsLifeCycle) Resume() {
log.Println("WAILS RESUME")
}
48 changes: 48 additions & 0 deletions backend/i18n/i18n.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package i18n

import (
"sync"

"golang.org/x/exp/slices"
)

var (
once sync.Once
instance *i18n
)

type i18n struct {
localeMap localeMap
availableLanguages []string
currentLocale string
}

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

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

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

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

func (i *i18n) Locale() (l locale) {
return i.localeMap[i.currentLocale]
}
56 changes: 56 additions & 0 deletions backend/i18n/locales.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package i18n

import (
"embed"
"encoding/json"
"path/filepath"
)

//go:embed locales
var locales embed.FS

const (
dirLocales = "locales"
)

type locale struct {
Lang struct {
Code string `json:"code"`
Text string `json:"text"`
} `json:"lang"`
AppName string `json:"appname"`
DisplayLanguage string `json:"display_language"`
ColorTheme struct {
Title string `json:"title"`
Light string `json:"light"`
Dark string `json:"dark"`
} `json:"color_theme"`
ApiService struct {
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)
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()))
json.Unmarshal(data, &t)
tm[t.Lang.Code] = t
availableLanguages = append(availableLanguages, t.Lang.Code)
}
}
return
}
24 changes: 24 additions & 0 deletions backend/i18n/locales/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"lang": {
"code": "en",
"text": "English"
},
"appname": "My Application",
"quit": "Quit",
"display_language": "Display Language",
"color_theme": {
"title": "Color Theme",
"light": "Light",
"dark": "Dark"
},
"api_service": {
"start": "Enable API service",
"stop": "Disable API service",
"swagger": "Open API docs"
},
"quit_dialog": {
"message": "Are you sure to quit?",
"default_button": "Quit",
"cancel_button": "Cancel"
}
}
24 changes: 24 additions & 0 deletions backend/i18n/locales/zh.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"lang": {
"code": "zh",
"text": "简体中文"
},
"appname": "我的应用程序",
"quit": "退出",
"display_language": "选择 显示语言",
"color_theme": {
"title": "选择 颜色主题",
"light": "默认浅色",
"dark": "默认深色"
},
"api_service": {
"start": "开启 API 服务",
"stop": "关闭 API 服务",
"swagger": "打开 API 文档"
},
"quit_dialog": {
"message": "确认退出 我的应用程序 吗?",
"default_button": "退出",
"cancel_button": "取消"
}
}
Binary file added backend/tray/icons/api-start.ico
Binary file not shown.
Binary file added backend/tray/icons/api-stop.ico
Binary file not shown.
Binary file added backend/tray/icons/icon.ico
Binary file not shown.
Binary file added backend/tray/icons/open-window.ico
Binary file not shown.
32 changes: 32 additions & 0 deletions backend/tray/tray.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tray

import (
_ "embed"
"sync"

"github.com/getlantern/systray"
)

//go:embed icons/icon.ico
var icon []byte

var (
once sync.Once
instance *tray
)

type tray struct {
}

func Tray() *tray {
once.Do(func() {
instance = &tray{}
systray.Register(instance.onReady, nil)
})
return instance
}

func (st *tray) onReady() {
systray.SetTemplateIcon(icon, icon)
systray.SetTitle("Webview example")
}
1 change: 1 addition & 0 deletions backend/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package utils
Loading

0 comments on commit a66f58f

Please sign in to comment.