arcnix 0.3.6

A friendly desktop workspace for managing servers over SSH
// Leave the blank part of the embedded titlebar to the desktop window manager.
(() => {
    let menu, reportedWidth, frame;
    const resize = new ResizeObserver(schedule);
    const mutations = new MutationObserver(schedule);

    function measure() {
        frame = 0;
        const next = document.querySelector('.part.titlebar .menubar');
        if (!next) return;
        if (menu !== next) {
            resize.disconnect();
            mutations.disconnect();
            menu = next;
            resize.observe(menu);
            mutations.observe(menu.closest('.part.titlebar'), {
                childList: true, subtree: true, characterData: true, attributes: true,
                attributeFilter: ['class', 'style', 'hidden'],
            });
            mutations.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
        }
        if (!document.documentElement.classList.contains('has-menu-slot')) return;
        // The native hit region starts at the host slot, including the
        // workbench theme's inset between the slot and the menu itself.
        const slot = menu.closest('.titlebar-left').getBoundingClientRect();
        let right = slot.left;
        for (const button of menu.querySelectorAll(':scope > .menubar-menu-button')) {
            const bounds = button.getBoundingClientRect();
            if (bounds.width && bounds.height && getComputedStyle(button).visibility === 'visible') {
                right = Math.max(right, bounds.right);
            }
        }
        const width = Math.round((Math.min(right, slot.right) - slot.left) * devicePixelRatio);
        if (width !== reportedWidth) {
            reportedWidth = width;
            window.ipc.postMessage(JSON.stringify({ op: 'menuWidth', width }));
        }
    }

    function schedule() {
        if (!frame) frame = requestAnimationFrame(measure);
    }

    window.addEventListener('resize', schedule);
    mutations.observe(document, { childList: true, subtree: true });
    schedule();
})();