拓展类型
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
package showView
|
||||
|
||||
import (
|
||||
"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"
|
||||
"strings"
|
||||
"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() {
|
||||
err := clipboard.WriteAll(data.Template)
|
||||
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)
|
||||
})
|
||||
|
||||
card := widget.NewCard(data.Title, data.Text, cardButton)
|
||||
|
||||
//image := canvas.NewImageFromResource(assets.LogoDataSR)
|
||||
//image.FillMode = canvas.ImageFillContain
|
||||
//card.SetImage(image)
|
||||
return card
|
||||
}
|
||||
|
||||
func UseErlangCard(data *models.ErlangCards) {
|
||||
var template = data.Template
|
||||
var vars = data.VarName
|
||||
|
||||
myApp := fyne.CurrentApp()
|
||||
myWindow := myApp.NewWindow(data.Title)
|
||||
myWindow.CenterOnScreen()
|
||||
myWindow.Resize(fyne.NewSize(500, 0))
|
||||
|
||||
var items []*widget.FormItem
|
||||
for index, key := range vars {
|
||||
wid := widget.NewEntry()
|
||||
if len(data.VarContent) > index {
|
||||
wid.SetText(data.VarContent[index])
|
||||
}
|
||||
items = append(items, &widget.FormItem{Text: key, Widget: wid})
|
||||
}
|
||||
|
||||
var text string
|
||||
if data.IsShowOut {
|
||||
text = replaceVars(template, data.VarContent)
|
||||
}
|
||||
showErlang := widget.NewLabel(text)
|
||||
showButton := widget.NewButton("...", nil)
|
||||
upOut := func() {
|
||||
if data.IsShowOut {
|
||||
text = replaceVars(template, data.VarContent)
|
||||
showErlang.SetText(text)
|
||||
showButton.SetText("隐藏输出")
|
||||
} else {
|
||||
text = ""
|
||||
showErlang.SetText(text)
|
||||
showButton.SetText("展示输出")
|
||||
}
|
||||
}
|
||||
upOut()
|
||||
showButton.OnTapped = func() {
|
||||
data.IsShowOut = !data.IsShowOut
|
||||
upOut()
|
||||
}
|
||||
|
||||
form := &widget.Form{
|
||||
Items: items,
|
||||
SubmitText: "生成并复制",
|
||||
OnSubmit: func() {
|
||||
var values []string
|
||||
for _, item := range items {
|
||||
values = append(values, item.Widget.(*widget.Entry).Text)
|
||||
}
|
||||
data.VarContent = values
|
||||
//out.SetText(replaceVars(template, data.VarContent))
|
||||
err := clipboard.WriteAll(replaceVars(template, data.VarContent))
|
||||
if err != nil {
|
||||
dialog.ShowError(err, myWindow)
|
||||
}
|
||||
upOut()
|
||||
},
|
||||
}
|
||||
|
||||
title := widget.NewCard(data.Title, data.Text, nil)
|
||||
|
||||
content := container.NewVBox(
|
||||
title,
|
||||
widget.NewLabel("输入模版参数:"),
|
||||
form,
|
||||
widget.NewLabel("输出:"),
|
||||
showErlang,
|
||||
showButton,
|
||||
)
|
||||
|
||||
myWindow.SetContent(content)
|
||||
myWindow.Show()
|
||||
}
|
||||
|
||||
func replaceVars(input string, replacements []string) string {
|
||||
varIndex := 0
|
||||
result := input
|
||||
|
||||
for strings.Contains(result, "$var") && varIndex < len(replacements) {
|
||||
result = strings.Replace(result, "$var", replacements[varIndex], 1)
|
||||
varIndex++
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user