mirror of
https://github.com/scorpion-26/gBar.git
synced 2024-11-24 12:12:09 +00:00
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:
parent
7c7c58f384
commit
3473da36f4
2 changed files with 24 additions and 11 deletions
14
src/Bar.cpp
14
src/Bar.cpp
|
@ -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");
|
||||
|
|
21
src/Widget.h
21
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>&& 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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue