7
7
import org .eclipse .swt .graphics .Color ;
8
8
import org .eclipse .swt .graphics .Font ;
9
9
import org .eclipse .swt .graphics .GC ;
10
+ import org .eclipse .swt .graphics .TextLayout ;
11
+ import org .eclipse .swt .widgets .Display ;
10
12
11
13
public final class QInlineSuggestionCloseBracketSegment implements IQInlineSuggestionSegment , IQInlineBracket {
12
14
private QInlineSuggestionOpenBracketSegment openBracket ;
@@ -15,16 +17,28 @@ public final class QInlineSuggestionCloseBracketSegment implements IQInlineSugge
15
17
private int lineInSuggestion ;
16
18
private String text ;
17
19
private Font adjustedTypedFont ;
20
+ private TextLayout layout ;
21
+ private TextLayout measureLayout ;
22
+ private boolean isMacOS ;
18
23
19
24
public QInlineSuggestionCloseBracketSegment (final int caretOffset , final int lineInSuggestion , final String text ,
20
- final char symbol ) {
25
+ final char symbol , final boolean isMacOS ) {
21
26
this .caretOffset = caretOffset ;
22
27
this .symbol = symbol ;
23
28
this .lineInSuggestion = lineInSuggestion ;
24
29
this .text = text ;
30
+ this .layout = isMacOS ? null : new TextLayout (Display .getCurrent ());
31
+ this .isMacOS = isMacOS ;
25
32
26
33
var qInvocationSessionInstance = QInvocationSession .getInstance ();
27
34
adjustedTypedFont = qInvocationSessionInstance .getBoldInlineFont ();
35
+ if (!isMacOS ) {
36
+ int [] tabStops = qInvocationSessionInstance .getViewer ().getTextWidget ().getTabStops ();
37
+ measureLayout = new TextLayout (Display .getCurrent ());
38
+ measureLayout .setText (text );
39
+ measureLayout .setFont (qInvocationSessionInstance .getInlineTextFont ());
40
+ measureLayout .setTabs (tabStops );
41
+ }
28
42
}
29
43
30
44
@ Override
@@ -57,25 +71,40 @@ public void render(final GC gc, final int currentCaretOffset) {
57
71
int invocationLine = widget .getLineAtOffset (invocationOffset );
58
72
int lineHt = widget .getLineHeight ();
59
73
int fontHt = gc .getFontMetrics ().getHeight ();
60
- // educated guess:
61
- int endPadding = gc .getAdvanceWidth (symbol ) / 4 ;
62
74
y = (invocationLine + lineInSuggestion + 1 ) * lineHt - fontHt ;
63
- x = gc .textExtent (text ).x ;
75
+ x = isMacOS ? gc .textExtent (text ).x : ( int ) measureLayout . getBounds (). width ;
64
76
if (lineInSuggestion == 0 ) {
65
77
x += widget .getLocationAtOffset (invocationOffset ).x ;
66
78
}
67
-
79
+ int scrollOffsetY = widget .getTopPixel ();
80
+ y -= scrollOffsetY ;
81
+ String textToRender = String .valueOf (symbol );
68
82
if (currentCaretOffset > openBracket .getRelevantOffset ()) {
69
83
Color typedColor = widget .getForeground ();
70
- gc .setForeground (typedColor );
71
- gc .setFont (adjustedTypedFont );
84
+ if (isMacOS ) {
85
+ gc .setForeground (typedColor );
86
+ gc .setFont (adjustedTypedFont );
87
+ gc .drawText (textToRender , x , y , false );
88
+ } else {
89
+ layout .setFont (adjustedTypedFont );
90
+ layout .setText (textToRender );
91
+ layout .setTabs (widget .getTabStops ());
92
+ gc .setAlpha (255 );
93
+ layout .draw (gc , x , y );
94
+ }
72
95
} else {
73
- gc .setForeground (Q_INLINE_HINT_TEXT_COLOR );
74
- gc .setFont (qInvocationSessionInstance .getInlineTextFont ());
96
+ if (isMacOS ) {
97
+ gc .setForeground (Q_INLINE_HINT_TEXT_COLOR );
98
+ gc .setFont (qInvocationSessionInstance .getInlineTextFont ());
99
+ gc .drawText (textToRender , x , y , true );
100
+ } else {
101
+ layout .setFont (qInvocationSessionInstance .getInlineTextFont ());
102
+ layout .setText (textToRender );
103
+ layout .setTabs (widget .getTabStops ());
104
+ gc .setAlpha (127 );
105
+ layout .draw (gc , x , y );
106
+ }
75
107
}
76
- int scrollOffsetY = widget .getTopPixel ();
77
- y -= scrollOffsetY ;
78
- gc .drawText (String .valueOf (symbol ), x , y , true );
79
108
}
80
109
81
110
@ Override
@@ -112,5 +141,11 @@ public QInlineSuggestionOpenBracketSegment getOpenBracket() {
112
141
113
142
@ Override
114
143
public void cleanUp () {
144
+ if (layout != null ) {
145
+ layout .dispose ();
146
+ }
147
+ if (measureLayout != null ) {
148
+ measureLayout .dispose ();
149
+ }
115
150
}
116
151
}
0 commit comments