Skip to content

Commit d4f414d

Browse files
committed
Initialize scale value based on viewport width
1 parent 5b295b7 commit d4f414d

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/cargo/core/compiler/timings.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,42 @@ let UNIT_COORDS = {};
2323
let REVERSE_UNIT_DEPS = {};
2424
let REVERSE_UNIT_RMETA_DEPS = {};
2525

26+
const MIN_GRAPH_WIDTH = 200;
27+
const MAX_GRAPH_WIDTH = 4096;
28+
29+
// How many pixels per second is added by each scale value
30+
const SCALE_PIXELS_PER_SEC = 8;
31+
32+
function scale_to_graph_width(scale) {
33+
// The scale corresponds to `SCALE_PIXELS_PER_SEC` pixels per seconds.
34+
// We thus multiply it by that, and the total duration, to get the graph width.
35+
const width = scale * SCALE_PIXELS_PER_SEC * DURATION;
36+
37+
// We then cap the size of the graph. It is hard to view if it is too large, and
38+
// browsers may not render a large graph because it takes too much memory.
39+
// 4096 is still ridiculously large, and probably won't render on mobile
40+
// browsers, but should be ok for many desktop environments.
41+
// Also use a minimum width of 200.
42+
return Math.max(MIN_GRAPH_WIDTH, Math.min(MAX_GRAPH_WIDTH, width));
43+
}
44+
45+
// This function performs the reverse of `scale_to_graph_width`.
46+
function width_to_graph_scale(width) {
47+
const maxWidth = Math.min(MAX_GRAPH_WIDTH, width);
48+
const minWidth = Math.max(MIN_GRAPH_WIDTH, width);
49+
50+
const trimmedWidth = Math.max(minWidth, Math.min(maxWidth, width));
51+
52+
const scale = Math.round(trimmedWidth / (DURATION * SCALE_PIXELS_PER_SEC));
53+
return Math.max(1, scale);
54+
}
55+
56+
// Init scale value and limits based on the client's window width and min/max graph width.
57+
const scaleElement = document.getElementById('scale');
58+
scaleElement.min = width_to_graph_scale(MIN_GRAPH_WIDTH);
59+
scaleElement.max = width_to_graph_scale(MAX_GRAPH_WIDTH);
60+
scaleElement.value = width_to_graph_scale(window.innerWidth * 0.75);
61+
2662
// Colors from css
2763
const getCssColor = name => getComputedStyle(document.body).getPropertyValue(name);
2864
const TEXT_COLOR = getCssColor('--text');
@@ -318,11 +354,7 @@ function setup_canvas(id, width, height) {
318354

319355
function draw_graph_axes(id, graph_height) {
320356
const scale = document.getElementById('scale').valueAsNumber;
321-
// Cap the size of the graph. It is hard to view if it is too large, and
322-
// browsers may not render a large graph because it takes too much memory.
323-
// 4096 is still ridiculously large, and probably won't render on mobile
324-
// browsers, but should be ok for many desktop environments.
325-
const graph_width = Math.min(scale * DURATION, 4096);
357+
const graph_width = scale_to_graph_width(scale);
326358
const px_per_sec = graph_width / DURATION;
327359
const canvas_width = Math.max(graph_width + X_LINE + 30, X_LINE + 250);
328360
const canvas_height = graph_height + MARGIN + Y_LINE;

src/cargo/core/compiler/timings.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,11 +776,18 @@ static HTML_CANVAS: &str = r#"
776776
<table class="input-table">
777777
<tr>
778778
<td><label for="min-unit-time">Min unit time:</label></td>
779-
<td><label for="scale">Scale:</label></td>
779+
<td title="Scale corresponds to a number of pixels per second. It is automatically initialized based on your viewport width.">
780+
<label for="scale">Scale:</label>
781+
</td>
780782
</tr>
781783
<tr>
782784
<td><input type="range" min="0" max="30" step="0.1" value="0" id="min-unit-time"></td>
783-
<td><input type="range" min="1" max="50" value="20" id="scale"></td>
785+
<!--
786+
The scale corresponds to some number of "pixels per second".
787+
Its min, max, and initial values are automatically set by JavaScript on page load,
788+
based on the client viewport.
789+
-->
790+
<td><input type="range" min="1" max="100" value="50" id="scale"></td>
784791
</tr>
785792
<tr>
786793
<td><output for="min-unit-time" id="min-unit-time-output"></output></td>

0 commit comments

Comments
 (0)