buildRuler method

Widget buildRuler()

Implementation

Widget buildRuler() {
  return SizedBox(
    height: 1,
    child: LayoutBuilder(
      builder: (context, constraints) {
        final width = constraints.maxWidth;
        final borderStyle = const Style(foreground: Color(120, 120, 120));
        final tickStyle = const Style(foreground: Color(180, 180, 180));
        if (width <= 2) {
          return Row([
            Text('│', style: borderStyle),
            Expanded(child: const SizedBox()),
            Text('│', style: borderStyle),
          ]);
        }

        final tickCount = 6;
        final segmentsWidth = width - 2;
        final children = <Widget>[Text('│', style: borderStyle)];

        for (var i = 0; i < tickCount; i++) {
          final (col, align) = switch (i) {
            0 => (0, TextAlign.left),
            _ when i == tickCount - 1 => (segmentsWidth, TextAlign.right),
            _ => (
              ((i + 0.5) * segmentsWidth / tickCount).round(),
              TextAlign.center,
            ),
          };

          final tickTimeMs = (offsetX + col * zoomLevel) / 1000.0;
          final label = switch (timeDisplayMode) {
            TimeDisplayMode.formatted => formatDuration(tickTimeMs),
            TimeDisplayMode.rawRelative => '${(tickTimeMs * 1000).round()}',
            TimeDisplayMode.rawAbsolute =>
              '${(tickTimeMs * 1000).round() + (baseTime ?? 0)}',
          };

          children.add(
            Expanded(
              child: Text(label, style: tickStyle, textAlign: align),
            ),
          );
        }

        children.add(Text('│', style: borderStyle));

        return Row(children);
      },
    ),
  );
}