@@ -4,10 +4,25 @@ import {
44 type CustomRenderer ,
55 getMiddleCenterBias ,
66 GridCellKind ,
7+ getEmHeight ,
78} from "@glideapps/glide-data-grid" ;
89import * as React from "react" ;
910import { roundedRect } from "../draw-fns.js" ;
1011
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+
1126interface RangeCellProps {
1227 readonly kind : "range-cell" ;
1328 readonly value : number ;
@@ -20,8 +35,6 @@ interface RangeCellProps {
2035
2136export type RangeCell = CustomCell < RangeCellProps > ;
2237
23- const RANGE_HEIGHT = 6 ;
24-
2538const inputStyle : React . CSSProperties = {
2639 marginRight : 8 ,
2740} ;
@@ -44,19 +57,22 @@ const renderer: CustomRenderer<RangeCell> = {
4457
4558 const rangeSize = max - min ;
4659 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 ;
4765
4866 ctx . save ( ) ;
4967 let labelWidth = 0 ;
5068 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 ;
5571 }
5672
5773 const rangeWidth = rect . width - theme . cellHorizontalPadding * 2 - labelWidth ;
5874
59- if ( rangeWidth >= RANGE_HEIGHT ) {
75+ if ( rangeWidth >= rangeHeight ) {
6076 const gradient = ctx . createLinearGradient ( x , yMid , x + rangeWidth , yMid ) ;
6177
6278 gradient . addColorStop ( 0 , theme . accentColor ) ;
@@ -66,17 +82,17 @@ const renderer: CustomRenderer<RangeCell> = {
6682
6783 ctx . beginPath ( ) ;
6884 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 ) ;
7086 ctx . fill ( ) ;
7187
7288 ctx . beginPath ( ) ;
7389 roundedRect (
7490 ctx ,
7591 x + 0.5 ,
76- yMid - RANGE_HEIGHT / 2 + 0.5 ,
92+ yMid - rangeHeight / 2 + 0.5 ,
7793 rangeWidth - 1 ,
78- RANGE_HEIGHT - 1 ,
79- ( RANGE_HEIGHT - 1 ) / 2
94+ rangeHeight - 1 ,
95+ ( rangeHeight - 1 ) / 2
8096 ) ;
8197 ctx . strokeStyle = theme . accentLight ;
8298 ctx . lineWidth = 1 ;
@@ -89,7 +105,7 @@ const renderer: CustomRenderer<RangeCell> = {
89105 ctx . fillText (
90106 label ,
91107 rect . x + rect . width - theme . cellHorizontalPadding ,
92- yMid + getMiddleCenterBias ( ctx , `12px ${ theme . fontFamily } ` )
108+ yMid + getMiddleCenterBias ( ctx , labelFont )
93109 ) ;
94110 }
95111
0 commit comments