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

View file

@ -58,6 +58,12 @@ struct SliderRange
double min, max, step; 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 enum class TimerResult
{ {
Ok, Ok,
@ -98,7 +104,7 @@ public:
std::vector<std::unique_ptr<Widget>>& GetWidgets() { return m_Childs; } std::vector<std::unique_ptr<Widget>>& GetWidgets() { return m_Childs; }
template<typename TWidget> 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 struct TimerPayload
{ {
@ -119,6 +125,13 @@ public:
} }
return true; return true;
}; };
if (dispatch == TimerDispatchBehaviour::ImmediateDispatch)
{
if (fn(payload) == false)
{
return;
}
}
g_timeout_add(timeoutMS, +fn, payload); g_timeout_add(timeoutMS, +fn, payload);
} }