Files
Renderive/render_2D/plottable/Hover_Tooltip.h
T
2026-08-11 13:49:53 +08:00

90 lines
2.7 KiB
C++

#pragma once
#include "../event/Event.h"
#include <mutex>
namespace renderive {
struct Hover_Tooltip_Snapshot {
bool enabled{true};
bool active{};
PointF position;
Brush background{Color::white(), Brush_Style::Solid};
Pen text_pen{Color::black()};
Font font;
};
template <class That>
class Hover_Tooltip_Mixin {
public:
[[nodiscard]] bool hover_tooltip_enabled() const {
std::lock_guard lock(tooltip_mutex_);
return tooltip_.enabled;
}
void set_use_hover_info(bool enabled) {
{
std::lock_guard lock(tooltip_mutex_);
tooltip_.enabled = enabled;
if (!enabled)
tooltip_.active = false;
}
static_cast<That*>(this)->tooltip_changed();
}
[[nodiscard]] Font hover_tooltip_font() const {
std::lock_guard lock(tooltip_mutex_);
return tooltip_.font;
}
void set_hover_tooltip_font(Font value) {
{ std::lock_guard lock(tooltip_mutex_); tooltip_.font = value; }
static_cast<That*>(this)->tooltip_changed();
}
[[nodiscard]] Brush hover_info_background_brush() const {
std::lock_guard lock(tooltip_mutex_);
return tooltip_.background;
}
void set_hover_tooltip_background_brush(Brush value) {
{ std::lock_guard lock(tooltip_mutex_); tooltip_.background = value; }
static_cast<That*>(this)->tooltip_changed();
}
[[nodiscard]] Pen tooltip_text_pen() const {
std::lock_guard lock(tooltip_mutex_);
return tooltip_.text_pen;
}
void set_tooltip_text_pen(Pen value) {
{ std::lock_guard lock(tooltip_mutex_); tooltip_.text_pen = value; }
static_cast<That*>(this)->tooltip_changed();
}
protected:
void update_hover(const Event& event) {
bool changed{};
{
std::lock_guard lock(tooltip_mutex_);
if (event.type == Event_Type::Pointer_Move) {
const auto& pointer = static_cast<const Pointer_Event&>(event);
changed = !tooltip_.active || tooltip_.position.x != pointer.position.x ||
tooltip_.position.y != pointer.position.y;
tooltip_.active = tooltip_.enabled;
tooltip_.position = pointer.position;
} else if (event.type == Event_Type::Leave) {
changed = tooltip_.active;
tooltip_.active = false;
}
}
if (changed)
static_cast<That*>(this)->tooltip_changed();
}
[[nodiscard]] Hover_Tooltip_Snapshot tooltip_snapshot() const {
std::lock_guard lock(tooltip_mutex_);
return tooltip_;
}
private:
mutable std::mutex tooltip_mutex_;
Hover_Tooltip_Snapshot tooltip_;
};
} // namespace renderive