123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- export class ColorZone {
- constructor(from, to, colorId) {
- this._colorZoneBrand = undefined;
- this.from = from | 0;
- this.to = to | 0;
- this.colorId = colorId | 0;
- }
- static compare(a, b) {
- if (a.colorId === b.colorId) {
- if (a.from === b.from) {
- return a.to - b.to;
- }
- return a.from - b.from;
- }
- return a.colorId - b.colorId;
- }
- }
- /**
- * A zone in the overview ruler
- */
- export class OverviewRulerZone {
- constructor(startLineNumber, endLineNumber, color) {
- this._overviewRulerZoneBrand = undefined;
- this.startLineNumber = startLineNumber;
- this.endLineNumber = endLineNumber;
- this.color = color;
- this._colorZone = null;
- }
- static compare(a, b) {
- if (a.color === b.color) {
- if (a.startLineNumber === b.startLineNumber) {
- return a.endLineNumber - b.endLineNumber;
- }
- return a.startLineNumber - b.startLineNumber;
- }
- return a.color < b.color ? -1 : 1;
- }
- setColorZone(colorZone) {
- this._colorZone = colorZone;
- }
- getColorZones() {
- return this._colorZone;
- }
- }
- export class OverviewZoneManager {
- constructor(getVerticalOffsetForLine) {
- this._getVerticalOffsetForLine = getVerticalOffsetForLine;
- this._zones = [];
- this._colorZonesInvalid = false;
- this._lineHeight = 0;
- this._domWidth = 0;
- this._domHeight = 0;
- this._outerHeight = 0;
- this._pixelRatio = 1;
- this._lastAssignedId = 0;
- this._color2Id = Object.create(null);
- this._id2Color = [];
- }
- getId2Color() {
- return this._id2Color;
- }
- setZones(newZones) {
- this._zones = newZones;
- this._zones.sort(OverviewRulerZone.compare);
- }
- setLineHeight(lineHeight) {
- if (this._lineHeight === lineHeight) {
- return false;
- }
- this._lineHeight = lineHeight;
- this._colorZonesInvalid = true;
- return true;
- }
- setPixelRatio(pixelRatio) {
- this._pixelRatio = pixelRatio;
- this._colorZonesInvalid = true;
- }
- getDOMWidth() {
- return this._domWidth;
- }
- getCanvasWidth() {
- return this._domWidth * this._pixelRatio;
- }
- setDOMWidth(width) {
- if (this._domWidth === width) {
- return false;
- }
- this._domWidth = width;
- this._colorZonesInvalid = true;
- return true;
- }
- getDOMHeight() {
- return this._domHeight;
- }
- getCanvasHeight() {
- return this._domHeight * this._pixelRatio;
- }
- setDOMHeight(height) {
- if (this._domHeight === height) {
- return false;
- }
- this._domHeight = height;
- this._colorZonesInvalid = true;
- return true;
- }
- getOuterHeight() {
- return this._outerHeight;
- }
- setOuterHeight(outerHeight) {
- if (this._outerHeight === outerHeight) {
- return false;
- }
- this._outerHeight = outerHeight;
- this._colorZonesInvalid = true;
- return true;
- }
- resolveColorZones() {
- const colorZonesInvalid = this._colorZonesInvalid;
- const lineHeight = Math.floor(this._lineHeight);
- const totalHeight = Math.floor(this.getCanvasHeight());
- const outerHeight = Math.floor(this._outerHeight);
- const heightRatio = totalHeight / outerHeight;
- const halfMinimumHeight = Math.floor(4 /* MINIMUM_HEIGHT */ * this._pixelRatio / 2);
- let allColorZones = [];
- for (let i = 0, len = this._zones.length; i < len; i++) {
- const zone = this._zones[i];
- if (!colorZonesInvalid) {
- const colorZone = zone.getColorZones();
- if (colorZone) {
- allColorZones.push(colorZone);
- continue;
- }
- }
- const y1 = Math.floor(heightRatio * (this._getVerticalOffsetForLine(zone.startLineNumber)));
- const y2 = Math.floor(heightRatio * (this._getVerticalOffsetForLine(zone.endLineNumber) + lineHeight));
- let ycenter = Math.floor((y1 + y2) / 2);
- let halfHeight = (y2 - ycenter);
- if (halfHeight < halfMinimumHeight) {
- halfHeight = halfMinimumHeight;
- }
- if (ycenter - halfHeight < 0) {
- ycenter = halfHeight;
- }
- if (ycenter + halfHeight > totalHeight) {
- ycenter = totalHeight - halfHeight;
- }
- const color = zone.color;
- let colorId = this._color2Id[color];
- if (!colorId) {
- colorId = (++this._lastAssignedId);
- this._color2Id[color] = colorId;
- this._id2Color[colorId] = color;
- }
- const colorZone = new ColorZone(ycenter - halfHeight, ycenter + halfHeight, colorId);
- zone.setColorZone(colorZone);
- allColorZones.push(colorZone);
- }
- this._colorZonesInvalid = false;
- allColorZones.sort(ColorZone.compare);
- return allColorZones;
- }
- }
|