1
- import { CustomCell , CustomRenderer , GridCellKind } from '@glideapps/glide-data-grid' ;
1
+ import {
2
+ CustomCell ,
3
+ CustomRenderer ,
4
+ getMiddleCenterBias ,
5
+ GridCellKind ,
6
+ measureTextCached ,
7
+ } from '@glideapps/glide-data-grid' ;
2
8
3
9
import { roundedRect } from 'kit/DataGrid/custom-renderers/utils' ;
4
10
import { Theme } from 'kit/Theme' ;
@@ -23,17 +29,21 @@ interface StateCellProps {
23
29
readonly appTheme : Theme ;
24
30
readonly kind : typeof STATE_CELL ;
25
31
readonly state : State ;
32
+ readonly label ?: string ;
26
33
}
27
34
28
35
const PI = Math . PI ;
29
36
const QUADRANT = PI / 2 ;
37
+ const TAG_HEIGHT = 18 ;
38
+ const maxLabelLength = 12 ;
39
+ const labelColor = '#CC0000' ;
30
40
31
41
export type StateCell = CustomCell < StateCellProps > ;
32
42
33
43
const renderer : CustomRenderer < StateCell > = {
34
44
draw : ( args , cell ) => {
35
45
const { ctx, rect, theme, requestAnimationFrame } = args ;
36
- const { state, appTheme } = cell . data ;
46
+ const { state, appTheme, label } = cell . data ;
37
47
38
48
const xPad = theme . cellHorizontalPadding ;
39
49
@@ -201,6 +211,32 @@ const renderer: CustomRenderer<StateCell> = {
201
211
}
202
212
}
203
213
214
+ if ( label ) {
215
+ const labelText =
216
+ label . length < maxLabelLength ? label : `${ label . slice ( 0 , maxLabelLength ) } ...` ;
217
+
218
+ const tagFont = `600 11px ${ theme . fontFamily } ` ;
219
+
220
+ ctx . fillStyle = labelColor ;
221
+ ctx . strokeStyle = labelColor ;
222
+ ctx . lineWidth = 2 ;
223
+
224
+ ctx . beginPath ( ) ;
225
+ ctx . font = tagFont ;
226
+ roundedRect (
227
+ ctx ,
228
+ x + 12 ,
229
+ y - 8 ,
230
+ measureTextCached ( labelText , ctx , tagFont ) . width + 10 ,
231
+ TAG_HEIGHT ,
232
+ 4 ,
233
+ ) ;
234
+ ctx . stroke ( ) ;
235
+ ctx . fill ( ) ;
236
+ ctx . fillStyle = '#fff' ;
237
+ ctx . fillText ( labelText , x + 16 , y - 8 + TAG_HEIGHT / 2 + getMiddleCenterBias ( ctx , tagFont ) ) ;
238
+ }
239
+
204
240
ctx . restore ( ) ;
205
241
206
242
return true ;
0 commit comments