From e263c7a722b8c2fcbe83596836653896a9e0258b Mon Sep 17 00:00:00 2001 From: Sleepy Flower Date: Wed, 27 Mar 2024 08:07:24 -0400 Subject: [PATCH] feat: use and for browser progress indicator instead of (#5015) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fallback for progress when canvas isn't available * Fix bugs preventing progress text from being updated * Use a different class for progress text fallback * Refactor: replace progress canvas with text * refactor: add progress bar * Refactor: Remove unused progress code * Match progress design hide progress bar and use SVG to recreate the ring bar * Refactor: use css variables for ring size and color values removed more canvas related code removed an em * Mirror styles changes to progress ring renamed ring-whole to ring-flatlight and have ring highlight and flatlight be the inverse of each other fix spelling error with decimalPlaces use getComputedStyle for get the radius of the ring defined in CSS use :is() CSS to simplify CSS code Change stroke thickness math to better match previous look * Trying to match progress canvas look on progress ring --------- Co-authored-by: Josh Goldberg ✨ --- lib/browser/progress.js | 138 ---------------------------------------- lib/reporters/html.js | 49 +++++++------- mocha.css | 71 ++++++++++++++++----- 3 files changed, 81 insertions(+), 177 deletions(-) delete mode 100644 lib/browser/progress.js diff --git a/lib/browser/progress.js b/lib/browser/progress.js deleted file mode 100644 index c82bc0824e..0000000000 --- a/lib/browser/progress.js +++ /dev/null @@ -1,138 +0,0 @@ -'use strict'; - -/** - @module browser/Progress -*/ - -/** - * Expose `Progress`. - */ - -module.exports = Progress; - -/** - * Initialize a new `Progress` indicator. - */ -function Progress() { - this.percent = 0; - this.size(0); - this.fontSize(11); - this.font('helvetica, arial, sans-serif'); -} - -/** - * Set progress size to `size`. - * - * @public - * @param {number} size - * @return {Progress} Progress instance. - */ -Progress.prototype.size = function (size) { - this._size = size; - return this; -}; - -/** - * Set text to `text`. - * - * @public - * @param {string} text - * @return {Progress} Progress instance. - */ -Progress.prototype.text = function (text) { - this._text = text; - return this; -}; - -/** - * Set font size to `size`. - * - * @public - * @param {number} size - * @return {Progress} Progress instance. - */ -Progress.prototype.fontSize = function (size) { - this._fontSize = size; - return this; -}; - -/** - * Set font to `family`. - * - * @param {string} family - * @return {Progress} Progress instance. - */ -Progress.prototype.font = function (family) { - this._font = family; - return this; -}; - -/** - * Update percentage to `n`. - * - * @param {number} n - * @return {Progress} Progress instance. - */ -Progress.prototype.update = function (n) { - this.percent = n; - return this; -}; - -/** - * Draw on `ctx`. - * - * @param {CanvasRenderingContext2d} ctx - * @return {Progress} Progress instance. - */ -Progress.prototype.draw = function (ctx) { - try { - var darkMatcher = window.matchMedia('(prefers-color-scheme: dark)'); - var isDarkMode = !!darkMatcher.matches; - var lightColors = { - outerCircle: '#9f9f9f', - innerCircle: '#eee', - text: '#000' - }; - var darkColors = { - outerCircle: '#888', - innerCircle: '#444', - text: '#fff' - }; - var colors = isDarkMode ? darkColors : lightColors; - - var percent = Math.min(this.percent, 100); - var size = this._size; - var half = size / 2; - var x = half; - var y = half; - var rad = half - 1; - var fontSize = this._fontSize; - - ctx.font = fontSize + 'px ' + this._font; - - var angle = Math.PI * 2 * (percent / 100); - ctx.clearRect(0, 0, size, size); - - // outer circle - ctx.strokeStyle = colors.outerCircle; - ctx.beginPath(); - ctx.arc(x, y, rad, 0, angle, false); - ctx.stroke(); - - // inner circle - ctx.strokeStyle = colors.innerCircle; - ctx.beginPath(); - ctx.arc(x, y, rad - 1, 0, angle, true); - ctx.stroke(); - - // text - var text = this._text || (percent | 0) + '%'; - var w = ctx.measureText(text).width; - - ctx.fillStyle = colors.text; - ctx.fillText(text, x - w / 2 + 1, y + fontSize / 2 - 1); - } catch (ignore) { - // don't fail if we can't render progress - } - return this; -}; diff --git a/lib/reporters/html.js b/lib/reporters/html.js index 034fb07f01..ae4a4546f8 100644 --- a/lib/reporters/html.js +++ b/lib/reporters/html.js @@ -10,7 +10,6 @@ var Base = require('./base'); var utils = require('../utils'); -var Progress = require('../browser/progress'); var escapeRe = require('escape-string-regexp'); var constants = require('../runner').constants; var EVENT_TEST_PASS = constants.EVENT_TEST_PASS; @@ -38,7 +37,7 @@ exports = module.exports = HTML; var statsTemplate = '