Radical – Launcher for Awesome(-git)

So, I upgraded to awesome-git a few days ago, and statusbars have been replaced with the more general ‘wiboxes’ widget boxes, and so, I just had to make something with them.

The result is radical, a launcher for awesome, similar to wbar or more loosely, awn.

The code:
Although it will hopefully find its way in to awesome’s source-code, for now you can just get it below.
(for usage, see my latest rc.lua, found, as always, here

Place the following in /usr/share/awesome/lib/radical.lua


local wibox = wibox
local widget = widget
local button = button
local image = image
local hooks = require("awful.hooks")
local util = require("awful.util")
local mouse = mouse
local screen = screen
local print = print
local math = math
local awbeautiful = require("awful.beautiful")

module("radical")
---------------------------------------------------
--Variable Declaration--------------------
local beautiful
local width
local maxWidth
local yOffset
local ontop
local update
local launchers
local defaults = 
{
width=75, maxWidth=125, yOffset = 50, ontop = true, update=0.05, launchers = {}
}
--Calculated Values---------------
local screenWidth = screen[1].geometry.width
local screenHeight = screen[1].geometry.height
local s
local r
local totalWidth
local initialOffset
--Other Values--------------------
local items = {}
local selected = -1
-----------------------------------------

----------------------------------------
--Utility Functions
local function clickableIcon(iconpath, align, callback)
   icon = widget({ type="imagebox", name="icon", align=align })
   icon.image = image(iconpath)
   icon:buttons({
		   button({ }, 1, callback),
		})
   return icon
end

local function load_theme(custom)
    beautiful = awbeautiful.get()
 end

local function recalculate()
   s = maxWidth/width
   r = width/2
   totalWidth = #launchers * width
   initialOffset = (screenWidth - totalWidth)/2
end
--------------------------------------------

--------------------------------------------
function init(values)
   print("initialising radical")
   width = values.width or defaults.width
   maxWidth = values.maxWidth or defaults.maxWidth
   yOffset = values.yOffset or defaults.yOffset
   ontop = values.ontop or defaults.ontop
   update = values.update or defaults.update
   launchers = values.launchers or defaults.launchers
   recalculate()
   load_theme()
   initItems()
end

local function updatePositions()
   if(selected == -1 or selected > #items) then
      return
   end
   local x = mouse.coords().x
   local o = items[selected]['centre'] - x  --offset
   if o < 0 then o = o * -1 end --make positive
   local n = ((r-o)/r*(s-1)+1)*width --newwidth
   local m = n/2
   local m = math.floor(m) - r
   for i = 1, #items do
      local normCentre = initialOffset + i * width - r
      if i < selected then
	 items&#91;i&#93;&#91;'centre'&#93; = normCentre - m
      else if i > selected then
	    items[i]['centre'] = normCentre + m
	 else
	    items[i]['centre'] = normCentre
	    items[i]['width'] = n
	    items[i]['height'] = n
	 end
      end
   end
   ---print("(" .. selected .. ") update positions: xonms = " .. x .. " " .. o .. " " .. n .. " " .. m)
   displayItems() --update positions
end

local function mouseOn(index)
   items[index]['wibox'].bg = beautiful.bg_focus
   selected = index
   hooks.timer.register(update, updatePositions)
end

local function mouseOff(index)
   items[index]['wibox'].bg = beautiful.bg_normal
   hooks.timer.unregister(updatePositions)
   selected = -1
   for i = 1, #launchers do
      initItem(i)
   end
   displayItems() --update positions
end

function initItem(i)
   items[i]['width'] = width
   items[i]['height'] = width
   items[i]['centre'] = initialOffset + i * width - r
end

function initItems()
   for i = 1, #launchers do
      items[i] = {}
      local wi = wibox(
		       {  bg=beautiful.bg_normal, 
			  position="floating"
		       })
      wi:geometry({x=0, y=0, width=100, height=100}) --just random initial values
      wi.screen = 1
      wi.visible = false
      wi.ontop = ontop
      local ico = clickableIcon(launchers[i][1], "left", 
				function() util.spawn(launchers[i][2]) end
			     )
      function ico.mouse_enter() mouseOn(i) end
      function ico.mouse_leave() mouseOff(i) end
      wi.widgets = {ico}
      items[i]['wibox'] = wi
      initItem(i)
   end
end

function displayItems()
  -- print "displaying items"
   for i = 1, #items do
      local x = items[i]['centre'] - items[i]['width']/2
      items[i]['wibox']:geometry(
				 {width = items[i]['width'],
				    height = items[i]['width'],
				    x = x}
			      )
      items[i]['wibox'].visible = true
   end
end

function hideItems()
   --print "hiding items"
   for i = 1, #items do
      items[i]['wibox'].visible = false
   end
end
Advertisements
Radical – Launcher for Awesome(-git)

2 thoughts on “Radical – Launcher for Awesome(-git)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s