terminal_painters.dart 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:xterm/buffer/cell_flags.dart';
  4. import 'package:xterm/buffer/line/line.dart';
  5. import 'package:xterm/mouse/position.dart';
  6. import 'package:xterm/terminal/terminal_ui_interaction.dart';
  7. import 'package:xterm/theme/terminal_style.dart';
  8. import 'package:xterm/util/bit_flags.dart';
  9. import 'char_size.dart';
  10. import 'cache.dart';
  11. class TerminalPainter extends CustomPainter {
  12. TerminalPainter({
  13. required this.terminal,
  14. required this.style,
  15. required this.charSize,
  16. required this.textLayoutCache,
  17. });
  18. final TerminalUiInteraction terminal;
  19. final TerminalStyle style;
  20. final CellSize charSize;
  21. final TextLayoutCache textLayoutCache;
  22. @override
  23. void paint(Canvas canvas, Size size) {
  24. _paintBackground(canvas);
  25. // if (oscillator.value) {
  26. // }
  27. _paintText(canvas);
  28. _paintSelection(canvas);
  29. }
  30. void _paintBackground(Canvas canvas) {
  31. final lines = terminal.getVisibleLines();
  32. for (var row = 0; row < lines.length; row++) {
  33. final line = lines[row];
  34. final offsetY = row * charSize.cellHeight;
  35. // final cellCount = math.min(terminal.viewWidth, line.length);
  36. final cellCount = terminal.terminalWidth;
  37. for (var col = 0; col < cellCount; col++) {
  38. final cellWidth = line.cellGetWidth(col);
  39. if (cellWidth == 0) {
  40. continue;
  41. }
  42. final cellFgColor = line.cellGetFgColor(col);
  43. final cellBgColor = line.cellGetBgColor(col);
  44. final effectBgColor = line.cellHasFlag(col, CellFlags.inverse)
  45. ? cellFgColor
  46. : cellBgColor;
  47. if (effectBgColor == 0x00) {
  48. continue;
  49. }
  50. // when a program reports black as background then it "really" means transparent
  51. if (effectBgColor == 0xFF000000) {
  52. continue;
  53. }
  54. // final cellFlags = line.cellGetFlags(i);
  55. // final cell = line.getCell(i);
  56. // final attr = cell.attr;
  57. final offsetX = col * charSize.cellWidth;
  58. final effectWidth = charSize.cellWidth * cellWidth + 1;
  59. final effectHeight = charSize.cellHeight + 1;
  60. // background color is already painted with opacity by the Container of
  61. // TerminalPainter so wo don't need to fallback to
  62. // terminal.theme.background here.
  63. final paint = Paint()..color = Color(effectBgColor);
  64. canvas.drawRect(
  65. Rect.fromLTWH(offsetX, offsetY, effectWidth, effectHeight),
  66. paint,
  67. );
  68. }
  69. }
  70. }
  71. void _paintSelection(Canvas canvas) {
  72. final paint = Paint()..color = Colors.white.withOpacity(0.3);
  73. for (var y = 0; y < terminal.terminalHeight; y++) {
  74. final offsetY = y * charSize.cellHeight;
  75. final absoluteY = terminal.convertViewLineToRawLine(y) -
  76. terminal.scrollOffsetFromBottom;
  77. for (var x = 0; x < terminal.terminalWidth; x++) {
  78. var cellCount = 0;
  79. while (
  80. (terminal.selection?.contains(Position(x + cellCount, absoluteY)) ??
  81. false) &&
  82. x + cellCount < terminal.terminalWidth) {
  83. cellCount++;
  84. }
  85. if (cellCount == 0) {
  86. continue;
  87. }
  88. final offsetX = x * charSize.cellWidth;
  89. final effectWidth = cellCount * charSize.cellWidth;
  90. final effectHeight = charSize.cellHeight;
  91. canvas.drawRect(
  92. Rect.fromLTWH(offsetX, offsetY, effectWidth, effectHeight),
  93. paint,
  94. );
  95. x += cellCount;
  96. }
  97. }
  98. }
  99. void _paintText(Canvas canvas) {
  100. final lines = terminal.getVisibleLines();
  101. for (var row = 0; row < lines.length; row++) {
  102. final line = lines[row];
  103. final offsetY = row * charSize.cellHeight;
  104. // final cellCount = math.min(terminal.viewWidth, line.length);
  105. final cellCount = terminal.terminalWidth;
  106. for (var col = 0; col < cellCount; col++) {
  107. final width = line.cellGetWidth(col);
  108. if (width == 0) {
  109. continue;
  110. }
  111. final offsetX = col * charSize.cellWidth;
  112. _paintCell(canvas, line, col, offsetX, offsetY);
  113. }
  114. }
  115. }
  116. void _paintCell(
  117. Canvas canvas,
  118. BufferLine line,
  119. int cell,
  120. double offsetX,
  121. double offsetY,
  122. ) {
  123. final codePoint = line.cellGetContent(cell);
  124. final fgColor = line.cellGetFgColor(cell);
  125. final bgColor = line.cellGetBgColor(cell);
  126. final flags = line.cellGetFlags(cell);
  127. if (codePoint == 0 || flags.hasFlag(CellFlags.invisible)) {
  128. return;
  129. }
  130. // final cellHash = line.cellGetHash(cell);
  131. final cellHash = hashValues(codePoint, fgColor, bgColor, flags);
  132. var character = textLayoutCache.getLayoutFromCache(cellHash);
  133. if (character != null) {
  134. canvas.drawParagraph(character, Offset(offsetX, offsetY));
  135. return;
  136. }
  137. final cellColor = flags.hasFlag(CellFlags.inverse) ? bgColor : fgColor;
  138. var color = Color(cellColor);
  139. if (flags & CellFlags.faint != 0) {
  140. color = color.withOpacity(0.5);
  141. }
  142. final styleToUse = PaintHelper.getStyleToUse(
  143. style,
  144. color,
  145. bold: flags.hasFlag(CellFlags.bold),
  146. italic: flags.hasFlag(CellFlags.italic),
  147. underline: flags.hasFlag(CellFlags.underline),
  148. );
  149. character = textLayoutCache.performAndCacheLayout(
  150. String.fromCharCode(codePoint), styleToUse, cellHash);
  151. canvas.drawParagraph(character, Offset(offsetX, offsetY));
  152. }
  153. @override
  154. bool shouldRepaint(CustomPainter oldDelegate) {
  155. /// paint only when the terminal has changed since last paint.
  156. return terminal.dirty;
  157. }
  158. }
  159. class CursorPainter extends CustomPainter {
  160. final bool visible;
  161. final CellSize charSize;
  162. final bool focused;
  163. final bool blinkVisible;
  164. final int cursorColor;
  165. final int textColor;
  166. final String composingString;
  167. final TextLayoutCache textLayoutCache;
  168. final TerminalStyle style;
  169. CursorPainter({
  170. required this.visible,
  171. required this.charSize,
  172. required this.focused,
  173. required this.blinkVisible,
  174. required this.cursorColor,
  175. required this.textColor,
  176. required this.composingString,
  177. required this.textLayoutCache,
  178. required this.style,
  179. });
  180. @override
  181. void paint(Canvas canvas, Size size) {
  182. bool isVisible = visible && (blinkVisible || composingString != '');
  183. if (isVisible) {
  184. _paintCursor(canvas);
  185. }
  186. }
  187. @override
  188. bool shouldRepaint(covariant CustomPainter oldDelegate) {
  189. if (oldDelegate is CursorPainter) {
  190. return blinkVisible != oldDelegate.blinkVisible ||
  191. focused != oldDelegate.focused ||
  192. visible != oldDelegate.visible ||
  193. charSize.cellWidth != oldDelegate.charSize.cellWidth ||
  194. charSize.cellHeight != oldDelegate.charSize.cellHeight ||
  195. composingString != oldDelegate.composingString;
  196. }
  197. return true;
  198. }
  199. void _paintCursor(Canvas canvas) {
  200. final paint = Paint()
  201. ..color = Color(cursorColor)
  202. ..strokeWidth = focused ? 0.0 : 1.0
  203. ..style = focused ? PaintingStyle.fill : PaintingStyle.stroke;
  204. canvas.drawRect(
  205. Rect.fromLTWH(0, 0, charSize.cellWidth, charSize.cellHeight), paint);
  206. if (composingString != '') {
  207. final styleToUse = PaintHelper.getStyleToUse(style, Color(textColor));
  208. final character = textLayoutCache.performAndCacheLayout(
  209. composingString, styleToUse, null);
  210. canvas.drawParagraph(character, Offset(0, 0));
  211. }
  212. }
  213. }
  214. class PaintHelper {
  215. static TextStyle getStyleToUse(
  216. TerminalStyle style,
  217. Color color, {
  218. bool bold = false,
  219. bool italic = false,
  220. bool underline = false,
  221. }) {
  222. return (style.textStyleProvider != null)
  223. ? style.textStyleProvider!(
  224. color: color,
  225. fontSize: style.fontSize,
  226. fontWeight: bold ? FontWeight.bold : FontWeight.normal,
  227. fontStyle: italic ? FontStyle.italic : FontStyle.normal,
  228. decoration:
  229. underline ? TextDecoration.underline : TextDecoration.none,
  230. )
  231. : TextStyle(
  232. color: color,
  233. fontSize: style.fontSize,
  234. fontWeight: bold ? FontWeight.bold : FontWeight.normal,
  235. fontStyle: italic ? FontStyle.italic : FontStyle.normal,
  236. decoration:
  237. underline ? TextDecoration.underline : TextDecoration.none,
  238. fontFamily: 'monospace',
  239. fontFamilyFallback: style.fontFamily,
  240. );
  241. }
  242. }