Skip to content

Commit

Permalink
Slight tweaking of UI code
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Aug 5, 2021
1 parent 3e0e80f commit 6e03d48
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,21 @@ export class ReactMeasuresView extends View {
}

// Render lane labels
const labelRect = {
origin: {
x: visibleArea.origin.x,
y: baseY,
},
size: {
width: visibleArea.size.width,
height: REACT_LANE_HEIGHT,
},
};
const label = this._profilerData.laneToLabelMap.get(lane);
if (label == null) {
console.warn(`Could not find label for lane ${lane}.`);
} else {
const labelRect = {
origin: {
x: visibleArea.origin.x,
y: baseY,
},
size: {
width: visibleArea.size.width,
height: REACT_LANE_HEIGHT,
},
};

drawText(label, context, labelRect, visibleArea, {
fillStyle: COLORS.TEXT_DIM_COLOR,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ import {COLORS, FONT_SIZE, TEXT_PADDING} from '../constants';

const cachedTextWidths = new Map();

export function getTextWidth(
context: CanvasRenderingContext2D,
text: string,
): number {
let measuredWidth = cachedTextWidths.get(text);
if (measuredWidth == null) {
measuredWidth = context.measureText(text).width;
cachedTextWidths.set(text, measuredWidth);
}

return ((measuredWidth: any): number);
}

export function trimText(
context: CanvasRenderingContext2D,
text: string,
Expand All @@ -22,13 +35,7 @@ export function trimText(
for (let i = text.length - 1; i >= 0; i--) {
const trimmedText = i === text.length - 1 ? text : text.substr(0, i) + '…';

let measuredWidth = cachedTextWidths.get(trimmedText);
if (measuredWidth == null) {
measuredWidth = context.measureText(trimmedText).width;
cachedTextWidths.set(trimmedText, measuredWidth);
}

if (measuredWidth <= width) {
if (getTextWidth(context, text) <= width) {
return trimmedText;
}
}
Expand Down

0 comments on commit 6e03d48

Please sign in to comment.