Skip to content

Commit

Permalink
Fix #460: Support different foreground color for armed fold icons
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbylight committed Sep 25, 2022
1 parent 852523e commit dbf9096
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 14 deletions.
Expand Up @@ -105,6 +105,7 @@ public class Theme {
public String lineNumberFont;
public int lineNumberFontSize;
public Color foldIndicatorFG;
public Color foldIndicatorArmedFG;
public Color foldBG;
public Color armedFoldBG;

Expand Down Expand Up @@ -171,6 +172,7 @@ public Theme(RSyntaxTextArea textArea) {
lineNumberFont = gutter.getLineNumberFont().getFamily();
lineNumberFontSize = gutter.getLineNumberFont().getSize();
foldIndicatorFG = gutter.getFoldIndicatorForeground();
foldIndicatorArmedFG = gutter.getFoldIndicatorArmedForeground();
foldBG = gutter.getFoldBackground();
armedFoldBG = gutter.getArmedFoldBackground();
}
Expand Down Expand Up @@ -227,6 +229,7 @@ public void apply(RSyntaxTextArea textArea) {
Font font = getFont(fontName, Font.PLAIN, fontSize);
gutter.setLineNumberFont(font);
gutter.setFoldIndicatorForeground(foldIndicatorFG);
gutter.setFoldIndicatorArmedForeground(foldIndicatorArmedFG);
gutter.setFoldBackground(foldBG);
gutter.setArmedFoldBackground(armedFoldBG);
}
Expand Down Expand Up @@ -473,8 +476,13 @@ public void save(OutputStream out) throws IOException {

elem = doc.createElement("foldIndicator");
elem.setAttribute("fg", colorToString(foldIndicatorFG));
if (foldIndicatorArmedFG != null) {
elem.setAttribute("armedFg", colorToString(foldIndicatorArmedFG));
}
elem.setAttribute("iconBg", colorToString(foldBG));
elem.setAttribute("iconArmedBg", colorToString(armedFoldBG));
if (armedFoldBG != null) {
elem.setAttribute("iconArmedBg", colorToString(armedFoldBG));
}
root.appendChild(elem);

elem = doc.createElement("iconRowHeader");
Expand Down Expand Up @@ -714,6 +722,11 @@ else if ("tabLine".equals(qName)) {
else if ("foldIndicator".equals(qName)) {
String color = attrs.getValue("fg");
theme.foldIndicatorFG = stringToColor(color);
color = attrs.getValue("armedFg");
// This field must have a value for downstream consumers to
// function properly, so default to regular FG if not armed
// variant isn't specified
theme.foldIndicatorArmedFG = stringToColor(color);
color = attrs.getValue("iconBg");
theme.foldBG = stringToColor(color);
color = attrs.getValue("iconArmedBg");
Expand Down
Expand Up @@ -47,6 +47,12 @@ public int getIconWidth() {
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {

FoldIndicator fi = (FoldIndicator)c;
Color fg = c.getForeground();
if (isArmed() && fi.getArmedForeground() != null) {
fg = fi.getArmedForeground();
}

int width = getIconWidth();
int height = getIconHeight();
Graphics2D g2d = (Graphics2D)g.create();
Expand All @@ -55,7 +61,7 @@ public void paintIcon(Component c, Graphics g, int x, int y) {

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(c.getForeground());
g2d.setColor(fg);
g2d.translate(x, y);
if (!isCollapsed()) {
g2d.rotate(Math.toRadians(90), width / 2f, height / 2f);
Expand Down
Expand Up @@ -57,6 +57,13 @@ public class FoldIndicator extends AbstractGutterComponent {
*/
private Fold foldWithOutlineShowing;


/**
* The color used for the foreground of armed folds.
*/
private Color armedForeground;


/**
* The color to use for fold icon backgrounds, if the default icons
* are used.
Expand Down Expand Up @@ -213,6 +220,17 @@ private Fold findOpenFoldClosestTo(Point p) {
}


/**
* Returns the foreground color used for armed folds.
*
* @return The foreground color used for armed folds.
* @see #setArmedForeground(Color)
*/
public Color getArmedForeground() {
return armedForeground;
}


/**
* Returns the strategy to use for rendering expanded folds.
*
Expand Down Expand Up @@ -727,6 +745,20 @@ public void setAdditionalLeftMargin(int leftMargin) {
}


/**
* Sets the foreground color used for armed folds.
*
* @param fg The new armed fold foreground.
* @see #getArmedForeground()
*/
public void setArmedForeground(Color fg) {
if (fg==null) {
fg = FoldIndicator.DEFAULT_FOREGROUND;
}
armedForeground = fg;
}


private void setCollapsedFoldIconAlpha(float collapsedFoldIconAlpha) {
collapsedFoldIconAlpha = Math.max(0, Math.min(collapsedFoldIconAlpha, 1));
if (collapsedFoldIconAlpha != this.collapsedFoldIconAlpha) {
Expand Down
28 changes: 28 additions & 0 deletions RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/Gutter.java
Expand Up @@ -369,6 +369,19 @@ public Color getFoldBackground() {
}


/**
* Returns the foreground color of the fold indicator for armed
* folds.
*
* @return The foreground color of the fold indicator for armed
* folds.
* @see #setFoldIndicatorArmedForeground(Color)
*/
public Color getFoldIndicatorArmedForeground() {
return foldIndicator.getArmedForeground();
}


/**
* Returns the foreground color of the fold indicator.
*
Expand Down Expand Up @@ -781,6 +794,21 @@ public void setFoldBackground(Color bg) {
}


/**
* Sets the foreground color used by the fold indicator for
* armed folds.
*
* @param fg The new armed fold indicator foreground.
* @see #getFoldIndicatorArmedForeground()
*/
public void setFoldIndicatorArmedForeground(Color fg) {
if (fg==null) {
fg = FoldIndicator.DEFAULT_FOREGROUND;
}
foldIndicator.setArmedForeground(fg);
}


/**
* Sets the foreground color used by the fold indicator.
*
Expand Down
Expand Up @@ -31,14 +31,23 @@ public int getIconWidth() {

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {

FoldIndicator fi = (FoldIndicator)c;
Color fg = fi.getForeground();
Color bg = fi.getFoldIconBackground();
if (isArmed() && fi.getFoldIconArmedBackground() != null) {
bg = fi.getFoldIconArmedBackground();
if (isArmed()) {
if (fi.getArmedForeground() != null) {
fg = fi.getArmedForeground();
}
if (fi.getFoldIconArmedBackground() != null) {
bg = fi.getFoldIconArmedBackground();
}
}

g.setColor(bg);
g.fillRect(x, y, 8, 8);
g.setColor(fi.getForeground());

g.setColor(fg);
g.drawRect(x, y, 8, 8);
g.drawLine(x + 2, y + 4, x + 2 + 4, y + 4);
if (isCollapsed()) {
Expand Down
Expand Up @@ -30,7 +30,7 @@
<!-- Gutter styling. -->
<gutterBorder color="81969A" />
<lineNumbers fg="81969A" currentFG="a9b7c6"/>
<foldIndicator fg="6A8088" iconBg="2f383c" iconArmedBg="3f484c" />
<foldIndicator fg="6A8088" armedFg="a9b7c6" iconBg="2f383c" iconArmedBg="3f484c" />
<iconRowHeader activeLineRange="3399ff" />

<!-- Syntax tokens. -->
Expand Down
Expand Up @@ -29,7 +29,7 @@
<!-- Gutter styling. -->
<gutterBorder color="dddddd"/>
<lineNumbers fg="787878" currentFG="06176b"/>
<foldIndicator fg="808080" iconBg="ffffff"/>
<foldIndicator fg="808080" armedFg="585858" iconBg="ffffff"/>
<iconRowHeader activeLineRange="3399ff"/>

<!-- Syntax tokens. -->
Expand Down
Expand Up @@ -29,7 +29,7 @@
<!-- Gutter styling. -->
<gutterBorder color="dddddd"/>
<lineNumbers fg="787878" currentFG="06176b"/>
<foldIndicator fg="808080" iconBg="ffffff"/>
<foldIndicator fg="808080" armedFg="585858" iconBg="ffffff"/>
<iconRowHeader activeLineRange="3399ff"/>

<!-- Syntax tokens. -->
Expand Down
Expand Up @@ -29,7 +29,7 @@
<!-- Gutter styling. -->
<gutterBorder color="325232"/>
<lineNumbers fg="66aa66"/>
<foldIndicator fg="325232" iconBg="001c00"/>
<foldIndicator fg="325232" armedFg="66aaa66" iconBg="001c00"/>
<iconRowHeader activeLineRange="3399ff"/>

<!-- Syntax tokens. -->
Expand Down
Expand Up @@ -29,9 +29,9 @@
<!-- Gutter styling. -->
<gutterBorder color="dddddd"/>
<lineNumbers fg="787878" currentFG="343434"/>
<foldIndicator fg="808080" iconBg="ffffff"/>
<foldIndicator fg="808080" armedFg="585858" iconBg="ffffff"/>
<iconRowHeader activeLineRange="3399ff"/>

<!-- Syntax tokens. -->
<tokenStyles>
<style token="IDENTIFIER" fg="000000"/>
Expand Down
Expand Up @@ -29,7 +29,7 @@
<!-- Gutter styling. -->
<gutterBorder color="dddddd" />
<lineNumbers fg="787878" />
<foldIndicator fg="808080" iconBg="ffffff" />
<foldIndicator fg="808080" armedFg="585858" iconBg="ffffff" />
<iconRowHeader activeLineRange="3399ff" />

<!-- Syntax tokens. -->
Expand Down
Expand Up @@ -24,7 +24,7 @@
<!-- Gutter styling. -->
<gutterBorder color="81969A"/>
<lineNumbers fg="81969A" currentFG="F9F9F9"/>
<foldIndicator fg="6A8088" iconBg="2f383c" iconArmedBg="3f484c"/>
<foldIndicator fg="6A8088" armedFg="a9b7c6" iconBg="2f383c" iconArmedBg="3f484c"/>
<iconRowHeader activeLineRange="878787"/>

<!-- Syntax tokens. -->
Expand Down
Expand Up @@ -71,6 +71,7 @@
fontSize CDATA #IMPLIED>
<!ATTLIST foldIndicator
fg CDATA #REQUIRED
armedFg CDATA #IMPLIED
iconBg CDATA #REQUIRED
iconArmedBg CDATA #IMPLIED>
<!ATTLIST iconRowHeader
Expand Down
Expand Up @@ -30,7 +30,7 @@
<!-- Gutter styling. -->
<gutterBorder color="808080"/>
<lineNumbers fg="2B91AF"/>
<foldIndicator fg="808080" iconBg="ffffff"/>
<foldIndicator fg="808080" armedFg="585858" iconBg="ffffff"/>
<iconRowHeader activeLineRange="3399ff"/>

<!-- Syntax tokens. -->
Expand Down
Expand Up @@ -321,6 +321,7 @@ private void initWithOddProperties(RSyntaxTextArea textArea,
gutter.setLineNumberColor(Color.orange);
gutter.setCurrentLineNumberColor(Color.orange);
gutter.setLineNumberFont(font);
gutter.setFoldIndicatorArmedForeground(Color.orange);
gutter.setFoldIndicatorForeground(Color.orange);
gutter.setFoldBackground(Color.orange);
gutter.setArmedFoldBackground(Color.orange);
Expand Down
Expand Up @@ -91,6 +91,28 @@ void testGetSetAdditionalLeftMargin_error_negativeValue() {
}


@Test
void testGetSetArmedForeground() {

RSyntaxTextArea textArea = createTextArea();
FoldIndicator fi = new FoldIndicator(textArea);

Color color = Color.red;
fi.setArmedForeground(color);
Assertions.assertEquals(color, fi.getArmedForeground());

color = Color.green;
fi.setArmedForeground(color);
Assertions.assertEquals(color, fi.getArmedForeground());

// Sets to default - not a public value, but also not Color.green.
fi.setArmedForeground(null);
Assertions.assertNotNull(fi.getArmedForeground());
Assertions.assertNotEquals(color, fi.getArmedForeground());

}


@Test
void testGetSetExpandedFoldRenderStrategy() {
RSyntaxTextArea textArea = createTextArea();
Expand Down
Expand Up @@ -315,6 +315,28 @@ void testGetSetFoldBackground() {
}


@Test
void testGetSetFoldIndicatorArmedForeground() {

RTextArea textArea = new RTextArea(PLAIN_TEXT);
Gutter gutter = new Gutter(textArea);

Color color = Color.red;
gutter.setFoldIndicatorArmedForeground(color);
Assertions.assertEquals(color, gutter.getFoldIndicatorArmedForeground());

color = Color.green;
gutter.setFoldIndicatorArmedForeground(color);
Assertions.assertEquals(color, gutter.getFoldIndicatorArmedForeground());

// Sets to default - not a public value, but also not Color.green.
gutter.setFoldIndicatorArmedForeground(null);
Assertions.assertNotNull(gutter.getFoldIndicatorArmedForeground());
Assertions.assertNotEquals(color, gutter.getFoldIndicatorArmedForeground());

}


@Test
void testGetSetFoldIndicatorForeground() {

Expand Down

0 comments on commit dbf9096

Please sign in to comment.