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 package desktop
import ( import (
"time"
"os/exec" "os/exec"
) )
const (
FILE_CHOOSER_DIALOG_NAME = "Open File"
FILE_CHOOSER_DIALOG_SLEEP = "0.2"
)
func (manager *DesktopManagerCtx) HandleFileChooserDialog(uri string) error { func (manager *DesktopManagerCtx) HandleFileChooserDialog(uri string) error {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
// TOOD: Use native API. // TODO: Use native API.
cmd := exec.Command( err := exec.Command(
"xdotool", "xdotool",
"search", "--name", "Open File", "windowfocus", "search", "--name", FILE_CHOOSER_DIALOG_NAME, "windowfocus",
"sleep", "0.2", "sleep", FILE_CHOOSER_DIALOG_SLEEP,
"key", "--clearmodifiers", "ctrl+l", "key", "--clearmodifiers", "ctrl+l",
"type", "--args", "1", uri + "//", "type", "--args", "1", uri + "//",
"key", "--clearmodifiers", "Return", "key", "Return",
"sleep", "1", "sleep", FILE_CHOOSER_DIALOG_SLEEP,
"key", "--clearmodifiers", "Down", ).Run()
"key", "--clearmodifiers", "ctrl+a",
"key", "--clearmodifiers", "Return", // TODO: Use native API.
"sleep", "0.3", 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 return err
} }
func (manager *DesktopManagerCtx) CloseFileChooserDialog() { func (manager *DesktopManagerCtx) CloseFileChooserDialog() {
for i := 0; i < 5; i++ { 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 return
} }
// TOOD: Use native API. // custom press Alt + F4
mu.Lock() // because xdotool is failing to send proper Alt+F4
exec.Command(
"xdotool", manager.ResetKeys()
"search", "--name", "Open File", "windowfocus", manager.KeyDown(65513) // Alt
"sleep", "0.2", manager.KeyDown(65473) // F4
"key", "--clearmodifiers", "alt+F4", time.Sleep(10 * time.Millisecond)
).Output() manager.ResetKeys()
mu.Unlock() mu.Unlock()
} }
} }
@ -49,12 +74,11 @@ func (manager *DesktopManagerCtx) IsFileChooserDialogOpened() bool {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
// TOOD: Use native API. // TODO: Use native API.
cmd := exec.Command( err := exec.Command(
"xdotool", "xdotool",
"search", "--name", "Open File", "search", "--name", FILE_CHOOSER_DIALOG_NAME,
) ).Run()
_, err := cmd.Output()
return err == nil return err == nil
} }