Skip to content

Commit

Permalink
fix: safely use default values when theme in undefined (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
sehilyi committed Aug 4, 2021
1 parent 6303398 commit afc4c6a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/core/gosling-to-higlass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { viridisColorMap } from './utils/colors';
import { IsDataDeep, IsChannelDeep, IsDataDeepTileset } from './gosling.schema.guards';
import { DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM } from './layout/defaults';
import { CompleteThemeDeep } from './utils/theme';
import { DEFAULT_TEXT_STYLE } from './utils/text-style';

/**
* Convert a gosling track into a HiGlass view and add it into a higlass model.
Expand Down Expand Up @@ -168,27 +169,28 @@ export function goslingToHiGlass(
if (typeof firstResolvedSpec.title === 'string') {
hgModel.setTextTrack(
bb.width,
theme.root.titleFontSize + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM,
(theme.root.titleFontSize ?? 18) + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM,
firstResolvedSpec.title,
theme.root.titleColor,
theme.root.titleFontSize ?? 18,
theme.root.titleFontWeight,
theme.root.titleAlign,
theme.root.titleBackgroundColor,
theme.root.titleFontFamily
theme.root.titleFontFamily ?? DEFAULT_TEXT_STYLE.fontFamily
);
}
if (typeof firstResolvedSpec.subtitle === 'string') {
hgModel.setTextTrack(
bb.width,
theme.root.subtitleFontSize + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM,
// TODO: better way to safely get the value when undefined?
(theme.root.subtitleFontSize ?? 14) + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM,
firstResolvedSpec.subtitle,
theme.root.subtitleColor,
theme.root.subtitleFontSize ?? 14,
theme.root.subtitleFontWeight,
theme.root.subtitleAlign,
theme.root.subtitleBackgroundColor,
theme.root.subtitleFontFamily
theme.root.subtitleFontFamily ?? DEFAULT_TEXT_STYLE.fontFamily
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/mark/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function drawBackground(
// refer to https:/higlass/higlass/blob/f82c0a4f7b2ab1c145091166b0457638934b15f3/app/scripts/PixiTrack.js#L129
const g = trackInfo.pBackground;

if (tm.spec().style?.background || theme.track.background !== 'transparent') {
if (tm.spec().style?.background || (theme.track.background && theme.track.background !== 'transparent')) {
g.clear();

const bg = tm.spec().style?.background ?? theme.track.background;
Expand All @@ -34,7 +34,7 @@ export function drawBackground(
g.drawRect(l, t, w, h);
}

if (theme.track.alternatingBackground !== 'transparent') {
if (theme.track.alternatingBackground && theme.track.alternatingBackground !== 'transparent') {
const spec = tm.spec();

if (!IsChannelDeep(spec.row) || spec.row.type !== 'nominal') {
Expand Down
3 changes: 2 additions & 1 deletion src/core/mark/outline-circular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export function drawCircularOutlines(
);
g.beginFill(
colorToHex(tm.spec().style?.background ?? theme.track.background),
tm.spec().style?.backgroundOpacity ?? (theme.track.background === 'transparent' ? 0 : 1)
tm.spec().style?.backgroundOpacity ??
(!theme.track.background || theme.track.background === 'transparent' ? 0 : 1)
);
g.moveTo(posStartInner.x, posStartInner.y);
g.arc(cx, cy, trackInnerRadius, startRad, endRad, true);
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils/bounding-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export function getRelativeTrackInfo(spec: GoslingSpec, theme: CompleteThemeDeep
if (spec.title || spec.subtitle) {
// If title and/or subtitle presents, offset the y position by title/subtitle size
const titleHeight =
(spec.title ? theme.root.titleFontSize + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM : 0) +
(spec.subtitle ? theme.root.subtitleFontSize + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM : 0);
(spec.title ? (theme.root.titleFontSize ?? 18) + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM : 0) +
(spec.subtitle ? (theme.root.subtitleFontSize ?? 14) + DEWFAULT_TITLE_PADDING_ON_TOP_AND_BOTTOM : 0);
const marginBottom = 4;

size.height += titleHeight + marginBottom;
Expand Down

0 comments on commit afc4c6a

Please sign in to comment.