# Menú de mosaico de ventanas

Al elegir un diseño en el botón verde, la ventana activa ocupa la zona seleccionada del escritorio.

## Cómo probarlo

Abre el menú del botón verde y sitúa la ventana en la mitad izquierda. Vuelve a abrirlo y coloca dos ventanas en paralelo.

## Detalles de la interacción

Las mitades y los cuartos colocan la ventana activa. Los diseños de dos y cuatro ventanas también ordenan las demás. Al pasar el puntero o mover el foco se muestra una vista previa. La selección aplica el diseño, Esc cierra la vista previa y la opción de restaurar recupera la disposición inicial.

## Controles por plataforma

- iOS · En esta demo web, toca el botón verde y elige un diseño. Prueba el de dos ventanas para leer ambos documentos en paralelo.
- Android · En esta demo web, toca el botón verde y elige una mitad o un cuarto. Abre de nuevo el menú para restaurar la disposición inicial.
- PC · Deja el puntero un momento sobre el botón verde o haz clic en él. Pasa por una opción para ver el destino y haz clic para aplicarla. Con el teclado, elige la opción y pulsa Intro.

## Dónde usarlo

Colocar un manuscrito junto a las notas de producción o ampliar una sola ventana.

## Instrucciones de implementación

> Añade un menú de distribución al botón de la ventana. Muestra el destino al pasar el puntero o al enfocar una opción y aplica el diseño al seleccionarla.

## Fuentes y créditos

