diff --git a/internal/desktop/filechooserdialog.go b/internal/desktop/filechooserdialog.go index 8c6fc776..0bd1af91 100644 --- a/internal/desktop/filechooserdialog.go +++ b/internal/desktop/filechooserdialog.go @@ -1,46 +1,71 @@ package desktop import ( + "time" "os/exec" ) +const ( + FILE_CHOOSER_DIALOG_NAME = "Open File" + FILE_CHOOSER_DIALOG_SLEEP = "0.2" +) + func (manager *DesktopManagerCtx) HandleFileChooserDialog(uri string) error { mu.Lock() defer mu.Unlock() - // TOOD: Use native API. - cmd := exec.Command( + // TODO: Use native API. + err := exec.Command( "xdotool", - "search", "--name", "Open File", "windowfocus", - "sleep", "0.2", + "search", "--name", FILE_CHOOSER_DIALOG_NAME, "windowfocus", + "sleep", FILE_CHOOSER_DIALOG_SLEEP, "key", "--clearmodifiers", "ctrl+l", "type", "--args", "1", uri + "//", - "key", "--clearmodifiers", "Return", - "sleep", "1", - "key", "--clearmodifiers", "Down", - "key", "--clearmodifiers", "ctrl+a", - "key", "--clearmodifiers", "Return", - "sleep", "0.3", - ) + "key", "Return", + "sleep", FILE_CHOOSER_DIALOG_SLEEP, + ).Run() + + // TODO: Use native API. + exec.Command( + "xdotool", + "search", "--name", FILE_CHOOSER_DIALOG_NAME, "windowfocus", + "sleep", FILE_CHOOSER_DIALOG_SLEEP, + "key", "Down", + "key", "--clearmodifiers", "ctrl+a", + "key", "Return", + "sleep", FILE_CHOOSER_DIALOG_SLEEP, + ).Run() - _, err := cmd.Output() return err } func (manager *DesktopManagerCtx) CloseFileChooserDialog() { for i := 0; i < 5; i++ { - if !manager.IsFileChooserDialogOpened() { + mu.Lock() + + manager.logger.Debug().Msg("attempting to close file chooser dialog") + + // TODO: Use native API. + err := exec.Command( + "xdotool", + "search", "--name", FILE_CHOOSER_DIALOG_NAME, "windowfocus", + ).Run() + + if err != nil { + mu.Unlock() + manager.logger.Info().Msg("file chooser dialog is closed") return } - // TOOD: Use native API. - mu.Lock() - exec.Command( - "xdotool", - "search", "--name", "Open File", "windowfocus", - "sleep", "0.2", - "key", "--clearmodifiers", "alt+F4", - ).Output() + // custom press Alt + F4 + // because xdotool is failing to send proper Alt+F4 + + manager.ResetKeys() + manager.KeyDown(65513) // Alt + manager.KeyDown(65473) // F4 + time.Sleep(10 * time.Millisecond) + manager.ResetKeys() + mu.Unlock() } } @@ -49,12 +74,11 @@ func (manager *DesktopManagerCtx) IsFileChooserDialogOpened() bool { mu.Lock() defer mu.Unlock() - // TOOD: Use native API. - cmd := exec.Command( + // TODO: Use native API. + err := exec.Command( "xdotool", - "search", "--name", "Open File", - ) + "search", "--name", FILE_CHOOSER_DIALOG_NAME, + ).Run() - _, err := cmd.Output() return err == nil }