c849f09ccbe278014bf8c1830ef786c14a87a03047d517ef91171480a1abfe7fc855ae6d788a985beefbfe762f2e949eb8f37cf4eed4c626f98bf859471172 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export class ColorZone {
  6. constructor(from, to, colorId) {
  7. this._colorZoneBrand = undefined;
  8. this.from = from | 0;
  9. this.to = to | 0;
  10. this.colorId = colorId | 0;
  11. }
  12. static compare(a, b) {
  13. if (a.colorId === b.colorId) {
  14. if (a.from === b.from) {
  15. return a.to - b.to;
  16. }
  17. return a.from - b.from;
  18. }
  19. return a.colorId - b.colorId;
  20. }
  21. }
  22. /**
  23. * A zone in the overview ruler
  24. */
  25. export class OverviewRulerZone {
  26. constructor(startLineNumber, endLineNumber, heightInLines, color) {
  27. this._overviewRulerZoneBrand = undefined;
  28. this.startLineNumber = startLineNumber;
  29. this.endLineNumber = endLineNumber;
  30. this.heightInLines = heightInLines;
  31. this.color = color;
  32. this._colorZone = null;
  33. }
  34. static compare(a, b) {
  35. if (a.color === b.color) {
  36. if (a.startLineNumber === b.startLineNumber) {
  37. if (a.heightInLines === b.heightInLines) {
  38. return a.endLineNumber - b.endLineNumber;
  39. }
  40. return a.heightInLines - b.heightInLines;
  41. }
  42. return a.startLineNumber - b.startLineNumber;
  43. }
  44. return a.color < b.color ? -1 : 1;
  45. }
  46. setColorZone(colorZone) {
  47. this._colorZone = colorZone;
  48. }
  49. getColorZones() {
  50. return this._colorZone;
  51. }
  52. }
  53. export class OverviewZoneManager {
  54. constructor(getVerticalOffsetForLine) {
  55. this._getVerticalOffsetForLine = getVerticalOffsetForLine;
  56. this._zones = [];
  57. this._colorZonesInvalid = false;
  58. this._lineHeight = 0;
  59. this._domWidth = 0;
  60. this._domHeight = 0;
  61. this._outerHeight = 0;
  62. this._pixelRatio = 1;
  63. this._lastAssignedId = 0;
  64. this._color2Id = Object.create(null);
  65. this._id2Color = [];
  66. }
  67. getId2Color() {
  68. return this._id2Color;
  69. }
  70. setZones(newZones) {
  71. this._zones = newZones;
  72. this._zones.sort(OverviewRulerZone.compare);
  73. }
  74. setLineHeight(lineHeight) {
  75. if (this._lineHeight === lineHeight) {
  76. return false;
  77. }
  78. this._lineHeight = lineHeight;
  79. this._colorZonesInvalid = true;
  80. return true;
  81. }
  82. setPixelRatio(pixelRatio) {
  83. this._pixelRatio = pixelRatio;
  84. this._colorZonesInvalid = true;
  85. }
  86. getDOMWidth() {
  87. return this._domWidth;
  88. }
  89. getCanvasWidth() {
  90. return this._domWidth * this._pixelRatio;
  91. }
  92. setDOMWidth(width) {
  93. if (this._domWidth === width) {
  94. return false;
  95. }
  96. this._domWidth = width;
  97. this._colorZonesInvalid = true;
  98. return true;
  99. }
  100. getDOMHeight() {
  101. return this._domHeight;
  102. }
  103. getCanvasHeight() {
  104. return this._domHeight * this._pixelRatio;
  105. }
  106. setDOMHeight(height) {
  107. if (this._domHeight === height) {
  108. return false;
  109. }
  110. this._domHeight = height;
  111. this._colorZonesInvalid = true;
  112. return true;
  113. }
  114. getOuterHeight() {
  115. return this._outerHeight;
  116. }
  117. setOuterHeight(outerHeight) {
  118. if (this._outerHeight === outerHeight) {
  119. return false;
  120. }
  121. this._outerHeight = outerHeight;
  122. this._colorZonesInvalid = true;
  123. return true;
  124. }
  125. resolveColorZones() {
  126. const colorZonesInvalid = this._colorZonesInvalid;
  127. const lineHeight = Math.floor(this._lineHeight);
  128. const totalHeight = Math.floor(this.getCanvasHeight());
  129. const outerHeight = Math.floor(this._outerHeight);
  130. const heightRatio = totalHeight / outerHeight;
  131. const halfMinimumHeight = Math.floor(4 /* Constants.MINIMUM_HEIGHT */ * this._pixelRatio / 2);
  132. const allColorZones = [];
  133. for (let i = 0, len = this._zones.length; i < len; i++) {
  134. const zone = this._zones[i];
  135. if (!colorZonesInvalid) {
  136. const colorZone = zone.getColorZones();
  137. if (colorZone) {
  138. allColorZones.push(colorZone);
  139. continue;
  140. }
  141. }
  142. const offset1 = this._getVerticalOffsetForLine(zone.startLineNumber);
  143. const offset2 = (zone.heightInLines === 0
  144. ? this._getVerticalOffsetForLine(zone.endLineNumber) + lineHeight
  145. : offset1 + zone.heightInLines * lineHeight);
  146. const y1 = Math.floor(heightRatio * offset1);
  147. const y2 = Math.floor(heightRatio * offset2);
  148. let ycenter = Math.floor((y1 + y2) / 2);
  149. let halfHeight = (y2 - ycenter);
  150. if (halfHeight < halfMinimumHeight) {
  151. halfHeight = halfMinimumHeight;
  152. }
  153. if (ycenter - halfHeight < 0) {
  154. ycenter = halfHeight;
  155. }
  156. if (ycenter + halfHeight > totalHeight) {
  157. ycenter = totalHeight - halfHeight;
  158. }
  159. const color = zone.color;
  160. let colorId = this._color2Id[color];
  161. if (!colorId) {
  162. colorId = (++this._lastAssignedId);
  163. this._color2Id[color] = colorId;
  164. this._id2Color[colorId] = color;
  165. }
  166. const colorZone = new ColorZone(ycenter - halfHeight, ycenter + halfHeight, colorId);
  167. zone.setColorZone(colorZone);
  168. allColorZones.push(colorZone);
  169. }
  170. this._colorZonesInvalid = false;
  171. allColorZones.sort(ColorZone.compare);
  172. return allColorZones;
  173. }
  174. }