diff --git a/src/Bar.cpp b/src/Bar.cpp index b5a3756..6050195 100644 --- a/src/Bar.cpp +++ b/src/Bar.cpp @@ -210,7 +210,7 @@ namespace Bar auto revealer = Widget::Create(); revealer->SetTransition({TransitionType::SlideLeft, 500}); // Add event to eventbox for the revealer to open - eventBox->SetEventFn( + eventBox->SetHoverFn( [textRevealer = revealer.get()](EventBox&, bool hovered) { textRevealer->SetRevealed(hovered); @@ -269,7 +269,7 @@ namespace Bar auto revealer = Widget::Create(); revealer->SetTransition({TransitionType::SlideLeft, 500}); // Add event to eventbox for the revealer to open - eventBox.SetEventFn( + eventBox.SetHoverFn( [slideRevealer = revealer.get()](EventBox&, bool hovered) { slideRevealer->SetRevealed(hovered); @@ -339,7 +339,7 @@ namespace Bar auto revealer = Widget::Create(); revealer->SetTransition({TransitionType::SlideLeft, 500}); // Add event to eventbox for the revealer to open - eventBox->SetEventFn( + eventBox->SetHoverFn( [textRevealer = revealer.get()](EventBox&, bool hovered) { textRevealer->SetRevealed(hovered); @@ -388,7 +388,7 @@ namespace Bar void WidgetPower(Widget& parent) { auto eventBox = Widget::Create(); - eventBox->SetEventFn(DynCtx::PowerBoxEvent); + eventBox->SetHoverFn(DynCtx::PowerBoxEvent); { auto powerBox = Widget::Create(); powerBox->SetClass("power-box"); diff --git a/src/Widget.cpp b/src/Widget.cpp index 89df64b..e2c01fd 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -187,9 +187,14 @@ void CenterBox::Create() ApplyPropertiesToWidget(); } -void EventBox::SetEventFn(std::function&& fn) +void EventBox::SetHoverFn(std::function&& fn) { - m_EventFn = fn; + m_HoverFn = fn; +} + +void EventBox::SetScrollFn(std::function&& fn) +{ + m_ScrollFn = fn; } void EventBox::Create() @@ -199,18 +204,37 @@ void EventBox::Create() auto enter = [](GtkWidget*, GdkEventCrossing*, gpointer data) -> gboolean { EventBox* box = (EventBox*)data; - box->m_EventFn(*box, true); + if (box->m_HoverFn) + box->m_HoverFn(*box, true); return false; }; auto leave = [](GtkWidget*, GdkEventCrossing*, void* data) -> gboolean { EventBox* box = (EventBox*)data; - box->m_EventFn(*box, false); + if (box->m_HoverFn) + box->m_HoverFn(*box, false); return false; }; - gtk_widget_set_events(m_Widget, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK); + auto scroll = [](GtkWidget*, GdkEventScroll event, void* data) -> gboolean + { + EventBox* box = (EventBox*)data; + if (box->m_ScrollFn) + { + if (event.direction == GDK_SCROLL_DOWN) + { + box->m_ScrollFn(*box, ScrollDirection::Down); + } + else if (event.direction == GDK_SCROLL_UP) + { + box->m_ScrollFn(*box, ScrollDirection::Up); + } + } + return false; + }; + gtk_widget_set_events(m_Widget, GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_SCROLL_MASK); g_signal_connect(m_Widget, "enter-notify-event", G_CALLBACK(+enter), this); g_signal_connect(m_Widget, "leave-notify-event", G_CALLBACK(+leave), this); + g_signal_connect(m_Widget, "scroll-event", G_CALLBACK(+scroll), this); ApplyPropertiesToWidget(); } diff --git a/src/Widget.h b/src/Widget.h index e2647d2..a479f97 100644 --- a/src/Widget.h +++ b/src/Widget.h @@ -80,6 +80,12 @@ enum class TimerResult Delete }; +enum class ScrollDirection +{ + Up, + Down +}; + template using Callback = std::function; template @@ -194,11 +200,13 @@ private: class EventBox : public Widget { public: - void SetEventFn(std::function&& fn); + void SetHoverFn(std::function&& fn); + void SetScrollFn(std::function&& fn); virtual void Create() override; private: - std::function m_EventFn; + std::function m_HoverFn; + std::function m_ScrollFn; }; class CairoArea : public Widget