REF move all widget building to common

This commit is contained in:
Nathan Dwarshuis 2021-07-11 23:57:56 -04:00
parent b5f3272c30
commit 0c655fadb9
3 changed files with 130 additions and 118 deletions

View File

@ -3,6 +3,7 @@ local M = {}
local Util = require 'Util' local Util = require 'Util'
local Arc = require 'Arc' local Arc = require 'Arc'
local Text = require 'Text' local Text = require 'Text'
local CompoundBar = require 'CompoundBar'
local CriticalText = require 'CriticalText' local CriticalText = require 'CriticalText'
local TextColumn = require 'TextColumn' local TextColumn = require 'TextColumn'
local Line = require 'Line' local Line = require 'Line'
@ -56,19 +57,13 @@ end
M.normal_font_spec = M.make_font_spec(FONT, NORMAL_FONT_SIZE, false) M.normal_font_spec = M.make_font_spec(FONT, NORMAL_FONT_SIZE, false)
M.label_font_spec = M.make_font_spec(FONT, PLOT_LABEL_FONT_SIZE, false) M.label_font_spec = M.make_font_spec(FONT, PLOT_LABEL_FONT_SIZE, false)
M.left_text_style = _G_Widget_.text_style( local _text_row_style = function(x_align, color)
M.normal_font_spec, return _G_Widget_.text_style(M.normal_font_spec, color, x_align, 'center')
_G_Patterns_.INACTIVE_TEXT_FG, end
'left',
'center'
)
M.right_text_style = _G_Widget_.text_style( M.left_text_style = _text_row_style('left', _G_Patterns_.INACTIVE_TEXT_FG)
M.normal_font_spec,
_G_Patterns_.PRIMARY_FG, M.right_text_style = _text_row_style('right', _G_Patterns_.PRIMARY_FG)
'right',
'center'
)
local _bare_text = function(pt, text, style) local _bare_text = function(pt, text, style)
return _G_Widget_.plainText(pt, text, style) return _G_Widget_.plainText(pt, text, style)
@ -236,6 +231,19 @@ M.annotated_scale_plot_set = function(asp, cr, value)
ScalePlot.update(asp.plot, cr, value) ScalePlot.update(asp.plot, cr, value)
end end
--------------------------------------------------------------------------------
-- arc (TODO this is just a dummy now to make everything organized
-- TODO perhaps implement this is a special case of compound dial where
-- I have multiple layers on top of each other
M.arc = function(x, y, r, thickness, pattern)
return _G_Widget_.Arc(
_G_Widget_.make_semicircle(x, y, r, 90, 360),
_G_Widget_.arc_style(thickness, pattern)
)
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- ring -- ring
@ -282,15 +290,19 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- dial -- dial
local threshold_indicator = function(threshold)
return _G_Widget_.threshold_style(
_G_Patterns_.INDICATOR_FG_PRIMARY,
_G_Patterns_.INDICATOR_FG_CRITICAL,
threshold
)
end
M.dial = function(x, y, radius, thickness, threshold) M.dial = function(x, y, radius, thickness, threshold)
return _G_Widget_.Dial( return _G_Widget_.Dial(
_G_Widget_.make_semicircle(x, y, radius, DIAL_THETA0, DIAL_THETA1), _G_Widget_.make_semicircle(x, y, radius, DIAL_THETA0, DIAL_THETA1),
_G_Widget_.arc_style(thickness, _G_Patterns_.INDICATOR_BG), _G_Widget_.arc_style(thickness, _G_Patterns_.INDICATOR_BG),
_G_Widget_.threshold_style( threshold_indicator(threshold)
_G_Patterns_.INDICATOR_FG_PRIMARY,
_G_Patterns_.INDICATOR_FG_CRITICAL,
threshold
)
) )
end end
@ -302,16 +314,53 @@ M.compound_dial = function(x, y, outer_radius, inner_radius, thickness,
return _G_Widget_.CompoundDial( return _G_Widget_.CompoundDial(
_G_Widget_.make_semicircle(x, y, outer_radius, DIAL_THETA0, DIAL_THETA1), _G_Widget_.make_semicircle(x, y, outer_radius, DIAL_THETA0, DIAL_THETA1),
_G_Widget_.arc_style(thickness, _G_Patterns_.INDICATOR_BG), _G_Widget_.arc_style(thickness, _G_Patterns_.INDICATOR_BG),
_G_Widget_.threshold_style( threshold_indicator(threshold),
_G_Patterns_.INDICATOR_FG_PRIMARY,
_G_Patterns_.INDICATOR_FG_CRITICAL,
threshold
),
inner_radius, inner_radius,
num_dials num_dials
) )
end end
--------------------------------------------------------------------------------
-- annotated compound bar
M.compound_bar = function(x, y, w, pad, labels, spacing, thickness, threshold)
return {
labels = _G_Widget_.TextColumn(
_G_Widget_.make_point(x, y),
labels,
M.left_text_style,
nil,
spacing
),
bars = _G_Widget_.CompoundBar(
_G_Widget_.make_point(x + pad, y),
w - pad,
_G_Widget_.line_style(
thickness,
_G_Patterns_.INDICATOR_BG,
CAIRO_LINE_JOIN_MITER
),
threshold_indicator(threshold),
spacing,
#labels,
false
)
}
end
M.compound_bar_draw_static = function(cb, cr)
TextColumn.draw(cb.labels, cr)
CompoundBar.draw_static(cb.bars, cr)
end
M.compound_bar_draw_dynamic = function(cb, cr)
CompoundBar.draw_dynamic(cb.bars, cr)
end
M.compound_bar_set = function(cb, i, value)
CompoundBar.set(cb.bars, i, value)
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- separator (eg a horizontal line) -- separator (eg a horizontal line)
@ -330,7 +379,6 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- text row (label with a value, aligned as far apart as possible) -- text row (label with a value, aligned as far apart as possible)
M.initTextRow = function(x, y, w, label) M.initTextRow = function(x, y, w, label)
return { return {
label = _left_text(_G_Widget_.make_point(x, y), label), label = _left_text(_G_Widget_.make_point(x, y), label),
@ -381,10 +429,23 @@ M.text_row_crit_set = function(row, cr, value)
CriticalText.set(row.value, cr, value) CriticalText.set(row.value, cr, value)
end end
--------------------------------------------------------------------------------
-- text column
M.text_column = function(x, y, spacing, labels, x_align, color)
return _G_Widget_.TextColumn(
_G_Widget_.make_point(x, y),
labels,
_text_row_style(x_align, color),
nil,
spacing
)
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- multiple text row separated by spacing -- multiple text row separated by spacing
M.initTextRows = function(x, y, w, spacing, labels) M.initTextRows_color = function(x, y, w, spacing, labels, color, format)
return { return {
labels = _G_Widget_.TextColumn( labels = _G_Widget_.TextColumn(
_G_Widget_.make_point(x, y), _G_Widget_.make_point(x, y),
@ -396,13 +457,25 @@ M.initTextRows = function(x, y, w, spacing, labels)
values = _G_Widget_.initTextColumnN( values = _G_Widget_.initTextColumnN(
_G_Widget_.make_point(x + w, y), _G_Widget_.make_point(x + w, y),
#labels, #labels,
M.right_text_style, _text_row_style('right', color),
nil, format,
spacing spacing
) )
} }
end end
M.initTextRows = function(x, y, w, spacing, labels)
return M.initTextRows_color(
x,
y,
w,
spacing,
labels,
_G_Patterns_.PRIMARY_FG,
nil
)
end
M.text_rows_draw_static = function(rows, cr) M.text_rows_draw_static = function(rows, cr)
TextColumn.draw(rows.labels, cr) TextColumn.draw(rows.labels, cr)
end end

View File

@ -1,9 +1,6 @@
local M = {} local M = {}
local Patterns = require 'Patterns'
local Line = require 'Line' local Line = require 'Line'
local TextColumn = require 'TextColumn'
local CompoundBar = require 'CompoundBar'
local Util = require 'Util' local Util = require 'Util'
local Common = require 'Common' local Common = require 'Common'
@ -45,41 +42,15 @@ local separator = Common.initSeparator(
local _BAR_Y_ = _SEP_Y_ + _SEPARATOR_SPACING_ local _BAR_Y_ = _SEP_Y_ + _SEPARATOR_SPACING_
local bars = _G_Widget_.CompoundBar( local fs = Common.compound_bar(
_G_Widget_.make_point( _G_INIT_DATA_.RIGHT_X,
_G_INIT_DATA_.RIGHT_X + _BAR_PAD_, _BAR_Y_,
_BAR_Y_ _G_INIT_DATA_.SECTION_WIDTH,
), _BAR_PAD_,
_G_INIT_DATA_.SECTION_WIDTH - _BAR_PAD_,
_G_Widget_.line_style(
12,
Patterns.INDICATOR_BG,
CAIRO_LINE_JOIN_MITER
),
_G_Widget_.threshold_style(
Patterns.INDICATOR_FG_PRIMARY,
Patterns.INDICATOR_FG_CRITICAL,
0.8
),
_SPACING_,
FS_NUM,
false
)
local labels = _G_Widget_.TextColumn(
_G_Widget_.make_point(
_G_INIT_DATA_.RIGHT_X,
_BAR_Y_
),
{'root', 'boot', 'home', 'data', 'dcache', 'tmpfs'}, {'root', 'boot', 'home', 'data', 'dcache', 'tmpfs'},
_G_Widget_.text_style( _SPACING_,
Common.normal_font_spec, 12,
_G_Patterns_.INACTIVE_TEXT_FG, 0.8
'left',
'center'
),
nil,
_SPACING_
) )
_SPACING_ = nil _SPACING_ = nil
@ -87,7 +58,6 @@ _BAR_PAD_ = nil
_FS_PATHS_ = nil _FS_PATHS_ = nil
_SEPARATOR_SPACING_ = nil _SEPARATOR_SPACING_ = nil
_BAR_Y_ = nil _BAR_Y_ = nil
_SEPARATOR_SPACING_ = nil
_SEP_Y_ = nil _SEP_Y_ = nil
local update = function(cr) local update = function(cr)
@ -96,26 +66,21 @@ local update = function(cr)
for i = 1, FS_NUM do for i = 1, FS_NUM do
local percent = Util.conky_numeric(conky_used_perc[i]) local percent = Util.conky_numeric(conky_used_perc[i])
CompoundBar.set(bars, i, percent * 0.01) Common.compound_bar_set(fs, i, percent * 0.01)
end end
end end
M.draw_static = function(cr) M.draw_static = function(cr)
Common.drawHeader(cr, header) Common.drawHeader(cr, header)
Common.text_row_draw_static(smart, cr) Common.text_row_draw_static(smart, cr)
Line.draw(separator, cr) Line.draw(separator, cr)
Common.compound_bar_draw_static(fs, cr)
TextColumn.draw(labels, cr)
CompoundBar.draw_static(bars, cr)
end end
M.draw_dynamic = function(cr, trigger) M.draw_dynamic = function(cr, trigger)
if trigger == 0 then update(cr) end if trigger == 0 then update(cr) end
Common.text_row_draw_dynamic(smart, cr) Common.text_row_draw_dynamic(smart, cr)
Common.compound_bar_draw_dynamic(fs, cr)
CompoundBar.draw_dynamic(bars, cr)
end end
return M return M

View File

@ -2,7 +2,6 @@ local M = {}
local Arc = require 'Arc' local Arc = require 'Arc'
local Dial = require 'Dial' local Dial = require 'Dial'
local TextColumn = require 'TextColumn'
local LabelPlot = require 'LabelPlot' local LabelPlot = require 'LabelPlot'
local Table = require 'Table' local Table = require 'Table'
local Util = require 'Util' local Util = require 'Util'
@ -55,12 +54,12 @@ local DIAL_X = _G_INIT_DATA_.RIGHT_X + DIAL_RADIUS + _DIAL_THICKNESS_ / 2
local DIAL_Y = header.bottom_y + DIAL_RADIUS + _DIAL_THICKNESS_ / 2 local DIAL_Y = header.bottom_y + DIAL_RADIUS + _DIAL_THICKNESS_ / 2
local dial = Common.dial(DIAL_X, DIAL_Y, DIAL_RADIUS, _DIAL_THICKNESS_, 0.8) local dial = Common.dial(DIAL_X, DIAL_Y, DIAL_RADIUS, _DIAL_THICKNESS_, 0.8)
local cache_arc = _G_Widget_.Arc( local cache_arc = Common.arc(
_G_Widget_.make_semicircle(DIAL_X, DIAL_Y, DIAL_RADIUS, 90, 360), DIAL_X,
_G_Widget_.arc_style( DIAL_Y,
_DIAL_THICKNESS_, DIAL_RADIUS,
_G_Patterns_.INDICATOR_FG_SECONDARY _DIAL_THICKNESS_,
) _G_Patterns_.INDICATOR_FG_SECONDARY
) )
local text_ring = Common.initTextRing( local text_ring = Common.initTextRing(
@ -85,38 +84,15 @@ local swap = Common.initTextRowCrit(
80 80
) )
local cache = { local cache = Common.initTextRows_color(
labels = _G_Widget_.TextColumn( _TEXT_LEFT_X_,
_G_Widget_.make_point( _LINE_1_Y_ + _TEXT_SPACING_,
_TEXT_LEFT_X_, _G_INIT_DATA_.SECTION_WIDTH - _TEXT_LEFT_X_OFFSET_ - DIAL_RADIUS * 2,
_LINE_1_Y_ + _TEXT_SPACING_ _TEXT_SPACING_,
), {'Page Cache', 'Buffers', 'Kernel Slab'},
{'Page Cache', 'Buffers', 'Kernel Slab'}, _G_Patterns_.SECONDARY_FG,
_G_Widget_.text_style( '%s%%'
Common.normal_font_spec, )
_G_Patterns_.INACTIVE_TEXT_FG,
'left',
'center'
),
nil,
_TEXT_SPACING_
),
percents = _G_Widget_.initTextColumnN(
_G_Widget_.make_point(
_RIGHT_X_,
_LINE_1_Y_ + _TEXT_SPACING_
),
3,
_G_Widget_.text_style(
Common.normal_font_spec,
_G_Patterns_.SECONDARY_FG,
'right',
'center'
),
'%s%%',
_TEXT_SPACING_
),
}
local _PLOT_Y_ = _PLOT_SECTION_BREAK_ + header.bottom_y + DIAL_RADIUS * 2 local _PLOT_Y_ = _PLOT_SECTION_BREAK_ + header.bottom_y + DIAL_RADIUS * 2
@ -158,15 +134,13 @@ local update = function(cr)
(swap_total_kb - swap_free_kb) (swap_total_kb - swap_free_kb)
/ swap_total_kb * 100)) / swap_total_kb * 100))
local _percents = cache.percents Common.text_rows_set(cache, cr, 1, Util.precision_round_to_string(
TextColumn.set(_percents, cr, 1, Util.precision_round_to_string(
cached_kb / MEM_TOTAL_KB * 100)) cached_kb / MEM_TOTAL_KB * 100))
TextColumn.set(_percents, cr, 2, Util.precision_round_to_string( Common.text_rows_set(cache, cr, 2, Util.precision_round_to_string(
buffers_kb / MEM_TOTAL_KB * 100)) buffers_kb / MEM_TOTAL_KB * 100))
TextColumn.set(_percents, cr, 3, Util.precision_round_to_string( Common.text_rows_set(cache, cr, 3, Util.precision_round_to_string(
slab_reclaimable_kb / MEM_TOTAL_KB * 100)) slab_reclaimable_kb / MEM_TOTAL_KB * 100))
LabelPlot.update(plot, used_percent) LabelPlot.update(plot, used_percent)
@ -203,7 +177,7 @@ M.draw_static = function(cr)
Dial.draw_static(dial, cr) Dial.draw_static(dial, cr)
Common.text_row_crit_draw_static(swap, cr) Common.text_row_crit_draw_static(swap, cr)
TextColumn.draw(cache.labels, cr) Common.text_rows_draw_static(cache, cr)
LabelPlot.draw_static(plot, cr) LabelPlot.draw_static(plot, cr)
Table.draw_static(tbl, cr) Table.draw_static(tbl, cr)
@ -217,7 +191,7 @@ M.draw_dynamic = function(cr)
Common.text_ring_draw_dynamic(text_ring, cr) Common.text_ring_draw_dynamic(text_ring, cr)
Common.text_row_crit_draw_dynamic(swap, cr) Common.text_row_crit_draw_dynamic(swap, cr)
TextColumn.draw(cache.percents, cr) Common.text_rows_draw_dynamic(cache, cr)
LabelPlot.draw_dynamic(plot, cr) LabelPlot.draw_dynamic(plot, cr)