Prevent polling keyboard state for each created ui::Message

Getting the keyboard state to fill the keyboard modifiers can be
expensive (mainly on Windows calling GetAsyncKeyState). So we can lazy
evaluate the modifiers when they are needed.
This commit is contained in:
David Capello 2025-04-01 22:11:31 -03:00
parent b130601716
commit 537ccd393f
2 changed files with 19 additions and 11 deletions

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018-2024 Igara Studio S.A.
// Copyright (C) 2018-2025 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
@ -26,18 +26,26 @@ Message::Message(MessageType type, KeyModifiers modifiers)
, m_display(nullptr)
, m_recipient(nullptr)
, m_commonAncestor(nullptr)
, m_modifiers(modifiers)
{
const os::SystemRef system = os::System::instance();
if (modifiers == kKeyUninitializedModifier && system)
m_modifiers = system->keyModifiers();
else
m_modifiers = modifiers;
}
Message::~Message()
{
}
KeyModifiers Message::modifiers() const
{
if (m_modifiers == kKeyUninitializedModifier) {
const os::SystemRef system = os::System::instance();
if (system)
m_modifiers = system->keyModifiers();
else
m_modifiers = kKeyNoneModifier;
}
return m_modifiers;
}
void Message::setDisplay(Display* display)
{
m_display = display;

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018-2024 Igara Studio S.A.
// Copyright (C) 2018-2025 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
@ -43,7 +43,7 @@ public:
Widget* recipient() const { return m_recipient; }
bool fromFilter() const { return hasFlag(FromFilter); }
void setFromFilter(const bool state) { setFlag(FromFilter, state); }
KeyModifiers modifiers() const { return m_modifiers; }
KeyModifiers modifiers() const;
bool shiftPressed() const { return (m_modifiers & kKeyShiftModifier) == kKeyShiftModifier; }
bool ctrlPressed() const { return (m_modifiers & kKeyCtrlModifier) == kKeyCtrlModifier; }
bool altPressed() const { return (m_modifiers & kKeyAltModifier) == kKeyAltModifier; }
@ -79,7 +79,7 @@ private:
Display* m_display;
Widget* m_recipient; // Recipient of this message
Widget* m_commonAncestor; // Common ancestor between the Leave <-> Enter messages
KeyModifiers m_modifiers; // Key modifiers pressed when message was created
mutable KeyModifiers m_modifiers; // Key modifiers pressed when message was created
};
class CallbackMessage : public Message {