Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion jabgui/src/main/java/org/jabref/gui/preview/PreviewViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.MalformedURLException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
Expand Down Expand Up @@ -52,7 +53,6 @@
public class PreviewViewer extends ScrollPane implements InvalidationListener {

private static final Logger LOGGER = LoggerFactory.getLogger(PreviewViewer.class);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the empty line as the thing which follow these multiple lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

// https://stackoverflow.com/questions/5669448/get-selected-texts-html-in-div/5670825#5670825
private static final String JS_GET_SELECTION_HTML_SCRIPT = """
function getSelectionHtml() {
Expand Down Expand Up @@ -87,6 +87,7 @@ function getSelectionHtml() {
private @Nullable BibEntry entry;
private PreviewLayout layout;
private String layoutText;
private final AtomicBoolean widthSet = new AtomicBoolean(false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does one really need an AtomicBoolean here? If the method is executed concurrently multiple times, there is a bigger flaw in the architecture.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looked more into it and its not required. using normal bool now


public PreviewViewer(DialogService dialogService,
GuiPreferences preferences,
Expand Down Expand Up @@ -125,6 +126,25 @@ private void configurePreviewView(ThemeManager themeManager) {
return;
}

previewView.getEngine().executeScript("document.body.offsetHeight;");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we looking at this? If the purpose is to force the webview layout the element, write out such intent (and say maybe there is a case that such forcing is necessary).


if (previewView.getEngine().executeScript("document.getElementById('content').scrollHeight") instanceof java.lang.Number heightVal) {
double actualHeight = (heightVal).doubleValue();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

LOGGER.debug("Preview Height {}", actualHeight);
previewView.setMaxHeight(actualHeight + 10);
previewView.setPrefHeight(actualHeight + 10);
}
previewView.getEngine().executeScript("document.body.style.overflow='hidden';");

if (previewView.getEngine().executeScript("document.getElementById('content').scrollWidth") instanceof java.lang.Number widthVal && !widthSet.get()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please comment why you need widthSet and not heightSet

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that Previewer is only created once in the Tooltip and here after, the same instance of the Previewer is created. In that case, we are just being arbitrary to say, however wide the first entry it is, let's just it's width. And granted, what you are facing right now is basically impossible. You cannot possibly get the scroll height of an element without knowing/first confining to some set width.

With that, I felt maybe you could hardcode some width value for the MainTableToolTip use case rather than create the appearance of respecting element's natural width. Or, to handle cases with tooltip element that's actually just less than the predetermined width, maybe a bit of JS is needed, instead of Java :)

double actualWidth = (widthVal).doubleValue();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

double updatedWidth = actualWidth + 350;

LOGGER.debug("Preview Width {}", actualWidth);
previewView.setPrefWidth(Math.min(updatedWidth, 500));
widthSet.set(true);
}

// https://stackoverflow.com/questions/15555510/javafx-stop-opening-url-in-webview-open-in-browser-instead
NodeList anchorList = previewView.getEngine().getDocument().getElementsByTagName("a");
for (int i = 0; i < anchorList.getLength(); i++) {
Expand Down
Loading