EventBox: Add scroll event

This commit is contained in:
scorpion-26 2023-02-12 14:28:46 +01:00
parent 6f3f398ead
commit b4dfaa62b1
3 changed files with 43 additions and 11 deletions

View file

@ -210,7 +210,7 @@ namespace Bar
auto revealer = Widget::Create<Revealer>();
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>();
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>();
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>();
eventBox->SetEventFn(DynCtx::PowerBoxEvent);
eventBox->SetHoverFn(DynCtx::PowerBoxEvent);
{
auto powerBox = Widget::Create<Box>();
powerBox->SetClass("power-box");

View file

@ -187,9 +187,14 @@ void CenterBox::Create()
ApplyPropertiesToWidget();
}
void EventBox::SetEventFn(std::function<void(EventBox&, bool)>&& fn)
void EventBox::SetHoverFn(std::function<void(EventBox&, bool)>&& fn)
{
m_EventFn = fn;
m_HoverFn = fn;
}
void EventBox::SetScrollFn(std::function<void(EventBox&, ScrollDirection)>&& 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();
}

View file

@ -80,6 +80,12 @@ enum class TimerResult
Delete
};
enum class ScrollDirection
{
Up,
Down
};
template<typename TWidget>
using Callback = std::function<void(TWidget&)>;
template<typename TWidget>
@ -194,11 +200,13 @@ private:
class EventBox : public Widget
{
public:
void SetEventFn(std::function<void(EventBox&, bool)>&& fn);
void SetHoverFn(std::function<void(EventBox&, bool)>&& fn);
void SetScrollFn(std::function<void(EventBox&, ScrollDirection)>&& fn);
virtual void Create() override;
private:
std::function<void(EventBox&, bool)> m_EventFn;
std::function<void(EventBox&, bool)> m_HoverFn;
std::function<void(EventBox&, ScrollDirection)> m_ScrollFn;
};
class CairoArea : public Widget