| 1 |
import { TextRenderer, CellRenderer, GraphicsContext } from '@lumino/datagrid'; |
| 2 |
|
| 3 |
import { ViewBasedJSONModel } from './viewbasedjsonmodel'; |
| 4 |
|
| 5 |
import { Theme } from '../utils'; |
| 6 |
|
| 7 |
import { TransformStateManager } from './transformStateManager'; |
| 8 |
|
| 9 |
import { DataGrid } from '@lumino/datagrid'; |
| 10 |
|
| 11 |
/** |
| 12 |
* A custom cell renderer for headers that provides a menu icon. |
| 13 |
*/ |
| 14 |
export class HeaderRenderer extends TextRenderer { |
| 15 |
constructor(options: HeaderRenderer.IOptions) { |
| 16 |
super(options.textOptions); |
| 17 |
this._isLightTheme = options.isLightTheme; |
| 18 |
this._grid = options.grid; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Model getter. |
| 23 |
*/ |
| 24 |
get model(): ViewBasedJSONModel { |
| 25 |
return this._grid.dataModel as ViewBasedJSONModel; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Draw the text for the cell. |
| 30 |
* |
| 31 |
* @param gc - The graphics context to use for drawing. |
| 32 |
* |
| 33 |
* @param config - The configuration data for the cell. |
| 34 |
*/ |
| 35 |
drawText(gc: GraphicsContext, config: CellRenderer.CellConfig): void { |
| 36 |
// Resolve the font for the cell. |
| 37 |
const font = CellRenderer.resolveOption(this.font, config); |
| 38 |
|
| 39 |
// Bail if there is no font to draw. |
| 40 |
if (!font) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Resolve the text color for the cell. |
| 45 |
const color = CellRenderer.resolveOption(this.textColor, config); |
| 46 |
|
| 47 |
// Bail if there is no text color to draw. |
| 48 |
if (!color) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// Format the cell value to text. |
| 53 |
const format = this.format; |
| 54 |
let text = format(config); |
| 55 |
|
| 56 |
// Bail if there is no text to draw. |
| 57 |
if (!text) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Resolve the vertical and horizontal alignment. |
| 62 |
const vAlign = CellRenderer.resolveOption(this.verticalAlignment, config); |
| 63 |
const hAlign = CellRenderer.resolveOption(this.horizontalAlignment, config); |
| 64 |
|
| 65 |
// Resolve the elision direction |
| 66 |
const elideDirection = CellRenderer.resolveOption( |
| 67 |
this.elideDirection, |
| 68 |
config, |
| 69 |
); |
| 70 |
|
| 71 |
// Resolve the text wrapping flag |
| 72 |
const wrapText = CellRenderer.resolveOption(this.wrapText, config); |
| 73 |
|
| 74 |
// Compute the padded text box height for the specified alignment. |
| 75 |
const boxHeight = config.height - (vAlign === 'center' ? 1 : 2); |
| 76 |
|
| 77 |
// Bail if the text box has no effective size. |
| 78 |
if (boxHeight <= 0) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Compute the text height for the gc font. |
| 83 |
const textHeight = TextRenderer.measureFontHeight(font); |
| 84 |
|
| 85 |
// Set up the text position variables. |
| 86 |
let textX: number; |
| 87 |
let textY: number; |
| 88 |
let boxWidth: number; |
| 89 |
|
| 90 |
// Compute the Y position for the text. |
| 91 |
switch (vAlign) { |
| 92 |
case 'top': |
| 93 |
textY = config.y + 2 + textHeight; |
| 94 |
break; |
| 95 |
case 'center': |
| 96 |
textY = config.y + config.height / 2 + textHeight / 2; |
| 97 |
break; |
| 98 |
case 'bottom': |
| 99 |
textY = config.y + config.height - 2; |
| 100 |
break; |
| 101 |
default: |
| 102 |
throw 'unreachable'; |
| 103 |
} |
| 104 |
|
| 105 |
// Compute the X position for the text. |
| 106 |
switch (hAlign) { |
| 107 |
case 'left': |
| 108 |
textX = config.x + 8; |
| 109 |
boxWidth = config.width - 14; |
| 110 |
break; |
| 111 |
case 'center': |
| 112 |
textX = config.x + config.width / 2; |
| 113 |
boxWidth = config.width; |
| 114 |
break; |
| 115 |
case 'right': |
| 116 |
textX = config.x + config.width - 8; |
| 117 |
boxWidth = config.width - 14; |
| 118 |
break; |
| 119 |
default: |
| 120 |
throw 'unreachable'; |
| 121 |
} |
| 122 |
|
| 123 |
// Clip the cell if the text is taller than the text box height. |
| 124 |
if (textHeight > boxHeight) { |
| 125 |
gc.beginPath(); |
| 126 |
gc.rect(config.x, config.y, config.width, config.height - 1); |
| 127 |
gc.clip(); |
| 128 |
} |
| 129 |
|
| 130 |
// Set the gc state. |
| 131 |
gc.font = font; |
| 132 |
gc.fillStyle = color; |
| 133 |
gc.textAlign = hAlign; |
| 134 |
gc.textBaseline = 'bottom'; |
| 135 |
|
| 136 |
// The current text width in pixels. |
| 137 |
let textWidth = gc.measureText(text).width; |
| 138 |
|
| 139 |
// Apply text wrapping if enabled. |
| 140 |
if (wrapText && textWidth > boxWidth) { |
| 141 |
// Make sure box clipping happens. |
| 142 |
gc.beginPath(); |
| 143 |
gc.rect(config.x, config.y, config.width, config.height - 1); |
| 144 |
gc.clip(); |
| 145 |
|
| 146 |
// Split column name to words based on |
| 147 |
// whitespace preceding a word boundary. |
| 148 |
// "Hello world" --> ["Hello ", "world"] |
| 149 |
const wordsInColumn = text.split(/\s(?=\b)/); |
| 150 |
|
| 151 |
// Y-coordinate offset for any additional lines |
| 152 |
let curY = textY; |
| 153 |
let textInCurrentLine = wordsInColumn.shift()!; |
| 154 |
|
| 155 |
// Single word. Applying text wrap on word by splitting |
| 156 |
// it into characters and fitting the maximum number of |
| 157 |
// characters possible per line (box width). |
| 158 |
if (wordsInColumn.length === 0) { |
| 159 |
let curLineTextWidth = gc.measureText(textInCurrentLine).width; |
| 160 |
while (curLineTextWidth > boxWidth && textInCurrentLine !== '') { |
| 161 |
// Iterating from the end of the string until we find a |
| 162 |
// substring (0,i) which has a width less than the box width. |
| 163 |
for (let i = textInCurrentLine.length; i > 0; i--) { |
| 164 |
const curSubString = textInCurrentLine.substring(0, i); |
| 165 |
const curSubStringWidth = gc.measureText(curSubString).width; |
| 166 |
if (curSubStringWidth < boxWidth || curSubString.length === 1) { |
| 167 |
// Found a substring which has a width less than the current |
| 168 |
// box width. Rendering that substring on the current line |
| 169 |
// and setting the remainder of the parent string as the next |
| 170 |
// string to iterate on for the next line. |
| 171 |
const nextLineText = textInCurrentLine.substring( |
| 172 |
i, |
| 173 |
textInCurrentLine.length, |
| 174 |
); |
| 175 |
textInCurrentLine = nextLineText; |
| 176 |
curLineTextWidth = gc.measureText(textInCurrentLine).width; |
| 177 |
gc.fillText(curSubString, textX, curY); |
| 178 |
curY += textHeight; |
| 179 |
// No need to continue iterating after we identified |
| 180 |
// an index to break the string on. |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// Multiple words in column header. Fitting maximum |
| 188 |
// number of words possible per line (box width). |
| 189 |
else { |
| 190 |
while (wordsInColumn.length !== 0) { |
| 191 |
// Processing the next word in the queue. |
| 192 |
const curWord = wordsInColumn.shift(); |
| 193 |
// Joining that word with the existing text for |
| 194 |
// the current line. |
| 195 |
const incrementedText = [textInCurrentLine, curWord].join(' '); |
| 196 |
const incrementedTextWidth = gc.measureText(incrementedText).width; |
| 197 |
if (incrementedTextWidth > boxWidth) { |
| 198 |
// If the newly combined text has a width larger than |
| 199 |
// the box width, we render the line before the current |
| 200 |
// word was added. We set the current word as the next |
| 201 |
// line. |
| 202 |
gc.fillText(textInCurrentLine, textX, curY); |
| 203 |
curY += textHeight; |
| 204 |
textInCurrentLine = curWord!; |
| 205 |
} else { |
| 206 |
// The combined text hasd a width less than the box width. We |
| 207 |
// set the the current line text to be the new combined text. |
| 208 |
textInCurrentLine = incrementedText; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
gc.fillText(textInCurrentLine!, textX, curY); |
| 213 |
// Terminating the call here as we don't want |
| 214 |
// to apply text eliding when wrapping is active. |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
// Elide text that is too long |
| 219 |
const elide = '\u2026'; |
| 220 |
|
| 221 |
// Compute elided text |
| 222 |
if (elideDirection === 'right') { |
| 223 |
while (textWidth > boxWidth && text.length > 1) { |
| 224 |
if (text.length > 4 && textWidth >= 2 * boxWidth) { |
| 225 |
// If text width is substantially bigger, take half the string |
| 226 |
text = text.substring(0, text.length / 2 + 1) + elide; |
| 227 |
} else { |
| 228 |
// Otherwise incrementally remove the last character |
| 229 |
text = text.substring(0, text.length - 2) + elide; |
| 230 |
} |
| 231 |
textWidth = gc.measureText(text).width; |
| 232 |
} |
| 233 |
} else { |
| 234 |
while (textWidth > boxWidth && text.length > 1) { |
| 235 |
if (text.length > 4 && textWidth >= 2 * boxWidth) { |
| 236 |
// If text width is substantially bigger, take half the string |
| 237 |
text = elide + text.substring(text.length / 2); |
| 238 |
} else { |
| 239 |
// Otherwise incrementally remove the last character |
| 240 |
text = elide + text.substring(2); |
| 241 |
} |
| 242 |
textWidth = gc.measureText(text).width; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
// Draw the text for the cell. |
| 247 |
gc.fillText(text, textX, textY); |
| 248 |
|
| 249 |
// Check if not bottom row of 'column-header' CellRegion |
| 250 |
if ( |
| 251 |
config.region === 'column-header' && |
| 252 |
config.row !== this._grid.dataModel!.rowCount('column-header') - 1 |
| 253 |
) { |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
// Fill the area behind the menu icon |
| 258 |
// Note: This seems to perform better than adding a clip path |
| 259 |
const backgroundSize = |
| 260 |
HeaderRenderer.iconWidth + |
| 261 |
HeaderRenderer.iconWidth + |
| 262 |
HeaderRenderer.iconSpacing + |
| 263 |
2 * HeaderRenderer.buttonPadding; |
| 264 |
|
| 265 |
gc.fillStyle = CellRenderer.resolveOption(this.backgroundColor, config); |
| 266 |
gc.fillRect( |
| 267 |
config.x + config.width - backgroundSize, |
| 268 |
config.y + config.height - backgroundSize, |
| 269 |
backgroundSize, |
| 270 |
backgroundSize, |
| 271 |
); |
| 272 |
|
| 273 |
const iconStart = |
| 274 |
config.x + |
| 275 |
config.width - |
| 276 |
HeaderRenderer.iconWidth - |
| 277 |
HeaderRenderer.buttonPadding; |
| 278 |
|
| 279 |
// Draw filter icon |
| 280 |
this.drawFilterIcon(gc, config); |
| 281 |
// Sets filter icon to gray fill |
| 282 |
gc.fillStyle = Theme.getBorderColor(1); |
| 283 |
gc.fill(); |
| 284 |
|
| 285 |
// Check for transform metadata |
| 286 |
if (this.model) { |
| 287 |
// Get cell metadata |
| 288 |
const schemaIndex = this.model.getSchemaIndex( |
| 289 |
config.region, |
| 290 |
config.column, |
| 291 |
); |
| 292 |
|
| 293 |
const column = this.model.currentView.dataset.columns[schemaIndex]; |
| 294 |
|
| 295 |
const colMetaData: TransformStateManager.IColumn | undefined = |
| 296 |
this.model.transformMetadata(column); |
| 297 |
|
| 298 |
// Fill filter icon if filter applied |
| 299 |
if (colMetaData && colMetaData['filter']) { |
| 300 |
gc.fillStyle = Theme.getBrandColor(this._isLightTheme ? 8 : 6); |
| 301 |
gc.fill(); |
| 302 |
} |
| 303 |
|
| 304 |
// Fill sort icon if sort applied |
| 305 |
if (colMetaData && colMetaData['sort']) { |
| 306 |
// Display ascending or descending icon depending on order |
| 307 |
if (colMetaData['sort'].desc) { |
| 308 |
this.drawSortArrow(gc, config, iconStart, false); |
| 309 |
} else { |
| 310 |
this.drawSortArrow(gc, config, iconStart, true); |
| 311 |
} |
| 312 |
gc.fillStyle = Theme.getBrandColor(this._isLightTheme ? 7 : 5); |
| 313 |
gc.fill(); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Draw the filter icon for the cell |
| 320 |
* |
| 321 |
* @param gc - The graphics context to use for drawing. |
| 322 |
* |
| 323 |
* @param config - The configuration data for the cell. |
| 324 |
*/ |
| 325 |
drawFilterIcon(gc: GraphicsContext, config: CellRenderer.CellConfig): void { |
| 326 |
const filterIconStart = |
| 327 |
config.x + |
| 328 |
config.width - |
| 329 |
HeaderRenderer.iconWidth - |
| 330 |
HeaderRenderer.buttonPadding; |
| 331 |
|
| 332 |
const filterRightStemWidthX: number = HeaderRenderer.iconWidth / 2 + 1; |
| 333 |
const filterLeftStemWidthX: number = HeaderRenderer.iconWidth / 2 - 1; |
| 334 |
const filterTop: number = |
| 335 |
config.height - HeaderRenderer.iconHeight - 1 + config.y; |
| 336 |
|
| 337 |
gc.beginPath(); |
| 338 |
// Start drawing in top left of filter icon |
| 339 |
gc.moveTo(filterIconStart, filterTop); |
| 340 |
|
| 341 |
gc.lineTo(filterIconStart + HeaderRenderer.iconWidth, filterTop); |
| 342 |
// Y is the y value of the top of the stem |
| 343 |
gc.lineTo( |
| 344 |
filterIconStart + filterRightStemWidthX, |
| 345 |
config.y + config.height - HeaderRenderer.iconHeight + 2, |
| 346 |
); |
| 347 |
// Y is the y value of the bottom of the stem |
| 348 |
gc.lineTo( |
| 349 |
filterIconStart + filterRightStemWidthX, |
| 350 |
config.y + config.height - 1.5 * HeaderRenderer.buttonPadding, |
| 351 |
); |
| 352 |
gc.lineTo( |
| 353 |
filterIconStart + filterLeftStemWidthX, |
| 354 |
config.y + config.height - 2 * HeaderRenderer.buttonPadding, |
| 355 |
); |
| 356 |
gc.lineTo( |
| 357 |
filterIconStart + filterLeftStemWidthX, |
| 358 |
config.y + config.height - HeaderRenderer.iconHeight + 2, |
| 359 |
); |
| 360 |
gc.closePath(); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Draw the ascending and descending sort icons for the cell |
| 365 |
* |
| 366 |
* @param gc - The graphics context to use for drawing. |
| 367 |
* |
| 368 |
* @param config - The configuration data for the cell. |
| 369 |
* |
| 370 |
* @param filterIconStart - The bottom right corner of drawing area. |
| 371 |
* |
| 372 |
* @param asc - Indicates whether to draw ascending or descending icon. |
| 373 |
*/ |
| 374 |
drawSortArrow( |
| 375 |
gc: GraphicsContext, |
| 376 |
config: CellRenderer.CellConfig, |
| 377 |
filterIconStart: number, |
| 378 |
asc: boolean, |
| 379 |
): void { |
| 380 |
const arrowWidth = HeaderRenderer.iconWidth - 2; |
| 381 |
const sortIconStart = filterIconStart - HeaderRenderer.iconSpacing; |
| 382 |
const ascArrowRightStemWidth: number = sortIconStart - arrowWidth / 2 + 0.5; |
| 383 |
const descArrowRightStemWidth: number = |
| 384 |
sortIconStart - arrowWidth / 2 - 0.5; |
| 385 |
const arrowHeadSideY: number = |
| 386 |
config.height + |
| 387 |
config.y - |
| 388 |
HeaderRenderer.buttonPadding - |
| 389 |
HeaderRenderer.iconHeight + |
| 390 |
4; |
| 391 |
const arrowMiddle: number = sortIconStart - arrowWidth / 2; |
| 392 |
const ascArrowTipY: number = |
| 393 |
config.height + config.y - HeaderRenderer.iconHeight - 1; |
| 394 |
const ascArrowBottomY: number = |
| 395 |
config.height - 8 + config.y + HeaderRenderer.buttonPadding; |
| 396 |
|
| 397 |
gc.beginPath(); |
| 398 |
|
| 399 |
if (asc) { |
| 400 |
// Draw starting in middle of arrow |
| 401 |
// Y is the tip of the ascending arrow |
| 402 |
gc.moveTo(arrowMiddle, ascArrowTipY); |
| 403 |
gc.lineTo(sortIconStart, arrowHeadSideY); |
| 404 |
gc.lineTo(sortIconStart, arrowHeadSideY + 1); |
| 405 |
|
| 406 |
// Draw to middle of arrow |
| 407 |
gc.lineTo(ascArrowRightStemWidth, arrowHeadSideY + 1); |
| 408 |
|
| 409 |
// Y is the bottom of the arrow stem |
| 410 |
gc.lineTo(arrowMiddle + 0.5, ascArrowBottomY); |
| 411 |
gc.lineTo(arrowMiddle - 0.5, ascArrowBottomY); |
| 412 |
gc.lineTo(arrowMiddle - 0.5, arrowHeadSideY + 1); |
| 413 |
gc.lineTo(sortIconStart - arrowWidth, arrowHeadSideY + 1); |
| 414 |
gc.lineTo(sortIconStart - arrowWidth, arrowHeadSideY); |
| 415 |
} else { |
| 416 |
// Draw starting in middle of arrow |
| 417 |
// Y is the tip of the descending arrow |
| 418 |
gc.moveTo(arrowMiddle, ascArrowBottomY); |
| 419 |
gc.lineTo(sortIconStart - arrowWidth, arrowHeadSideY + 4.5); |
| 420 |
gc.lineTo(sortIconStart - arrowWidth, arrowHeadSideY + 3.5); |
| 421 |
|
| 422 |
// Draw to middle of arrow |
| 423 |
gc.lineTo(descArrowRightStemWidth, arrowHeadSideY + 3.5); |
| 424 |
|
| 425 |
// Y is the bottom of the arrow stem |
| 426 |
gc.lineTo(descArrowRightStemWidth, ascArrowTipY); |
| 427 |
gc.lineTo(arrowMiddle + 0.5, ascArrowTipY); |
| 428 |
|
| 429 |
// Draw left side of descending arrow |
| 430 |
gc.lineTo(arrowMiddle + 0.5, arrowHeadSideY + 3.5); |
| 431 |
gc.lineTo(sortIconStart, arrowHeadSideY + 3.5); |
| 432 |
gc.lineTo(sortIconStart, arrowHeadSideY + 4.5); |
| 433 |
} |
| 434 |
gc.closePath(); |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Indicates the size of the menu icon, to support the current implementation |
| 439 |
* of hit testing. |
| 440 |
*/ |
| 441 |
static buttonSize = 11; |
| 442 |
static iconHeight = 12; |
| 443 |
static iconWidth = 7; |
| 444 |
static buttonPadding = 3; |
| 445 |
static iconSpacing = 1.5; |
| 446 |
|
| 447 |
private _isLightTheme: boolean; |
| 448 |
private _grid: DataGrid; |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* The namespace for the `HeaderRenderer` class statics. |
| 453 |
*/ |
| 454 |
export namespace HeaderRenderer { |
| 455 |
/** |
| 456 |
* An options object for initializing a renderer. |
| 457 |
*/ |
| 458 |
export interface IOptions { |
| 459 |
/** |
| 460 |
* The data model this renderer should get metadata from. |
| 461 |
*/ |
| 462 |
textOptions: TextRenderer.IOptions; |
| 463 |
isLightTheme: boolean; |
| 464 |
grid: DataGrid; |
| 465 |
} |
| 466 |
} |