From 902f445757af43cd5f7396e7c05d2e9a6932edd9 Mon Sep 17 00:00:00 2001 From: scorpion-26 <58082714+scorpion-26@users.noreply.github.com> Date: Sat, 4 Feb 2023 16:09:08 +0100 Subject: [PATCH] Abstract out the cairo drawing area And rename CairoSensor to Sensor --- src/Bar.cpp | 36 ++++++++++++++++++------------------ src/Widget.cpp | 12 ++++++------ src/Widget.h | 20 ++++++++++++++++---- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/Bar.cpp b/src/Bar.cpp index 9421f63..32d4671 100644 --- a/src/Bar.cpp +++ b/src/Bar.cpp @@ -20,7 +20,7 @@ namespace Bar } static Text* cpuText; - static TimerResult UpdateCPU(CairoSensor& sensor) + static TimerResult UpdateCPU(Sensor& sensor) { double usage = System::GetCPUUsage(); double temp = System::GetCPUTemp(); @@ -31,7 +31,7 @@ namespace Bar } static Text* batteryText; - static TimerResult UpdateBattery(CairoSensor& sensor) + static TimerResult UpdateBattery(Sensor& sensor) { double percentage = System::GetBatteryPercentage(); @@ -41,7 +41,7 @@ namespace Bar } static Text* ramText; - static TimerResult UpdateRAM(CairoSensor& sensor) + static TimerResult UpdateRAM(Sensor& sensor) { System::RAMInfo info = System::GetRAMInfo(); double used = info.totalGiB - info.freeGiB; @@ -54,7 +54,7 @@ namespace Bar #if defined WITH_NVIDIA || defined WITH_AMD static Text* gpuText; - static TimerResult UpdateGPU(CairoSensor& sensor) + static TimerResult UpdateGPU(Sensor& sensor) { System::GPUInfo info = System::GetGPUInfo(); @@ -65,7 +65,7 @@ namespace Bar } static Text* vramText; - static TimerResult UpdateVRAM(CairoSensor& sensor) + static TimerResult UpdateVRAM(Sensor& sensor) { System::VRAMInfo info = System::GetVRAMInfo(); @@ -77,7 +77,7 @@ namespace Bar #endif static Text* diskText; - static TimerResult UpdateDisk(CairoSensor& sensor) + static TimerResult UpdateDisk(Sensor& sensor) { System::DiskInfo info = System::GetDiskInfo(); @@ -182,7 +182,7 @@ namespace Bar #endif } - void Sensor(Widget& parent, TimerCallback&& callback, const std::string& sensorClass, const std::string& textClass, Text*& textPtr) + void WidgetSensor(Widget& parent, TimerCallback&& callback, const std::string& sensorClass, const std::string& textClass, Text*& textPtr) { auto eventBox = Widget::Create(); { @@ -205,13 +205,13 @@ namespace Bar revealer->AddChild(std::move(text)); } - auto cairoSensor = Widget::Create(); - cairoSensor->SetClass(sensorClass); - cairoSensor->AddTimer(std::move(callback), DynCtx::updateTime); - cairoSensor->SetHorizontalTransform({24, true, Alignment::Fill}); + auto sensor = Widget::Create(); + sensor->SetClass(sensorClass); + sensor->AddTimer(std::move(callback), DynCtx::updateTime); + sensor->SetHorizontalTransform({24, true, Alignment::Fill}); box->AddChild(std::move(revealer)); - box->AddChild(std::move(cairoSensor)); + box->AddChild(std::move(sensor)); } eventBox->AddChild(std::move(box)); } @@ -313,20 +313,20 @@ namespace Bar 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 (RuntimeConfig::Get().hasNvidia || RuntimeConfig::Get().hasAMD) { - Sensor(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::UpdateVRAM, "vram-util-progress", "vram-data-text", DynCtx::vramText); + WidgetSensor(parent, DynCtx::UpdateGPU, "gpu-util-progress", "gpu-data-text", DynCtx::gpuText); } #endif - Sensor(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::UpdateRAM, "ram-util-progress", "ram-data-text", DynCtx::ramText); + WidgetSensor(parent, DynCtx::UpdateCPU, "cpu-util-progress", "cpu-data-text", DynCtx::cpuText); // Only show battery percentage if battery folder is set and exists 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); } } diff --git a/src/Widget.cpp b/src/Widget.cpp index 426c32e..667e4f4 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -214,13 +214,13 @@ void EventBox::Create() ApplyPropertiesToWidget(); } -void CairoSensor::Create() +void CairoArea::Create() { m_Widget = gtk_drawing_area_new(); auto drawFn = [](GtkWidget*, cairo_t* c, void* data) -> gboolean { - CairoSensor* sensor = (CairoSensor*)data; - sensor->Draw(c); + CairoArea* area = (CairoArea*)data; + area->Draw(c); return false; }; @@ -229,7 +229,7 @@ void CairoSensor::Create() ApplyPropertiesToWidget(); } -void CairoSensor::SetValue(double val) +void Sensor::SetValue(double val) { m_Val = val; 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; } -void CairoSensor::Draw(cairo_t* cr) +void Sensor::Draw(cairo_t* cr) { GtkAllocation dim; gtk_widget_get_allocation(m_Widget, &dim); diff --git a/src/Widget.h b/src/Widget.h index 24870d5..271bce4 100644 --- a/src/Widget.h +++ b/src/Widget.h @@ -53,9 +53,14 @@ struct SensorStyle double strokeWidth = 4; }; -struct SliderRange +struct Range { - double min, max, step; + double min, max; +}; + +struct SliderRange : Range +{ + double step; }; enum class TimerDispatchBehaviour @@ -191,17 +196,24 @@ private: std::function m_EventFn; }; -class CairoSensor : public Widget +class CairoArea : public Widget { public: virtual void Create() override; +protected: + virtual void Draw(cairo_t* cr) = 0; +}; + +class Sensor : public CairoArea +{ +public: // Goes from 0-1 void SetValue(double val); void SetStyle(SensorStyle style); private: - void Draw(cairo_t* cr); + void Draw(cairo_t* cr) override; double m_Val; SensorStyle m_Style{};