local Y=require("yaru.core") local gfx=require("yaru.gfx") local ui=require("yaru.ui") local unicode=require("unicode") local M={} function M.open(dir) local o=Y.overlay if o and o.kind=="switcher"then return o.step(dir)end local list={} for i=#Y.windows,1,-1 do local w=Y.windows[i] if w.ws==Y.ws then list[#list+1]=w end end if#list==0 then return end o={kind="switcher",sel=#list>1 and(dir<0 and#list or 2)or 1} local W,H=Y.size() local cw=16 local n=math.min(#list,(W-8)//cw) local bw=n*cw+2 local x0,y0=(W-bw)//2,H//2-3 local function activate() local w=list[o.sel] Y.closeOverlay() if w then Y.focusWin(w)end end function o.step(d) o.sel=(o.sel-1+d)%#list+1 gfx.damage(x0,y0-1,bw+1,12) end function o.draw() local t=Y.theme ui.card(x0,y0,bw,9,t.menu) local first=math.max(1,math.min(o.sel-n+1,#list-n+1)) for i=first,math.min(#list,first+n-1)do local w=list[i] local x=x0+1+(i-first)*cw local sel=i==o.sel local bg=sel and t.menuHi or t.menu if sel then ui.round(x,y0+1,cw-1,6,bg)end local a=Y.apps[w.app] Y.drawIcon(x+(cw-9)//2,y0+1,a and a.icon or"generic") local name=ui.trunc(a and a.name or(w.title or""),cw-2) gfx.text(x+(cw-1-unicode.len(name))//2,y0+5,name,t.menuFg,bg) local idx=i o.hits[#o.hits+1]={x,y0+1,cw-1,6,function()o.sel=idx;activate()end} end local title=ui.trunc(list[o.sel].title or"",bw-4) gfx.text(x0+(bw-unicode.len(title))//2,y0+7,title,t.dim,t.menu) end function o.keyUp(_,char,code) local K=Y.K if code==K.lalt or code==K.ralt or code==K.lsuper or code==K.rsuper or code==K.lctrl or code==K.rctrl then if not(Y.mods.alt()or Y.mods.super()or Y.mods.ctrl())then activate()end end end function o.key(_,char,code) local K=Y.K if code==K.esc then Y.closeOverlay() elseif code==K.enter then activate() elseif code==K.left then o.step(-1) elseif code==K.right or code==K.tab then o.step(1)end end function o.touch()Y.closeOverlay()return true end Y.openOverlay(o) return o end return M