Advertisement
ahoka

tile.lua with padding

Dec 19th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.76 KB | None | 0 0
  1. ---------------------------------------------------------------------------
  2. -- @author Donald Ephraim Curtis <[email protected]>
  3. -- @author Julien Danjou <[email protected]>
  4. -- @copyright 2009 Donald Ephraim Curtis
  5. -- @copyright 2008 Julien Danjou
  6. -- @release v3.4.13
  7. ---------------------------------------------------------------------------
  8.  
  9. -- Grab environment we need
  10. local ipairs = ipairs
  11. local tonumber = tonumber
  12. local beautiful = beautiful
  13. local math = math
  14. local tag = require("awful.tag")
  15.  
  16. --- Tiled layouts module for awful
  17. module("awful.layout.suit.tile")
  18.  
  19. local function tile_group(cls, wa, orientation, fact, group)
  20.     local padding = tonumber(beautiful.padding)
  21.     if padding == nil then
  22.         padding = 0
  23.     end
  24.     -- get our orientation right
  25.     local height = "height"
  26.     local width = "width"
  27.     local x = "x"
  28.     local y = "y"
  29.     if orientation == "top" or orientation == "bottom" then
  30.         height = "width"
  31.         width = "height"
  32.         x = "y"
  33.         y = "x"
  34.     end
  35.  
  36.     -- make this more generic (not just width)
  37.     available = wa[width] - (group.coord - wa[x])
  38.  
  39.     -- find our total values
  40.     local total_fact = 0
  41.     local min_fact = 1
  42.     local size = group.size
  43.     for c = group.first,group.last do
  44.         -- determine the width/height based on the size_hint
  45.         local i = c - group.first +1
  46.         local size_hints = cls[c].size_hints
  47.         local size_hint = size_hints["min_"..width] or size_hints["base_"..width] or 0
  48.         size_hint = size_hint + cls[c].border_width*2
  49.         size = math.max(size_hint, size)
  50.  
  51.         -- calculate the height
  52.         if not fact[i] then
  53.             fact[i] = min_fact
  54.         else
  55.             min_fact = math.min(fact[i],min_fact)
  56.         end
  57.         total_fact = total_fact + fact[i]
  58.     end
  59.     size = math.min(size, available)
  60.  
  61.     local coord = wa[y]
  62.     local geom = {}
  63.     local used_size = 0
  64.     local unused = wa[height]
  65.     for c = group.first,group.last do
  66.         local i = c - group.first +1
  67.         geom[width] = size - padding * (group.first == 1 and 2 or 1)
  68.         geom[height] = math.floor(unused * fact[i] / total_fact) - padding * 2
  69.         geom[x] = group.coord + ((orientation ~= "right" or group.first == 1) and padding or 0)
  70.         geom[y] = coord + padding
  71.         geom = cls[c]:geometry(geom)
  72.         coord = coord + geom[height] + padding
  73.         unused = unused - geom[height] - padding
  74.         total_fact = total_fact - fact[i]
  75.         used_size = math.max(used_size, size)
  76.     end
  77.  
  78.     return used_size
  79. end
  80.  
  81. local function tile(param, orientation)
  82.     local t = tag.selected(param.screen)
  83.     orientation = orientation or "right"
  84.  
  85.     -- this handles are different orientations
  86.     local height = "height"
  87.     local width = "width"
  88.     local x = "x"
  89.     local y = "y"
  90.     if orientation == "top" or orientation == "bottom" then
  91.         height = "width"
  92.         width = "height"
  93.         x = "y"
  94.         y = "x"
  95.     end
  96.  
  97.     local cls = param.clients
  98.     local nmaster = math.min(tag.getnmaster(t), #cls)
  99.     local nother = math.max(#cls - nmaster,0)
  100.  
  101.     local mwfact = tag.getmwfact(t)
  102.     local wa = param.workarea
  103.     local ncol = tag.getncol(t)
  104.  
  105.     local data = tag.getdata(t).windowfact
  106.  
  107.     if not data then
  108.         data = {}
  109.         tag.getdata(t).windowfact = data
  110.     end
  111.  
  112.     local coord = wa[x]
  113.     local place_master = true
  114.     if orientation == "left" or orientation == "top" then
  115.         -- if we are on the left or top we need to render the other windows first
  116.         place_master = false
  117.     end
  118.  
  119.     -- this was easier than writing functions because there is a lot of data we need
  120.     for d = 1,2 do
  121.         if place_master and nmaster > 0 then
  122.             local size = wa[width]
  123.             if nother > 0 then
  124.                 size = math.min(wa[width] * mwfact, wa[width] - (coord - wa[x]))
  125.             end
  126.             if not data[0] then
  127.                 data[0] = {}
  128.             end
  129.             coord = coord + tile_group(cls, wa, orientation, data[0], { first = 1, last = nmaster, coord = coord, size = size })
  130.         end
  131.  
  132.         if not place_master and nother > 0 then
  133.             local last = nmaster
  134.  
  135.             -- we have to modify the work area size to consider left and top views
  136.             local wasize = wa[width]
  137.             if nmaster > 0 and (orientation == "left" or orientation == "top") then
  138.                 wasize = wa[width] - wa[width]*mwfact
  139.             end
  140.             for i = 1,ncol do
  141.                 -- Try to get equal width among remaining columns
  142.                 local size = math.min( (wasize - (coord - wa[x])) / (ncol - i + 1) )
  143.                 local first = last + 1
  144.                 last = last + math.floor((#cls - last)/(ncol - i + 1))
  145.                 -- tile the column and update our current x coordinate
  146.                 if not data[i] then
  147.                     data[i] = {}
  148.                 end
  149.                 coord = coord + tile_group(cls, wa, orientation, data[i], { first = first, last = last, coord = coord, size = size })
  150.             end
  151.         end
  152.         place_master = not place_master
  153.     end
  154.  
  155. end
  156.  
  157. right = {}
  158. right.name = "tile"
  159. right.arrange = tile
  160.  
  161. --- The main tile algo, on left.
  162. -- @param screen The screen number to tile.
  163. left = {}
  164. left.name = "tileleft"
  165. function left.arrange(p)
  166.     return tile(p, "left")
  167. end
  168.  
  169. --- The main tile algo, on bottom.
  170. -- @param screen The screen number to tile.
  171. bottom = {}
  172. bottom.name = "tilebottom"
  173. function bottom.arrange(p)
  174.     return tile(p, "bottom")
  175. end
  176.  
  177. --- The main tile algo, on top.
  178. -- @param screen The screen number to tile.
  179. top = {}
  180. top.name = "tiletop"
  181. function top.arrange(p)
  182.     return tile(p, "top")
  183. end
  184.  
  185. arrange = right.arrange
  186. name = right.name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement