neko/internal/desktop/filechooserdialog.go

85 lines
1.7 KiB
Go
Raw Normal View History

2021-01-18 11:50:03 +13:00
package desktop
import (
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 (
FILE_CHOOSER_DIALOG_NAME = "Open File"
FILE_CHOOSER_DIALOG_SLEEP = "0.2"
)
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.
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, "windowfocus",
"sleep", FILE_CHOOSER_DIALOG_SLEEP,
2021-01-18 11:50:03 +13:00
"key", "--clearmodifiers", "ctrl+l",
"type", "--args", "1", uri + "//",
2021-01-21 11:51:53 +13:00
"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",
2021-01-18 11:50:03 +13:00
"key", "--clearmodifiers", "ctrl+a",
2021-01-21 11:51:53 +13:00
"key", "Return",
"sleep", FILE_CHOOSER_DIALOG_SLEEP,
).Run()
2021-01-18 11:50:03 +13:00
return err
}
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")
return
}
2021-01-21 11:51:53 +13:00
// 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()
2021-01-18 22:34:33 +13:00
mu.Unlock()
}
2021-01-18 11:50:03 +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
}