gpt4 book ai didi

java - SWT Bullet 更改整个背景

转载 作者:行者123 更新时间:2023-12-04 04:53:36 31 4
gpt4 key购买 nike

我试图突出当前 Activity 行的项目符号背景,但如果我只是设置项目符号的背景颜色,它只会突出显示数字部分。我希望项目符号占据的所有空间都突出显示。

Only the 9 is highlighted

我希望 9 左侧的所有空间也都突出显示,并且可能也突出显示在右侧。

我用来获得到目前为止所拥有的代码是

@Override
public void lineGetStyle(LineStyleEvent event) {
// Set the line number
int activeLine = styledText.getLineAtOffset(styledText.getCaretOffset());
int currentLine = styledText.getLineAtOffset(event.lineOffset);
event.bulletIndex = currentLine;

int width = 36;
if (styledText.getLineCount() > 999)
width = (int) ((Math.floor(Math.log10(styledText.getLineCount()))+1) * 12);

// Set the style, 12 pixles wide for each digit
StyleRange style = new StyleRange();
style.metrics = new GlyphMetrics(0, 0, width);

if (activeLine == currentLine) {
style.background = highlightedLine;
}

style.foreground = mainBackground;


// Create and set the bullet
event.bullet = new Bullet(ST.BULLET_NUMBER, style);

event.styles = matchKeywords(event);
}

这可能吗?

最佳答案

您可以使用自定义绘画来做到这一点。首先,将项目符号类型更改为 ST.BULLET_CUSTOM .然后添加一个 PaintObjectListener :

styledText.addPaintObjectListener(new PaintObjectListener() {
public void paintObject(PaintObjectEvent event) {
drawBullet(event.bullet, event.gc, event.x, event.y, event.bulletIndex, event.ascent, event.descent);
}
});

drawBullet 的标准实现在 StyledTextRenderer .我已将其更改为接受自定义项目符号作为编号项目符号,并在背景中绘制一个额外的矩形:
void drawBullet(Bullet bullet, GC gc, int paintX, int paintY, int index, int lineAscent, int lineDescent) {
StyleRange style = bullet.style;
GlyphMetrics metrics = style.metrics;
Color color = style.foreground;
if (color != null) gc.setForeground(color);
Font font = style.font;
if (font != null) gc.setFont(font);
String string = "";
int type = bullet.type & (ST.BULLET_DOT|ST.BULLET_CUSTOM|ST.BULLET_NUMBER|ST.BULLET_LETTER_LOWER|ST.BULLET_LETTER_UPPER);
switch (type) {
case ST.BULLET_DOT: string = "\u2022"; break;
case ST.BULLET_CUSTOM: string = String.valueOf(index + 1); break;
case ST.BULLET_NUMBER: string = String.valueOf(index + 1); break;
case ST.BULLET_LETTER_LOWER: string = String.valueOf((char) (index % 26 + 97)); break;
case ST.BULLET_LETTER_UPPER: string = String.valueOf((char) (index % 26 + 65)); break;
}
if ((bullet.type & ST.BULLET_TEXT) != 0) string += bullet.text;

gc.setBackground(style.background);
gc.fillRectangle(paintX, paintY, metrics.width, styledText.getLineHeight());

Display display = styledText.getDisplay();
TextLayout layout = new TextLayout(display);
layout.setText(string);
layout.setAscent(lineAscent);
layout.setDescent(lineDescent);
style = (StyleRange)style.clone();
style.metrics = null;
if (style.font == null) style.font = styledText.getFont();
layout.setStyle(style, 0, string.length());
int x = paintX + Math.max(0, metrics.width - layout.getBounds().width - 8);
layout.draw(gc, x, paintY);
layout.dispose();
}

关于java - SWT Bullet 更改整个背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17072473/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com