修正基本架构逻辑,基本实现离线功能
This commit is contained in:
+14
-12
@@ -7,23 +7,25 @@ import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// CreateCards 创建卡片列表
|
||||
func CreateCards(_ fyne.Window) fyne.CanvasObject {
|
||||
// allCreateCards 创建卡片列表
|
||||
func allCreateCards(_ fyne.Window) fyne.CanvasObject {
|
||||
var itemList []fyne.CanvasObject
|
||||
keys := make([]string, 0, len(CardTypeMap))
|
||||
for key, _ := range CardTypeMap {
|
||||
keys := make([]string, 0, len(cardTypeMap))
|
||||
for key, _ := range cardTypeMap {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
info := CardTypeMap[key]
|
||||
itemList = append(itemList, widget.NewButton(info.Name, func() {
|
||||
cWin := fyne.CurrentApp().NewWindow(info.Name)
|
||||
cWin.SetContent(info.createView(cWin))
|
||||
cWin.Resize(info.createSize)
|
||||
cWin.CenterOnScreen()
|
||||
cWin.Show()
|
||||
}))
|
||||
info := cardTypeMap[key]
|
||||
if info.createView != nil {
|
||||
itemList = append(itemList, widget.NewButton(info.Name, func() {
|
||||
cWin := fyne.CurrentApp().NewWindow(info.Name)
|
||||
cWin.SetContent(info.createView(cWin))
|
||||
cWin.Resize(info.createSize)
|
||||
cWin.CenterOnScreen()
|
||||
cWin.Show()
|
||||
}))
|
||||
}
|
||||
}
|
||||
return container.NewCenter(container.NewVBox(itemList...))
|
||||
}
|
||||
|
||||
@@ -9,23 +9,17 @@ import (
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"work_cation/global"
|
||||
"work_cation/models"
|
||||
"work_cation/pkg/utils"
|
||||
"work_cation/repo"
|
||||
"work_cation/service"
|
||||
)
|
||||
|
||||
var erlangCard *models.ErlangCards
|
||||
|
||||
func CreateErlangCard(w fyne.Window) fyne.CanvasObject {
|
||||
if erlangCard == nil {
|
||||
erlangCard = &models.ErlangCards{
|
||||
UUID: utils.Uuid.CreateUUID(),
|
||||
UserID: repo.User.GetUserInfo(global.DB).ID,
|
||||
}
|
||||
}
|
||||
var (
|
||||
// 创建数据
|
||||
erlangCard = &models.ErlangCards{BaseCard: models.NewBaseCard(models.ToolTypeErlang, repo.User.GetUserInfo(global.DB).ID)}
|
||||
// 基础表单
|
||||
formBase = &widget.Form{}
|
||||
// 变量表单
|
||||
@@ -49,8 +43,7 @@ func CreateErlangCard(w fyne.Window) fyne.CanvasObject {
|
||||
return
|
||||
}
|
||||
dialog.ShowInformation("ok", "创建成功", w)
|
||||
// 更换
|
||||
erlangCard.UUID = utils.Uuid.CreateUUID()
|
||||
time.AfterFunc(2*time.Second, func() { w.Close() })
|
||||
})
|
||||
|
||||
return container.NewBorder(container.NewHBox(widget.NewLabel("新建"), widget.NewSeparator()),
|
||||
|
||||
+22
-7
@@ -4,6 +4,7 @@ import (
|
||||
"fyne.io/fyne/v2"
|
||||
"work_cation/models"
|
||||
"work_cation/views/createView"
|
||||
"work_cation/views/showView"
|
||||
)
|
||||
|
||||
// Tutorial 定义教程的数据结构
|
||||
@@ -16,9 +17,9 @@ type Tutorial struct {
|
||||
var (
|
||||
// Tutorials 定义每个教程的元数据
|
||||
Tutorials = map[string]Tutorial{
|
||||
"welcome": {"主页", "", UserViews, true},
|
||||
"canvas": {"我的", "", myCardsViews, true},
|
||||
"create": {"新建", "", CreateCards, true},
|
||||
"welcome": {"主页", "", mainUserViews, true},
|
||||
"canvas": {"我的", "", allCardsViews, true},
|
||||
"create": {"新建", "", allCreateCards, true},
|
||||
}
|
||||
|
||||
// TutorialIndex 定义我们的教程应该如何在索引树中布局
|
||||
@@ -26,7 +27,7 @@ var (
|
||||
"": {"welcome", "canvas", "create"},
|
||||
//"collections": {"list", "table", "tree"},
|
||||
//"containers": {"apptabs", "border", "box", "center", "doctabs", "grid", "scroll", "split"},
|
||||
//"widgets": {"accordion", "button", "card", "entry", "form", "input", "progress", "text", "toolbar"},
|
||||
//"widgets": {"accordion", "button", "card", "entry", "form", "input", "progress", "text", "toolbar"}, 58 * 3 + 106 = 280
|
||||
}
|
||||
)
|
||||
|
||||
@@ -34,11 +35,25 @@ type cardInfo struct {
|
||||
Name string
|
||||
createView func(w fyne.Window) fyne.CanvasObject
|
||||
createSize fyne.Size
|
||||
showView func(fyne.Window, *models.BaseCard) (fyne.CanvasObject, error)
|
||||
}
|
||||
|
||||
var (
|
||||
CardTypeMap = map[string]cardInfo{
|
||||
models.ToolTypeErlang: {"Erlang代码脚本模版", createView.CreateErlangCard, fyne.NewSize(600, 400)},
|
||||
models.ToolTypeExecFiles: {"自定义可执行工具", createView.CreateExecFile, fyne.NewSize(600, 520)},
|
||||
cardTypeMap = map[string]cardInfo{
|
||||
models.ToolTypeNoVarErlang: {
|
||||
"Erlang代码脚本",
|
||||
nil,
|
||||
fyne.NewSize(0, 0),
|
||||
showView.ErlangCardNoVarView},
|
||||
models.ToolTypeErlang: {
|
||||
"Erlang代码脚本模版",
|
||||
createView.CreateErlangCard,
|
||||
fyne.NewSize(600, 400),
|
||||
showView.ErlangCardView},
|
||||
models.ToolTypeExecFiles: {
|
||||
"自定义可执行工具",
|
||||
createView.CreateExecFile,
|
||||
fyne.NewSize(600, 520),
|
||||
showView.ExecFileCardView},
|
||||
}
|
||||
)
|
||||
|
||||
+56
-11
@@ -3,24 +3,69 @@ package views
|
||||
import (
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/theme"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"work_cation/global"
|
||||
"work_cation/models"
|
||||
"work_cation/repo"
|
||||
"work_cation/views/showView"
|
||||
"work_cation/service"
|
||||
)
|
||||
|
||||
func myCardsViews(w fyne.Window) fyne.CanvasObject {
|
||||
var items []fyne.CanvasObject
|
||||
// 卡片展示界面
|
||||
func allCardsViews(w fyne.Window) fyne.CanvasObject {
|
||||
//var items []fyne.CanvasObject
|
||||
gridWrap := container.NewGridWrap(fyne.NewSize(200, 200))
|
||||
for _, baseCard := range repo.BaseCard.FindAll(global.DB) {
|
||||
switch baseCard.ToolType {
|
||||
case models.ToolTypeErlang:
|
||||
erlangCard := repo.ErlangCardRepo.Find(global.DB, baseCard.UUID)
|
||||
items = append(items, showView.ErlangCardView(w, &erlangCard))
|
||||
case models.ToolTypeExecFiles:
|
||||
if info, is := cardTypeMap[baseCard.ToolType]; is {
|
||||
var baseCardCopy = baseCard
|
||||
items = append(items, showView.ExecFileCardView(w, &baseCardCopy))
|
||||
view, err := info.showView(w, &baseCardCopy)
|
||||
if err != nil {
|
||||
view = errorCardView(w, &baseCardCopy, err)
|
||||
}
|
||||
var baseCardV *widget.Card
|
||||
baseCardV = baseCardView(w, baseCardV, baseCardCopy, gridWrap, view)
|
||||
|
||||
gridWrap.Add(baseCardV)
|
||||
}
|
||||
}
|
||||
return container.NewBorder(nil, nil, nil, nil, container.NewScroll(
|
||||
container.NewGridWrap(fyne.NewSize(200, 200), items...)))
|
||||
scroll := container.NewScroll(gridWrap)
|
||||
return container.NewBorder(nil, nil, nil, nil, scroll)
|
||||
}
|
||||
|
||||
// 单卡界面
|
||||
func baseCardView(
|
||||
w fyne.Window,
|
||||
baseCardV *widget.Card,
|
||||
baseCardCopy models.BaseCard,
|
||||
gridWrap *fyne.Container,
|
||||
view fyne.CanvasObject) *widget.Card {
|
||||
|
||||
baseCardV = widget.NewCard(baseCardCopy.Title, baseCardCopy.Text, container.NewBorder(nil, widget.NewToolbar(
|
||||
widget.NewToolbarAction(theme.ContentAddIcon(), func() {}),
|
||||
widget.NewToolbarAction(theme.DeleteIcon(), func() {
|
||||
err := service.BaseCard.Delete(global.DB, baseCardCopy)
|
||||
if err != nil {
|
||||
dialog.ShowInformation("删除失败", err.Error(), w)
|
||||
return
|
||||
}
|
||||
gridWrap.Remove(baseCardV)
|
||||
}),
|
||||
widget.NewToolbarAction(theme.SettingsIcon(), func() {}),
|
||||
widget.NewToolbarAction(theme.ViewRefreshIcon(), func() {
|
||||
|
||||
}),
|
||||
), nil, nil, view))
|
||||
return baseCardV
|
||||
}
|
||||
|
||||
func errorCardView(w fyne.Window, data *models.BaseCard, err error) fyne.CanvasObject {
|
||||
cardButton := widget.NewButton("出错尝试修复", func() {
|
||||
dialog.ShowInformation("结果", "修复失败", w)
|
||||
})
|
||||
showErrButton := widget.NewButton("工具解析错误", func() {
|
||||
dialog.ShowInformation("错误", err.Error(), w)
|
||||
})
|
||||
card := widget.NewCard(data.Title, data.Text, container.NewVBox(showErrButton, cardButton))
|
||||
return card
|
||||
}
|
||||
|
||||
@@ -1,44 +1,67 @@
|
||||
package showView
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/container"
|
||||
"fyne.io/fyne/v2/data/binding"
|
||||
"fyne.io/fyne/v2/dialog"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"github.com/atotto/clipboard"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"work_cation/cfg"
|
||||
"work_cation/models"
|
||||
)
|
||||
|
||||
func ErlangCardView(w fyne.Window, data *models.ErlangCards) fyne.CanvasObject {
|
||||
if len(data.VarName) == 0 {
|
||||
return widget.NewCard(data.Title, data.Text, widget.NewButton("复制", func() {
|
||||
cleanedText := strings.ReplaceAll(data.Template, "\r", "")
|
||||
cleanedText = strings.ReplaceAll(cleanedText, "\n", "")
|
||||
err := clipboard.WriteAll(cleanedText)
|
||||
if err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
dialog.NewInformation("复制脚本成功", data.Template, w).Show()
|
||||
}))
|
||||
}
|
||||
txtBound := binding.NewString()
|
||||
txtBound.Set(data.Template)
|
||||
txtWid := widget.NewEntryWithData(txtBound)
|
||||
|
||||
txtWid.Wrapping = fyne.TextWrapOff
|
||||
cardButton := widget.NewButton("OPEN", func() {
|
||||
go UseErlangCard(data)
|
||||
func ErlangCardNoVarView(w fyne.Window, baseInfo *models.BaseCard) (fyne.CanvasObject, error) {
|
||||
button := widget.NewButton("复制", func() {
|
||||
// 读取类型文件数据
|
||||
infoPath := filepath.Join(cfg.T.CardDir, baseInfo.UUID, cfg.T.CardInfo)
|
||||
bytes, err := os.ReadFile(infoPath)
|
||||
if err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
var data models.ErlangCards
|
||||
err = json.Unmarshal(bytes, &data)
|
||||
if err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
cleanedText := strings.ReplaceAll(data.Template, "\r", "")
|
||||
cleanedText = strings.ReplaceAll(cleanedText, "\n", "")
|
||||
err = clipboard.WriteAll(cleanedText)
|
||||
if err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
dialog.NewInformation("复制脚本成功", data.Template, w).Show()
|
||||
})
|
||||
return button, nil
|
||||
}
|
||||
|
||||
card := widget.NewCard(data.Title, data.Text, cardButton)
|
||||
|
||||
func ErlangCardView(w fyne.Window, baseInfo *models.BaseCard) (fyne.CanvasObject, error) {
|
||||
cardButton := widget.NewButton("OPEN", func() {
|
||||
// 读取类型文件数据
|
||||
infoPath := filepath.Join(cfg.T.CardDir, baseInfo.UUID, cfg.T.CardInfo)
|
||||
bytes, err := os.ReadFile(infoPath)
|
||||
if err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
var data models.ErlangCards
|
||||
err = json.Unmarshal(bytes, &data)
|
||||
if err != nil {
|
||||
dialog.ShowError(err, w)
|
||||
return
|
||||
}
|
||||
go UseErlangCard(&data)
|
||||
})
|
||||
//image := canvas.NewImageFromResource(assets.LogoDataSR)
|
||||
//image.FillMode = canvas.ImageFillContain
|
||||
//card.SetImage(image)
|
||||
return card
|
||||
return cardButton, nil
|
||||
}
|
||||
|
||||
func UseErlangCard(data *models.ErlangCards) {
|
||||
|
||||
@@ -13,12 +13,12 @@ import (
|
||||
"work_cation/models"
|
||||
)
|
||||
|
||||
func ExecFileCardView(w fyne.Window, data *models.BaseCard) fyne.CanvasObject {
|
||||
func ExecFileCardView(w fyne.Window, data *models.BaseCard) (fyne.CanvasObject, error) {
|
||||
cardButton := widget.NewButton("启动", func() { runExecFile(data, w) })
|
||||
openDirButton := widget.NewButton("打开目录", func() { openDir(data, w) })
|
||||
|
||||
card := widget.NewCard(data.Title, data.Text, container.NewVBox(cardButton, openDirButton))
|
||||
return card
|
||||
card := container.NewVBox(cardButton, openDirButton)
|
||||
return card, nil
|
||||
}
|
||||
|
||||
func runExecFile(data *models.BaseCard, w fyne.Window) {
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ import (
|
||||
"work_cation/repo"
|
||||
)
|
||||
|
||||
func UserViews(w fyne.Window) fyne.CanvasObject {
|
||||
func mainUserViews(w fyne.Window) fyne.CanvasObject {
|
||||
image := canvas.NewImageFromResource(assets.LogoDataSR)
|
||||
image.FillMode = canvas.ImageFillContain
|
||||
|
||||
|
||||
Reference in New Issue
Block a user