From bf3340e52613e17d27f480f58f4d31b5970c94a6 Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sun, 11 May 2025 15:07:37 +0500 Subject: [PATCH] Add drag-and-drop support for single file selection Implemented functionality to handle single file drag-and-drop in the UI, including error handling for multiple files and directories. Updated localization files to support new messages related to drag-and-drop usage and errors. --- convertor/view/conversion.go | 51 +++++++++++++++++++++++++++++++++++- kernel/window.go | 5 ++++ languages/active.en.toml | 16 +++++++++++ languages/active.kk.toml | 16 +++++++++++ languages/active.ru.toml | 4 +++ 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/convertor/view/conversion.go b/convertor/view/conversion.go index eee6aae..5d95e18 100644 --- a/convertor/view/conversion.go +++ b/convertor/view/conversion.go @@ -14,6 +14,7 @@ import ( "git.kor-elf.net/kor-elf/gui-for-ffmpeg/setting" "github.com/nicksnyder/go-i18n/v2/i18n" "image/color" + "os" "path/filepath" ) @@ -195,6 +196,10 @@ func newFileForConversion(app kernel.AppContract) *fileForConversion { buttonTitle := app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ MessageID: "choose", + }) + "\n\r\n\r" + app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ + MessageID: "or", + }) + "\n\r\n\r" + app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ + MessageID: "dragAndDrop1File", }) fileForConversion.message = canvas.NewText("", color.RGBA{R: 255, G: 0, B: 0, A: 255}) @@ -225,10 +230,54 @@ func newFileForConversion(app kernel.AppContract) *fileForConversion { fileForConversion.eventSelectFile(nil) listableURI := storage.NewFileURI(filepath.Dir(r.URI().Path())) - locationURI, err = storage.ListerForURI(listableURI) + locationURI, _ = storage.ListerForURI(listableURI) }, locationURI) }) + app.GetWindow().SetOnDropped(func(position fyne.Position, uris []fyne.URI) { + if len(uris) == 0 { + return + } + + if len(uris) > 1 { + fileForConversion.message.Text = app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ + MessageID: "errorDragAndDrop1File", + }) + setStringErrorStyle(fileForConversion.message) + fileForConversion.eventSelectFile(errors.New(fileForConversion.message.Text)) + return + } + + uri := uris[0] + info, err := os.Stat(uri.Path()) + if err != nil { + fileForConversion.message.Text = err.Error() + setStringErrorStyle(fileForConversion.message) + fileForConversion.eventSelectFile(err) + return + } + if info.IsDir() { + fileForConversion.message.Text = app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{ + MessageID: "errorIsFolder", + }) + setStringErrorStyle(fileForConversion.message) + fileForConversion.eventSelectFile(errors.New(fileForConversion.message.Text)) + return + } + + fileForConversion.file.Path = uri.Path() + fileForConversion.file.Name = uri.Name() + fileForConversion.file.Ext = uri.Extension() + + fileForConversion.message.Text = uri.Path() + setStringSuccessStyle(fileForConversion.message) + + fileForConversion.eventSelectFile(nil) + + listableURI := storage.NewFileURI(filepath.Dir(uri.Path())) + locationURI, _ = storage.ListerForURI(listableURI) + }) + return fileForConversion } diff --git a/kernel/window.go b/kernel/window.go index 7d44a5e..7bc565c 100644 --- a/kernel/window.go +++ b/kernel/window.go @@ -12,6 +12,7 @@ type WindowContract interface { SetMainMenu(menu *fyne.MainMenu) NewFileOpen(callback func(fyne.URIReadCloser, error), location fyne.ListableURI) *dialog.FileDialog NewFolderOpen(callback func(fyne.ListableURI, error), location fyne.ListableURI) *dialog.FileDialog + SetOnDropped(callback func(position fyne.Position, uris []fyne.URI)) ShowAndRun() GetLayout() LayoutContract } @@ -80,3 +81,7 @@ func (w Window) ShowAndRun() { func (w Window) GetLayout() LayoutContract { return w.layout } + +func (w Window) SetOnDropped(callback func(position fyne.Position, uris []fyne.URI)) { + w.windowFyne.SetOnDropped(callback) +} diff --git a/languages/active.en.toml b/languages/active.en.toml index 291aa8d..ba1f596 100644 --- a/languages/active.en.toml +++ b/languages/active.en.toml @@ -62,6 +62,10 @@ other = "Will be downloaded from the site:" hash = "sha1-55f87f114628fa2d5d8e67d1e1cda22c0e4f9271" other = "Downloading..." +[dragAndDrop1File] +hash = "sha1-7259670822df1cc92ef5f06ed3c0e9407746975a" +other = "drag and drop 1 file" + [encoderGroupAudio] hash = "sha1-24321cb5400df96be8f3e2131918bebdb3a01bba" other = "Audio" @@ -218,6 +222,10 @@ other = "Couldn't convert video" hash = "sha1-531abc3f0d12727e542df6e5a22de91098380fc1" other = "could not create file 'database' in folder 'data'" +[errorDragAndDrop1File] +hash = "sha1-a8edb5cbd622f3ce4ec07a2377e22ec5fad4491b" +other = "You can only drag and drop 1 file." + [errorFFmpeg] hash = "sha1-ccf0b95c0d1b392dc215258d917eb4e5d0b88ed0" other = "this is not FFmpeg" @@ -234,6 +242,10 @@ other = "this is not FFprobe" hash = "sha1-da7b37d7df3fafbd153665b13888413d52b24c17" other = "Failed to determine FFprobe version" +[errorIsFolder] +hash = "sha1-f937d090b6e320957514d850657cdf2f911dc6aa" +other = "You can only drag and drop a file" + [errorQueue] hash = "sha1-72aecd9ad85642d84d62dbbf3cf70953c5f696c7" other = "Error" @@ -294,6 +306,10 @@ other = "License information" hash = "sha1-359fff328717c05104e51a2d29f05bf1875d26b7" other = "Licenses from other products used in the program" +[or] +hash = "sha1-30bb0333ca1583110e4ced513b5d2455b86f529b" +other = "or" + [parameterCheckbox] hash = "sha1-9e35221d454870996fd51d576249cf47d1784a3c" other = "Enable option" diff --git a/languages/active.kk.toml b/languages/active.kk.toml index 404c9bd..88f96b5 100644 --- a/languages/active.kk.toml +++ b/languages/active.kk.toml @@ -62,6 +62,10 @@ other = "Сайттан жүктеледі:" hash = "sha1-55f87f114628fa2d5d8e67d1e1cda22c0e4f9271" other = "Жүктеп алынуда..." +[dragAndDrop1File] +hash = "sha1-7259670822df1cc92ef5f06ed3c0e9407746975a" +other = "1 файлды сүйреңіз" + [encoderGroupAudio] hash = "sha1-24321cb5400df96be8f3e2131918bebdb3a01bba" other = "Аудио" @@ -218,6 +222,10 @@ other = "Бейнені түрлендіру мүмкін болмады" hash = "sha1-531abc3f0d12727e542df6e5a22de91098380fc1" other = "'data' қалтасында 'database' файлын жасау мүмкін болмады" +[errorDragAndDrop1File] +hash = "sha1-a8edb5cbd622f3ce4ec07a2377e22ec5fad4491b" +other = "Тек 1 файлды сүйреп апаруға болады" + [errorFFmpeg] hash = "sha1-ccf0b95c0d1b392dc215258d917eb4e5d0b88ed0" other = "бұл FFmpeg емес" @@ -234,6 +242,10 @@ other = "бұл FFprobe емес" hash = "sha1-da7b37d7df3fafbd153665b13888413d52b24c17" other = "FFprobe нұсқасын анықтау мүмкін болмады" +[errorIsFolder] +hash = "sha1-f937d090b6e320957514d850657cdf2f911dc6aa" +other = "Тек файлды сүйреп апаруға болады" + [errorQueue] hash = "sha1-72aecd9ad85642d84d62dbbf3cf70953c5f696c7" other = "Қате" @@ -294,6 +306,10 @@ other = "Лицензия туралы ақпарат" hash = "sha1-359fff328717c05104e51a2d29f05bf1875d26b7" other = "Бағдарламада пайдаланылатын басқа өнімдердің лицензиялары" +[or] +hash = "sha1-30bb0333ca1583110e4ced513b5d2455b86f529b" +other = "немесе" + [parameterCheckbox] hash = "sha1-9e35221d454870996fd51d576249cf47d1784a3c" other = "Опцияны қосу" diff --git a/languages/active.ru.toml b/languages/active.ru.toml index 875acee..e4dc5b9 100644 --- a/languages/active.ru.toml +++ b/languages/active.ru.toml @@ -14,6 +14,7 @@ converterVideoFilesTitle = "Конвертер видео, аудио и кар download = "Скачать" downloadFFmpegFromSite = "Будет скачано с сайта:" downloadRun = "Скачивается..." +dragAndDrop1File = "перетащить 1 файл" encoderGroupAudio = "Аудио" encoderGroupImage = "Картинки" encoderGroupVideo = "Видео" @@ -53,10 +54,12 @@ encoder_xbm = "XBM (X BitMap) image" error = "Произошла ошибка!" errorConverter = "не смогли отконвертировать видео" errorDatabase = "не смогли создать файл 'database' в папке 'data'" +errorDragAndDrop1File = "Можно перетащить только 1 файл" errorFFmpeg = "это не FFmpeg" errorFFmpegVersion = "Не смогли определить версию FFmpeg" errorFFprobe = "это не FFprobe" errorFFprobeVersion = "Не смогли определить версию FFprobe" +errorIsFolder = "Можно перетаскивать только файл" errorQueue = "Ошибка" errorSelectedEncoder = "Конвертер не выбран" errorSelectedFolderSave = "Папка для сохранения не выбрана!" @@ -72,6 +75,7 @@ languageSelectionFormHead = "Переключить язык" languageSelectionHead = "Выберите язык" licenseLink = "Сведения о лицензии" licenseLinkOther = "Лицензии от других продуктов, которые используются в программе" +or = "или" parameterCheckbox = "Включить параметр" pathToFfmpeg = "Путь к FFmpeg:" pathToFfprobe = "Путь к FFprobe:"