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 Arc = require 'Arc'
local Text = require 'Text'
local CompoundBar = require 'CompoundBar'
local CriticalText = require 'CriticalText'
local TextColumn = require 'TextColumn'
local Line = require 'Line'
@ -56,19 +57,13 @@ end
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.left_text_style = _G_Widget_.text_style(
M.normal_font_spec,
_G_Patterns_.INACTIVE_TEXT_FG,
'left',
'center'
)
local _text_row_style = function(x_align, color)
return _G_Widget_.text_style(M.normal_font_spec, color, x_align, 'center')
end
M.right_text_style = _G_Widget_.text_style(
M.normal_font_spec,
_G_Patterns_.PRIMARY_FG,
'right',
'center'
)
M.left_text_style = _text_row_style('left', _G_Patterns_.INACTIVE_TEXT_FG)
M.right_text_style = _text_row_style('right', _G_Patterns_.PRIMARY_FG)
local _bare_text = function(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)
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
@ -282,15 +290,19 @@ end
--------------------------------------------------------------------------------
-- dial
M.dial = function(x, y, radius, thickness, threshold)
return _G_Widget_.Dial(
_G_Widget_.make_semicircle(x, y, radius, DIAL_THETA0, DIAL_THETA1),
_G_Widget_.arc_style(thickness, _G_Patterns_.INDICATOR_BG),
_G_Widget_.threshold_style(
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)
return _G_Widget_.Dial(
_G_Widget_.make_semicircle(x, y, radius, DIAL_THETA0, DIAL_THETA1),
_G_Widget_.arc_style(thickness, _G_Patterns_.INDICATOR_BG),
threshold_indicator(threshold)
)
end
@ -302,16 +314,53 @@ M.compound_dial = function(x, y, outer_radius, inner_radius, thickness,
return _G_Widget_.CompoundDial(
_G_Widget_.make_semicircle(x, y, outer_radius, DIAL_THETA0, DIAL_THETA1),
_G_Widget_.arc_style(thickness, _G_Patterns_.INDICATOR_BG),
_G_Widget_.threshold_style(
_G_Patterns_.INDICATOR_FG_PRIMARY,
_G_Patterns_.INDICATOR_FG_CRITICAL,
threshold
),
threshold_indicator(threshold),
inner_radius,
num_dials
)
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)
@ -330,7 +379,6 @@ end
--------------------------------------------------------------------------------
-- text row (label with a value, aligned as far apart as possible)
M.initTextRow = function(x, y, w, label)
return {
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)
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
M.initTextRows = function(x, y, w, spacing, labels)
M.initTextRows_color = function(x, y, w, spacing, labels, color, format)
return {
labels = _G_Widget_.TextColumn(
_G_Widget_.make_point(x, y),
@ -396,13 +457,25 @@ M.initTextRows = function(x, y, w, spacing, labels)
values = _G_Widget_.initTextColumnN(
_G_Widget_.make_point(x + w, y),
#labels,
M.right_text_style,
nil,
_text_row_style('right', color),
format,
spacing
)
}
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)
TextColumn.draw(rows.labels, cr)
end

View File

@ -1,9 +1,6 @@
local M = {}
local Patterns = require 'Patterns'
local Line = require 'Line'
local TextColumn = require 'TextColumn'
local CompoundBar = require 'CompoundBar'
local Util = require 'Util'
local Common = require 'Common'
@ -45,41 +42,15 @@ local separator = Common.initSeparator(
local _BAR_Y_ = _SEP_Y_ + _SEPARATOR_SPACING_
local bars = _G_Widget_.CompoundBar(
_G_Widget_.make_point(
_G_INIT_DATA_.RIGHT_X + _BAR_PAD_,
_BAR_Y_
),
_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(
local fs = Common.compound_bar(
_G_INIT_DATA_.RIGHT_X,
_BAR_Y_
),
_BAR_Y_,
_G_INIT_DATA_.SECTION_WIDTH,
_BAR_PAD_,
{'root', 'boot', 'home', 'data', 'dcache', 'tmpfs'},
_G_Widget_.text_style(
Common.normal_font_spec,
_G_Patterns_.INACTIVE_TEXT_FG,
'left',
'center'
),
nil,
_SPACING_
_SPACING_,
12,
0.8
)
_SPACING_ = nil
@ -87,7 +58,6 @@ _BAR_PAD_ = nil
_FS_PATHS_ = nil
_SEPARATOR_SPACING_ = nil
_BAR_Y_ = nil
_SEPARATOR_SPACING_ = nil
_SEP_Y_ = nil
local update = function(cr)
@ -96,26 +66,21 @@ local update = function(cr)
for i = 1, FS_NUM do
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
M.draw_static = function(cr)
Common.drawHeader(cr, header)
Common.text_row_draw_static(smart, cr)
Line.draw(separator, cr)
TextColumn.draw(labels, cr)
CompoundBar.draw_static(bars, cr)
Common.compound_bar_draw_static(fs, cr)
end
M.draw_dynamic = function(cr, trigger)
if trigger == 0 then update(cr) end
Common.text_row_draw_dynamic(smart, cr)
CompoundBar.draw_dynamic(bars, cr)
Common.compound_bar_draw_dynamic(fs, cr)
end
return M

View File

@ -2,7 +2,6 @@ local M = {}
local Arc = require 'Arc'
local Dial = require 'Dial'
local TextColumn = require 'TextColumn'
local LabelPlot = require 'LabelPlot'
local Table = require 'Table'
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 = Common.dial(DIAL_X, DIAL_Y, DIAL_RADIUS, _DIAL_THICKNESS_, 0.8)
local cache_arc = _G_Widget_.Arc(
_G_Widget_.make_semicircle(DIAL_X, DIAL_Y, DIAL_RADIUS, 90, 360),
_G_Widget_.arc_style(
local cache_arc = Common.arc(
DIAL_X,
DIAL_Y,
DIAL_RADIUS,
_DIAL_THICKNESS_,
_G_Patterns_.INDICATOR_FG_SECONDARY
)
)
local text_ring = Common.initTextRing(
@ -85,38 +84,15 @@ local swap = Common.initTextRowCrit(
80
)
local cache = {
labels = _G_Widget_.TextColumn(
_G_Widget_.make_point(
local cache = Common.initTextRows_color(
_TEXT_LEFT_X_,
_LINE_1_Y_ + _TEXT_SPACING_
),
_LINE_1_Y_ + _TEXT_SPACING_,
_G_INIT_DATA_.SECTION_WIDTH - _TEXT_LEFT_X_OFFSET_ - DIAL_RADIUS * 2,
_TEXT_SPACING_,
{'Page Cache', 'Buffers', 'Kernel Slab'},
_G_Widget_.text_style(
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_
),
}
'%s%%'
)
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 * 100))
local _percents = cache.percents
TextColumn.set(_percents, cr, 1, Util.precision_round_to_string(
Common.text_rows_set(cache, cr, 1, Util.precision_round_to_string(
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))
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))
LabelPlot.update(plot, used_percent)
@ -203,7 +177,7 @@ M.draw_static = function(cr)
Dial.draw_static(dial, 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)
Table.draw_static(tbl, cr)
@ -217,7 +191,7 @@ M.draw_dynamic = function(cr)
Common.text_ring_draw_dynamic(text_ring, 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)