Skip to content

Commit

Permalink
Cleaning browser initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Jul 20, 2020
1 parent 0996083 commit d577051
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 70 deletions.
62 changes: 39 additions & 23 deletions src/main/java/org/luwrain/interaction/javafx/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class App extends Application
static private final int MIN_TABLE_WIDTH = 16;
static private final int MIN_TABLE_HEIGHT = 8;

StackPane root = null;
private StackPane rootPane = null;
Stage stage = null;

private ResizableCanvas canvas = null;
Expand Down Expand Up @@ -72,14 +72,14 @@ public final class App extends Application
this.stage = stage;
stage.setResizable(true);
stage.setTitle("LUWRAIN");
this.root=new StackPane();
this.root.resize(1024, 768);
this.rootPane = new StackPane();
this.rootPane.resize(1024, 768);
this.canvas = new ResizableCanvas();
this.root.getChildren().add(canvas);
stage.setScene(new Scene(root));
this.canvas.bindWidthAndHeight(root);
this.rootPane.getChildren().add(canvas);
stage.setScene(new Scene(rootPane));
this.canvas.bindWidthAndHeight(rootPane);
this.gc = canvas.getGraphicsContext2D();
this.root.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
this.rootPane.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
ThreadControl.appStarted(this);
}

Expand All @@ -88,22 +88,13 @@ void setInteractionFont(Font font, Font font2)
{
NullCheck.notNull(font, "font");
NullCheck.notNull(font2, "font2");
this.font=font;
this.font2=font2;
this.font = font;
this.font2 = font2;
final Text text = new Text("A");
text.setFont(font);
bounds = text.getLayoutBounds();
}

Font getInteractionFont()
{
return font;
}

Font getInteractionFont2()
{
return font;
}

synchronized boolean initTable()
{
Expand Down Expand Up @@ -185,11 +176,11 @@ void setMargin(int marginLeft,int marginTop,int marginRight,int marginBottom)
this.marginBottom = marginBottom;
}

void setSizeAndShow(int width,int height)
void setSizeAndShow(int width, int height)
{
this.canvasWidth=width;
this.canvasHeight=height;
this.root.resize(width,height);
this.canvasWidth = width;
this.canvasHeight = height;
this.rootPane.resize(width,height);
this.stage.sizeToScene();
this.stage.show();
}
Expand All @@ -198,7 +189,7 @@ void setUndecoratedSizeAndShow(double width,double height)
{
this.canvasWidth=width;
this.canvasHeight=height;
this.root.resize(width,height);
this.rootPane.resize(width, height);
//canvas.resize(width, height);
this.stage.initStyle(StageStyle.UNDECORATED); // WARN: can't change style after first window show
this.stage.setWidth(width);
Expand Down Expand Up @@ -350,4 +341,29 @@ synchronized void drawHorizontalLine(int left,int right,int y)
if (horizLines[y] != null)
horizLines[y].cover(left, right);
}

void putNew(Node node)
{
NullCheck.notNull(node, "node");
rootPane.getChildren().add(node);
if (node instanceof ResizableCanvas)
((ResizableCanvas)node).bindWidthAndHeight(rootPane);
}

void remove(Node node)
{
NullCheck.notNull(node, "node");
rootPane.getChildren().remove(node);
}

Font getInteractionFont()
{
return font;
}

Font getInteractionFont2()
{
return font;
}

}
52 changes: 32 additions & 20 deletions src/main/java/org/luwrain/interaction/javafx/JavaFxInteraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

import java.io.*;

import javafx.stage.Screen;
Expand Down Expand Up @@ -248,12 +250,35 @@ private Font createFont2(int desirableFontSize)
NullCheck.notNull(params, "params");
switch(modeName.toUpperCase())
{
case "BROWSER":
return new Browser(this);
case "BROWSER": {
final AtomicReference res = new AtomicReference();
Utils.runInFxThreadSync(()->{
try {
final Browser browser = new Browser(this, null);
final boolean wasEmpty = browsers.isEmpty();
this.browsers.add(browser);
app.putNew(browser.webView);
if(wasEmpty)
setCurrentBrowser(browser, false);
res.set(browser);
}
catch(Throwable e)
{
res.set(e);
}
});
if (res.get() == null)
return null;
if (res.get() instanceof Browser)
return (Browser)res.get();
if (res.get() instanceof Throwable)
throw new RuntimeException((Throwable)res.get());
return null;
}
case "PDF": {
final PdfPreview preview = new PdfPreview(this, params);
preview.init();
return preview;
final PdfPreview preview = new PdfPreview(this, params);
preview.init();
return preview;
}
default:
Log.error(LOG_COMPONENT, "unknown graphical mode name: " + modeName);
Expand All @@ -271,18 +296,6 @@ private void setCurrentBrowser(Browser newCurrentBrowser, boolean visibility)
currentBrowser.setVisibility(visibility);
}

