polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
156 lines • 5.23 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tooltip = void 0;
const blessed_1 = __importDefault(require("blessed"));
class Tooltip {
constructor(screen, eventBus) {
this.isVisible = false;
this.screen = screen;
this.eventBus = eventBus;
}
show(config) {
if (this.hideTimeout) {
clearTimeout(this.hideTimeout);
this.hideTimeout = undefined;
}
if (this.isVisible) {
this.hide();
}
this.currentConfig = config;
this.createTooltipBox(config);
if (config.autoHideDelay && config.autoHideDelay > 0) {
this.hideTimeout = setTimeout(() => {
this.hide();
}, config.autoHideDelay);
}
this.eventBus.emit('tooltip:shown', {
content: config.content,
x: config.x,
y: config.y,
timestamp: new Date(),
});
}
hide() {
if (!this.isVisible || !this.tooltipBox)
return;
try {
this.screen.remove(this.tooltipBox);
this.tooltipBox = null;
this.isVisible = false;
this.screen.render();
if (this.hideTimeout) {
clearTimeout(this.hideTimeout);
this.hideTimeout = undefined;
}
this.eventBus.emit('tooltip:hidden', {
timestamp: new Date(),
});
}
catch (error) {
console.error('Failed to hide tooltip:', error);
this.tooltipBox = null;
this.isVisible = false;
}
}
updatePosition(x, y) {
if (!this.isVisible || !this.tooltipBox || !this.currentConfig)
return;
try {
const { adjustedX, adjustedY } = this.calculatePosition(x, y, this.currentConfig);
this.tooltipBox.left = adjustedX;
this.tooltipBox.top = adjustedY;
this.screen.render();
}
catch (error) {
console.error('Failed to update tooltip position:', error);
}
}
isTooltipVisible() {
return this.isVisible;
}
createTooltipBox(config) {
try {
const { adjustedX, adjustedY } = this.calculatePosition(config.x, config.y, config);
this.tooltipBox = blessed_1.default.box({
left: adjustedX,
top: adjustedY,
width: Math.min(config.maxWidth || 40, config.content.length + 4),
height: this.calculateContentHeight(config.content, config.maxWidth || 40),
content: config.content,
tags: false,
border: {
type: 'line',
},
style: {
fg: config.style?.fg || 'white',
bg: config.style?.bg || 'black',
border: {
fg: config.style?.border?.fg || 'yellow',
bg: config.style?.border?.bg || 'black',
},
},
padding: {
left: 1,
right: 1,
top: 0,
bottom: 0,
},
});
this.screen.append(this.tooltipBox);
this.screen.render();
this.isVisible = true;
}
catch (error) {
console.error('Failed to create tooltip:', error);
this.isVisible = false;
this.tooltipBox = null;
}
}
calculatePosition(x, y, config) {
const screenWidth = this.screen.width || 80;
const screenHeight = this.screen.height || 24;
const tooltipWidth = Math.min(config.maxWidth || 40, config.content.length + 4);
const tooltipHeight = this.calculateContentHeight(config.content, config.maxWidth || 40);
let adjustedX = x + 2;
let adjustedY = y - 1;
if (adjustedX + tooltipWidth > screenWidth) {
adjustedX = x - tooltipWidth - 2;
}
if (adjustedX < 0) {
adjustedX = 0;
}
if (adjustedY + tooltipHeight > screenHeight) {
adjustedY = y - tooltipHeight - 1;
}
if (adjustedY < 0) {
adjustedY = 0;
}
return { adjustedX, adjustedY };
}
calculateContentHeight(content, maxWidth) {
const effectiveWidth = maxWidth - 4;
const lines = content.split('\n');
let totalLines = 0;
for (const line of lines) {
if (line.length <= effectiveWidth) {
totalLines += 1;
}
else {
totalLines += Math.ceil(line.length / effectiveWidth);
}
}
return Math.max(1, totalLines) + 2;
}
destroy() {
this.hide();
if (this.hideTimeout) {
clearTimeout(this.hideTimeout);
this.hideTimeout = undefined;
}
}
}
exports.Tooltip = Tooltip;
//# sourceMappingURL=tooltip.js.map