Permalink
Browse files

Add abstract resizing

  • Loading branch information...
1 parent 290ede8 commit 1bb388a4d159553c87ca21bc91a30723ee884a15 @Elv13 committed Jul 4, 2012
View
@@ -1,45 +0,0 @@
----------------------------------------------------------------------------
--- @author Julien Danjou <julien@danjou.info>
--- @copyright 2008-2009 Julien Danjou
--- @release v3.4-rc3
----------------------------------------------------------------------------
-
-local setmetatable = setmetatable
-local type = type
-local button = require("awful.button")
-local capi = { image = image,
- widget = widget,
- mouse = mouse }
-
-module("awful.widget.button")
-
---- Create a button widget. When clicked, the image is deplaced to make it like
--- a real button.
--- @param args Standard widget table arguments, plus image for the image path or
--- the image object.
--- @return A textbox widget configured as a button.
-function new(args)
- if not args or not args.image then return end
- local img_release
- if type(args.image) == "string" then
- img_release = capi.image(args.image)
- elseif type(args.image) == "image" then
- img_release = args.image
- else
- return
- end
- local img_press = img_release:crop(-2, -2, img_release.width, img_release.height)
- args.type = "imagebox"
- local w = capi.widget(args)
- w.image = img_release
- w:buttons(button({}, 1, function () w.image = img_press end, function () w.image = img_release end))
- w:add_signal("mouse::leave", function () w.image = img_release end)
- w:add_signal("mouse::enter", function ()
- if capi.mouse.coords().buttons[1] then w.image = img_press end
- end)
- return w
-end
-
-setmetatable(_M, { __call = function(_, ...) return new(...) end })
-
--- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
View
@@ -1,87 +0,0 @@
----------------------------------------------------------------------------
--- @author Julien Danjou <julien@danjou.info>
--- @copyright 2008-2009 Julien Danjou
--- @release v3.4-rc3
----------------------------------------------------------------------------
-
--- Grab environment we need
-local math = math
-local type = type
-local ipairs = ipairs
-local setmetatable = setmetatable
-local capi = { widget = widget, button = button }
-
---- Common widget code
-module("awful.widget.common")
-
--- Private structures
-tagwidgets = setmetatable({}, { __mode = 'k' })
-
-function list_update(w, buttons, label, data, widgets, objects)
- -- Hack: if it has been registered as a widget in a wibox,
- -- it's w.len since __len meta does not work on table until Lua 5.2.
- -- Otherwise it's standard #w.
- local len = (w.len or #w) / 2
- -- Add more widgets
- if len < #objects then
- for i = len * 2 + 1, #objects * 2, 2 do
- local ib = capi.widget({ type = "imagebox", align = widgets.imagebox.align })
- local tb = capi.widget({ type = "textbox", align = widgets.textbox.align })
-
- w[i] = ib
- w[i + 1] = tb
- w[i + 1]:margin({ left = widgets.textbox.margin.left, right = widgets.textbox.margin.right })
- w[i + 1].bg_resize = widgets.textbox.bg_resize or false
- w[i + 1].bg_align = widgets.textbox.bg_align or ""
-
- if type(objects[math.floor(i / 2) + 1]) == "tag" then
- tagwidgets[ib] = objects[math.floor(i / 2) + 1]
- tagwidgets[tb] = objects[math.floor(i / 2) + 1]
- end
- end
- -- Remove widgets
- elseif len > #objects then
- for i = #objects * 2 + 1, len * 2, 2 do
- w[i] = nil
- w[i + 1] = nil
- end
- end
-
- -- update widgets text
- for k = 1, #objects * 2, 2 do
- local o = objects[(k + 1) / 2]
- if buttons then
- if not data[o] then
- data[o] = { }
- for kb, b in ipairs(buttons) do
- -- Create a proxy button object: it will receive the real
- -- press and release events, and will propagate them the the
- -- button object the user provided, but with the object as
- -- argument.
- local btn = capi.button { modifiers = b.modifiers, button = b.button }
- btn:add_signal("press", function () b:emit_signal("press", o) end)
- btn:add_signal("release", function () b:emit_signal("release", o) end)
- data[o][#data[o] + 1] = btn
- end
- end
- w[k]:buttons(data[o])
- w[k + 1]:buttons(data[o])
- end
-
- local text, bg, bg_image, icon = label(o)
- w[k + 1].text, w[k + 1].bg, w[k + 1].bg_image = text, bg, bg_image
- w[k].bg, w[k].image = bg, icon
- if not w[k + 1].text then
- w[k+1].visible = false
- else
- w[k+1].visible = true
- end
- if not w[k].image then
- w[k].visible = false
- else
- w[k].visible = true
- end
- end
-end
-
--- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
View
@@ -1,237 +0,0 @@
----------------------------------------------------------------------------
--- @author Julien Danjou &lt;julien@danjou.info&gt;
--- @copyright 2009 Julien Danjou
--- @release v3.4-rc3
----------------------------------------------------------------------------
-
-local setmetatable = setmetatable
-local ipairs = ipairs
-local math = math
-local table = table
-local capi = { image = image,
- widget = widget }
-local layout = require("awful.widget.layout")
-
---- A graph widget.
-module("awful.widget.graph")
-
-local data = setmetatable({}, { __mode = "k" })
-
---- Set the graph border color.
--- If the value is nil, no border will be drawn.
--- @name set_border_color
--- @class function
--- @param graph The graph.
--- @param color The border color to set.
-
---- Set the graph foreground color as a gradient.
--- @name set_gradient_colors
--- @class function
--- @param graph The graph.
--- @param gradient_colors A table with gradients colors. The distance between each color
--- can also be specified. Example: { "red", "blue" } or { "red", "green",
--- "blue", blue = 10 } to specify blue distance from other colors.
-
---- Set the graph foreground colors gradient angle. Default is 270 degrees
--- (horizontal).
--- @name set_gradient_angle
--- @class function
--- @param graph The graph.
--- @param gradient_angle Angle of gradient in degrees.
-
---- Set the graph foreground color.
--- @name set_color
--- @class function
--- @param graph The graph.
--- @param color The graph color.
-
---- Set the graph background color.
--- @name set_background_color
--- @class function
--- @param graph The graph.
--- @param color The graph background color.
-
---- Set the maximum value the graph should handle.
--- If "scale" is also set, the graph never scales up below this value, but it
--- automatically scales down to make all data fit.
--- @name set_max_value
--- @class function
--- @param graph The graph.
--- @param value The value.
-
---- Set the graph to automatically scale its values. Default is false.
--- @name set_scale
--- @class function
--- @param graph The graph.
--- @param scale A boolean value
-
-local properties = { "width", "height", "border_color",
- "gradient_colors", "gradient_angle", "color",
- "background_color", "max_value", "scale" }
-
-local function update(graph)
- -- Create new empty image
- local img = capi.image.argb32(data[graph].width, data[graph].height, nil)
-
- local border_width = 0
- if data[graph].border_color then
- border_width = 1
- end
-
- local values = data[graph].values
- local max_value = data[graph].max_value
-
- if data[graph].scale then
- for _, v in ipairs(values) do
- if v > max_value then
- max_value = v
- end
- end
- end
-
- -- Draw background
- -- Draw full gradient
- if data[graph].gradient_colors then
- img:draw_rectangle_gradient(border_width, border_width+3,
- data[graph].width - (2 * border_width),
- data[graph].height - (2 * border_width)-6,
- data[graph].gradient_colors,
- data[graph].gradient_angle or 270)
- else
- img:draw_rectangle(border_width, border_width +3,
- data[graph].width - (2 * border_width),
- data[graph].height - (2 * border_width)-6,
- true, data[graph].color or "red")
- end
-
- -- No value? Draw nothing.
- if #values ~= 0 then
- -- Draw reverse
- for i = 0, #values - 1 do
- local value = values[#values - i]
- if value >= 0 then
- value = value / max_value
- img:draw_line(data[graph].width - border_width - i - 1,
- border_width + ((data[graph].height - 2 * border_width) * (1 - value)) +3,
- data[graph].width - border_width - i - 1,
- border_width,
- data[graph].background_color or "#000000aa")
- end
- end
- end
-
- -- If we did not draw values everywhere, draw a square over the last left
- -- part to set everything to 0 :-)
- if #values < data[graph].width - (2 * border_width) then
- img:draw_rectangle(border_width, border_width,
- data[graph].width - (2 * border_width) - #values,
- data[graph].height - (2 * border_width)-6,
- true, data[graph].background_color or "#000000aa")
- end
-
- -- Draw the border last so that it overlaps other stuff
- if data[graph].border_color then
- -- Draw border
- img:draw_rectangle(0, 3, data[graph].width, data[graph].height -6,
- false, data[graph].border_color or "white")
- end
-
- -- Update the image
- graph.widget.image = img
-end
-
---- Add a value to the graph
--- @param graph The graph.
--- @param value The value between 0 and 1.
-local function add_value(graph, value)
- if not graph then return end
-
- local value = value or 0
- local max_value = data[graph].max_value
- value = math.max(0, value)
- if not data[graph].scale then
- value = math.min(max_value, value)
- end
-
- table.insert(data[graph].values, value)
-
- local border_width = 0
- if data[graph].border then border_width = 2 end
-
- -- Ensure we never have more data than we can draw
- while #data[graph].values > data[graph].width - border_width do
- table.remove(data[graph].values, 1)
- end
-
- update(graph)
- return graph
-end
-
-
---- Set the graph height.
--- @param graph The graph.
--- @param height The height to set.
-function set_height(graph, height)
- if height >= 5 then
- data[graph].height = height
- update(graph)
- end
- return graph
-end
-
---- Set the graph width.
--- @param graph The graph.
--- @param width The width to set.
-function set_width(graph, width)
- if width >= 5 then
- data[graph].width = width
- update(graph)
- end
- return graph
-end
-
--- Build properties function
-for _, prop in ipairs(properties) do
- if not _M["set_" .. prop] then
- _M["set_" .. prop] = function(graph, value)
- data[graph][prop] = value
- update(graph)
- return graph
- end
- end
-end
-
---- Create a graph widget.
--- @param args Standard widget() arguments. You should add width and height
--- key to set graph geometry.
--- @return A graph widget.
-function new(args)
- local args = args or {}
- args.type = "imagebox"
-
- local width = args.width or 100
- local height = args.height or 20
-
- if width < 5 or height < 5 then return end
-
- local graph = {}
- graph.widget = capi.widget(args)
- graph.widget.resize = false
-
- data[graph] = { width = width, height = height, values = {}, max_value = 1 }
-
- -- Set methods
- graph.add_value = add_value
-
- for _, prop in ipairs(properties) do
- graph["set_" .. prop] = _M["set_" .. prop]
- end
-
- graph.layout = args.layout or layout.horizontal.leftright
-
- return graph
-end
-
-setmetatable(_M, { __call = function(_, ...) return new(...) end })
-
--- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
View
@@ -1,21 +0,0 @@
----------------------------------------------------------------------------
--- @author Julien Danjou &lt;julien@danjou.info&gt;
--- @copyright 2008-2009 Julien Danjou
--- @release v3.4-rc3
----------------------------------------------------------------------------
-
-require("awful.widget.taglist")
-require("awful.widget.tasklist")
-require("awful.widget.button")
-require("awful.widget.launcher")
-require("awful.widget.prompt")
-require("awful.widget.progressbar")
-require("awful.widget.graph")
-require("awful.widget.layoutbox")
-require("awful.widget.textclock")
-require("awful.widget.layout")
-
---- Widget module for awful
-module("awful.widget")
-
--- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
Oops, something went wrong.

0 comments on commit 1bb388a

Please sign in to comment.