2024-01-14 16:31:07 +06:00
|
|
|
package convertor
|
|
|
|
|
|
|
|
import (
|
2024-01-18 20:23:23 +06:00
|
|
|
"errors"
|
2024-01-14 16:31:07 +06:00
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/canvas"
|
|
|
|
"fyne.io/fyne/v2/container"
|
|
|
|
"fyne.io/fyne/v2/dialog"
|
|
|
|
"fyne.io/fyne/v2/widget"
|
2024-02-04 21:00:55 +06:00
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/src/helper"
|
|
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/src/localizer"
|
2024-01-28 22:01:16 +06:00
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
2024-01-14 16:31:07 +06:00
|
|
|
"image/color"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ViewContract interface {
|
|
|
|
Main(
|
2024-01-20 01:53:25 +06:00
|
|
|
runConvert func(setting HandleConvertSetting, progressbar *widget.ProgressBar) error,
|
2024-01-14 16:31:07 +06:00
|
|
|
)
|
2024-01-31 21:02:12 +06:00
|
|
|
SelectFFPath(
|
2024-02-01 00:23:28 +06:00
|
|
|
ffmpegPath string,
|
|
|
|
ffprobePath string,
|
|
|
|
save func(ffmpegPath string, ffprobePath string) error,
|
|
|
|
cancel func(),
|
2024-02-04 20:16:15 +06:00
|
|
|
donwloadFFmpeg func(progressBar *widget.ProgressBar, progressMessage *canvas.Text) error,
|
2024-01-31 21:02:12 +06:00
|
|
|
)
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
type View struct {
|
2024-01-28 22:01:16 +06:00
|
|
|
w fyne.Window
|
|
|
|
localizerService localizer.ServiceContract
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
type HandleConvertSetting struct {
|
2024-01-18 20:23:23 +06:00
|
|
|
VideoFileInput *File
|
|
|
|
DirectoryForSave string
|
|
|
|
OverwriteOutputFiles bool
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
2024-01-15 20:28:02 +06:00
|
|
|
type enableFormConversionStruct struct {
|
|
|
|
fileVideoForConversion *widget.Button
|
2024-01-18 20:23:23 +06:00
|
|
|
buttonForSelectedDir *widget.Button
|
2024-01-15 20:28:02 +06:00
|
|
|
form *widget.Form
|
|
|
|
}
|
|
|
|
|
2024-01-28 22:01:16 +06:00
|
|
|
func NewView(w fyne.Window, localizerService localizer.ServiceContract) *View {
|
|
|
|
return &View{
|
|
|
|
w: w,
|
|
|
|
localizerService: localizerService,
|
|
|
|
}
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v View) Main(
|
2024-01-20 01:53:25 +06:00
|
|
|
runConvert func(setting HandleConvertSetting, progressbar *widget.ProgressBar) error,
|
2024-01-14 16:31:07 +06:00
|
|
|
) {
|
2024-01-15 20:28:02 +06:00
|
|
|
form := &widget.Form{}
|
2024-01-14 16:31:07 +06:00
|
|
|
|
2024-02-01 00:28:06 +06:00
|
|
|
conversionMessage := canvas.NewText("", color.RGBA{R: 255, G: 0, B: 0, A: 255})
|
2024-01-14 16:31:07 +06:00
|
|
|
conversionMessage.TextSize = 16
|
|
|
|
conversionMessage.TextStyle = fyne.TextStyle{Bold: true}
|
|
|
|
|
|
|
|
progress := widget.NewProgressBar()
|
|
|
|
|
2024-01-15 20:28:02 +06:00
|
|
|
fileVideoForConversion, fileVideoForConversionMessage, fileInput := v.getButtonFileVideoForConversion(form, progress, conversionMessage)
|
2024-01-18 20:23:23 +06:00
|
|
|
buttonForSelectedDir, buttonForSelectedDirMessage, pathToSaveDirectory := v.getButtonForSelectingDirectoryForSaving()
|
|
|
|
|
|
|
|
isOverwriteOutputFiles := false
|
2024-01-28 22:01:16 +06:00
|
|
|
checkboxOverwriteOutputFilesTitle := v.localizerService.GetMessage(&i18n.LocalizeConfig{
|
|
|
|
MessageID: "checkboxOverwriteOutputFilesTitle",
|
|
|
|
})
|
|
|
|
checkboxOverwriteOutputFiles := widget.NewCheck(checkboxOverwriteOutputFilesTitle, func(b bool) {
|
2024-01-18 20:23:23 +06:00
|
|
|
isOverwriteOutputFiles = b
|
|
|
|
})
|
2024-01-15 20:28:02 +06:00
|
|
|
|
|
|
|
form.Items = []*widget.FormItem{
|
2024-01-28 22:01:16 +06:00
|
|
|
{
|
|
|
|
Text: v.localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: "fileVideoForConversionTitle"}),
|
|
|
|
Widget: fileVideoForConversion,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Widget: fileVideoForConversionMessage,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Text: v.localizerService.GetMessage(&i18n.LocalizeConfig{MessageID: "buttonForSelectedDirTitle"}),
|
|
|
|
Widget: buttonForSelectedDir,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Widget: buttonForSelectedDirMessage,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Widget: checkboxOverwriteOutputFiles,
|
|
|
|
},
|
2024-01-15 20:28:02 +06:00
|
|
|
}
|
2024-01-28 22:01:16 +06:00
|
|
|
form.SubmitText = v.localizerService.GetMessage(&i18n.LocalizeConfig{
|
|
|
|
MessageID: "converterVideoFilesSubmitTitle",
|
|
|
|
})
|
2024-01-15 20:28:02 +06:00
|
|
|
|
|
|
|
enableFormConversionStruct := enableFormConversionStruct{
|
|
|
|
fileVideoForConversion: fileVideoForConversion,
|
2024-01-18 20:23:23 +06:00
|
|
|
buttonForSelectedDir: buttonForSelectedDir,
|
2024-01-15 20:28:02 +06:00
|
|
|
form: form,
|
|
|
|
}
|
|
|
|
|
|
|
|
form.OnSubmit = func() {
|
2024-01-18 20:23:23 +06:00
|
|
|
if len(*pathToSaveDirectory) == 0 {
|
2024-01-28 22:01:16 +06:00
|
|
|
showConversionMessage(conversionMessage, errors.New(v.localizerService.GetMessage(&i18n.LocalizeConfig{
|
|
|
|
MessageID: "errorSelectedFolderSave",
|
|
|
|
})))
|
2024-01-18 20:23:23 +06:00
|
|
|
enableFormConversion(enableFormConversionStruct)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conversionMessage.Text = ""
|
|
|
|
|
2024-01-15 20:28:02 +06:00
|
|
|
fileVideoForConversion.Disable()
|
2024-01-18 20:23:23 +06:00
|
|
|
buttonForSelectedDir.Disable()
|
2024-01-15 20:28:02 +06:00
|
|
|
form.Disable()
|
|
|
|
|
|
|
|
setting := HandleConvertSetting{
|
2024-01-18 20:23:23 +06:00
|
|
|
VideoFileInput: fileInput,
|
|
|
|
DirectoryForSave: *pathToSaveDirectory,
|
|
|
|
OverwriteOutputFiles: isOverwriteOutputFiles,
|
2024-01-15 20:28:02 +06:00
|
|
|
}
|
2024-01-20 01:53:25 +06:00
|
|
|
err := runConvert(setting, progress)
|
2024-01-15 20:28:02 +06:00
|
|
|
if err != nil {
|
|
|
|
showConversionMessage(conversionMessage, err)
|
|
|
|
enableFormConversion(enableFormConversionStruct)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
enableFormConversion(enableFormConversionStruct)
|
|
|
|
}
|
|
|
|
|
2024-01-28 22:01:16 +06:00
|
|
|
converterVideoFilesTitle := v.localizerService.GetMessage(&i18n.LocalizeConfig{
|
|
|
|
MessageID: "converterVideoFilesTitle",
|
|
|
|
})
|
|
|
|
v.w.SetContent(widget.NewCard(converterVideoFilesTitle, "", container.NewVBox(form, conversionMessage, progress)))
|
2024-01-15 20:28:02 +06:00
|
|
|
form.Disable()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v View) getButtonFileVideoForConversion(form *widget.Form, progress *widget.ProgressBar, conversionMessage *canvas.Text) (*widget.Button, *canvas.Text, *File) {
|
|
|
|
fileInput := &File{}
|
|
|
|
|
2024-02-01 00:28:06 +06:00
|
|
|
fileVideoForConversionMessage := canvas.NewText("", color.RGBA{R: 255, G: 0, B: 0, A: 255})
|
2024-01-15 20:28:02 +06:00
|
|
|
fileVideoForConversionMessage.TextSize = 16
|
|
|
|
fileVideoForConversionMessage.TextStyle = fyne.TextStyle{Bold: true}
|
|
|
|
|
2024-01-28 22:01:16 +06:00
|
|
|
buttonTitle := v.localizerService.GetMessage(&i18n.LocalizeConfig{
|
|
|
|
MessageID: "choose",
|
|
|
|
})
|
|
|
|
|
|
|
|
button := widget.NewButton(buttonTitle, func() {
|
2024-01-14 16:31:07 +06:00
|
|
|
fileDialog := dialog.NewFileOpen(
|
|
|
|
func(r fyne.URIReadCloser, err error) {
|
|
|
|
if err != nil {
|
|
|
|
fileVideoForConversionMessage.Text = err.Error()
|
|
|
|
setStringErrorStyle(fileVideoForConversionMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-15 20:28:02 +06:00
|
|
|
fileInput.Path = r.URI().Path()
|
|
|
|
fileInput.Name = r.URI().Name()
|
|
|
|
fileInput.Ext = r.URI().Extension()
|
|
|
|
|
2024-01-14 16:31:07 +06:00
|
|
|
fileVideoForConversionMessage.Text = r.URI().Path()
|
|
|
|
setStringSuccessStyle(fileVideoForConversionMessage)
|
|
|
|
|
|
|
|
form.Enable()
|
2024-01-20 17:05:16 +06:00
|
|
|
progress.Value = 0
|
|
|
|
progress.Refresh()
|
2024-01-15 20:28:02 +06:00
|
|
|
conversionMessage.Text = ""
|
2024-01-14 16:31:07 +06:00
|
|
|
}, v.w)
|
2024-01-23 21:33:01 +06:00
|
|
|
helper.FileDialogResize(fileDialog, v.w)
|
2024-01-14 16:31:07 +06:00
|
|
|
fileDialog.Show()
|
|
|
|
})
|
|
|
|
|
2024-01-15 20:28:02 +06:00
|
|
|
return button, fileVideoForConversionMessage, fileInput
|
2024-01-14 16:31:07 +06:00
|
|
|
}
|
|
|
|
|
2024-01-18 20:23:23 +06:00
|
|
|
func (v View) getButtonForSelectingDirectoryForSaving() (button *widget.Button, buttonMessage *canvas.Text, dirPath *string) {
|
2024-02-01 00:28:06 +06:00
|
|
|
buttonMessage = canvas.NewText("", color.RGBA{R: 255, G: 0, B: 0, A: 255})
|
2024-01-18 20:23:23 +06:00
|
|
|
buttonMessage.TextSize = 16
|
|
|
|
buttonMessage.TextStyle = fyne.TextStyle{Bold: true}
|
|
|
|
|
|
|
|
path := ""
|
|
|
|
dirPath = &path
|
|
|
|
|
2024-01-28 22:01:16 +06:00
|
|
|
buttonTitle := v.localizerService.GetMessage(&i18n.LocalizeConfig{
|
|
|
|
MessageID: "choose",
|
|
|
|
})
|
|
|
|
|
|
|
|
button = widget.NewButton(buttonTitle, func() {
|
2024-01-18 20:23:23 +06:00
|
|
|
fileDialog := dialog.NewFolderOpen(
|
|
|
|
func(r fyne.ListableURI, err error) {
|
|
|
|
if err != nil {
|
|
|
|
buttonMessage.Text = err.Error()
|
|
|
|
setStringErrorStyle(buttonMessage)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path = r.Path()
|
|
|
|
|
|
|
|
buttonMessage.Text = r.Path()
|
|
|
|
setStringSuccessStyle(buttonMessage)
|
|
|
|
}, v.w)
|
2024-01-23 21:33:01 +06:00
|
|
|
helper.FileDialogResize(fileDialog, v.w)
|
2024-01-18 20:23:23 +06:00
|
|
|
fileDialog.Show()
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-14 16:31:07 +06:00
|
|
|
func setStringErrorStyle(text *canvas.Text) {
|
2024-02-01 00:28:06 +06:00
|
|
|
text.Color = color.RGBA{R: 255, G: 0, B: 0, A: 255}
|
2024-01-14 16:31:07 +06:00
|
|
|
text.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
func setStringSuccessStyle(text *canvas.Text) {
|
2024-02-01 00:28:06 +06:00
|
|
|
text.Color = color.RGBA{R: 49, G: 127, B: 114, A: 255}
|
2024-01-14 16:31:07 +06:00
|
|
|
text.Refresh()
|
|
|
|
}
|
2024-01-15 20:28:02 +06:00
|
|
|
|
|
|
|
func showConversionMessage(conversionMessage *canvas.Text, err error) {
|
|
|
|
conversionMessage.Text = err.Error()
|
|
|
|
setStringErrorStyle(conversionMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func enableFormConversion(enableFormConversionStruct enableFormConversionStruct) {
|
|
|
|
enableFormConversionStruct.fileVideoForConversion.Enable()
|
2024-01-18 20:23:23 +06:00
|
|
|
enableFormConversionStruct.buttonForSelectedDir.Enable()
|
2024-01-15 20:28:02 +06:00
|
|
|
enableFormConversionStruct.form.Enable()
|
|
|
|
}
|