Skip to content

Commit

Permalink
allow setting size of window through grinder
Browse files Browse the repository at this point in the history
  • Loading branch information
hrj committed Oct 1, 2015
1 parent 4158420 commit 6cf4508
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions src/Platform_Core/org/lobobrowser/main/GrinderServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutionException;

import javax.imageio.ImageIO;
import javax.swing.SwingUtilities;

import org.lobobrowser.ua.NavigatorFrame;
import org.lobobrowser.ua.NavigatorProgressEvent;
Expand Down Expand Up @@ -79,6 +81,11 @@ public void run() {
final String path = commandLine.substring(blankIdx + 1).trim();
handleTo(s, br, path);
}
} else if ("SET_SIZE".equals(command)) {
if (blankIdx != -1) {
final String params = commandLine.substring(blankIdx + 1).trim();
handleResize(s, br, params);
}
} else if ("SCREENSHOT".equals(command)) {
handleScreenShot(s, br);
} else if ("CLOSE".equals(command)) {
Expand All @@ -95,12 +102,42 @@ public void run() {

}

private void handleResize(final Socket s, final BufferedReader br, final String params) throws IOException {
final String[] parts = params.split("[,\\s]+");
final int w = Integer.parseInt(parts[0]);
final int h = Integer.parseInt(parts[1]);
try {
SwingUtilities.invokeAndWait(() -> {
frame.resizeWindowTo(w, h);
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}

markDoneAndWaitForAck(s, br);
}

// TODO: Add way to mark error
private static void markDoneAndWaitForAck(final Socket s, final BufferedReader br) throws IOException {
final DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeInt(0);
dos.flush();

// Wait for ACK
br.readLine();
}

private void handleScreenShot(final Socket s, final BufferedReader br) throws IOException {
final Component component = frame.getComponentContent().getComponent();
final BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
// TODO: paint in AWT event queue
component.paint(g);
try {
SwingUtilities.invokeAndWait(() -> {
component.paint(g);
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(img, "PNG", bos);
final OutputStream os = s.getOutputStream();
Expand Down Expand Up @@ -134,12 +171,8 @@ private void handleTo(final Socket s, final BufferedReader br, final String path
defSupport.layoutCompletion().get();
}
}
final DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeInt(0);
dos.flush();

// Wait for ACK
br.readLine();
markDoneAndWaitForAck(s, br);
}

public int getPort() {
Expand Down

0 comments on commit 6cf4508

Please sign in to comment.