filechooserdialog refactor desktop.

This commit is contained in:
Miroslav Šedivý 2021-01-20 23:51:53 +01:00
parent 64dd31844b
commit 7ff6ada205

View File

@ -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
}