window.Kronick = window.Kronick || {};
Kronick.showToolWindow =
function showToolWindow(elementId) {
const windowEl =
document.getElementById(elementId);
if (!windowEl) {
return;
}
windowEl.classList.remove('window-minimized');
windowEl.style.display = 'flex';
Kronick.removeWindowTaskTab?.(elementId);
Kronick.bringToolWindowToFront(
windowEl
);
requestAnimationFrame(() => {
Kronick.keepWindowInViewport(
windowEl
);
});
};
Kronick.hideToolWindow =
function hideToolWindow(elementId) {
const windowEl =
document.getElementById(elementId);
if (!windowEl) {
return;
}
windowEl.classList.remove('window-minimized', 'window-maximized');
windowEl.style.display = 'none';
Kronick.removeWindowTaskTab?.(elementId);
};
Kronick.bringToolWindowToFront =
function bringToolWindowToFront(windowEl) {
if (!windowEl) {
return;
}
Kronick.state.highestWindowZIndex += 1;
windowEl.style.zIndex =
String(
Kronick.state
.highestWindowZIndex
);
};
Kronick.keepWindowInViewport =
function keepWindowInViewport(windowEl) {
if (
!windowEl
|| windowEl.style.display === 'none'
) {
return;
}
const rect =
windowEl.getBoundingClientRect();
const maxLeft = Math.max(
0,
window.innerWidth
- Math.min(
rect.width,
window.innerWidth
)
);
const maxTop = Math.max(
0,
window.innerHeight - 44
);
const newLeft = Math.max(
0,
Math.min(rect.left, maxLeft)
);
const newTop = Math.max(
0,
Math.min(rect.top, maxTop)
);
windowEl.style.left =
`${newLeft}px`;
windowEl.style.top =
`${newTop}px`;
windowEl.style.right = 'auto';
windowEl.style.bottom = 'auto';
windowEl.style.transform = 'none';
};
Kronick.keepVisibleWindowsInViewport =
function keepVisibleWindowsInViewport() {
document
.querySelectorAll(
'.tool-frame-wrap, '
+ '.shared-media-wrap'
)
.forEach(windowEl => {
if (
windowEl.style.display
!== 'none'
) {
Kronick.keepWindowInViewport(
windowEl
);
}
});
};
Kronick.makeToolWindowDraggable =
function makeToolWindowDraggable(
windowEl,
headerEl,
storageKey
) {
if (!windowEl || !headerEl) {
return;
}
const saved =
localStorage.getItem(storageKey);
if (saved) {
try {
const pos = JSON.parse(saved);
windowEl.style.left =
`${Number(pos.left || 0)}px`;
windowEl.style.top =
`${Number(pos.top || 0)}px`;
windowEl.style.right = 'auto';
windowEl.style.bottom = 'auto';
windowEl.style.transform = 'none';
} catch {
localStorage.removeItem(
storageKey
);
}
}
let dragging = false;
let startX = 0;
let startY = 0;
let startLeft = 0;
let startTop = 0;
headerEl.addEventListener(
'mousedown',
event => {
if (
event.button !== 0
|| event.target.closest(
'button'
)
) {
return;
}
dragging = true;
const rect =
windowEl
.getBoundingClientRect();
startX = event.clientX;
startY = event.clientY;
startLeft = rect.left;
startTop = rect.top;
windowEl.style.left =
`${startLeft}px`;
windowEl.style.top =
`${startTop}px`;
windowEl.style.right = 'auto';
windowEl.style.bottom = 'auto';
windowEl.style.transform = 'none';
Kronick.bringToolWindowToFront(
windowEl
);
document.body.classList.add(
'window-dragging'
);
event.preventDefault();
}
);
document.addEventListener(
'mousemove',
event => {
if (!dragging) {
return;
}
const rect =
windowEl
.getBoundingClientRect();
const maxLeft = Math.max(
0,
window.innerWidth
- rect.width
);
const maxTop = Math.max(
0,
window.innerHeight - 44
);
const newLeft = Math.max(
0,
Math.min(
maxLeft,
startLeft
+ event.clientX
- startX
)
);
const newTop = Math.max(
0,
Math.min(
maxTop,
startTop
+ event.clientY
- startY
)
);
windowEl.style.left =
`${newLeft}px`;
windowEl.style.top =
`${newTop}px`;
}
);
document.addEventListener(
'mouseup',
() => {
if (!dragging) {
return;
}
dragging = false;
document.body.classList.remove(
'window-dragging'
);
localStorage.setItem(
storageKey,
JSON.stringify({
left: parseInt(
windowEl.style.left
|| '0',
10
),
top: parseInt(
windowEl.style.top
|| '0',
10
)
})
);
}
);
};
Kronick.openMusicBrowser =
function openMusicBrowser() {
Kronick.showToolWindow(
'musicFrameWrap'
);
Kronick.notifyMediaFrame?.();
};
Kronick.closeMusicBrowser =
function closeMusicBrowser() {
Kronick.hideToolWindow(
'musicFrameWrap'
);
};
Kronick.openGiphyBrowser =
function openGiphyBrowser(
targetMode = 'room'
) {
Kronick.state.giphyTargetMode =
targetMode;
Kronick.showToolWindow(
'giphyFrameWrap'
);
const giphyFrame =
document.getElementById(
'giphyFrame'
);
setTimeout(() => {
if (giphyFrame?.contentWindow) {
giphyFrame.contentWindow
.postMessage(
{
type:
'KRONICK_GIPHY_MODE',
mode:
Kronick.state
.giphyTargetMode
},
Kronick.config.APP_ORIGIN
);
}
}, 100);
};
Kronick.closeGiphyBrowser =
function closeGiphyBrowser() {
Kronick.hideToolWindow(
'giphyFrameWrap'
);
Kronick.state.giphyTargetMode =
'room';
};
Kronick.openChatWindow =
function openChatWindow() {
Kronick.showToolWindow(
'chatFrameWrap'
);
Kronick.sendChatHistoryToFrame();
Kronick.clearChatUnreadCount();
};
Kronick.closeChatWindow =
function closeChatWindow() {
Kronick.hideToolWindow(
'chatFrameWrap'
);
};
/* Lightweight desktop-style window management. */
Kronick.getWindowTaskbar = function getWindowTaskbar() {
return document.getElementById('windowTaskbar');
};
Kronick.removeWindowTaskTab = function removeWindowTaskTab(elementId) {
document.querySelector(`[data-window-tab-for="${elementId}"]`)?.remove();
};
Kronick.addWindowTaskTab = function addWindowTaskTab(windowEl) {
const taskbar = Kronick.getWindowTaskbar();
if (!taskbar || !windowEl || !windowEl.id) return;
Kronick.removeWindowTaskTab(windowEl.id);
const button = document.createElement('button');
button.type = 'button';
button.className = 'window-task-tab';
button.dataset.windowTabFor = windowEl.id;
button.innerHTML = `${windowEl.dataset.windowIcon || '□'}${windowEl.dataset.windowTitle || 'Window'}`;
button.addEventListener('click', () => Kronick.restoreToolWindow(windowEl.id));
taskbar.appendChild(button);
};
Kronick.minimizeToolWindow = function minimizeToolWindow(elementId) {
const windowEl = document.getElementById(elementId);
if (!windowEl) return;
windowEl.classList.add('window-minimized');
windowEl.style.display = 'none';
Kronick.addWindowTaskTab(windowEl);
};
Kronick.restoreToolWindow = function restoreToolWindow(elementId) {
const windowEl = document.getElementById(elementId);
if (!windowEl) return;
windowEl.classList.remove('window-minimized');
windowEl.style.display = 'flex';
Kronick.removeWindowTaskTab(elementId);
Kronick.bringToolWindowToFront(windowEl);
requestAnimationFrame(() => Kronick.keepWindowInViewport(windowEl));
if (elementId === 'chatFrameWrap') Kronick.clearChatUnreadCount?.();
};
Kronick.toggleMaximizeToolWindow = function toggleMaximizeToolWindow(elementId) {
const windowEl = document.getElementById(elementId);
if (!windowEl) return;
if (windowEl.classList.contains('window-maximized')) {
const saved = windowEl.dataset.normalBounds;
windowEl.classList.remove('window-maximized');
if (saved) {
try {
const b = JSON.parse(saved);
for (const key of ['left','top','width','height']) {
if (b[key]) windowEl.style[key] = b[key];
}
} catch {}
}
Kronick.keepWindowInViewport(windowEl);
return;
}
const r = windowEl.getBoundingClientRect();
windowEl.dataset.normalBounds = JSON.stringify({left:`${r.left}px`,top:`${r.top}px`,width:`${r.width}px`,height:`${r.height}px`});
windowEl.classList.add('window-maximized');
windowEl.style.removeProperty('left');
windowEl.style.removeProperty('top');
windowEl.style.removeProperty('width');
windowEl.style.removeProperty('height');
Kronick.bringToolWindowToFront(windowEl);
};
Kronick.makeToolWindowResizable = function makeToolWindowResizable(windowEl) {
if (!windowEl || windowEl.querySelector(':scope > .window-resize-handle')) return;
const handle = document.createElement('div');
handle.className = 'window-resize-handle';
handle.setAttribute('aria-hidden', 'true');
windowEl.appendChild(handle);
let active = false, sx = 0, sy = 0, sw = 0, sh = 0;
handle.addEventListener('pointerdown', event => {
if (windowEl.classList.contains('window-maximized')) return;
active = true; sx = event.clientX; sy = event.clientY;
const r = windowEl.getBoundingClientRect(); sw = r.width; sh = r.height;
handle.setPointerCapture?.(event.pointerId);
event.preventDefault(); event.stopPropagation();
});
handle.addEventListener('pointermove', event => {
if (!active) return;
const maxW = Math.max(260, window.innerWidth - windowEl.getBoundingClientRect().left - 4);
const maxH = Math.max(200, window.innerHeight - windowEl.getBoundingClientRect().top - 4);
windowEl.style.width = `${Math.min(maxW, Math.max(280, sw + event.clientX - sx))}px`;
windowEl.style.height = `${Math.min(maxH, Math.max(220, sh + event.clientY - sy))}px`;
});
const stop = () => { active = false; };
handle.addEventListener('pointerup', stop);
handle.addEventListener('pointercancel', stop);
};
Kronick.initializeWindowManager = function initializeWindowManager() {
document.querySelectorAll('.tool-frame-wrap, .shared-media-wrap').forEach(windowEl => {
const header = windowEl.querySelector('.tool-frame-header');
if (!header || header.querySelector('.window-manager-actions')) {
Kronick.makeToolWindowResizable(windowEl);
return;
}
const existingClose = header.querySelector('.window-close-button');
const actions = document.createElement('div');
actions.className = 'window-manager-actions';
const min = document.createElement('button');
min.type = 'button'; min.className = 'window-control-button'; min.title = 'Minimize'; min.textContent = '−';
min.addEventListener('click', e => { e.stopPropagation(); Kronick.minimizeToolWindow(windowEl.id); });
const max = document.createElement('button');
max.type = 'button'; max.className = 'window-control-button'; max.title = 'Maximize / restore'; max.textContent = '▢';
max.addEventListener('click', e => { e.stopPropagation(); Kronick.toggleMaximizeToolWindow(windowEl.id); });
actions.append(min, max);
if (existingClose) actions.appendChild(existingClose);
header.appendChild(actions);
Kronick.makeToolWindowResizable(windowEl);
});
};