package main
import (
"fmt"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"strconv"
"strings"
)
func main() {
mw := &MyMainWindow{}
if err := (MainWindow{
Title: "工具",
// 指定窗口的大小
MinSize: Size{},
AssignTo: &mw.MainWindow,
Layout: VBox{
MarginsZero: true,
},
Children: []Widget{
HSplitter{
MaxSize: Size{0, 50},
Children: []Widget{
Label{Text: " 类型: "},
LineEdit{
AssignTo: &mw.outtitle,
ReadOnly: true,
MaxSize: Size{Height: 4,Width: 1},
},
Label{Text: " 功能: "},
RadioButtonGroup{
Buttons: []RadioButton{
RadioButton{
Name: "all",
Text: "以 ,号隔开",
Value: "1",
AssignTo: &mw.type_1,
},
RadioButton{
Name: "all",
Text: "转化成用“,”连接",
Value: "2",
AssignTo: &mw.type_2,
},
RadioButton{
Name: "all",
Text: "转化成用空格连接",
Value: "3",
AssignTo: &mw.type_3,
},
},
},
Label{Text: " 数量: 0",AssignTo: &mw.numLabel},
},
},
VSplitter{
Children: []Widget{
TextEdit{AssignTo: &mw.inTE,HScroll:true,VScroll:true},
TextEdit{AssignTo: &mw.outTE, ReadOnly: true,HScroll:true,VScroll:true},
PushButton{
AssignTo: &mw.scream,
Text: "运行",
},
},
},
},
}.Create()); err != nil {
return
}
mw.type_1.Clicked().Attach(func() {
go func() {
mw.SetType(mw.type_1.Value(),",号隔开")
}()
})
mw.type_2.Clicked().Attach(func() {
go func() {
mw.SetType(mw.type_2.Value(),"转化成用’,‘连接")
}()
})
mw.type_3.Clicked().Attach(func() {
go func() {
mw.SetType(mw.type_3.Value(),"转化成用空格连接")
}()
})
mw.scream.Clicked().Attach(func() {
if mw.type_id == 0 {
walk.MsgBox(mw, "提示", "请选择功能选项", walk.MsgBoxIconWarning)
return
}
switch mw.type_id {
case 1:
go func() {
mw.run_type_1_scream()
}()
break
case 2:
go func() {
mw.run_type_2_scream()
}()
break
case 3:
go func() {
mw.run_type_3_scream()
}()
break
}
})
mw.Run()
}
type MyMainWindow struct {
*walk.MainWindow
lb *walk.ListBox
te *walk.TextEdit
wv *walk.WebView
type_1 *walk.RadioButton
type_2 *walk.RadioButton
type_3 *walk.RadioButton
scream *walk.PushButton
curtitle string
cururl string
outtitle *walk.LineEdit
out_num *walk.LineEdit
inTE *walk.TextEdit
outTE *walk.TextEdit
type_id int
typeLabel *walk.Label
numLabel *walk.Label
}
func (this *MyMainWindow) SetType(type_id interface{},str string) {
fmt.Println(type_id)
this.type_id,_ = strconv.Atoi(type_id.(string))
this.outtitle.SetText(strings.ToUpper(str))
}
/**
,号隔开
*/
func (this *MyMainWindow) run_type_1_scream() {
str := this.inTE.Text()
result := removeDuplicateElement(strings.Split(str,"\r\n"))
this.count_sum(len(result))
this.outTE.SetText(strings.ToUpper(strings.Join(result,",")))
}
/**
转化成用’,‘连接
*/
func (this *MyMainWindow) run_type_2_scream() {
str := this.inTE.Text()
result := removeDuplicateElement(strings.Split(str,"\r\n"))
result_str := []string{}
for _,v := range result {
result_str =append( result_str,`"` + v +`"`)
}
this.count_sum(len(result_str))
this.outTE.SetText(strings.ToUpper(strings.Join(result_str,",")))
}
/**
转化成用空格连接
*/
func (this *MyMainWindow) run_type_3_scream() {
str := this.inTE.Text()
result := removeDuplicateElement(strings.Split(str,"\r\n"))
this.count_sum(len(result))
this.outTE.SetText(strings.ToUpper(strings.Join(result," ")))
}
func (this *MyMainWindow) count_sum(num int) {
title := " 数量: " + strconv.Itoa(num)
this.numLabel.SetText(title)
}
//去重
func removeDuplicateElement(languages []string) []string {
result := make([]string, 0, len(languages))
temp := map[string]struct{}{}
for _, item := range languages {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}