Skip to content

Commit

Permalink
Merge pull request #230 from SKOORAG/popup_ui_thread
Browse files Browse the repository at this point in the history
Correctly synchronize UI and other threads when opening a SX popup.
  • Loading branch information
RaiMan committed Nov 18, 2019
2 parents d370564 + 226e2fc commit 69d5693
Showing 1 changed file with 45 additions and 36 deletions.
81 changes: 45 additions & 36 deletions API/src/main/java/org/sikuli/script/SX.java
Expand Up @@ -4,14 +4,25 @@

package org.sikuli.script;

import org.sikuli.basics.Debug;

import javax.swing.*;
import java.awt.*;
import java.util.Date;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;

import org.sikuli.basics.Debug;

public class SX {

static public class Log {
Expand Down Expand Up @@ -109,7 +120,6 @@ class RunInput implements Runnable {
String preset = "";
Boolean hidden = false;
Integer timeout = 0;
boolean running = true;
Map<String, Object> parameters = new HashMap<>();
Object returnValue;

Expand Down Expand Up @@ -154,7 +164,20 @@ public void run() {
messageText.setEditable(false);
messageText.setBackground(new JLabel().getBackground());
final JPasswordField passwordField = new JPasswordField(preset);
toGetFocus = passwordField;

frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
frame.removeWindowListener(this);
new Thread(() -> {
pause(0.3);
EventQueue.invokeLater(() -> {
passwordField.requestFocusInWindow();
});
}).start();
}
});

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(passwordField);
Expand All @@ -171,31 +194,13 @@ public void run() {
}
}
}
stop();
}

JComponent toGetFocus = null;

public void setFocus() {
if (isNotNull(toGetFocus)) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
toGetFocus.requestFocusInWindow();
}
});
synchronized(this) {
frame.dispose();
this.notify();
}
}

public boolean isRunning() {
return running;
}

public void stop() {
frame.dispose();
running = false;
}

public int getTimeout() {
if (Integer.MAX_VALUE == timeout) {
return timeout;
Expand All @@ -207,15 +212,19 @@ public Object getReturnValue() {
return returnValue;
}
}

RunInput popRun = new RunInput(popType, args);
new Thread(popRun).start();
pause(0.3);
popRun.setFocus();
long end = new Date().getTime() + popRun.getTimeout();
while (popRun.isRunning()) {
pause(0.3);
if (end < new Date().getTime()) {
popRun.stop();

if(EventQueue.isDispatchThread()) {
popRun.run();
} else {
synchronized(popRun) {
EventQueue.invokeLater(popRun);
try {
popRun.wait(popRun.getTimeout());
} catch (InterruptedException e) {
Debug.error("Interrupted while waiting for popup close: %s", e.getMessage());
}
}
}
return popRun.getReturnValue();
Expand Down

0 comments on commit 69d5693

Please sign in to comment.