Prevent hover styling and focus on input elements when disabled

This commit is contained in:
Mr-Wiseguy 2025-04-26 02:55:29 -04:00
parent b8ecd9608d
commit 43ec2d27be
7 changed files with 89 additions and 26 deletions

View file

@ -81,10 +81,21 @@ namespace recompui {
}
break;
case EventType::Hover:
set_style_enabled(hover_state, std::get<EventHover>(e.variant).active);
set_style_enabled(hover_state, std::get<EventHover>(e.variant).active && is_enabled());
break;
case EventType::Enable:
set_style_enabled(disabled_state, !std::get<EventEnable>(e.variant).active);
{
bool enable_active = std::get<EventEnable>(e.variant).active;
set_style_enabled(disabled_state, !enable_active);
if (enable_active) {
set_cursor(Cursor::Pointer);
set_focusable(true);
}
else {
set_cursor(Cursor::None);
set_focusable(false);
}
}
break;
case EventType::Focus:
set_style_enabled(focus_state, std::get<EventFocus>(e.variant).active);

View file

@ -3,6 +3,7 @@
namespace recompui {
Clickable::Clickable(Element *parent, bool draggable) : Element(parent, Events(EventType::Click, EventType::Hover, EventType::Enable, draggable ? EventType::Drag : EventType::None)) {
set_cursor(Cursor::Pointer);
if (draggable) {
set_drag(Drag::Drag);
}
@ -11,24 +12,39 @@ namespace recompui {
void Clickable::process_event(const Event &e) {
switch (e.type) {
case EventType::Click: {
const EventClick &click = std::get<EventClick>(e.variant);
for (const auto &function : pressed_callbacks) {
function(click.x, click.y);
if (is_enabled()) {
const EventClick &click = std::get<EventClick>(e.variant);
for (const auto &function : pressed_callbacks) {
function(click.x, click.y);
}
break;
}
break;
}
case EventType::Hover:
set_style_enabled(hover_state, std::get<EventHover>(e.variant).active);
set_style_enabled(hover_state, std::get<EventHover>(e.variant).active && is_enabled());
break;
case EventType::Enable:
set_style_enabled(disabled_state, !std::get<EventEnable>(e.variant).active);
break;
case EventType::Drag: {
const EventDrag &drag = std::get<EventDrag>(e.variant);
for (const auto &function : dragged_callbacks) {
function(drag.x, drag.y, drag.phase);
{
bool enable_active = std::get<EventEnable>(e.variant).active;
set_style_enabled(disabled_state, !enable_active);
if (enable_active) {
set_cursor(Cursor::Pointer);
set_focusable(true);
}
else {
set_cursor(Cursor::None);
set_focusable(false);
}
}
break;
case EventType::Drag: {
if (is_enabled()) {
const EventDrag &drag = std::get<EventDrag>(e.variant);
for (const auto &function : dragged_callbacks) {
function(drag.x, drag.y, drag.phase);
}
break;
}
}
default:
break;

View file

@ -249,12 +249,12 @@ void Element::process_event(const Event &) {
}
void Element::enable_focus() {
set_property(Rml::PropertyId::TabIndex, Rml::Style::TabIndex::Auto);
set_property(Rml::PropertyId::Focus, Rml::Style::Focus::Auto);
set_property(Rml::PropertyId::NavUp, Rml::Style::Nav::Auto);
set_property(Rml::PropertyId::NavDown, Rml::Style::Nav::Auto);
set_property(Rml::PropertyId::NavLeft, Rml::Style::Nav::Auto);
set_property(Rml::PropertyId::NavRight, Rml::Style::Nav::Auto);
set_tab_index_auto();
set_focusable(true);
set_nav_auto(NavDirection::Up);
set_nav_auto(NavDirection::Down);
set_nav_auto(NavDirection::Left);
set_nav_auto(NavDirection::Right);
}
void Element::clear_children() {

View file

@ -84,12 +84,17 @@ namespace recompui {
}
break;
case EventType::Update:
if (circle_element->is_style_enabled(focus_state)) {
circle_element->set_background_color(recompui::get_pulse_color(750));
queue_update();
if (is_enabled()) {
if (circle_element->is_style_enabled(focus_state)) {
circle_element->set_background_color(recompui::get_pulse_color(750));
queue_update();
}
else {
circle_element->set_background_color(Color{ 204, 204, 204, 255 });
}
}
else {
circle_element->set_background_color(Color{ 204, 204, 204, 255 });
circle_element->set_background_color(Color{ 102, 102, 102, 255 });
}
break;
case EventType::Navigate:
@ -102,14 +107,32 @@ namespace recompui {
do_step(true);
}
}
break;
case EventType::Enable:
{
bool enable_active = std::get<EventEnable>(e.variant).active;
circle_element->set_enabled(enable_active);
if (enable_active) {
set_cursor(Cursor::Pointer);
set_focusable(true);
circle_element->set_background_color(Color{ 204, 204, 204, 255 });
}
else {
set_cursor(Cursor::None);
set_focusable(false);
circle_element->set_background_color(Color{ 102, 102, 102, 255 });
}
}
break;
default:
break;
}
}
Slider::Slider(Element *parent, SliderType type) : Element(parent, Events(EventType::Focus, EventType::Update, EventType::Navigate)) {
Slider::Slider(Element *parent, SliderType type) : Element(parent, Events(EventType::Focus, EventType::Update, EventType::Navigate, EventType::Enable)) {
this->type = type;
set_cursor(Cursor::Pointer);
set_display(Display::Flex);
set_flex_direction(FlexDirection::Row);
set_text_align(TextAlign::Left);

View file

@ -429,7 +429,7 @@ namespace recompui {
void Style::set_cursor(Cursor cursor) {
switch (cursor) {
case Cursor::None:
assert(false && "Unimplemented.");
set_property(Rml::PropertyId::Cursor, Rml::Property("", Rml::Unit::STRING));
break;
case Cursor::Pointer:
set_property(Rml::PropertyId::Cursor, Rml::Property("pointer", Rml::Unit::STRING));
@ -603,6 +603,10 @@ namespace recompui {
void Style::set_tab_index_none() {
set_property(Rml::PropertyId::TabIndex, Rml::Style::Nav::None);
}
void Style::set_focusable(bool focusable) {
set_property(Rml::PropertyId::Focus, focusable ? Rml::Style::Focus::Auto : Rml::Style::Focus::None);
}
} // namespace recompui

View file

@ -100,6 +100,7 @@ namespace recompui {
void set_nav_manual(NavDirection dir, const std::string& target);
void set_tab_index_auto();
void set_tab_index_none();
void set_focusable(bool focusable);
virtual bool is_element() { return false; }
ResourceId get_resource_id() { return resource_id; }
};

View file

@ -93,7 +93,7 @@ namespace recompui {
break;
case EventType::Hover: {
bool hover_active = std::get<EventHover>(e.variant).active;
bool hover_active = std::get<EventHover>(e.variant).active && is_enabled();
set_style_enabled(hover_state, hover_active);
floater->set_style_enabled(hover_state, hover_active);
break;
@ -107,6 +107,14 @@ namespace recompui {
bool enable_active = std::get<EventEnable>(e.variant).active;
set_style_enabled(disabled_state, !enable_active);
floater->set_style_enabled(disabled_state, !enable_active);
if (enable_active) {
set_cursor(Cursor::Pointer);
set_focusable(true);
}
else {
set_cursor(Cursor::None);
set_focusable(false);
}
break;
}
case EventType::Update: {