Skip to content

Commit

Permalink
Fix prefix spacing for cut & paste
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Jan 18, 2025
1 parent ddfbf04 commit a3e465e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.xeres.ui.support.chat.ChatLine;
import io.xeres.ui.support.clipboard.ClipboardUtils;
import io.xeres.ui.support.util.TextFlowUtils;
import io.xeres.ui.support.util.TextFlowUtils.Options;
import io.xeres.ui.support.util.TextSelectRange;
import javafx.scene.Cursor;
import javafx.scene.Node;
Expand Down Expand Up @@ -319,21 +320,21 @@ private String getSelectionAsText()

assert textFlow.getChildren().size() >= 3;

return TextFlowUtils.getTextFlowAsText(textFlow, textSelectRange.getStart(), textSelectRange.getEnd() + 1, 2);
return TextFlowUtils.getTextFlowAsText(textFlow, textSelectRange.getStart(), textSelectRange.getEnd() + 1, Options.SPACED_PREFIXES);
}
else
{
if (direction == Direction.UP)
{
return textFlows.reversed().stream()
.map(textFlow -> TextFlowUtils.getTextFlowAsText(textFlow, getOffsetFromSelectionMode()))
.map(textFlow -> TextFlowUtils.getTextFlowAsText(textFlow, getOffsetFromSelectionMode(), Options.SPACED_PREFIXES))
.collect(Collectors.joining("\n"));

}
else
{
return textFlows.stream()
.map(textFlow -> TextFlowUtils.getTextFlowAsText(textFlow, getOffsetFromSelectionMode()))
.map(textFlow -> TextFlowUtils.getTextFlowAsText(textFlow, getOffsetFromSelectionMode(), Options.SPACED_PREFIXES))
.collect(Collectors.joining("\n"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.micrometer.common.util.StringUtils;
import io.xeres.ui.support.clipboard.ClipboardUtils;
import io.xeres.ui.support.util.TextFlowUtils.Options;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.input.MouseEvent;
Expand Down Expand Up @@ -90,7 +91,7 @@ public void release(MouseEvent e)

public void copy()
{
var text = TextFlowUtils.getTextFlowAsText(textFlow, textSelectRange.getStart(), textSelectRange.getEnd() + 1);
var text = TextFlowUtils.getTextFlowAsText(textFlow, textSelectRange.getStart(), textSelectRange.getEnd() + 1, Options.NONE);
if (StringUtils.isNotBlank(text))
{
ClipboardUtils.copyTextToClipboard(text);
Expand Down
24 changes: 13 additions & 11 deletions ui/src/main/java/io/xeres/ui/support/util/TextFlowUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,24 @@ private TextFlowUtils()
throw new UnsupportedOperationException("Utility class");
}

public enum Options
{
NONE,
SPACED_PREFIXES // This lacks flexibility but will do for now
}

/**
* Returns a text flow as a string.
*
* @param textFlow the text flow, not null
* @param textFlow the text flow, not null
* @param beginIndex the beginning index, inclusive
* @param options the options
* @return the string, not null
*/
public static String getTextFlowAsText(TextFlow textFlow, int beginIndex)
public static String getTextFlowAsText(TextFlow textFlow, int beginIndex, Options options)
{
Objects.requireNonNull(textFlow);
return getTextFlowAsText(textFlow, beginIndex, getTextFlowCount(textFlow));
return getTextFlowAsText(textFlow, beginIndex, getTextFlowCount(textFlow), options);
}

/**
Expand All @@ -58,17 +65,12 @@ public static String getTextFlowAsText(TextFlow textFlow, int beginIndex)
* @param textFlow the text flow, not null
* @param beginIndex the beginning index, inclusive
* @param endIndex the ending index, exclusive
* @param options the options
* @return the string, not null
*/
public static String getTextFlowAsText(TextFlow textFlow, int beginIndex, int endIndex)
{
var context = new Context(textFlow.getChildrenUnmodifiable(), beginIndex, endIndex, 0);
return context.getText();
}

public static String getTextFlowAsText(TextFlow textFlow, int beginIndex, int endIndex, int prefixNeedingSpace)
public static String getTextFlowAsText(TextFlow textFlow, int beginIndex, int endIndex, Options options)
{
var context = new Context(textFlow.getChildrenUnmodifiable(), beginIndex, endIndex, prefixNeedingSpace);
var context = new Context(textFlow.getChildrenUnmodifiable(), beginIndex, endIndex, options == Options.SPACED_PREFIXES ? 2 : 0);
return context.getText();
}

Expand Down
14 changes: 13 additions & 1 deletion ui/src/main/resources/view/about/about.fxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Copyright (c) 2019-2023 by David Gerber - https://zapek.com
~ Copyright (c) 2019-2025 by David Gerber - https://zapek.com
~
~ This file is part of Xeres.
~
Expand Down Expand Up @@ -198,6 +198,18 @@
<DisclosedHyperlink text="Apache 2.0 license." uri="https://www.apache.org/licenses/LICENSE-2.0"/>
<Text text="&#10;&#10;"/>

<DisclosedHyperlink text="Java" uri="https://www.oracle.com/java/"/>
<Text text="&#10;"/>
<Text text=" © Oracle. Licensed under the "/>
<DisclosedHyperlink text="GPLv2 + classpath exception." uri="https://openjdk.org/legal/gplv2+ce.html"/>
<Text text="&#10;&#10;"/>

<DisclosedHyperlink text="JavaFX" uri="https://openjfx.io/"/>
<Text text="&#10;"/>
<Text text=" © Oracle. Licensed under the "/>
<DisclosedHyperlink text="GPLv2 + classpath exception." uri="https://openjdk.org/legal/gplv2+ce.html"/>
<Text text="&#10;&#10;"/>

<DisclosedHyperlink text="JavaFX-Weaver" uri="https://github.com/rgielen/javafx-weaver"/>
<Text text="&#10;"/>
<Text text=" © René Gielen. Licensed under the "/>
Expand Down

0 comments on commit a3e465e

Please sign in to comment.