Skip to content

Commit

Permalink
Improved rendering, add hyperlink support
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Jan 8, 2022
1 parent 72aa5af commit 7ee876d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
3 changes: 0 additions & 3 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,6 @@
<!-- Number of possible paths through the source -->
<property name="max" value="15"/>
</module>
<module name="AnonInnerLength">
<property name="max" value="60"/>
</module>
<module name="OneStatementPerLine"/>
<module name="OneTopLevelClass"/>
<module name="UnusedImports"/>
Expand Down
78 changes: 59 additions & 19 deletions src/main/java/me/coley/recaf/ui/controls/pane/UpdatePane.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
Expand All @@ -11,11 +12,14 @@
import me.coley.recaf.ui.controls.ActionButton;
import me.coley.recaf.ui.controls.ExceptionAlert;
import me.coley.recaf.util.Log;
import me.coley.recaf.util.UiUtil;
import me.coley.recaf.util.self.SelfUpdater;
import org.commonmark.node.*;
import org.commonmark.parser.Parser;

import java.io.IOException;
import java.net.URL;
import java.util.function.Consumer;

import static me.coley.recaf.util.LangUtil.translate;

Expand Down Expand Up @@ -60,58 +64,94 @@ private BorderPane getHeader() {
private BorderPane getNotes() {
TextFlow flow = new TextFlow();
Parser parser = Parser.builder().build();
Node document = parser.parse(SelfUpdater.getLatestPatchnotes().replaceAll("\\(\\[.+\\)\\)", ""));
Node document = parser.parse(SelfUpdater.getLatestPatchnotes());
document.accept(new AbstractVisitor() {
@Override
public void visit(Paragraph paragraph) {
StringBuilder sb = new StringBuilder();
Node node = paragraph.getFirstChild();
build(sb, node);
addLine(sb.toString(), null);
// Add all content to same line
parse(paragraph, this::text, this::link);
newLine();
}

@Override
public void visit(Heading heading) {
// Skip the version H2 text
if (heading.getLevel() <= 2)
return;
addLine(build(heading), "h2");
// Extract heading text
StringBuilder sb = new StringBuilder();
parse(heading, text -> sb.append(text.getLiteral()), null);
// Render
addText(sb.toString(), "h2");
newLine();
}

@Override
public void visit(BulletList list) {
Node item = list.getFirstChild();
do {
String itemText = build(item);
addLine(" ● " + itemText, null);
// Prefix with bullet point
addText(" ● ", null);
// Add all content to same line
parse(item, this::text, this::link);
// New line between items
newLine();
item = item.getNext();
} while (item != null);
}

private String build(Node node) {
StringBuilder sb = new StringBuilder();
build(sb, node);
return sb.toString();
private void text(Text text) {
addText(text.getLiteral(), null);
}

private void link(Link link) {
String text = link.getTitle();
if (text == null) {
StringBuilder sb = new StringBuilder();
parse(link, t -> sb.append(t.getLiteral()), null);
text = sb.toString();
}
addLink(text, link.getDestination());
}

private void build(StringBuilder sb, Node node) {
if (node instanceof Text)
sb.append(((Text) node).getLiteral());
else {
private void parse(Node node, Consumer<Text> textHandler, Consumer<Link> linkHandler) {
if (node instanceof Text) {
if (textHandler != null) textHandler.accept((Text) node);
} else if (linkHandler != null && node instanceof Link) {
linkHandler.accept((Link) node);
} else {
Node child = node.getFirstChild();
do {
build(sb, child);
parse(child, textHandler, linkHandler);
child = child.getNext();
} while (child != null);
}
}

private void addLine(String text, String style) {
javafx.scene.text.Text t = new javafx.scene.text.Text(text + "\n");
private void addLink(String text, String url) {
Hyperlink link = new Hyperlink(text);
link.getStyleClass().add("a");
link.setOnAction(e -> {
try {
UiUtil.showDocument(new URL(url).toURI());
} catch (Exception ex) {
Log.error("Could not open URL: " + url);
}
});
flow.getChildren().add(link);
}

private void addText(String text, String style) {
javafx.scene.text.Text t = new javafx.scene.text.Text(text);
if (style != null)
t.getStyleClass().add(style);
flow.getChildren().add(t);
}

private void newLine() {
javafx.scene.text.Text t = new javafx.scene.text.Text("\n");
flow.getChildren().add(t);
}
});
BorderPane pane = new BorderPane(flow);
pane.getStyleClass().add("content");
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/style/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
-fx-font-size: 15px;
-fx-font-weight: bold;
}
.a {
-fx-underline: true;
-fx-text-fill: rgb(24, 101, 217);
-fx-fill: rgb(24, 101, 217);
}
.bold {
-fx-font-weight: bold;
}
.underlined {
-fx-underline: true;
}
.faint {
-fx-text-fill: rgb(116, 116, 116);
-fx-fill: rgb(116, 116, 116);
}
.no-border {
-fx-border-width: 0;
Expand Down

0 comments on commit 7ee876d

Please sign in to comment.