Skip to content

Commit 37b2e08

Browse files
committed
Improve IME text handling and styling
1 parent 1227fed commit 37b2e08

File tree

5 files changed

+314
-133
lines changed

5 files changed

+314
-133
lines changed

Diff for: editor/imetooltip.go

+34-21
Original file line numberDiff line numberDiff line change
@@ -169,43 +169,56 @@ func (i *IMETooltip) parsePreeditString(preeditStr string) {
169169
start = i.s.tooltip.cursorPos - length
170170
}
171171

172-
g := &Highlight{}
173-
h := &Highlight{}
172+
preeditHl := i.s.getHighlightByHlname("GuiImePreeditText")
173+
currConvHl := i.s.getHighlightByHlname("GuiImeCurrentConversionText")
174174

175-
if i.s.ws.foreground == nil || i.s.ws.background == nil {
176-
return
175+
if preeditHl == nil {
176+
if i.s.ws.foreground == nil || i.s.ws.background == nil {
177+
return
178+
}
179+
180+
preeditHl = &Highlight{}
181+
preeditHl.foreground = i.s.ws.foreground
182+
preeditHl.background = i.s.ws.background
177183
}
178-
g.foreground = i.s.ws.foreground
179-
g.background = i.s.ws.background
180-
if i.s.ws.screenbg == "light" {
181-
h.foreground = warpColor(i.s.ws.background, -30)
182-
h.background = warpColor(i.s.ws.foreground, -30)
183-
} else {
184-
h.foreground = warpColor(i.s.ws.foreground, 30)
185-
h.background = warpColor(i.s.ws.background, 30)
184+
185+
if currConvHl == nil {
186+
if i.s.ws.foreground == nil || i.s.ws.background == nil {
187+
return
188+
}
189+
190+
currConvHl = &Highlight{}
191+
192+
if i.s.ws.screenbg == "light" {
193+
currConvHl.foreground = warpColor(i.s.ws.background, -30)
194+
currConvHl.background = warpColor(i.s.ws.foreground, -30)
195+
} else {
196+
currConvHl.foreground = warpColor(i.s.ws.foreground, 30)
197+
currConvHl.background = warpColor(i.s.ws.background, 30)
198+
}
199+
currConvHl.underline = true
186200
}
187-
h.underline = true
188201

189202
if preeditStr != "" {
190203
r := []rune(preeditStr)
191204

192205
if length > 0 {
193206
if start > 0 {
194-
i.updateText(g, string(r[:start]), i.s.ws.font.letterSpace, i.getFont().qfont)
207+
i.updateText(preeditHl, string(r[:start]), i.s.ws.font.letterSpace, i.getFont().qfont)
195208
if start+length < len(r) {
196-
i.updateText(h, string(r[start:start+length]), i.s.ws.font.letterSpace, i.getFont().qfont)
197-
i.updateText(g, string(r[start+length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
209+
i.updateText(currConvHl, string(r[start:start+length]), i.s.ws.font.letterSpace, i.getFont().qfont)
210+
i.updateText(preeditHl, string(r[start+length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
198211
} else {
199-
i.updateText(h, string(r[start:]), i.s.ws.font.letterSpace, i.getFont().qfont)
212+
i.updateText(currConvHl, string(r[start:]), i.s.ws.font.letterSpace, i.getFont().qfont)
200213
}
201214
} else if start == 0 && length < len(r) {
202-
i.updateText(h, string(r[0:length]), i.s.ws.font.letterSpace, i.getFont().qfont)
203-
i.updateText(g, string(r[length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
215+
i.updateText(currConvHl, string(r[0:length]), i.s.ws.font.letterSpace, i.getFont().qfont)
216+
i.updateText(preeditHl, string(r[length:]), i.s.ws.font.letterSpace, i.getFont().qfont)
204217
} else {
205-
i.updateText(g, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
218+
i.updateText(preeditHl, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
206219
}
207220
} else {
208-
i.updateText(g, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
221+
i.updateText(preeditHl, preeditStr, i.s.ws.font.letterSpace, i.getFont().qfont)
209222
}
210223
}
211224
}

Diff for: editor/screen.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,29 @@ func (s *Screen) makeHighlight(args interface{}) *Highlight {
12381238
return &highlight
12391239
}
12401240

1241-
func (s *Screen) getHighlight(name string) (hl *Highlight) {
1241+
func (s *Screen) getHighlightByHlname(name string) (hl *Highlight) {
1242+
keys := make([]int, 0, len(s.hlAttrDef))
1243+
for k := range s.hlAttrDef {
1244+
if s.hlAttrDef[k].hlName == "" {
1245+
continue
1246+
}
1247+
keys = append(keys, k)
1248+
}
1249+
1250+
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
1251+
1252+
for _, k := range keys {
1253+
h := s.hlAttrDef[k]
1254+
if h.hlName == name {
1255+
hl = h
1256+
break
1257+
}
1258+
}
1259+
1260+
return
1261+
}
1262+
1263+
func (s *Screen) getHighlightByUiname(name string) (hl *Highlight) {
12421264
keys := make([]int, 0, len(s.hlAttrDef))
12431265
for k := range s.hlAttrDef {
12441266
if s.hlAttrDef[k].uiName == "" {

Diff for: editor/tooltip.go

+38-27
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,23 @@ func (t *Tooltip) drawContent(p *gui.QPainter, getFont func() *Font) {
6363

6464
fg := chunk.hl.fg()
6565
bg := chunk.hl.bg()
66-
// bold := chunk.hl.bold
67-
underline := chunk.hl.underline
68-
// undercurl := chunk.hl.undercurl
69-
// strikethrough := chunk.hl.strikethrough
66+
sp := chunk.hl.special
67+
68+
69+
bold := chunk.hl.bold
7070
italic := chunk.hl.italic
71+
underline := chunk.hl.underline
72+
undercurl := chunk.hl.undercurl
73+
underdouble := chunk.hl.underdouble
74+
underdotted := chunk.hl.underdotted
75+
underdashed := chunk.hl.underdashed
76+
strikethrough := chunk.hl.strikethrough
7177

7278
// set foreground color
7379
p.SetPen2(fg.QColor())
7480

7581
r := []rune(chunk.str)
7682
for _, rr := range r {
77-
// setFont(p)
7883
p.SetFont(resolveFontFallback(getFont(), t.fallbackfonts, string(rr)).qfont)
7984
font := p.Font()
8085

@@ -89,12 +94,9 @@ func (t *Tooltip) drawContent(p *gui.QPainter, getFont func() *Font) {
8994
bg.QColor(),
9095
)
9196

92-
// set italic
93-
if italic {
94-
font.SetItalic(true)
95-
} else {
96-
font.SetItalic(false)
97-
}
97+
// set italic, bold
98+
font.SetItalic(italic)
99+
font.SetBold(bold)
98100

99101
p.DrawText(
100102
core.NewQPointF3(
@@ -104,23 +106,32 @@ func (t *Tooltip) drawContent(p *gui.QPainter, getFont func() *Font) {
104106
string(rr),
105107
)
106108

107-
// draw underline
109+
//
110+
// set text decoration
111+
//
112+
113+
if strikethrough {
114+
drawStrikethrough(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
115+
}
116+
108117
if underline {
109-
var underlinePos float64 = 1
110-
if t.s.ws.palette != nil && t.s.ws.palette.widget.IsVisible() {
111-
underlinePos = 2
112-
}
113-
114-
// draw underline
115-
p.FillRect4(
116-
core.NewQRectF4(
117-
x,
118-
y+height-underlinePos,
119-
chunk.width,
120-
underlinePos,
121-
),
122-
fg.QColor(),
123-
)
118+
drawUnderline(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
119+
}
120+
121+
if undercurl {
122+
drawUndercurl(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
123+
}
124+
125+
if underdouble {
126+
drawUnderdouble(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
127+
}
128+
129+
if underdotted {
130+
drawUnderdotted(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
131+
}
132+
133+
if underdashed {
134+
drawUnderdashed(p, t.font, sp.QColor(), 0, x, x+chunk.width, 0, 0)
124135
}
125136

126137
x += chunk.width

0 commit comments

Comments
 (0)