Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
Add a "Waiting..." label (#32)
Browse files Browse the repository at this point in the history
* Resovle #31 

* Display a "Waiting..." label during waiting for response

* Fix a legacy concurrent issue

* Update usage demo
  • Loading branch information
blaisewang authored Dec 27, 2019
1 parent 4cf120b commit f602f52
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 65 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'blaise.img2latex'
version '0.2.7'
version '0.3.0'

compileJava {
sourceCompatibility = 11
Expand Down
Binary file modified demo/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 81 additions & 52 deletions src/main/java/BackGridPane.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import javafx.concurrent.Task;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;


/**
Expand All @@ -20,6 +24,7 @@ class BackGridPane extends GridPane {

private static ImageView clipboardImageView = new ImageView();
private static ImageView renderedImageView = new ImageView();
private static Label waitingTextLabel = new Label("Waiting...");
private static ProgressBar confidenceProgressBar = new ProgressBar(0);

private static final Color PANE_BORDER_COLOR = new Color(0.898, 0.902, 0.9216, 1);
Expand Down Expand Up @@ -48,36 +53,43 @@ class BackGridPane extends GridPane {

// 8 * 1 layout
this.setVgap(8);
this.setHgap(1);
this.setHgap(2);

// add "Clipboard Image" text label
Label clipboardTextLabel = Utilities.getTextLabel("Clipboard Image");
Utilities.setNodeLeftMargin(clipboardTextLabel, PREFERRED_MARGIN);
Utilities.setDefaultNodeMargin(clipboardTextLabel, PREFERRED_MARGIN, 0);
this.add(clipboardTextLabel, 0, 0);

waitingTextLabel.setFont(Font.font("Arial Black", FontWeight.BOLD, 12));
waitingTextLabel.setTextFill(new Color(0.3882, 0.7882, 0.3373, 1));
waitingTextLabel.setVisible(false);
GridPane.setHalignment(waitingTextLabel, HPos.RIGHT);
Utilities.setDefaultNodeMargin(waitingTextLabel, 0, PREFERRED_MARGIN);
this.add(waitingTextLabel, 1, 0);

// get bordered ImageView
BorderPane clipboardBorderPane = setImageViewBorder(clipboardImageView);
this.add(clipboardBorderPane, 0, 1);
this.add(clipboardBorderPane, 0, 1, 2, 1);

// add "Rendered Equation" text label
Label renderedTextLabel = Utilities.getTextLabel("Rendered Equation");
Utilities.setNodeLeftMargin(renderedTextLabel, PREFERRED_MARGIN);
this.add(renderedTextLabel, 0, 2);
Utilities.setDefaultNodeMargin(renderedTextLabel, PREFERRED_MARGIN, 0);
this.add(renderedTextLabel, 0, 2, 2, 1);

// get bordered ImageView
BorderPane renderedBorderPane = setImageViewBorder(renderedImageView);
this.add(renderedBorderPane, 0, 3);
this.add(renderedBorderPane, 0, 3, 2, 1);

// add front grid panel
this.add(frontGridPane, 0, 4);
this.add(frontGridPane, 0, 4, 2, 1);

// add "Confidence" label text
Label confidenceText = Utilities.getTextLabel("Confidence");
Utilities.setNodeLeftMargin(confidenceText, PREFERRED_MARGIN);
this.add(confidenceText, 0, 5);
Utilities.setDefaultNodeMargin(confidenceText, PREFERRED_MARGIN, 0);
this.add(confidenceText, 0, 5, 2, 1);

// confidence progress bar
Utilities.setNodeLeftMargin(confidenceProgressBar, PREFERRED_MARGIN);
Utilities.setDefaultNodeMargin(confidenceProgressBar, PREFERRED_MARGIN, 0);
confidenceProgressBar.setPrefSize(PREFERRED_WIDTH - 2 * PREFERRED_MARGIN - 1, 20);
// red for less than 20% certainty, yellow for 20% ~ 60%, and green for above 60%
confidenceProgressBar.progressProperty().addListener((observable, oldValue, newValue) -> {
Expand All @@ -89,7 +101,7 @@ class BackGridPane extends GridPane {
setStyle("-fx-accent: #63c956;");
}
});
this.add(confidenceProgressBar, 0, 6);
this.add(confidenceProgressBar, 0, 6, 2, 1);

}

Expand Down Expand Up @@ -158,55 +170,72 @@ void requestHandler() {
// clear last location
copiedButton.setVisible(false);

Response response = Utilities.concurrentCall(clipboardImage);

// if response received
if (response != null) {
// error occurred
if (response.getError() != null) {
// clear error image and last results
clearErrorImage();
// show error content with a alert dialog
Utilities.showErrorDialog(response.getError());
// show waiting label
waitingTextLabel.setVisible(true);

return;
Task<Response> task = new Task<>() {
@Override
protected Response call() {
return Utilities.concurrentCall(clipboardImage);
}
};
task.setOnSucceeded(event -> {

// hide waiting label
waitingTextLabel.setVisible(false);

Response response = task.getValue();

// if response received
if (response != null) {
// error occurred
if (response.getError() != null) {
// clear error image and last results
clearErrorImage();
// show error content with a alert dialog
Utilities.showErrorDialog(response.getError());

return;
}

// put default result into the system clipboard
Utilities.putStringIntoClipboard(response.getLatex_styled());
// set CopiedButton to the corresponded location
frontGridPane.setCopiedButtonRowIndex();

// set rendered image to renderedImageView
renderedImageView.setImage(JLaTeXMathRendering.render(response.getLatex_styled()));

// set results to corresponded TextFields.
latexStyledResult.setFormattedText(response.getLatex_styled());
textResult.setFormattedText(response.getText());
// no equation found in image
if (response.is_not_math()) {
// add $$ ... $$ wrapper, similar handling as Mathpix Snip
notNumberedBlockModeResult.setFormattedText(Utilities.addDoubleDollarWrapper(response.getLatex_styled()));
} else {
notNumberedBlockModeResult.setFormattedText(response.getText_display());
}
numberedBlockModeResult.setFormattedText(Utilities.addEquationWrapper(response.getLatex_styled()));

double confidence = response.getLatex_confidence();

// minimal confidence is set to 1%
if (confidence > 0 && confidence < 0.01) {
confidence = 0.01;
}

confidenceProgressBar.setProgress(confidence);

// put default result into the system clipboard
Utilities.putStringIntoClipboard(response.getLatex_styled());
// set CopiedButton to the corresponded location
frontGridPane.setCopiedButtonRowIndex();

// set rendered image to renderedImageView
renderedImageView.setImage(JLaTeXMathRendering.render(response.getLatex_styled()));

// set results to corresponded TextFields.
latexStyledResult.setFormattedText(response.getLatex_styled());
textResult.setFormattedText(response.getText());
// no equation found in image
if (response.is_not_math()) {
// add $$ ... $$ wrapper, similar handling as Mathpix Snip
notNumberedBlockModeResult.setFormattedText(Utilities.addDoubleDollarWrapper(response.getLatex_styled()));
} else {
notNumberedBlockModeResult.setFormattedText(response.getText_display());
}
numberedBlockModeResult.setFormattedText(Utilities.addEquationWrapper(response.getLatex_styled()));

double confidence = response.getLatex_confidence();
// no response received
clearErrorImage();

// minimal confidence is set to 1%
if (confidence > 0 && confidence < 0.01) {
confidence = 0.01;
}

confidenceProgressBar.setProgress(confidence);

} else {

// no response received
clearErrorImage();

}
});
new Thread(task).start();

} else {

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/FrontGridPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,31 @@ class FrontGridPane extends GridPane {

// add "Result" textL label
Label resultTextLabel = Utilities.getTextLabel("Result");
Utilities.setNodeLeftMargin(resultTextLabel, itemMargin);
Utilities.setDefaultNodeMargin(resultTextLabel, itemMargin, 0);
this.add(resultTextLabel, 0, 0);

// used to indicate which result on the left side has been copied, default invisible
copiedButton.setVisible(false);
Utilities.setNodeLeftMargin(copiedButton, itemMargin / 2);
Utilities.setDefaultNodeMargin(copiedButton, itemMargin / 2, 0);
this.add(copiedButton, 1, 1);

// add latexStyledResult
Utilities.setNodeLeftMargin(latexStyledResult, itemMargin);
Utilities.setDefaultNodeMargin(latexStyledResult, itemMargin, 0);
setTextFieldEvent(latexStyledResult, 1);
this.add(latexStyledResult, 0, 1);

// add textResult
Utilities.setNodeLeftMargin(textResult, itemMargin);
Utilities.setDefaultNodeMargin(textResult, itemMargin, 0);
setTextFieldEvent(textResult, 2);
this.add(textResult, 0, 2);

// add notNumberedBlockModeResult
Utilities.setNodeLeftMargin(notNumberedBlockModeResult, itemMargin);
Utilities.setDefaultNodeMargin(notNumberedBlockModeResult, itemMargin, 0);
setTextFieldEvent(notNumberedBlockModeResult, 3);
this.add(notNumberedBlockModeResult, 0, 3);

// add numberedBlockModeResult
Utilities.setNodeLeftMargin(numberedBlockModeResult, itemMargin);
Utilities.setDefaultNodeMargin(numberedBlockModeResult, itemMargin, 0);
setTextFieldEvent(numberedBlockModeResult, 4);
this.add(numberedBlockModeResult, 0, 4);

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ static Response concurrentCall(Image image) {
}

/**
* Method to set left margin to a node
* Method to set left and right margin to a node with default bottom margin
*
* @param node node to be set left margin
* @param leftMargin left margin
* @param node node to be set margin
* @param left left margin
* @param right right margin
*/
static void setNodeLeftMargin(Node node, int leftMargin) {
GridPane.setMargin(node, new Insets(0, 0, 5, leftMargin));
static void setDefaultNodeMargin(Node node, int left, int right) {
GridPane.setMargin(node, new Insets(0, right, 5, left));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/project.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=0.2.7
version=0.3.0
applicationName=Image2LaTeX

0 comments on commit f602f52

Please sign in to comment.