gui2/label: Use the hyperlink mouse cursor while hovering links

This also moves gui2:🏷️:can_mouse_focus()'s declaration to a
section that makes more sense for it to be in. Thanks to jyrkive for
pointing out the existence of this method.
This commit is contained in:
Iris Morelle 2020-12-11 07:37:05 -03:00
parent 99188ee077
commit 213453e6cf
3 changed files with 70 additions and 2 deletions

View file

@ -18,6 +18,7 @@
### Units
* Undead variations for Falcon, Giant Rat, serpents, and Gorer/Tusklet
### User interface
* Text labels now use the hyperlink mouse cursor while hovering links.
### WML Engine
### Miscellaneous and Bug Fixes
* Fixed several possible crashes in wmllint

View file

@ -25,6 +25,7 @@
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "cursor.hpp"
#include "desktop/clipboard.hpp"
#include "desktop/open.hpp"
#include "gettext.hpp"
@ -52,6 +53,8 @@ label::label(const implementation::builder_label& builder)
{
connect_signal<event::LEFT_BUTTON_CLICK>(std::bind(&label::signal_handler_left_button_click, this, std::placeholders::_2, std::placeholders::_3));
connect_signal<event::RIGHT_BUTTON_CLICK>(std::bind(&label::signal_handler_right_button_click, this, std::placeholders::_2, std::placeholders::_3));
connect_signal<event::MOUSE_MOTION>(std::bind(&label::signal_handler_mouse_motion, this, std::placeholders::_2, std::placeholders::_3, std::placeholders::_5));
connect_signal<event::MOUSE_LEAVE>(std::bind(&label::signal_handler_mouse_leave, this, std::placeholders::_2, std::placeholders::_3));
}
bool label::can_wrap() const
@ -205,6 +208,51 @@ void label::signal_handler_right_button_click(const event::ui_event /* event */,
handled = true;
}
void label::signal_handler_mouse_motion(const event::ui_event /* event */, bool& handled, const point& coordinate)
{
DBG_GUI_E << "label mouse motion" << std::endl;
if(!get_link_aware()) {
return; // without marking event as "handled"
}
point mouse = coordinate;
mouse.x -= get_x();
mouse.y -= get_y();
update_mouse_cursor(!get_label_link(mouse).empty());
handled = true;
}
void label::signal_handler_mouse_leave(const event::ui_event /*event*/, bool& handled)
{
DBG_GUI_E << "label mouse leave" << std::endl;
if(!get_link_aware()) {
return; // without marking event as "handled"
}
// We left the widget, so just unconditionally reset the cursor
update_mouse_cursor(false);
handled = true;
}
void label::update_mouse_cursor(bool enable)
{
// Someone else may set the mouse cursor for us to something unusual (e.g.
// the WAIT cursor) so we ought to mess with that only if it's set to
// NORMAL or HYPERLINK.
if(enable && cursor::get() == cursor::NORMAL) {
cursor::set(cursor::HYPERLINK);
} else if(!enable && cursor::get() == cursor::HYPERLINK) {
cursor::set(cursor::NORMAL);
}
}
// }---------- DEFINITION ---------{
label_definition::label_definition(const config& cfg)

View file

@ -60,6 +60,12 @@ public:
/** See @ref widget::disable_click_dismiss. */
bool disable_click_dismiss() const override;
/** See @ref widget::can_mouse_focus. */
virtual bool can_mouse_focus() const override
{
return !tooltip().empty() || get_link_aware();
}
/***** ***** ***** setters / getters for members ***** ****** *****/
void set_can_wrap(const bool wrap)
@ -73,8 +79,6 @@ public:
void set_link_color(const color_t& color);
virtual bool can_mouse_focus() const override { return !tooltip().empty(); }
void set_can_shrink(bool can_shrink)
{
can_shrink_ = can_shrink;
@ -153,6 +157,21 @@ private:
* Right click signal handler: checks if we clicked on a hyperlink, copied to clipboard
*/
void signal_handler_right_button_click(const event::ui_event event, bool & handled);
/**
* Mouse motion signal handler: checks if the cursor is on a hyperlink
*/
void signal_handler_mouse_motion(const event::ui_event event, bool& handled, const point& coordinate);
/**
* Mouse leave signal handler: checks if the cursor left a hyperlink
*/
void signal_handler_mouse_leave(const event::ui_event event, bool& handled);
/**
* Implementation detail for (re)setting the hyperlink cursor.
*/
void update_mouse_cursor(bool enable);
};
// }---------- DEFINITION ---------{