rui_orig/demo/filePickerDemo.go

54 lines
1.3 KiB
Go
Raw Normal View History

2021-11-04 14:59:25 +03:00
package main
import (
"github.com/anoshenko/rui"
)
const filePickerDemoText = `
GridLayout {
2021-11-07 09:43:13 +03:00
width = 100%, height = 100%, cell-height = "auto, 1fr", cell-width = "1fr, auto",
2021-11-04 14:59:25 +03:00
content = [
FilePicker {
id = filePicker, accept = "txt, html"
},
2021-11-07 09:43:13 +03:00
Button {
id = fileDownload, row = 0, column = 1, content = "Download file", disabled = true,
}
2021-11-04 14:59:25 +03:00
EditView {
2021-11-07 09:43:13 +03:00
id = selectedFileData, row = 1, column = 0:1, type = multiline, read-only = true, wrap = true,
2021-11-04 14:59:25 +03:00
}
]
}
`
2021-11-07 09:43:13 +03:00
var downloadedFile []byte = nil
var downloadedFilename = ""
2021-11-04 14:59:25 +03:00
func createFilePickerDemo(session rui.Session) rui.View {
view := rui.CreateViewFromText(session, filePickerDemoText)
if view == nil {
return nil
}
rui.Set(view, "filePicker", rui.FileSelectedEvent, func(picker rui.FilePicker, files []rui.FileInfo) {
if len(files) > 0 {
picker.LoadFile(files[0], func(file rui.FileInfo, data []byte) {
if data != nil {
2021-11-07 09:43:13 +03:00
downloadedFile = data
downloadedFilename = files[0].Name
2021-11-04 14:59:25 +03:00
rui.Set(view, "selectedFileData", rui.Text, string(data))
2021-11-07 09:43:13 +03:00
rui.Set(view, "fileDownload", rui.Disabled, false)
2021-11-04 14:59:25 +03:00
} else {
rui.Set(view, "selectedFileData", rui.Text, rui.LastError())
}
})
}
})
2021-11-07 09:43:13 +03:00
rui.Set(view, "fileDownload", rui.ClickEvent, func() {
view.Session().DownloadFileData(downloadedFilename, downloadedFile)
})
2021-11-04 14:59:25 +03:00
return view
}