2021-01-18 11:50:03 +13:00
|
|
|
package desktop
|
|
|
|
|
|
|
|
import (
|
2021-01-26 08:23:40 +13:00
|
|
|
"fmt"
|
2021-01-21 11:51:53 +13:00
|
|
|
"time"
|
2021-01-18 11:50:03 +13:00
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2021-01-21 11:51:53 +13:00
|
|
|
const (
|
2021-01-26 08:23:40 +13:00
|
|
|
FILE_CHOOSER_DIALOG_NAME = "Open File"
|
|
|
|
FILE_CHOOSER_DIALOG_SHORT_SLEEP = "0.2"
|
|
|
|
FILE_CHOOSER_DIALOG_LONG_SLEEP = "0.4"
|
2021-01-21 11:51:53 +13:00
|
|
|
)
|
|
|
|
|
2021-01-18 11:50:03 +13:00
|
|
|
func (manager *DesktopManagerCtx) HandleFileChooserDialog(uri string) error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2021-01-21 11:51:53 +13:00
|
|
|
// TODO: Use native API.
|
2021-01-26 08:23:40 +13:00
|
|
|
err1 := exec.Command(
|
2021-01-18 11:50:03 +13:00
|
|
|
"xdotool",
|
2021-01-21 11:51:53 +13:00
|
|
|
"search", "--name", FILE_CHOOSER_DIALOG_NAME, "windowfocus",
|
2021-01-26 08:23:40 +13:00
|
|
|
"sleep", FILE_CHOOSER_DIALOG_SHORT_SLEEP,
|
2021-01-18 11:50:03 +13:00
|
|
|
"key", "--clearmodifiers", "ctrl+l",
|
|
|
|
"type", "--args", "1", uri + "//",
|
2021-01-26 08:23:40 +13:00
|
|
|
"sleep", FILE_CHOOSER_DIALOG_SHORT_SLEEP,
|
|
|
|
"key", "Delete", // remove autocomplete results
|
|
|
|
"sleep", FILE_CHOOSER_DIALOG_SHORT_SLEEP,
|
2021-01-21 11:51:53 +13:00
|
|
|
"key", "Return",
|
2021-01-26 08:23:40 +13:00
|
|
|
"sleep", FILE_CHOOSER_DIALOG_LONG_SLEEP,
|
2021-01-21 11:51:53 +13:00
|
|
|
"key", "Down",
|
2021-01-18 11:50:03 +13:00
|
|
|
"key", "--clearmodifiers", "ctrl+a",
|
2021-01-21 11:51:53 +13:00
|
|
|
"key", "Return",
|
2021-01-26 08:23:40 +13:00
|
|
|
"sleep", FILE_CHOOSER_DIALOG_LONG_SLEEP,
|
|
|
|
).Run()
|
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
return err1
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use native API.
|
|
|
|
err2 := exec.Command(
|
|
|
|
"xdotool",
|
|
|
|
"search", "--name", FILE_CHOOSER_DIALOG_NAME,
|
2021-01-21 11:51:53 +13:00
|
|
|
).Run()
|
2021-01-18 11:50:03 +13:00
|
|
|
|
2021-01-26 08:23:40 +13:00
|
|
|
// if last command didn't return error, consider dialog as still open
|
|
|
|
if err2 == nil {
|
|
|
|
return fmt.Errorf("unable to select files in dialog")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-01-18 11:50:03 +13:00
|
|
|
}
|
|
|
|
|
2021-01-20 09:01:31 +13:00
|
|
|
func (manager *DesktopManagerCtx) CloseFileChooserDialog() {
|
2021-01-18 22:34:33 +13:00
|
|
|
for i := 0; i < 5; i++ {
|
2021-01-21 11:51:53 +13:00
|
|
|
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")
|
2021-01-20 09:01:31 +13:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-21 11:51:53 +13:00
|
|
|
// custom press Alt + F4
|
|
|
|
// because xdotool is failing to send proper Alt+F4
|
|
|
|
|
|
|
|
manager.ResetKeys()
|
2021-01-26 05:36:43 +13:00
|
|
|
//nolint
|
2021-01-21 11:51:53 +13:00
|
|
|
manager.KeyDown(65513) // Alt
|
2021-01-26 05:36:43 +13:00
|
|
|
//nolint
|
2021-01-21 11:51:53 +13:00
|
|
|
manager.KeyDown(65473) // F4
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
manager.ResetKeys()
|
|
|
|
|
2021-01-18 22:34:33 +13:00
|
|
|
mu.Unlock()
|
|
|
|
}
|
2021-01-18 11:50:03 +13:00
|
|
|
}
|
|
|
|
|
2021-01-20 09:01:31 +13:00
|
|
|
func (manager *DesktopManagerCtx) IsFileChooserDialogOpened() bool {
|
2021-01-18 11:50:03 +13:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2021-01-21 11:51:53 +13:00
|
|
|
// TODO: Use native API.
|
|
|
|
err := exec.Command(
|
2021-01-18 11:50:03 +13:00
|
|
|
"xdotool",
|
2021-01-21 11:51:53 +13:00
|
|
|
"search", "--name", FILE_CHOOSER_DIALOG_NAME,
|
|
|
|
).Run()
|
2021-01-18 11:50:03 +13:00
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|