2024-01-14 16:31:07 +06:00
|
|
|
|
package convertor
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-18 20:23:23 +06:00
|
|
|
|
"errors"
|
2024-01-23 21:33:01 +06:00
|
|
|
|
"ffmpegGui/helper"
|
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"
|
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type View struct {
|
|
|
|
|
w fyne.Window
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-14 16:31:07 +06:00
|
|
|
|
func NewView(w fyne.Window) *View {
|
|
|
|
|
return &View{w}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
conversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
|
|
|
|
|
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
|
|
|
|
|
checkboxOverwriteOutputFiles := widget.NewCheck("Разрешить перезаписать файл", func(b bool) {
|
|
|
|
|
isOverwriteOutputFiles = b
|
|
|
|
|
})
|
2024-01-15 20:28:02 +06:00
|
|
|
|
|
|
|
|
|
form.Items = []*widget.FormItem{
|
|
|
|
|
{Text: "Файл для ковертации:", Widget: fileVideoForConversion},
|
|
|
|
|
{Widget: fileVideoForConversionMessage},
|
2024-01-20 01:53:25 +06:00
|
|
|
|
{Text: "Папка куда будет сохраняться:", Widget: buttonForSelectedDir},
|
2024-01-18 20:23:23 +06:00
|
|
|
|
{Widget: buttonForSelectedDirMessage},
|
|
|
|
|
{Widget: checkboxOverwriteOutputFiles},
|
2024-01-15 20:28:02 +06:00
|
|
|
|
}
|
|
|
|
|
form.SubmitText = "Конвертировать"
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
showConversionMessage(conversionMessage, errors.New("Не выбрали папку для сохранения!"))
|
|
|
|
|
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-20 01:53:25 +06:00
|
|
|
|
v.w.SetContent(widget.NewCard("Конвертор видео файлов в mp4", "", 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{}
|
|
|
|
|
|
|
|
|
|
fileVideoForConversionMessage := canvas.NewText("", color.RGBA{255, 0, 0, 255})
|
|
|
|
|
fileVideoForConversionMessage.TextSize = 16
|
|
|
|
|
fileVideoForConversionMessage.TextStyle = fyne.TextStyle{Bold: true}
|
|
|
|
|
|
|
|
|
|
button := widget.NewButton("выбрать", 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) {
|
|
|
|
|
buttonMessage = canvas.NewText("", color.RGBA{255, 0, 0, 255})
|
|
|
|
|
buttonMessage.TextSize = 16
|
|
|
|
|
buttonMessage.TextStyle = fyne.TextStyle{Bold: true}
|
|
|
|
|
|
|
|
|
|
path := ""
|
|
|
|
|
dirPath = &path
|
|
|
|
|
|
|
|
|
|
button = widget.NewButton("выбрать", func() {
|
|
|
|
|
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) {
|
|
|
|
|
text.Color = color.RGBA{255, 0, 0, 255}
|
|
|
|
|
text.Refresh()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setStringSuccessStyle(text *canvas.Text) {
|
|
|
|
|
text.Color = color.RGBA{49, 127, 114, 255}
|
|
|
|
|
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()
|
|
|
|
|
}
|