|
| 1 | +/// A traffic light in the app bar showing what the R process is doing. |
| 2 | +/// |
| 3 | +/// Time-stamp: "Sunday 2026-08-16 09:20:00 +1000 Graham Williams" |
| 4 | +/// |
| 5 | +/// Copyright (C) 2026, Togaware Pty Ltd. |
| 6 | +/// |
| 7 | +/// Licensed under the GNU General Public License, Version 3 (the "License"); |
| 8 | +/// |
| 9 | +/// License: https://opensource.org/license/gpl-3-0 |
| 10 | +/// |
| 11 | +// This program is free software: you can redistribute it and/or modify it under |
| 12 | +// the terms of the GNU General Public License as published by the Free Software |
| 13 | +// Foundation, either version 3 of the License, or (at your option) any later |
| 14 | +// version. |
| 15 | +// |
| 16 | +// This program is distributed in the hope that it will be useful, but WITHOUT |
| 17 | +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 18 | +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 19 | +// details. |
| 20 | +// |
| 21 | +// You should have received a copy of the GNU General Public License along with |
| 22 | +// this program. If not, see <https://opensource.org/license/gpl-3-0>. |
| 23 | +/// |
| 24 | +/// Authors: Graham Williams |
| 25 | +
|
| 26 | +library; |
| 27 | + |
| 28 | +import 'package:flutter/material.dart'; |
| 29 | + |
| 30 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 31 | +import 'package:markdown_tooltip/markdown_tooltip.dart'; |
| 32 | + |
| 33 | +import 'package:rattle/providers/r_status.dart'; |
| 34 | + |
| 35 | +/// Show whether R is idle, busy, or has stopped. |
| 36 | +/// |
| 37 | +/// Rattle hands our R code to an R session running alongside the app, and until |
| 38 | +/// R has finished with it the panels have nothing new to show. Without some |
| 39 | +/// indication of that, a slow model build is indistinguishable from an app that |
| 40 | +/// has stopped responding. The light reports what R is actually doing. |
| 41 | +
|
| 42 | +class RStatusLight extends ConsumerWidget { |
| 43 | + const RStatusLight({super.key}); |
| 44 | + |
| 45 | + @override |
| 46 | + Widget build(BuildContext context, WidgetRef ref) { |
| 47 | + final RStatus status = ref.watch(rStatusProvider); |
| 48 | + |
| 49 | + final (Color colour, String label, String advice) = switch (status) { |
| 50 | + RStatus.ready => ( |
| 51 | + Colors.green, |
| 52 | + 'Ready', |
| 53 | + 'R has finished and is waiting for you. Any output is on the panel ' |
| 54 | + 'behind, with the detail in the **Console** tab.', |
| 55 | + ), |
| 56 | + RStatus.running => ( |
| 57 | + Colors.amber, |
| 58 | + 'Running', |
| 59 | + 'R is running your code. A model over a large dataset can take a ' |
| 60 | + 'while, so the panels will not update until this turns green. ' |
| 61 | + 'Watch the **Console** tab to see how it is going.', |
| 62 | + ), |
| 63 | + RStatus.failed => ( |
| 64 | + Colors.red, |
| 65 | + 'Stopped', |
| 66 | + 'R reported an error, or the R session has stopped. Whatever you ' |
| 67 | + 'last asked for may be missing or incomplete. See the ' |
| 68 | + '**Console** tab for what R reported. The light turns green ' |
| 69 | + 'again once the next request succeeds.', |
| 70 | + ), |
| 71 | + }; |
| 72 | + |
| 73 | + return MarkdownTooltip( |
| 74 | + message: ''' |
| 75 | +
|
| 76 | + **R is $label** |
| 77 | +
|
| 78 | + $advice |
| 79 | +
|
| 80 | + ''', |
| 81 | + child: Padding( |
| 82 | + padding: const EdgeInsets.symmetric(horizontal: 12), |
| 83 | + child: Row( |
| 84 | + mainAxisSize: MainAxisSize.min, |
| 85 | + children: [ |
| 86 | + // A ring around the light so that it reads as a light rather than |
| 87 | + // as a coloured dot, and so it stays visible on any app bar colour. |
| 88 | + |
| 89 | + Container( |
| 90 | + width: 16, |
| 91 | + height: 16, |
| 92 | + decoration: BoxDecoration( |
| 93 | + color: colour, |
| 94 | + shape: BoxShape.circle, |
| 95 | + border: Border.all(color: Colors.black26), |
| 96 | + ), |
| 97 | + ), |
| 98 | + const SizedBox(width: 8), |
| 99 | + Text( |
| 100 | + label, |
| 101 | + style: const TextStyle(fontSize: 14), |
| 102 | + ), |
| 103 | + ], |
| 104 | + ), |
| 105 | + ), |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments