@@ -4,10 +4,25 @@ import {
4
4
type CustomRenderer ,
5
5
getMiddleCenterBias ,
6
6
GridCellKind ,
7
+ getEmHeight ,
7
8
} from "@glideapps/glide-data-grid" ;
8
9
import * as React from "react" ;
9
10
import { roundedRect } from "../draw-fns.js" ;
10
11
12
+ function adaptFontSize ( font : string , percentage : number ) : string {
13
+ const regex = / ( \d + \. ? \d * ) \s * ( p x | r e m | e m | % | p t ) / ;
14
+ const match = font . match ( regex ) ;
15
+
16
+ if ( match ) {
17
+ const value = parseFloat ( match [ 1 ] ) ;
18
+ const unit = match [ 2 ] ;
19
+ const scaledValue = value * percentage ;
20
+ return font . replace ( regex , `${ Number ( scaledValue . toPrecision ( 3 ) ) } ${ unit } ` ) ;
21
+ }
22
+
23
+ return font ;
24
+ }
25
+
11
26
interface RangeCellProps {
12
27
readonly kind : "range-cell" ;
13
28
readonly value : number ;
@@ -20,8 +35,6 @@ interface RangeCellProps {
20
35
21
36
export type RangeCell = CustomCell < RangeCellProps > ;
22
37
23
- const RANGE_HEIGHT = 6 ;
24
-
25
38
const inputStyle : React . CSSProperties = {
26
39
marginRight : 8 ,
27
40
} ;
@@ -44,19 +57,22 @@ const renderer: CustomRenderer<RangeCell> = {
44
57
45
58
const rangeSize = max - min ;
46
59
const fillRatio = ( value - min ) / rangeSize ;
60
+ // Only use 90% of the base font size for the label
61
+ const labelFont = `${ adaptFontSize ( theme . baseFontStyle , 0.9 ) } ${ theme . fontFamily } ` ;
62
+
63
+ const emHeight = getEmHeight ( ctx , labelFont ) ;
64
+ const rangeHeight = emHeight / 2 ;
47
65
48
66
ctx . save ( ) ;
49
67
let labelWidth = 0 ;
50
68
if ( label !== undefined ) {
51
- ctx . font = `12px ${ theme . fontFamily } ` ; // fixme this is slow
52
- labelWidth =
53
- measureTextCached ( measureLabel ?? label , ctx , `12px ${ theme . fontFamily } ` ) . width +
54
- theme . cellHorizontalPadding ;
69
+ ctx . font = labelFont ; // fixme this is slow
70
+ labelWidth = measureTextCached ( measureLabel ?? label , ctx , labelFont ) . width + theme . cellHorizontalPadding ;
55
71
}
56
72
57
73
const rangeWidth = rect . width - theme . cellHorizontalPadding * 2 - labelWidth ;
58
74
59
- if ( rangeWidth >= RANGE_HEIGHT ) {
75
+ if ( rangeWidth >= rangeHeight ) {
60
76
const gradient = ctx . createLinearGradient ( x , yMid , x + rangeWidth , yMid ) ;
61
77
62
78
gradient . addColorStop ( 0 , theme . accentColor ) ;
@@ -66,17 +82,17 @@ const renderer: CustomRenderer<RangeCell> = {
66
82
67
83
ctx . beginPath ( ) ;
68
84
ctx . fillStyle = gradient ;
69
- roundedRect ( ctx , x , yMid - RANGE_HEIGHT / 2 , rangeWidth , RANGE_HEIGHT , RANGE_HEIGHT / 2 ) ;
85
+ roundedRect ( ctx , x , yMid - rangeHeight / 2 , rangeWidth , rangeHeight , rangeHeight / 2 ) ;
70
86
ctx . fill ( ) ;
71
87
72
88
ctx . beginPath ( ) ;
73
89
roundedRect (
74
90
ctx ,
75
91
x + 0.5 ,
76
- yMid - RANGE_HEIGHT / 2 + 0.5 ,
92
+ yMid - rangeHeight / 2 + 0.5 ,
77
93
rangeWidth - 1 ,
78
- RANGE_HEIGHT - 1 ,
79
- ( RANGE_HEIGHT - 1 ) / 2
94
+ rangeHeight - 1 ,
95
+ ( rangeHeight - 1 ) / 2
80
96
) ;
81
97
ctx . strokeStyle = theme . accentLight ;
82
98
ctx . lineWidth = 1 ;
@@ -89,7 +105,7 @@ const renderer: CustomRenderer<RangeCell> = {
89
105
ctx . fillText (
90
106
label ,
91
107
rect . x + rect . width - theme . cellHorizontalPadding ,
92
- yMid + getMiddleCenterBias ( ctx , `12px ${ theme . fontFamily } ` )
108
+ yMid + getMiddleCenterBias ( ctx , labelFont )
93
109
) ;
94
110
}
95
111
0 commit comments