blingbling_widget_template 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. -- @author cedlemo
  2. local setmetatable = setmetatable
  3. local ipairs = ipairs
  4. local math = math
  5. local table = table
  6. local type = type
  7. local string = string
  8. local color = require("gears.color")
  9. local base = require("wibox.widget.base")
  10. local helpers = require("blingbling.helpers")
  11. local superproperties = require('blingbling.superproperties')
  12. local widgetname = { mt = {} }
  13. local data = setmetatable({}, { __mode = "k" })
  14. local properties = {}
  15. function widgetname.draw(graphtype, wibox, cr, width, height)
  16. local v_margin = superproperties.v_margin
  17. if data[p_graph].v_margin and data[p_graph].v_margin <= data[p_graph].height/4 then
  18. v_margin = data[p_graph].v_margin
  19. end
  20. local h_margin = superproperties.h_margin
  21. if data[p_graph].h_margin and data[p_graph].h_margin <= data[p_graph].width / 3 then
  22. h_margin = data[p_graph].h_margin
  23. end
  24. local background_border = data[graph].background_border or superproperties.background_border
  25. local background_color = data[graph].background_color or superproperties.background_color
  26. local rounded_size = data[graph].rounded_size or superproperties.rounded_size
  27. local graph_background_color = data[graph].graph_background_color or superproperties.graph_background_color
  28. local graph_background_border = data[graph].graph_background_border or superproperties.graph_background_border
  29. local graph_color = data[graph].graph_color or superproperties.graph_color
  30. local graph_line_color = data[graph].graph_line_color or superproperties.graph_line_color
  31. local text_color = data[graph].text_color or superproperties.text_color
  32. local text_background_color = data[graph].text_background_color or superproperties.text_background_color
  33. local font_size =data[graph].font_size or superproperties.font_size
  34. local font = data[graph].font or superproperties.font
  35. end
  36. function widgetname.fit(graphtype, width, height)
  37. return data[graphtype].width, data[graphtype].height
  38. end
  39. --- Add a value to the graphtype
  40. -- For compatibility between old and new awesome widget, add_value can be replaced by set_value
  41. -- @usage mygraph:add_value(a) or mygraph:set_value(a)
  42. -- @param graphtype The graphtype.
  43. -- @param value The value between 0 and 1.
  44. -- @param group The stack color group index.
  45. local function add_value(graphtype, value, group)
  46. if not graph then return end
  47. local value = value or 0
  48. local values = data[graphtype].values
  49. if string.find(value, "nan") then
  50. value=0
  51. end
  52. local values = data[graphtype].values
  53. table.remove(values, #values)
  54. table.insert(values,1,value)
  55. graphtype:emit_signal("widget::updated")
  56. return graphtype
  57. end
  58. --or
  59. --- Set the graphtype value.
  60. -- @param p_graph The progress bar.
  61. -- @param value The progress bar value between 0 and 1.
  62. local function set_value(graphtype, value)
  63. local value = value or 0
  64. local max_value = data[graphtype].max_value
  65. data[graphtype].value = math.min(max_value, math.max(0, value))
  66. graphtype:emit_signal("widget::updated")
  67. return graphtype
  68. end
  69. --- Set the graphtype height.
  70. -- @param graphtype The graph.
  71. -- @param height The height to set.
  72. function widgetname:set_height( height)
  73. if height >= 5 then
  74. data[self].height = height
  75. self:emit_signal("widget::updated")
  76. end
  77. return self
  78. end
  79. --- Set the graph width.
  80. -- @param graph The graph.
  81. -- @param width The width to set.
  82. function widgetname:set_width( width)
  83. if width >= 5 then
  84. data[self].width = width
  85. self:emit_signal("widget::updated")
  86. end
  87. return self
  88. end
  89. -- Build properties function
  90. for _, prop in ipairs(properties) do
  91. if not widgetname["set_" .. prop] then
  92. widgetname["set_" .. prop] = function(graphtype, value)
  93. data[graphtype][prop] = value
  94. graphtype:emit_signal("widget::updated")
  95. return graphtype
  96. end
  97. end
  98. end
  99. --- Create a graphtype widget.
  100. -- @param args Standard widget() arguments. You should add width and height
  101. -- key to set graph geometry.
  102. -- @return A graph widget.
  103. function widgetname.new(args)
  104. local args = args or {}
  105. args.width = args.width or 100
  106. args.height = args.height or 20
  107. if args.width < 5 or args.height < 5 then return end
  108. local graphtype = base.make_widget()
  109. data[graphtype] = {}
  110. for _, v in ipairs(properties) do
  111. data[graphtype][v] = args[v]
  112. end
  113. data[graphtype].values = {}
  114. --or
  115. data[graphtype].value = 0
  116. data[graphtype].max_value = 1
  117. -- Set methods
  118. graphtype.set_value = add_value
  119. graphtype.add_value = add_value
  120. graphtype.draw = widgetname.draw
  121. graphtype.fit = widgetname.fit
  122. for _, prop in ipairs(properties) do
  123. graphtype["set_" .. prop] = widgetname["set_" .. prop]
  124. end
  125. return graphtype
  126. end
  127. function widgetname.mt:__call(...)
  128. return widgetname.new(...)
  129. end
  130. return setmetatable(widgetname, widgetname.mt)