diff --git a/src/Bar.cpp b/src/Bar.cpp index ba93321..3fa80ba 100644 --- a/src/Bar.cpp +++ b/src/Bar.cpp @@ -234,6 +234,11 @@ namespace Bar box->SetSpacing({8, false}); box->SetHorizontalTransform({-1, false, Alignment::Right}); { + auto icon = Widget::Create(); + icon->SetClass("audio-icon"); + icon->SetText("墳"); + DynCtx::audioIcon = icon.get(); + auto slider = Widget::Create(); slider->SetOrientation(Orientation::Horizontal); slider->SetHorizontalTransform({100, true, Alignment::Fill}); @@ -243,11 +248,6 @@ namespace Bar slider->OnValueChange(DynCtx::OnChangeVolume); slider->SetRange({0, 1, 0.01}); - auto icon = Widget::Create(); - icon->SetClass("audio-icon"); - icon->SetText("墳"); - DynCtx::audioIcon = icon.get(); - box->AddChild(std::move(slider)); box->AddChild(std::move(icon)); } @@ -260,7 +260,6 @@ namespace Bar { auto box = Widget::Create(); box->SetSpacing({0, false}); - box->AddTimer(DynCtx::UpdateBluetooth, DynCtx::updateTime); { auto devText = Widget::Create(); DynCtx::btDevText = devText.get(); @@ -273,6 +272,7 @@ namespace Bar box->AddChild(std::move(devText)); box->AddChild(std::move(iconText)); } + box->AddTimer(DynCtx::UpdateBluetooth, DynCtx::updateTime); parent.AddChild(std::move(box)); } @@ -424,10 +424,10 @@ namespace Bar #endif auto time = Widget::Create(); - time->AddTimer(DynCtx::UpdateTime, 1000); time->SetHorizontalTransform({-1, true, Alignment::Center}); time->SetClass("time-text"); time->SetText("Uninitialized"); + time->AddTimer(DynCtx::UpdateTime, 1000); auto right = Widget::Create(); right->SetClass("right"); diff --git a/src/Widget.h b/src/Widget.h index 2997c94..4427cef 100644 --- a/src/Widget.h +++ b/src/Widget.h @@ -58,6 +58,12 @@ struct SliderRange double min, max, step; }; +enum class TimerDispatchBehaviour +{ + ImmediateDispatch, // Call immediately after adding the timeout, then every x ms + LateDispatch // Call for the first time after x ms. +}; + enum class TimerResult { Ok, @@ -95,10 +101,10 @@ public: void AddChild(std::unique_ptr&& widget); void RemoveChild(size_t idx); - std::vector>& GetWidgets() {return m_Childs;} + std::vector>& GetWidgets() { return m_Childs; } template - void AddTimer(TimerCallback&& callback, uint32_t timeoutMS) + void AddTimer(TimerCallback&& callback, uint32_t timeoutMS, TimerDispatchBehaviour dispatch = TimerDispatchBehaviour::ImmediateDispatch) { struct TimerPayload { @@ -119,6 +125,13 @@ public: } return true; }; + if (dispatch == TimerDispatchBehaviour::ImmediateDispatch) + { + if (fn(payload) == false) + { + return; + } + } g_timeout_add(timeoutMS, +fn, payload); } @@ -181,7 +194,7 @@ class CairoSensor : public Widget { public: virtual void Create() override; - + // Goes from 0-1 void SetValue(double val); void SetStyle(SensorStyle style); @@ -197,7 +210,7 @@ class Revealer : public Widget { public: void SetTransition(Transition transition); - + void SetRevealed(bool revealed); virtual void Create() override;