Skip to content

Fix low contrast on downloads graph labels and borders #8679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/components/download-graph.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<canvas
{{did-insert this.createChart}}
{{did-update this.updateChart @data}}
{{did-update this.updateColorScheme this.colorScheme.resolvedScheme}}
{{did-update this.updateStacked @stacked}}
{{will-destroy this.destroyChart}}
/>
Expand Down
36 changes: 34 additions & 2 deletions app/components/download-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@ const ONE_DAY = 24 * 60 * 60 * 1000;

export default class DownloadGraph extends Component {
@service chartjs;
@service colorScheme;

@action loadChartJs() {
waitForPromise(this.chartjs.loadTask.perform()).catch(() => {
// Ignore Promise rejections. We'll handle them through the derived state properties.
});
}

get fontColor() {
return this.colorScheme.isDark ? '#ADBABD' : '#666';
}

get borderColor() {
return this.colorScheme.isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)';
}

@action createChart(element) {
let Chart = this.chartjs.loadTask.lastSuccessful.value;

let { fontColor: color, borderColor } = this;

this.chart = new Chart(element, {
type: 'line',
data: this.data,
options: {
color,
maintainAspectRatio: false,
layout: {
padding: 10,
Expand All @@ -37,9 +49,15 @@ export default class DownloadGraph extends Component {
x: {
type: 'time',
time: { tooltipFormat: 'MMM d', unit: 'day' },
ticks: { maxTicksLimit: 13 },
ticks: { maxTicksLimit: 13, color },
grid: { color: borderColor },
},
y: {
beginAtZero: true,
stacked: true,
ticks: { precision: 0, color },
grid: { color: borderColor },
},
y: { beginAtZero: true, stacked: true, ticks: { precision: 0 } },
},
interaction: {
mode: 'index',
Expand All @@ -50,6 +68,20 @@ export default class DownloadGraph extends Component {
});
}

@action updateColorScheme() {
let { chart } = this;

if (chart) {
let { fontColor, borderColor } = this;
chart.options.color = fontColor;
chart.options.scales.x.ticks.color = fontColor;
chart.options.scales.x.grid.color = borderColor;
chart.options.scales.y.ticks.color = fontColor;
chart.options.scales.y.grid.color = borderColor;
chart.update();
}
}

@action updateChart() {
let { chart } = this;

Expand Down
39 changes: 39 additions & 0 deletions app/services/color-scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { action } from '@ember/object';
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';

import { restartableTask, waitForEvent } from 'ember-concurrency';

import * as localStorage from '../utils/local-storage';

const DEFAULT_SCHEME = 'light';
Expand All @@ -10,6 +12,16 @@ const LS_KEY = 'color-scheme';

export default class DesignService extends Service {
@tracked _scheme = localStorage.getItem(LS_KEY);
@tracked resolvedScheme;

constructor() {
super(...arguments);
this.restartWatcherTask();
}

get isDark() {
return this.resolvedScheme === 'dark';
}

get scheme() {
return VALID_SCHEMES.has(this._scheme) ? this._scheme : DEFAULT_SCHEME;
Expand All @@ -18,5 +30,32 @@ export default class DesignService extends Service {
@action set(scheme) {
this._scheme = scheme;
localStorage.setItem(LS_KEY, scheme);
this.restartWatcherTask();
}

restartWatcherTask() {
this.watcherTask.perform().catch(() => {
// Ignore Promise rejections. This shouldn't be able to fail, and task cancellations are expected.
});
}

/**
* This task watches for changes in the system color scheme and updates the `resolvedScheme` property accordingly.
*/
watcherTask = restartableTask(async () => {
let mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');
// eslint-disable-next-line no-constant-condition
while (true) {
let scheme = this.scheme;
if (scheme === 'system') {
scheme = mediaQueryList.matches ? 'dark' : 'light';
}

if (this.resolvedScheme !== scheme) {
this.resolvedScheme = scheme;
}

await waitForEvent(mediaQueryList, 'change');
}
});
}