FifeGUI 0.2.0
A C++ GUI library designed for games.
activitybaritem.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2026 Fifengine contributors
3
4// Project headers
5#include "fifechan/widgets/activitybaritem.hpp"
6
7// Standard library includes
8#include <memory>
9#include <string>
10
11// Project headers (subdirs before local)
12#include "fifechan/widgets/label.hpp"
13#include "fifechan/widgets/tooltip.hpp"
14
15namespace fcn
16{
17 ActivityBarItem::ActivityBarItem(std::string const & icon, std::string const & tooltip, Widget* panel) :
18 mPanel(panel), mTooltip(tooltip)
19 {
20 setCaption(icon);
22
23 // Register as widget listener on the panel if provided
24 if (mPanel != nullptr) {
25 mPanel->addWidgetListener(this);
26 }
27 }
28
29 ActivityBarItem::~ActivityBarItem()
30 {
31 // Remove ourselves as a widget listener from the panel
32 if (mPanel != nullptr) {
34 }
35 }
36
38 {
39 // Remove ourselves as a widget listener from the old panel
40 if (mPanel != nullptr) {
41 mPanel->removeWidgetListener(this);
42 }
43
44 mPanel = panel;
45
46 // Register as widget listener on the new panel
47 if (mPanel != nullptr) {
48 mPanel->addWidgetListener(this);
49 }
50 }
51
53 {
54 return mPanel;
55 }
56
58 {
59 mSize = size;
60 Widget::setSize(size, size);
61 }
62
64 {
65 return mSize;
66 }
67
68 void ActivityBarItem::setTooltip(std::string const & tooltip)
69 {
70 mTooltip = tooltip;
71 }
72
73 std::string const & ActivityBarItem::getTooltip() const
74 {
75 return mTooltip;
76 }
77
79 {
80 // Call base class implementation
81 Button::mouseEntered(mouseEvent);
82
83 // Show tooltip if we have tooltip text
84 if (!mTooltip.empty()) {
85 // Lazily create tooltip widget if needed
86 if (!mTooltipWidget) {
87 mTooltipWidget = std::make_unique<Tooltip>();
88 mTooltipWidget->setSize(150, 30); // Default size, can be adjusted
89
90 // Set up tooltip spec with content callback
91 TooltipSpec spec;
92 spec.content = [this](int /*widgetId*/) {
93 return mTooltip;
94 };
95 spec.delayMs = 500; // 500ms delay before showing
96 mTooltipWidget->setSpec(spec);
97 }
98
99 // Position tooltip relative to this widget (offset to the right)
100 int const tooltipX = getWidth() + 5; // 5px offset to the right
101 int const tooltipY = 0;
102 mTooltipWidget->setPosition(tooltipX, tooltipY);
103
104 // Start hover state
105 mTooltipWidget->startHover();
106
107 // Add tooltip as a child of this widget
108 add(mTooltipWidget.get());
109 }
110 }
111
113 {
114 // Call base class implementation
115 Button::mouseExited(mouseEvent);
116
117 // Hide tooltip if it exists
118 if (mTooltipWidget) {
119 mTooltipWidget->endHover();
120
121 // Remove tooltip from this widget
122 remove(mTooltipWidget.get());
123 }
124 }
125
127 {
129 // Sync panel visibility with button state
130 if (mPanel != nullptr) {
131 mPanel->setVisible(selected);
132 }
133 }
134
136 {
138 // Toggle panel visibility
139 if (mPanel != nullptr) {
140 mPanel->setVisible(isSelected());
141 }
142 }
143
145 {
146 // Set panel visibility directly
147 if (mPanel != nullptr) {
148 mPanel->setVisible(visible);
149 }
150
151 // Update button selected state to match
153
154 // Fire action event so listeners know something changed
156 }
157
159 {
160 // Only react to our own panel's visibility changes
161 if (event.getSource() == mPanel) {
162 // Sync button state to match panel (which is now hidden)
163 if (isSelected()) {
165 }
166 }
167 }
168
170 {
171 // Only react to our own panel's visibility changes
172 if (event.getSource() == mPanel) {
173 // Sync button state to match panel (which is now visible)
174 if (!isSelected()) {
176 }
177 }
178 }
179
180} // namespace fcn
void toggleSelected() override
Toggles the check box between being selected and not being selected.
std::unique_ptr< Tooltip > mTooltipWidget
Tooltip widget instance (lazily created).
void widgetShown(Event const &event) override
Invoked when a widget is shown, i.e it's set to be visible.
Widget * getPanel() const
Gets the target panel that this item controls.
void setPanel(Widget *panel)
Sets the target panel that this item controls.
std::string const & getTooltip() const
Gets the tooltip text for this activity bar item.
std::string mTooltip
Tooltip text for this item.
void setTooltip(std::string const &tooltip)
Sets the tooltip text for this activity bar item.
void setPanelVisible(bool visible)
Sets the panel visibility and updates button state accordingly.
void setSize(int size)
Sets the default size for activity bar items.
void setSelected(bool selected) override
Sets the check box to be selected or not.
int mSize
Default size for items.
int getSize() const
Gets the default size.
void widgetHidden(Event const &event) override
Invoked when a widget is hidden, i.e it's set to be not visible.
ActivityBarItem(std::string const &icon, std::string const &tooltip="", Widget *panel=nullptr)
Constructor.
Widget * mPanel
Panel that this item controls.
void mouseEntered(MouseEvent &mouseEvent) override
Called when the mouse has entered into the widget area.
void mouseExited(MouseEvent &mouseEvent) override
Called when the mouse has exited the widget area.
void mouseEntered(MouseEvent &mouseEvent) override
Called when the mouse has entered into the widget area.
Definition button.cpp:211
void mouseExited(MouseEvent &mouseEvent) override
Called when the mouse has exited the widget area.
Definition button.cpp:206
void setCaption(std::string const &caption)
Sets the caption of the button.
Definition button.cpp:46
Base class for all GUI event objects.
Definition event.hpp:25
Widget * getSource() const
Gets the source widget of the event.
Definition event.cpp:20
Represents a mouse event.
virtual bool isSelected() const
Checks if the check box is selected.
virtual void toggleSelected()
Toggles the check box between being selected and not being selected.
virtual void setSelected(bool selected)
Sets the check box to be selected or not.
Abstract base class defining the common behavior, properties, and lifecycle of all GUI elements.
Definition widget.hpp:55
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:251
void add(Widget *widget)
Adds a child to the widget.
Definition widget.cpp:1449
void removeWidgetListener(WidgetListener *widgetListener)
Removes an added widget listener from the widget.
Definition widget.cpp:923
virtual void remove(Widget *widget)
Removes a specific child from the widget.
Definition widget.cpp:1425
virtual void setSize(int width, int height)
Sets the size of the widget.
Definition widget.cpp:1064
void distributeActionEvent()
Distributes an action event to all action listeners of the widget.
Definition widget.cpp:1290
Used replacement tokens by configure_file():
Tooltip specification (data + behavior).
Definition tooltip.hpp:61
std::function< std::string(int widgetId)> content
Function that generates tooltip content for a widget id.
Definition tooltip.hpp:65
int delayMs
Delay in milliseconds before showing the tooltip.
Definition tooltip.hpp:70