terminal_painters.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 'cache.dart';
  10. import 'char_size.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. final searchResult = terminal.searchHits;
  102. for (var row = 0; row < lines.length; row++) {
  103. final line = lines[row];
  104. final offsetY = row * charSize.cellHeight;
  105. // final cellCount = math.min(terminal.viewWidth, line.length);
  106. final cellCount = terminal.terminalWidth;
  107. for (var col = 0; col < cellCount; col++) {
  108. final width = line.cellGetWidth(col);
  109. if (width == 0) {
  110. continue;
  111. }
  112. final offsetX = col * charSize.cellWidth;
  113. final absoluteY = terminal.convertViewLineToRawLine(row) -
  114. terminal.scrollOffsetFromBottom;
  115. _paintCell(canvas, line, col, offsetX, offsetY,
  116. searchResult.contains(absoluteY, col));
  117. }
  118. }
  119. }
  120. void _paintCell(
  121. Canvas canvas,
  122. BufferLine line,
  123. int cell,
  124. double offsetX,
  125. double offsetY,
  126. bool isInSearchResult,
  127. ) {
  128. final codePoint = line.cellGetContent(cell);
  129. var fgColor = line.cellGetFgColor(cell);
  130. final bgColor = line.cellGetBgColor(cell);
  131. final flags = line.cellGetFlags(cell);
  132. if (codePoint == 0 || flags.hasFlag(CellFlags.invisible)) {
  133. return;
  134. }
  135. if (isInSearchResult) {
  136. fgColor = Color.fromARGB(255, 255, 0, 0).value;
  137. }
  138. // final cellHash = line.cellGetHash(cell);
  139. final cellHash = hashValues(codePoint, fgColor, bgColor, flags);
  140. var character = textLayoutCache.getLayoutFromCache(cellHash);
  141. if (character != null) {
  142. canvas.drawParagraph(character, Offset(offsetX, offsetY));
  143. return;
  144. }
  145. final cellColor = flags.hasFlag(CellFlags.inverse) ? bgColor : fgColor;
  146. var color = Color(cellColor);
  147. if (flags & CellFlags.faint != 0) {
  148. color = color.withOpacity(0.5);
  149. }
  150. final styleToUse = PaintHelper.getStyleToUse(
  151. style,
  152. color,
  153. bold: flags.hasFlag(CellFlags.bold),
  154. italic: flags.hasFlag(CellFlags.italic),
  155. underline: flags.hasFlag(CellFlags.underline),
  156. );
  157. character = textLayoutCache.performAndCacheLayout(
  158. String.fromCharCode(codePoint), styleToUse, cellHash);
  159. canvas.drawParagraph(character, Offset(offsetX, offsetY));
  160. }
  161. @override
  162. bool shouldRepaint(CustomPainter oldDelegate) {
  163. /// paint only when the terminal has changed since last paint.
  164. return terminal.dirty;
  165. }
  166. }
  167. class CursorPainter extends CustomPainter {
  168. final bool visible;
  169. final CellSize charSize;
  170. final bool focused;
  171. final bool blinkVisible;
  172. final int cursorColor;
  173. final int textColor;
  174. final String composingString;
  175. final TextLayoutCache textLayoutCache;
  176. final TerminalStyle style;
  177. CursorPainter({
  178. required this.visible,
  179. required this.charSize,
  180. required this.focused,
  181. required this.blinkVisible,
  182. required this.cursorColor,
  183. required this.textColor,
  184. required this.composingString,
  185. required this.textLayoutCache,
  186. required this.style,
  187. });
  188. @override
  189. void paint(Canvas canvas, Size size) {
  190. bool isVisible =
  191. visible && (blinkVisible || composingString != '' || !focused);
  192. if (isVisible) {
  193. _paintCursor(canvas);
  194. }
  195. }
  196. @override
  197. bool shouldRepaint(covariant CustomPainter oldDelegate) {
  198. if (oldDelegate is CursorPainter) {
  199. return blinkVisible != oldDelegate.blinkVisible ||
  200. focused != oldDelegate.focused ||
  201. visible != oldDelegate.visible ||
  202. charSize.cellWidth != oldDelegate.charSize.cellWidth ||
  203. charSize.cellHeight != oldDelegate.charSize.cellHeight ||
  204. composingString != oldDelegate.composingString;
  205. }
  206. return true;
  207. }
  208. void _paintCursor(Canvas canvas) {
  209. final paint = Paint()
  210. ..color = Color(cursorColor)
  211. ..strokeWidth = focused ? 0.0 : 1.0
  212. ..style = focused ? PaintingStyle.fill : PaintingStyle.stroke;
  213. canvas.drawRect(
  214. Rect.fromLTWH(0, 0, charSize.cellWidth, charSize.cellHeight), paint);
  215. if (composingString != '') {
  216. final styleToUse = PaintHelper.getStyleToUse(style, Color(textColor));
  217. final character = textLayoutCache.performAndCacheLayout(
  218. composingString, styleToUse, null);
  219. canvas.drawParagraph(character, Offset(0, 0));
  220. }
  221. }
  222. }
  223. class PaintHelper {
  224. static TextStyle getStyleToUse(
  225. TerminalStyle style,
  226. Color color, {
  227. bool bold = false,
  228. bool italic = false,
  229. bool underline = false,
  230. }) {
  231. return (style.textStyleProvider != null)
  232. ? style.textStyleProvider!(
  233. color: color,
  234. fontSize: style.fontSize,
  235. fontWeight: bold && !style.ignoreBoldFlag
  236. ? FontWeight.bold
  237. : FontWeight.normal,
  238. fontStyle: italic ? FontStyle.italic : FontStyle.normal,
  239. decoration:
  240. underline ? TextDecoration.underline : TextDecoration.none,
  241. )
  242. : TextStyle(
  243. color: color,
  244. fontSize: style.fontSize,
  245. fontWeight: bold && !style.ignoreBoldFlag
  246. ? FontWeight.bold
  247. : FontWeight.normal,
  248. fontStyle: italic ? FontStyle.italic : FontStyle.normal,
  249. decoration:
  250. underline ? TextDecoration.underline : TextDecoration.none,
  251. fontFamily: 'monospace',
  252. fontFamilyFallback: style.fontFamily,
  253. );
  254. }
  255. }