Widget: Add immediate timer dispatch

gLib will not immediately run the callback after the timeout has been
added.
This caused everything to be a few ms late.
Now the timeout will trigger early by default.
This commit is contained in:
scorpion-26 2023-01-29 12:23:44 +01:00
parent 7c7c58f384
commit 3473da36f4
2 changed files with 24 additions and 11 deletions

View file

@ -234,6 +234,11 @@ namespace Bar
box->SetSpacing({8, false});
box->SetHorizontalTransform({-1, false, Alignment::Right});
{
auto icon = Widget::Create<Text>();
icon->SetClass("audio-icon");
icon->SetText("");
DynCtx::audioIcon = icon.get();
auto slider = Widget::Create<Slider>();
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<Text>();
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>();
box->SetSpacing({0, false});
box->AddTimer<Box>(DynCtx::UpdateBluetooth, DynCtx::updateTime);
{
auto devText = Widget::Create<Text>();
DynCtx::btDevText = devText.get();
@ -273,6 +272,7 @@ namespace Bar
box->AddChild(std::move(devText));
box->AddChild(std::move(iconText));
}
box->AddTimer<Box>(DynCtx::UpdateBluetooth, DynCtx::updateTime);
parent.AddChild(std::move(box));
}
@ -424,10 +424,10 @@ namespace Bar
#endif
auto time = Widget::Create<Text>();
time->AddTimer<Text>(DynCtx::UpdateTime, 1000);
time->SetHorizontalTransform({-1, true, Alignment::Center});
time->SetClass("time-text");
time->SetText("Uninitialized");
time->AddTimer<Text>(DynCtx::UpdateTime, 1000);
auto right = Widget::Create<Box>();
right->SetClass("right");

View file

@ -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>&& widget);
void RemoveChild(size_t idx);
std::vector<std::unique_ptr<Widget>>& GetWidgets() {return m_Childs;}
std::vector<std::unique_ptr<Widget>>& GetWidgets() { return m_Childs; }
template<typename TWidget>
void AddTimer(TimerCallback<TWidget>&& callback, uint32_t timeoutMS)
void AddTimer(TimerCallback<TWidget>&& 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);
}