[Apple · Colocar ventanas en mosaico en el Mac](<https://support.apple.com/es-es/guide/mac-help/mchlef287e5d/mac>)

## Ejemplo en HTML

Guarda el siguiente código en un archivo HTML y ábrelo en tu navegador. Puedes usar el ejemplo sin conexión.

```html
<!doctype html>
<html lang="es"><head><script>if(new URLSearchParams(location.search).get('reduce')==='1')document.documentElement.classList.add('motion-reduced');</script><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Menú de disposición de ventanas · Motion Lab</title>
<style>
*{box-sizing:border-box}
:root{color-scheme:light;font:15px/1.4 system-ui,-apple-system,sans-serif;color:#202923;background:#e6e6da}
body{display:grid;align-content:center;justify-items:center;min-height:100svh;margin:0;padding:clamp(9px,2.3vw,22px)}
main{width:min(100%,860px);min-width:0}
.scene-heading,.caption{display:flex;align-items:baseline;justify-content:space-between;gap:8px;flex-wrap:wrap}
h1{margin:0 0 9px;font-size:clamp(20px,3.4vw,33px);line-height:1.15;letter-spacing:-.055em}
.scene-heading small,.caption output{font:600 11px/1.4 ui-monospace,monospace;color:#384e43}
button{font:inherit;cursor:pointer;touch-action:manipulation}
button:focus-visible{outline:3px solid #dc7040;outline-offset:2px}
[hidden]{display:none!important}
#workbench{position:relative;isolation:isolate;overflow:hidden;height:clamp(300px,65svh,490px);border:1px solid #344b40;border-radius:7px;background-color:#50675a;background-image:repeating-linear-gradient(161deg,#587261 0 1px,transparent 1px 12px),linear-gradient(140deg,#77917c,#334f43 70%)}
.window{position:absolute;overflow:hidden;container-type:inline-size;border:1px solid #25362f;border-radius:7px;background:#f4f0e2;box-shadow:0 5px 16px #14251d48;color:#26342c;transition:top 180ms ease-out,left 180ms ease-out,width 180ms ease-out,height 180ms ease-out}
#studio-window{z-index:2;left:15%;top:17%;width:70%;height:66%}
.bar{height:40px;display:flex;align-items:center;gap:7px;padding:0 9px;background:#26332e;color:#f6f1e2;font-size:12px;font-weight:600;letter-spacing:-.015em}
.bar>span:last-child{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.lights{display:flex;align-items:center;gap:5px;flex:none}
.light,.green{display:block;width:9px;height:9px;border-radius:50%}
.light:first-child{background:#df8b6d}.light:nth-child(2){background:#dfbe70}
#tile-trigger{display:grid;place-items:center;position:relative;z-index:1;width:38px;height:38px;margin:0 -5px 0 -5px;border:0;border-radius:5px;background:transparent;color:#103e28}
#tile-trigger .green{display:grid;place-items:center;width:16px;height:16px;background:#91cf7c;box-shadow:inset 0 0 0 1px #080b0820}
.green:before{content:"";width:6px;height:5px;border:1px solid currentColor;border-top-width:2px}
#tile-trigger:is(:hover,:focus-visible,[aria-expanded=true]){background:#ffffff1c}
#tile-trigger:focus-visible{outline-offset:-3px}
.proof{height:calc(100% - 40px);display:grid;grid-template-columns:1.05fr 1fr;align-items:center;gap:6%;padding:clamp(7px,4.4cqw,27px);background:#f1ebd8}
.proof svg{width:100%;height:100%;min-height:0;border:1px solid #4f5c4e;display:block;background:#debc85}
.proof-title{min-width:0}.proof-title i{display:block;font:italic 600 clamp(19px,7.9cqw,59px)/.95 Georgia,serif;letter-spacing:-.07em;color:#294d44}
.proof-title p{margin:8px 0 0;font:600 clamp(10px,2.8cqw,15px)/1.3 system-ui,sans-serif}
.aux .bar{height:29px;font-size:11px}.aux-body{display:grid;align-content:center;height:calc(100% - 29px);gap:8px;padding:clamp(5px,6cqw,26px);overflow:hidden}
.aux p{margin:0;color:#384a3c;font-size:clamp(9px,6cqw,19px);font-weight:620;line-height:1.2}
.ruling{height:16px;background:repeating-linear-gradient(0deg,transparent 0 6px,#b5b5a6 6px 7px)}
.swatches{height:30px;background:linear-gradient(90deg,#334e42 0 31%,#f5eedb 31% 34%,#c26c49 34% 65%,#f5eedb 65% 68%,#b89b60 68%)}
.ticks{height:14px;background:repeating-linear-gradient(90deg,#596f60 0 2px,transparent 2px 10px)}
#tile-preview{position:absolute;inset:0;pointer-events:none;z-index:3}
#tile-preview i{position:absolute;border:2px solid #dcefd7;border-radius:7px;background:#bfd7ad44;box-shadow:inset 0 0 0 1px #325542,0 0 0 1px #325542}
#tile-menu{position:absolute;z-index:4;width:min(235px,calc(100% - 8px));padding:8px;border:1px solid #6b786d;border-radius:10px;background:#1c2925;color:#f4f3e8;box-shadow:0 9px 22px #12211966;max-height:calc(100% - 8px);overflow:auto;overscroll-behavior:contain}
#tile-caret{position:absolute;z-index:5;width:10px;height:10px;background:#1c2925;border:solid #6b786d;border-width:1px 0 0 1px;transform:rotate(45deg);pointer-events:none}
#tile-caret[data-above]{transform:rotate(225deg)}
.group + .group{border-top:1px solid #425048;margin-top:3px;padding-top:5px}
.group-label{display:block;color:#c7d3c8;font-size:11px;font-weight:650;margin:0 3px 2px}
.choices{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:2px}
.choices button{display:grid;place-content:center;justify-items:center;min-height:38px;border:0;border-radius:4px;color:#e5ecdf;background:transparent;padding:3px}
.choices button:is(:hover,:focus-visible){background:#42554a}
.choices button[aria-pressed=true]{box-shadow:inset 0 0 0 1px #a6bba9}
.choices button:focus-visible{outline-color:#ffbd83;outline-offset:-2px}
.icon{display:block;position:relative;width:24px;height:17px;border:1.5px solid currentColor;border-radius:2px;overflow:hidden}
.icon i{position:absolute;display:block;background:#e0e8da}
.choices .restore-icon{font:24px/1 system-ui}
.choices small{max-width:100%;margin-top:3px;font:600 9px/1.1 system-ui,sans-serif;text-align:center;overflow-wrap:anywhere}
.caption{margin-top:8px;align-items:center;gap:5px 14px}
.caption p{margin:0;color:#35453a;font-size:12px}.caption output{font-size:11px}
@container(max-width:180px){.proof{grid-template-columns:1fr;padding:7px}.proof-title{display:none}.bar{gap:4px;padding-inline:5px}}
@media(pointer:coarse){.choices button,#tile-trigger{min-height:44px}#tile-trigger{min-width:44px}.bar:has(#tile-trigger){height:44px}.proof{height:calc(100% - 44px)}}
@media(prefers-reduced-motion:reduce){.window{transition:none}}
.motion-reduced .window,.reduced-motion .window{transition:none}
</style></head><body>
<main>
<header class="scene-heading"><h1>Mesa de grabado</h1><small>Espacio de edición</small></header>
<div id="workbench">
  <section id="studio-window" class="window" data-layout="restore" aria-label="Ventana de prueba">
    <div class="bar"><div class="lights"><i class="light" aria-hidden="true"></i><i class="light" aria-hidden="true"></i><button id="tile-trigger" type="button" aria-label="Menú de disposición de ventanas" title="Menú de disposición de ventanas" aria-haspopup="dialog" aria-controls="tile-menu" aria-expanded="false"><span class="green" aria-hidden="true"></span></button></div><span>Pruebas de hoy</span></div>
    <div class="proof"><svg viewBox="0 0 250 170" preserveAspectRatio="xMidYMid slice" aria-hidden="true"><defs><pattern id="hatch" width="7" height="7" patternUnits="userSpaceOnUse" patternTransform="rotate(17)"><path d="M1 0 V7" stroke="#d6c192" stroke-width="1"/></pattern></defs><rect width="250" height="170" fill="#dcb67d"/><circle cx="165" cy="48" r="25" fill="#b6593e"/><path d="M0 80Q61 124 113 73T250 55V170H0Z" fill="#597360"/><path d="M0 123Q63 81 133 113T250 80V170H0Z" fill="#294c41"/><path d="M0 127Q68 89 145 120T250 93V170H0Z" fill="url(#hatch)"/></svg>
    <div class="proof-title"><i lang="en">Folded<br>Hills</i><p>Grabado de montañas superpuestas</p></div></div></section>
  <section class="window aux" id="notes-window" aria-label="Nota de producción" hidden><div class="bar">Nota de producción</div><div class="aux-body"><p>Revisar los márgenes de corte</p><div class="ruling" aria-hidden="true"></div></div></section>
  <section class="window aux" id="swatch-window" aria-label="Muestras de tinta" hidden><div class="bar">Muestras de tinta</div><div class="aux-body"><div class="swatches" aria-hidden="true"></div><p>Impresión a tres tintas</p></div></section>
  <section class="window aux" id="queue-window" aria-label="Cola de impresión" hidden><div class="bar">Cola de impresión</div><div class="aux-body"><p>Prueba de la primera edición</p><div class="ticks" aria-hidden="true"></div></div></section>
  <div id="tile-preview" hidden aria-hidden="true"></div>
  <i id="tile-caret" hidden aria-hidden="true"></i>
  <div id="tile-menu" role="dialog" aria-label="Menú de disposición de ventanas" hidden>
    <div class="group" role="group" aria-labelledby="half-label"><span class="group-label" id="half-label">Ocupar media pantalla</span><div class="choices" id="half-options"></div></div>
    <div class="group" role="group" aria-labelledby="quarter-label"><span class="group-label" id="quarter-label">Mover a una esquina</span><div class="choices" id="quarter-options"></div></div>
    <div class="group" role="group" aria-labelledby="arrange-label"><span class="group-label" id="arrange-label">Llenar y organizar</span><div class="choices" id="arrange-options"></div></div>
  </div>
</div>
<footer class="caption"><p>Elige una disposición de ventanas con el botón verde.</p><output id="placement-status">Disposición original</output></footer>
</main>
<script>
const bench=document.querySelector('#workbench'), mainWindow=document.querySelector('#studio-window');
const trigger=document.querySelector('#tile-trigger'), menu=document.querySelector('#tile-menu'), preview=document.querySelector('#tile-preview');
const caret=document.querySelector('#tile-caret');
const parts={
  restore:[.15,.17,.70,.66], fill:[0,0,1,1],
  left:[0,0,.5,1], right:[.5,0,.5,1], top:[0,0,1,.5], bottom:[0,.5,1,.5],
  'top-left':[0,0,.5,.5], 'top-right':[.5,0,.5,.5], 'bottom-left':[0,.5,.5,.5], 'bottom-right':[.5,.5,.5,.5]
};
const names={left:'Mitad izquierda',right:'Mitad derecha',top:'Mitad superior',bottom:'Mitad inferior','top-left':'Cuadrante superior izquierdo','top-right':'Cuadrante superior derecho','bottom-left':'Cuadrante inferior izquierdo','bottom-right':'Cuadrante inferior derecho',fill:'Ocupar toda la mesa',pair:'Dos ventanas en paralelo',grid:'Cuadrícula de cuatro ventanas',restore:'Disposición original'};
const shortNames={fill:'Llenar',pair:'Dos',grid:'Cuatro',restore:'Restaurar'};
const windows=[mainWindow,...document.querySelectorAll('.aux')];
function areas(key){
  if(key==='pair')return [parts.left,parts.right];
  if(key==='grid')return [parts['top-left'],parts['top-right'],parts['bottom-left'],parts['bottom-right']];
  return [parts[key]];
}
function positionArea(element,[x,y,w,h],gap){
  element.style.left='calc('+x*100+'% + '+gap+'px)';
  element.style.top='calc('+y*100+'% + '+gap+'px)';
  element.style.width='calc('+w*100+'% - '+2*gap+'px)';
  element.style.height='calc('+h*100+'% - '+2*gap+'px)';
}
function layout(key){
  const targets=areas(key);
  windows.forEach((win,i)=>{
    win.hidden=!targets[i];
    if(targets[i])positionArea(win,targets[i],key==='restore'?0:3);
  });
  mainWindow.dataset.layout=key;
  document.querySelector('#placement-status').textContent=names[key];
  for(const option of menu.querySelectorAll('[data-tile]'))option.setAttribute('aria-pressed',String(option.dataset.tile===key));
}
function drawPreview(key){
  preview.dataset.layout=key;
  preview.replaceChildren(...areas(key).map(rect=>{
    const tile=document.createElement('i');
    positionArea(tile,rect,key==='restore'?0:3);
    return tile;
  }));
  preview.hidden=false;
}
function hidePreview(){preview.hidden=true;preview.removeAttribute('data-layout');}
for(const [target,keys] of [
  ['half-options',['left','right','top','bottom']],
  ['quarter-options',['top-left','top-right','bottom-left','bottom-right']],
  ['arrange-options',['fill','pair','grid','restore']]
]){
  for(const key of keys){
    const b=document.createElement('button'); b.type='button';b.dataset.tile=key;b.title=names[key];b.setAttribute('aria-label',names[key]);b.setAttribute('aria-pressed',String(key==='restore'));
    const glyph=document.createElement('span');glyph.setAttribute('aria-hidden','true');
    if(key==='restore'){glyph.className='restore-icon';glyph.textContent='↶';}
    else{
      glyph.className='icon';
      areas(key).forEach(rect=>{const bit=document.createElement('i');positionArea(bit,rect,1);glyph.append(bit);});
    }
    b.append(glyph);
    if(shortNames[key]){
      const caption=document.createElement('small');caption.textContent=shortNames[key];caption.setAttribute('aria-hidden','true');b.append(caption);
    }
    document.getElementById(target).append(b);
  }
}
let hoverTimer,leaveTimer,mode='',lastHover=null;
function dock(){
  if(menu.hidden)return;
  const b=bench.getBoundingClientRect(),t=trigger.getBoundingClientRect();
  const width=menu.offsetWidth,height=menu.offsetHeight,innerW=bench.clientWidth,innerH=bench.clientHeight;
  const x=t.left-b.left-bench.clientLeft, y=t.top-b.top-bench.clientTop;
  const below=y+t.height+5, above=y-height-5;
  const candidate=below+height+4<=innerH||above<4?below:above;
  const left=Math.max(4,Math.min(x-10,innerW-width-4)), top=Math.max(4,Math.min(candidate,innerH-height-4));
  menu.style.left=left+'px';menu.style.top=top+'px';
  const isAbove=top+height<=y;
  caret.hidden=!(isAbove||top>=y+t.height);
  caret.toggleAttribute('data-above',isAbove);
  caret.style.left=Math.max(left+11,Math.min(x+t.width/2-5,left+width-21))+'px';
  caret.style.top=(isAbove?top+height-6:top-4)+'px';
}
const options=[...menu.querySelectorAll('[data-tile]')];
let host=null;
try{
  if(window.frameElement?.closest('dialog'))host=window.parent;
}catch{}
function escapeMenu(e){
  if(e.key!=='Escape'||menu.hidden)return;
  e.preventDefault();e.stopPropagation();closeMenu(true);
}
function openMenu(next,keyboard=false){
  clearTimeout(hoverTimer);clearTimeout(leaveTimer);
  mode=next;menu.hidden=false;trigger.setAttribute('aria-expanded','true');dock();
  // Hover can open the popover while keyboard focus is still in the surrounding dialog.
  host?.addEventListener('keydown',escapeMenu,true);
  if(keyboard)options[0].focus({preventScroll:true});
}
function closeMenu(restoreFocus){
  clearTimeout(hoverTimer);clearTimeout(leaveTimer);
  mode='';lastHover=null;menu.hidden=true;caret.hidden=true;hidePreview();trigger.setAttribute('aria-expanded','false');
  host?.removeEventListener('keydown',escapeMenu,true);
  if(restoreFocus)trigger.focus({preventScroll:true});
}
function leave(){
  clearTimeout(hoverTimer);
  if(mode==='hover')leaveTimer=setTimeout(()=>closeMenu(false),210);
}
trigger.addEventListener('pointerenter',e=>{
  clearTimeout(leaveTimer);
  const profile=document.documentElement.dataset.motionPlatform;
  if(e.pointerType==='touch'||profile==='ios'||profile==='android'||!menu.hidden)return;
  hoverTimer=setTimeout(()=>openMenu('hover'),320);
});
trigger.addEventListener('pointerleave',leave);
trigger.addEventListener('click',e=>{
  if(mode==='pinned'){closeMenu(true);return;}
  openMenu('pinned',e.detail===0);
});
trigger.addEventListener('keydown',e=>{
  if(e.key==='ArrowDown'){e.preventDefault();openMenu('pinned',true);}
});
menu.addEventListener('pointerenter',()=>clearTimeout(leaveTimer));
menu.addEventListener('pointerleave',()=>{
  lastHover=null;
  if(menu.contains(document.activeElement))drawPreview(document.activeElement.dataset.tile);
  else hidePreview();
  leave();
});
menu.addEventListener('pointerover',e=>{
  const b=e.target.closest('[data-tile]');
  if(e.pointerType!=='touch'&&b){lastHover=b;drawPreview(b.dataset.tile);}
});
menu.addEventListener('focusin',e=>{if(e.target.dataset.tile)drawPreview(e.target.dataset.tile);});
menu.addEventListener('focusout',e=>{
  if(menu.contains(e.relatedTarget))return;
  if(lastHover)drawPreview(lastHover.dataset.tile);else hidePreview();
});
menu.addEventListener('click',e=>{
  const b=e.target.closest('[data-tile]');
  if(b){layout(b.dataset.tile);closeMenu(true);}
});
menu.addEventListener('keydown',e=>{
  const index=options.indexOf(e.target);
  const offsets={ArrowLeft:-1,ArrowRight:1,ArrowUp:-4,ArrowDown:4};
  if(index<0)return;
  const dest=e.key==='Home'?0:e.key==='End'?options.length-1:index+offsets[e.key];
  if(Number.isFinite(dest)){
    e.preventDefault();
    options[(dest+options.length)%options.length].focus();
  }
});
document.addEventListener('keydown',escapeMenu,true);
document.addEventListener('pointerdown',e=>{if(!trigger.contains(e.target)&&!menu.contains(e.target))closeMenu(false);},true);
document.addEventListener('focusin',e=>{if(!menu.hidden&&!menu.contains(e.target)&&!trigger.contains(e.target))closeMenu(false);});
window.addEventListener('blur',()=>closeMenu(false));
window.addEventListener('pagehide',()=>closeMenu(false));
window.addEventListener('resize',dock);
if('ResizeObserver' in window)new ResizeObserver(dock).observe(bench);
mainWindow.addEventListener('transitionend',dock);
layout('restore');
</script></body></html>
```
