FifeGUI 0.3.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 <cassert>
9#include <memory>
10#include <string>
11#include <utility>
12
13// Project headers (subdirs before local)
14#include "fifechan/widgets/label.hpp"
15#include "fifechan/widgets/tooltip.hpp"
16
17namespace fcn
18{
19 ActivityBarItem::ActivityBarItem(std::string const & icon, std::string tooltip, Widget* panel) :
20 mPanel(panel), mTooltip(std::move(tooltip))
21 {
22 setCaption(icon);
24
25 // Register as widget listener on the panel if provided
26 if (mPanel != nullptr) {
27 mPanel->addWidgetListener(this);
28 }
29 }
30
31 ActivityBarItem::~ActivityBarItem()
32 {
33 // Remove ourselves as a widget listener from the panel
34 if (mPanel != nullptr) {
36 }
37 }
38
40 {
41 // Remove ourselves as a widget listener from the old panel
42 if (mPanel != nullptr) {
43 mPanel->removeWidgetListener(this);
44 }
45
46 mPanel = panel;
47
48 // Register as widget listener on the new panel
49 if (mPanel != nullptr) {
50 mPanel->addWidgetListener(this);
51 }
52 }
53
55 {
56 return mPanel;
57 }
58
60 {
61 assert("size must be positive" && size > 0);
62 mSize = size;
63 Widget::setSize(size, size);
64 }
65
67 {
68 return mSize;
69 }
70
71 void ActivityBarItem::setTooltip(std::string const & tooltip)
72 {
73 mTooltip = tooltip;
74 }
75
76 std::string const & ActivityBarItem::getTooltip() const
77 {
78 return mTooltip;
79 }
80
82 {
83 // Call base class implementation
84 Button::mouseEntered(mouseEvent);
85
86 // Show tooltip if we have tooltip text
87 if (!mTooltip.empty()) {
88 // Lazily create tooltip widget if needed
89 if (!mTooltipWidget) {
90 mTooltipWidget = std::make_unique<Tooltip>();
91 mTooltipWidget->setSize(150, 30); // Default size, can be adjusted
92
93 // Set up tooltip spec with content callback
94 TooltipSpec spec;
95 spec.content = [this](int /*widgetId*/) {
96 return mTooltip;
97 };
98 spec.delayMs = 500; // 500ms delay before showing
99 mTooltipWidget->setSpec(spec);
100 }
101
102 // Position tooltip relative to this widget (offset to the right)
103 int const tooltipX = getWidth() + 5; // 5px offset to the right
104 int const tooltipY = 0;
105 mTooltipWidget->setPosition(tooltipX, tooltipY);
106
107 // Start hover state
108 mTooltipWidget->startHover();
109
110 // Add tooltip as a child of this widget
111 add(mTooltipWidget.get());
112 }
113 }
114
116 {
117 // Call base class implementation
118 Button::mouseExited(mouseEvent);
119
120 // Hide tooltip if it exists
121 if (mTooltipWidget) {
122 mTooltipWidget->endHover();
123
124 // Remove tooltip from this widget
125 remove(mTooltipWidget.get());
126 }
127 }
128
130 {
132 // Sync panel visibility with button state
133 if (mPanel != nullptr) {
134 mPanel->setVisible(selected);
135 }
136 }
137
139 {
141 // Toggle panel visibility
142 if (mPanel != nullptr) {
143 mPanel->setVisible(isSelected());
144 }
145 }
146
148 {
149 // Set panel visibility directly
150 if (mPanel != nullptr) {
151 mPanel->setVisible(visible);
152 }
153
154 // Update button selected state to match
156
157 // Fire action event so listeners know something changed
159 }
160
162 {
163 // Only react to our own panel's visibility changes
164 if (event.getSource() == mPanel) {
165 // Sync button state to match panel (which is now hidden)
166 if (isSelected()) {
168 }
169 }
170 }
171
173 {
174 // Only react to our own panel's visibility changes
175 if (event.getSource() == mPanel) {
176 // Sync button state to match panel (which is now visible)
177 if (!isSelected()) {
179 }
180 }
181 }
182
183} // namespace fcn
void toggleSelected() override
Toggles the check box between being selected and not being selected.
ActivityBarItem(std::string const &icon, std::string tooltip="", Widget *panel=nullptr)
Constructor.
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.
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:56
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:252
void add(Widget *widget)
Adds a child to the widget.
Definition widget.cpp:1446
void removeWidgetListener(WidgetListener *widgetListener)
Removes an added widget listener from the widget.
Definition widget.cpp:924
virtual void remove(Widget *widget)
Removes a specific child from the widget.
Definition widget.cpp:1422
virtual void setSize(int width, int height)
Sets the size of the widget.
Definition widget.cpp:1065
void distributeActionEvent()
Distributes an action event to all action listeners of the widget.
Definition widget.cpp:1291
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