Abstract out the cairo drawing area

And rename CairoSensor to Sensor
This commit is contained in:
scorpion-26 2023-02-04 16:09:08 +01:00
parent c0038d83b8
commit 902f445757
3 changed files with 40 additions and 28 deletions

View file

@ -20,7 +20,7 @@ namespace Bar
} }
static Text* cpuText; static Text* cpuText;
static TimerResult UpdateCPU(CairoSensor& sensor) static TimerResult UpdateCPU(Sensor& sensor)
{ {
double usage = System::GetCPUUsage(); double usage = System::GetCPUUsage();
double temp = System::GetCPUTemp(); double temp = System::GetCPUTemp();
@ -31,7 +31,7 @@ namespace Bar
} }
static Text* batteryText; static Text* batteryText;
static TimerResult UpdateBattery(CairoSensor& sensor) static TimerResult UpdateBattery(Sensor& sensor)
{ {
double percentage = System::GetBatteryPercentage(); double percentage = System::GetBatteryPercentage();
@ -41,7 +41,7 @@ namespace Bar
} }
static Text* ramText; static Text* ramText;
static TimerResult UpdateRAM(CairoSensor& sensor) static TimerResult UpdateRAM(Sensor& sensor)
{ {
System::RAMInfo info = System::GetRAMInfo(); System::RAMInfo info = System::GetRAMInfo();
double used = info.totalGiB - info.freeGiB; double used = info.totalGiB - info.freeGiB;
@ -54,7 +54,7 @@ namespace Bar
#if defined WITH_NVIDIA || defined WITH_AMD #if defined WITH_NVIDIA || defined WITH_AMD
static Text* gpuText; static Text* gpuText;
static TimerResult UpdateGPU(CairoSensor& sensor) static TimerResult UpdateGPU(Sensor& sensor)
{ {
System::GPUInfo info = System::GetGPUInfo(); System::GPUInfo info = System::GetGPUInfo();
@ -65,7 +65,7 @@ namespace Bar
} }
static Text* vramText; static Text* vramText;
static TimerResult UpdateVRAM(CairoSensor& sensor) static TimerResult UpdateVRAM(Sensor& sensor)
{ {
System::VRAMInfo info = System::GetVRAMInfo(); System::VRAMInfo info = System::GetVRAMInfo();
@ -77,7 +77,7 @@ namespace Bar
#endif #endif
static Text* diskText; static Text* diskText;
static TimerResult UpdateDisk(CairoSensor& sensor) static TimerResult UpdateDisk(Sensor& sensor)
{ {
System::DiskInfo info = System::GetDiskInfo(); System::DiskInfo info = System::GetDiskInfo();
@ -182,7 +182,7 @@ namespace Bar
#endif #endif
} }
void Sensor(Widget& parent, TimerCallback<CairoSensor>&& callback, const std::string& sensorClass, const std::string& textClass, Text*& textPtr) void WidgetSensor(Widget& parent, TimerCallback<Sensor>&& callback, const std::string& sensorClass, const std::string& textClass, Text*& textPtr)
{ {
auto eventBox = Widget::Create<EventBox>(); auto eventBox = Widget::Create<EventBox>();
{ {
@ -205,13 +205,13 @@ namespace Bar
revealer->AddChild(std::move(text)); revealer->AddChild(std::move(text));
} }
auto cairoSensor = Widget::Create<CairoSensor>(); auto sensor = Widget::Create<Sensor>();
cairoSensor->SetClass(sensorClass); sensor->SetClass(sensorClass);
cairoSensor->AddTimer<CairoSensor>(std::move(callback), DynCtx::updateTime); sensor->AddTimer<Sensor>(std::move(callback), DynCtx::updateTime);
cairoSensor->SetHorizontalTransform({24, true, Alignment::Fill}); sensor->SetHorizontalTransform({24, true, Alignment::Fill});
box->AddChild(std::move(revealer)); box->AddChild(std::move(revealer));
box->AddChild(std::move(cairoSensor)); box->AddChild(std::move(sensor));
} }
eventBox->AddChild(std::move(box)); eventBox->AddChild(std::move(box));
} }
@ -313,20 +313,20 @@ namespace Bar
void WidgetSensors(Widget& parent) void WidgetSensors(Widget& parent)
{ {
Sensor(parent, DynCtx::UpdateDisk, "disk-util-progress", "disk-data-text", DynCtx::diskText); WidgetSensor(parent, DynCtx::UpdateDisk, "disk-util-progress", "disk-data-text", DynCtx::diskText);
#if defined WITH_NVIDIA || defined WITH_AMD #if defined WITH_NVIDIA || defined WITH_AMD
if (RuntimeConfig::Get().hasNvidia || RuntimeConfig::Get().hasAMD) if (RuntimeConfig::Get().hasNvidia || RuntimeConfig::Get().hasAMD)
{ {
Sensor(parent, DynCtx::UpdateVRAM, "vram-util-progress", "vram-data-text", DynCtx::vramText); WidgetSensor(parent, DynCtx::UpdateVRAM, "vram-util-progress", "vram-data-text", DynCtx::vramText);
Sensor(parent, DynCtx::UpdateGPU, "gpu-util-progress", "gpu-data-text", DynCtx::gpuText); WidgetSensor(parent, DynCtx::UpdateGPU, "gpu-util-progress", "gpu-data-text", DynCtx::gpuText);
} }
#endif #endif
Sensor(parent, DynCtx::UpdateRAM, "ram-util-progress", "ram-data-text", DynCtx::ramText); WidgetSensor(parent, DynCtx::UpdateRAM, "ram-util-progress", "ram-data-text", DynCtx::ramText);
Sensor(parent, DynCtx::UpdateCPU, "cpu-util-progress", "cpu-data-text", DynCtx::cpuText); WidgetSensor(parent, DynCtx::UpdateCPU, "cpu-util-progress", "cpu-data-text", DynCtx::cpuText);
// Only show battery percentage if battery folder is set and exists // Only show battery percentage if battery folder is set and exists
if (System::GetBatteryPercentage() >= 0) if (System::GetBatteryPercentage() >= 0)
{ {
Sensor(parent, DynCtx::UpdateBattery, "battery-util-progress", "battery-data-text", DynCtx::batteryText); WidgetSensor(parent, DynCtx::UpdateBattery, "battery-util-progress", "battery-data-text", DynCtx::batteryText);
} }
} }

View file

@ -214,13 +214,13 @@ void EventBox::Create()
ApplyPropertiesToWidget(); ApplyPropertiesToWidget();
} }
void CairoSensor::Create() void CairoArea::Create()
{ {
m_Widget = gtk_drawing_area_new(); m_Widget = gtk_drawing_area_new();
auto drawFn = [](GtkWidget*, cairo_t* c, void* data) -> gboolean auto drawFn = [](GtkWidget*, cairo_t* c, void* data) -> gboolean
{ {
CairoSensor* sensor = (CairoSensor*)data; CairoArea* area = (CairoArea*)data;
sensor->Draw(c); area->Draw(c);
return false; return false;
}; };
@ -229,7 +229,7 @@ void CairoSensor::Create()
ApplyPropertiesToWidget(); ApplyPropertiesToWidget();
} }
void CairoSensor::SetValue(double val) void Sensor::SetValue(double val)
{ {
m_Val = val; m_Val = val;
if (m_Widget) if (m_Widget)
@ -238,12 +238,12 @@ void CairoSensor::SetValue(double val)
} }
} }
void CairoSensor::SetStyle(SensorStyle style) void Sensor::SetStyle(SensorStyle style)
{ {
m_Style = style; m_Style = style;
} }
void CairoSensor::Draw(cairo_t* cr) void Sensor::Draw(cairo_t* cr)
{ {
GtkAllocation dim; GtkAllocation dim;
gtk_widget_get_allocation(m_Widget, &dim); gtk_widget_get_allocation(m_Widget, &dim);

View file

@ -53,9 +53,14 @@ struct SensorStyle
double strokeWidth = 4; double strokeWidth = 4;
}; };
struct SliderRange struct Range
{ {
double min, max, step; double min, max;
};
struct SliderRange : Range
{
double step;
}; };
enum class TimerDispatchBehaviour enum class TimerDispatchBehaviour
@ -191,17 +196,24 @@ private:
std::function<void(EventBox&, bool)> m_EventFn; std::function<void(EventBox&, bool)> m_EventFn;
}; };
class CairoSensor : public Widget class CairoArea : public Widget
{ {
public: public:
virtual void Create() override; virtual void Create() override;
protected:
virtual void Draw(cairo_t* cr) = 0;
};
class Sensor : public CairoArea
{
public:
// Goes from 0-1 // Goes from 0-1
void SetValue(double val); void SetValue(double val);
void SetStyle(SensorStyle style); void SetStyle(SensorStyle style);
private: private:
void Draw(cairo_t* cr); void Draw(cairo_t* cr) override;
double m_Val; double m_Val;
SensorStyle m_Style{}; SensorStyle m_Style{};