public void registerBrowser(Browser browser, WebView webView)
{
NullCheck.notNull(browser, "browser");
NullCheck.notNull(webView, "webView");
InvalidThreadException.checkThread("JavaFxInteraction.registerBrowser()");
final boolean wasEmpty = browsers.isEmpty();
browsers.add(browser);
app.root.getChildren().add(webView);
if(wasEmpty)
setCurrentBrowser(browser, false);
}

public boolean closeBrowser(Browser browser)
{
NullCheck.notNull(browser, "browser");
Expand All @@ -298,14 +311,13 @@ void registerCanvas(ResizableCanvas canvas)
{
NullCheck.notNull(canvas, "canvas");
InvalidThreadException.checkThread("JavaFxInteraction.registerBrowser()");
app.root.getChildren().add(canvas);
canvas.bindWidthAndHeight(app.root);
app.putNew(canvas);
}

void closeCanvas(ResizableCanvas canvas)
{
NullCheck.notNull(canvas, "canvas");
app.root.getChildren().remove(canvas);
app.remove(canvas);
}

public void enableGraphicalMode()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2018 Michael Pozhidaev <[email protected]>
Copyright 2012-2020 Michael Pozhidaev <[email protected]>
Copyright 2015-2016 Roman Volovodov <[email protected]>
This file is part of LUWRAIN.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2018 Michael Pozhidaev <[email protected]>
Copyright 2012-2020 Michael Pozhidaev <[email protected]>
Copyright 2015-2016 Roman Volovodov <[email protected]>
This file is part of LUWRAIN.
Expand Down
38 changes: 15 additions & 23 deletions src/main/java/org/luwrain/interaction/javafx/browser/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,35 @@ static final class DomScanResult
}

protected final String injectedScript;
protected WebView webView = null;
protected WebEngine webEngine = null;
public final WebView webView;
protected final WebEngine webEngine;
protected DomScanResult domScanRes = null;

protected JSObject injectionRes = null;
protected JSObject jsWindow = null;

protected Base(String injectedScript)
protected Base(BrowserEvents events, String injectedScript)
{
NullCheck.notNull(injectedScript, "injectedScript");
NullCheck.notNull(events, "events");
Utils.ensureFxThread();
this.injectedScript = injectedScript;
this.webView = new WebView();
this.webEngine = webView.getEngine();
this.webView.setOnKeyReleased((event)->onKeyReleased(event));
this.webEngine.getLoadWorker().stateProperty().addListener((ov,oldState,newState)->onStateChanged(events, ov, oldState, newState));
this.webEngine.getLoadWorker().progressProperty().addListener((ov,o,n)->events.onProgress(n));
this.webEngine.setOnAlert((event)->events.onAlert(event.getData()));
this.webEngine.setPromptHandler((event)->events.onPrompt(event.getMessage(),event.getDefaultValue()));
this.webEngine.setConfirmHandler((param)->events.onConfirm(param));
this.webEngine.setOnError((event)->events.onError(event.getMessage()));
this.webView.setVisible(false);
}

abstract void setVisibility(boolean enabled);

protected void init(BrowserEvents events)
{
NullCheck.notNull(events, "events");
Utils.ensureFxThread();
try {
this.webView = new WebView();
this.webEngine = webView.getEngine();
webView.setOnKeyReleased((event)->onKeyReleased(event));
webEngine.getLoadWorker().stateProperty().addListener((ov,oldState,newState)->onStateChanged(events, ov, oldState, newState));
webEngine.getLoadWorker().progressProperty().addListener((ov,o,n)->events.onProgress(n));
webEngine.setOnAlert((event)->events.onAlert(event.getData()));
webEngine.setPromptHandler((event)->events.onPrompt(event.getMessage(),event.getDefaultValue()));
webEngine.setConfirmHandler((param)->events.onConfirm(param));
webEngine.setOnError((event)->events.onError(event.getMessage()));
webView.setVisible(false);
}
catch(Throwable e)
{
Log.error(LOG_COMPONENT, "unable to initialize WebEngine and WebView:" + e.getClass().getName() + ":" + e.getMessage());
this.webView = null;
this.webEngine = null;
}
}

DomScanResult getDomScanResult()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ public final class Browser extends Base implements org.luwrain.browser.Browser

private final JavaFxInteraction interaction;

public Browser(JavaFxInteraction interaction)
public Browser(JavaFxInteraction interaction, BrowserEvents events)
{
super(readTextResource(RESCAN_RESOURCE_PATH));
super(events, readTextResource(RESCAN_RESOURCE_PATH));
NullCheck.notNull(interaction, "interaction");
this.interaction = interaction;
}

@Override public void init(BrowserEvents events)
{
/*
NullCheck.notNull(events, "events");
Utils.runInFxThreadSync(()->{
super.init(events);
interaction.registerBrowser(this, webView);
});
*/
}

private boolean initialized()
Expand Down

0 comments on commit d577051

Please sign in to comment.