aboutsummaryrefslogtreecommitdiff
path: root/spike/settings/source/SpikeView.mc
blob: 68bacc7f1290cc93553505408ee356dd61f0ea54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Author:  cirakg & claude ai
// Summary: Scrollable text screen showing the spike's measurements.
//
// Lines come from SpikeReport. The first visible line moves with
// SpikeDelegate (UP/DOWN buttons or swipes); text stays in the middle band of
// round screens so nothing is cut off at the top or bottom.

import Toybox.Graphics;
import Toybox.Lang;
import Toybox.WatchUi;

class SpikeView extends WatchUi.View {

    // Share of the screen height left free at the top and at the bottom.
    private const MARGIN_FRACTION = 0.14;

    private var _report as SpikeReport;
    private var _firstLine as Number = 0;

    function initialize(report as SpikeReport) {
        View.initialize();
        _report = report;
    }

    // Move the first visible line by delta lines, staying within the text.
    function scroll(delta as Number) as Void {
        var last = _report.lines.size() - 1;
        _firstLine += delta;
        if (_firstLine > last) {
            _firstLine = last;
        }
        if (_firstLine < 0) {
            _firstLine = 0;
        }
        WatchUi.requestUpdate();
    }

    function onUpdate(dc as Graphics.Dc) as Void {
        dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK);
        dc.clear();
        var font = Graphics.FONT_XTINY;
        var lineHeight = dc.getFontHeight(font);
        var top = (dc.getHeight() * MARGIN_FRACTION).toNumber();
        var bottom = dc.getHeight() - top;
        var x = dc.getWidth() / 2;
        var y = top;
        var lines = _report.lines;
        for (var i = _firstLine; i < lines.size() && y + lineHeight <= bottom; i++) {
            dc.drawText(x, y, font, lines[i], Graphics.TEXT_JUSTIFY_CENTER);
            y += lineHeight;
        }
    }
}