// PresetAddViewRenderer.js - Renders the add/edit preset view import { AspectRatioUtils } from "../AspectRatioUtils.js"; import { getIconHtml } from "../IconUtils.js"; /** * Renderer for the add/edit preset view */ export class PresetAddViewRenderer { constructor(parentDialog) { this.parentDialog = parentDialog; } /** * Renders the add/edit preset view with category selection and preview * @param {HTMLElement} container - Container to render into */ render(container) { // Title const titleDiv = document.createElement('div'); titleDiv.className = 'resolution-master-preset-add-title'; titleDiv.textContent = 'Add Presets to Category'; container.appendChild(titleDiv); // Category selection section const categorySection = this.createCategorySection(); container.appendChild(categorySection); // Quick add form (shown only when category is selected) if (this.parentDialog.selectedCategory) { this.renderQuickAddForm(container); // Validation message const validationMsg = document.createElement('div'); validationMsg.id = 'validation-msg'; validationMsg.style.cssText = ` color: #f55; font-size: 12px; margin-top: 4px; margin-bottom: 8px; min-height: 14px; `; container.appendChild(validationMsg); // Preset preview section this.renderPresetPreview(container); } } /** * Creates the category selection section * @returns {HTMLElement} Category section */ createCategorySection() { const categorySection = document.createElement('div'); categorySection.className = 'resolution-master-preset-add-category-section'; const categoryLabel = document.createElement('div'); categoryLabel.className = 'resolution-master-preset-add-category-label'; categoryLabel.textContent = 'Category'; categorySection.appendChild(categoryLabel); // Container for category button and rename button const categoryButtonContainer = document.createElement('div'); categoryButtonContainer.className = 'resolution-master-preset-add-category-btn-container'; const categoryButton = this.createCategoryButton(); categoryButtonContainer.appendChild(categoryButton); // Rename category button (only shown when category is selected) if (this.parentDialog.selectedCategory) { const renameCategoryBtn = this.createRenameCategoryButton(); categoryButtonContainer.appendChild(renameCategoryBtn); } categorySection.appendChild(categoryButtonContainer); return categorySection; } /** * Creates the category selection button * @returns {HTMLElement} Category button */ createCategoryButton() { const categoryButton = document.createElement('button'); categoryButton.id = 'category-select-btn'; categoryButton.className = 'resolution-master-preset-add-category-btn'; if (!this.parentDialog.selectedCategory) { categoryButton.classList.add('placeholder'); } categoryButton.textContent = this.parentDialog.selectedCategory || 'Click to select category...'; categoryButton.addEventListener('click', (e) => { this.parentDialog.showCategoryDropdown(e); }); return categoryButton; } /** * Creates the rename category button * @returns {HTMLElement} Rename button */ createRenameCategoryButton() { const renameCategoryBtn = document.createElement('button'); renameCategoryBtn.className = 'resolution-master-preset-add-rename-category-btn'; renameCategoryBtn.innerHTML = getIconHtml(this.parentDialog.editIcon, '✏️'); // Tooltip handled by TooltipManager renameCategoryBtn.addEventListener('click', () => { this.parentDialog.renameDialogManager.showRenameCategoryDialog(this.parentDialog.selectedCategory); }); return renameCategoryBtn; } /** * Renders the quick add form * @param {HTMLElement} container - Container to render into */ renderQuickAddForm(container) { const isEditMode = this.parentDialog.editingPresetData !== null; const sectionBorderColor = isEditMode ? 'rgba(80, 255, 80, 0.5)' : 'rgba(90, 170, 255, 0.3)'; const sectionBgColor = isEditMode ? 'rgba(80, 255, 80, 0.1)' : 'rgba(90, 170, 255, 0.1)'; const titleColor = isEditMode ? '#5f5' : '#5af'; const quickAddSection = document.createElement('div'); quickAddSection.style.cssText = ` margin-bottom: 12px; padding: 10px; background: ${sectionBgColor}; border: 1px solid ${sectionBorderColor}; border-radius: 6px; `; const quickAddTitle = document.createElement('div'); quickAddTitle.id = 'quick-add-title'; quickAddTitle.textContent = this.parentDialog.editingPresetData ? `Quick Edit Preset: ${this.parentDialog.editingPresetData.name}` : 'Quick Add Preset'; quickAddTitle.style.cssText = `color: ${titleColor}; font-size: 13px; font-weight: bold; margin-bottom: 6px;`; quickAddSection.appendChild(quickAddTitle); const quickAddForm = this.createQuickAddForm(); quickAddSection.appendChild(quickAddForm); container.appendChild(quickAddSection); } /** * Creates the quick add form * @returns {HTMLElement} Form element */ createQuickAddForm() { const quickAddForm = document.createElement('div'); quickAddForm.style.cssText = 'display: flex; flex-direction: column; gap: 4px;'; // Name input with error message const nameGroup = this.createNameInput(); quickAddForm.appendChild(nameGroup); // Width and Height in a row const dimensionsRow = this.createDimensionsRow(); quickAddForm.appendChild(dimensionsRow); // Preview container for shape visualization const previewContainer = this.createPreviewContainer(); quickAddForm.appendChild(previewContainer); // Button container for + and X buttons const buttonContainer = this.createButtonContainer(); quickAddForm.appendChild(buttonContainer); return quickAddForm; } /** * Creates the name input group * @returns {HTMLElement} Name group */ createNameInput() { const nameGroup = document.createElement('div'); nameGroup.style.cssText = 'display: flex; flex-direction: column; gap: 4px;'; const nameLabel = document.createElement('label'); nameLabel.textContent = 'Name'; nameLabel.style.cssText = 'color: #ccc; font-size: 11px; font-weight: bold;'; const nameInput = document.createElement('input'); nameInput.id = 'quick-name-input'; nameInput.type = 'text'; nameInput.placeholder = 'Preset name'; nameInput.value = this.parentDialog.editingPresetData ? this.parentDialog.editingPresetData.name : ''; nameInput.style.cssText = ` padding: 6px; border: 1px solid #555; border-radius: 4px; background: #333; color: #fff; font-size: 13px; outline: none; `; const nameErrorMsg = document.createElement('div'); nameErrorMsg.id = 'name-error-msg'; nameErrorMsg.style.cssText = ` color: #f55; font-size: 11px; margin-top: 2px; min-height: 14px; `; nameGroup.appendChild(nameLabel); nameGroup.appendChild(nameInput); nameGroup.appendChild(nameErrorMsg); return nameGroup; } /** * Creates the dimensions row (width + height) * @returns {HTMLElement} Dimensions row */ createDimensionsRow() { const dimensionsRow = document.createElement('div'); dimensionsRow.style.cssText = 'display: flex; gap: 6px;'; // Width input const widthGroup = this.createDimensionInput('Width', 'quick-width-input', 'width-error-msg', this.parentDialog.editingPresetData ? this.parentDialog.editingPresetData.width : ''); dimensionsRow.appendChild(widthGroup); // Height input const heightGroup = this.createDimensionInput('Height', 'quick-height-input', 'height-error-msg', this.parentDialog.editingPresetData ? this.parentDialog.editingPresetData.height : ''); dimensionsRow.appendChild(heightGroup); return dimensionsRow; } /** * Creates a dimension input group (width or height) */ createDimensionInput(label, inputId, errorId, value) { const group = document.createElement('div'); group.style.cssText = 'flex: 1; display: flex; flex-direction: column; gap: 4px;'; const labelEl = document.createElement('label'); labelEl.textContent = label; labelEl.style.cssText = 'color: #ccc; font-size: 11px; font-weight: bold;'; const input = document.createElement('input'); input.id = inputId; input.type = 'number'; input.placeholder = '512'; input.min = '64'; input.step = '1'; input.value = value; input.style.cssText = ` padding: 6px; border: 1px solid #555; border-radius: 4px; background: #333; color: #fff; font-size: 13px; outline: none; `; const errorMsg = document.createElement('div'); errorMsg.id = errorId; errorMsg.style.cssText = ` color: #f55; font-size: 11px; margin-top: 2px; min-height: 14px; `; group.appendChild(labelEl); group.appendChild(input); group.appendChild(errorMsg); return group; } /** * Creates the preview container * @returns {HTMLElement} Preview container */ createPreviewContainer() { const previewContainer = document.createElement('div'); previewContainer.style.cssText = ` margin-top: 6px; padding: 8px; background: rgba(0, 0, 0, 0.2); border: 1px solid #444; border-radius: 4px; display: flex; flex-direction: column; align-items: center; gap: 6px; `; const previewLabel = document.createElement('div'); previewLabel.textContent = 'Preview:'; const labelColor = this.parentDialog.editingPresetData ? '#5f5' : '#5af'; previewLabel.style.cssText = `color: ${labelColor}; font-size: 11px; font-weight: bold;`; const previewCanvas = document.createElement('div'); previewCanvas.id = 'preview-canvas'; previewCanvas.style.cssText = ` width: 160px; height: 120px; display: flex; align-items: center; justify-content: center; position: relative; `; const previewShape = document.createElement('div'); previewShape.id = 'preview-shape'; previewShape.style.cssText = ` border: 2px solid #5af; background: rgba(90, 170, 255, 0.1); display: flex; flex-direction: column; align-items: center; justify-content: center; transition: all 0.2s; `; const previewText = document.createElement('div'); previewText.id = 'preview-text'; previewText.style.cssText = ` color: #5af; font-size: 11px; font-weight: bold; text-align: center; line-height: 1.4; `; previewShape.appendChild(previewText); previewCanvas.appendChild(previewShape); previewContainer.appendChild(previewLabel); previewContainer.appendChild(previewCanvas); // Set up event listeners after adding to DOM setTimeout(() => this.setupFormValidation(), 0); return previewContainer; } /** * Sets up form validation and preview updates */ setupFormValidation() { const nameInput = document.getElementById('quick-name-input'); const widthInput = document.getElementById('quick-width-input'); const heightInput = document.getElementById('quick-height-input'); if (!nameInput || !widthInput || !heightInput) return; // Function to update preview shape const updatePreviewShape = () => { const width = parseInt(widthInput.value) || 0; const height = parseInt(heightInput.value) || 0; const previewShape = document.getElementById('preview-shape'); const previewText = document.getElementById('preview-text'); const previewCanvas = document.getElementById('preview-canvas'); if (!previewShape || !previewText) return; if (width > 0 && height > 0) { const maxWidth = 145; const maxHeight = 105; const scale = Math.min(maxWidth / width, maxHeight / height); const scaledWidth = width * scale; const scaledHeight = height * scale; previewShape.style.width = `${scaledWidth}px`; previewShape.style.height = `${scaledHeight}px`; const isEditMode = this.parentDialog.editingPresetData !== null; const borderColor = isEditMode ? '#5f5' : '#5af'; const bgColor = isEditMode ? 'rgba(80, 255, 80, 0.1)' : 'rgba(90, 170, 255, 0.1)'; const textColor = isEditMode ? '#5f5' : '#5af'; previewShape.style.border = `2px solid ${borderColor}`; previewShape.style.background = bgColor; const gcd = (a, b) => b === 0 ? a : gcd(b, a % b); const divisor = gcd(width, height); const ratioW = width / divisor; const ratioH = height / divisor; previewText.innerHTML = `