local Y=require("yaru.core") local gfx=require("yaru.gfx") local ui=require("yaru.ui") local image=require("yaru.image") local fs=require("filesystem") local unicode=require("unicode") local M={} local function listPics(dir) local r={} if fs.isDirectory(dir)then for n in fs.list(dir)do if n:lower():match("%.pic$")then r[#r+1]=fs.concat(dir,n)end end end table.sort(r) return r end function M.open(path) local st={} local win=Y.newWindow{app="viewer",title="Image Viewer",w=70,h=26,minW=30,minH=8} local function load(p) st.path,st.err,st.img=p,nil,nil if p then local img,err=image.load(p,true) st.img,st.err=img,err st.list=listPics(fs.path(p)) for i,q in ipairs(st.list)do if q==p then st.idx=i end end win.title=fs.name(p) end end local function step(d) if not st.list or#st.list==0 then return end st.idx=((st.idx or 1)-1+d)%#st.list+1 load(st.list[st.idx]) end function win.header(w,hw) ui.button(w,2,1,"Open…",function() Y.dialog{title="Open Image",text="Path to a .pic file",entry=st.path or"/home/Pictures/", buttons={{"Cancel"},{"Open",function(p)if p and p~=""then load(p)end end,"suggested"}}} end,nil,8) ui.button(w,11,1,"‹",function()step(-1)end,"flat",3) ui.button(w,14,1,"›",function()step(1)end,"flat",3) if st.path then ui.button(w,18,1,"Edit",function()Y.launch("paint",st.path)end,"flat",6) ui.button(w,25,1,"Set as Background",function() Y.cfg.wallpaper=st.path;Y.saveConfig();Y.applyTheme() end,"flat") end end function win.draw(w,cw,ch) local t=Y.theme gfx.fill(1,1,cw,ch,t.dark and 0x1E1E1E or 0xEBEBEB) if st.img then local iw,ih=st.img[1],st.img[2] if iw<=cw and ih<=ch-1 then gfx.image((cw-iw)//2+1,(ch-1-ih)//2+1,st.img) else local s=math.min(cw/iw,(ch-1)/ih) local fw,fh=math.max(1,math.floor(iw*s)),math.max(1,math.floor(ih*s)) gfx.imageScaled((cw-fw)//2+1,(ch-1-fh)//2+1,fw,fh,st.img) end local info=string.format("%dx%d · %d of %d",iw,ih,st.idx or 1,st.list and#st.list or 1) gfx.text((cw-unicode.len(info))//2,ch,info,t.dim) else local s=st.err or"Open a picture (.pic, MineOS OCIF format)" gfx.text(math.max(1,(cw-unicode.len(s))//2),ch//2,ui.trunc(s,cw-2),t.dim) end end function win.key(w,char,code) local K=Y.K if code==K.left or code==K.pgup then step(-1)elseif code==K.right or code==K.pgdn or code==0x39 then step(1)end end function win.scroll(w,x,y,dir)step(-dir)end load(path) return win end return M