Skip to content

Commit

Permalink
Traktor: Edit control enable global key events when becoming hidden.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Jun 7, 2024
1 parent 41a33d6 commit d1104ab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
62 changes: 40 additions & 22 deletions code/Ui/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ Edit::Edit()
{
}

Edit::~Edit()
{
T_FATAL_ASSERT(!m_modal);
}

bool Edit::create(Widget* parent, const std::wstring& text, uint32_t style, const EditValidator* validator)
{
if (!Widget::create(parent, style | WsFocus | WsDoubleBuffer))
Expand All @@ -58,7 +63,6 @@ bool Edit::create(Widget* parent, const std::wstring& text, uint32_t style, cons
m_acceptTab = (bool)((style & WsWantAllInput) != 0);
m_readOnly = (bool)((style & WsReadOnly) != 0);

addEventHandler< FocusEvent >(this, &Edit::eventFocus);
addEventHandler< MouseTrackEvent >(this, &Edit::eventMouseTrack);
addEventHandler< MouseButtonDownEvent >(this, &Edit::eventButtonDown);
addEventHandler< MouseDoubleClickEvent >(this, &Edit::eventDoubleClick);
Expand All @@ -69,6 +73,8 @@ bool Edit::create(Widget* parent, const std::wstring& text, uint32_t style, cons
{
addEventHandler< KeyDownEvent >(this, &Edit::eventKeyDown);
addEventHandler< KeyEvent >(this, &Edit::eventKey);
addEventHandler< FocusEvent >(this, &Edit::eventFocus);
addEventHandler< ShowEvent >(this, &Edit::eventShow);
}

setText(text);
Expand All @@ -78,7 +84,10 @@ bool Edit::create(Widget* parent, const std::wstring& text, uint32_t style, cons
void Edit::destroy()
{
if (m_modal)
{
Application::getInstance()->enableEventHandlers< KeyDownEvent >();
m_modal = false;
}

Widget::destroy();
}
Expand Down Expand Up @@ -251,27 +260,6 @@ Size Edit::getMaximumSize() const
return Size(65535, height);
}

void Edit::eventFocus(FocusEvent* event)
{
m_caretBlink = true;
if (event->gotFocus())
{
// Disable global KeyDown events while Edit got focus to prevent
// global hooks, such as ShortcutTable, to receive events.
Application::getInstance()->disableEventHandlers< KeyDownEvent >();
startTimer(500);
m_modal = true;
}
else
{
Application::getInstance()->enableEventHandlers< KeyDownEvent >();
stopTimer();
deselect();
m_modal = false;
}
update();
}

void Edit::eventMouseTrack(MouseTrackEvent* event)
{
m_hover = event->entered();
Expand Down Expand Up @@ -524,6 +512,36 @@ void Edit::eventKey(KeyEvent* event)
}
}

void Edit::eventFocus(FocusEvent* event)
{
m_caretBlink = true;
if (event->gotFocus())
{
// Disable global KeyDown events while Edit got focus to prevent
// global hooks, such as ShortcutTable, to receive events.
Application::getInstance()->disableEventHandlers< KeyDownEvent >();
startTimer(500);
m_modal = true;
}
else
{
Application::getInstance()->enableEventHandlers< KeyDownEvent >();
stopTimer();
deselect();
m_modal = false;
}
update();
}

void Edit::eventShow(ShowEvent* event)
{
if (!event->isVisible() && m_modal)
{
Application::getInstance()->enableEventHandlers< KeyDownEvent >();
m_modal = false;
}
}

void Edit::eventPaint(PaintEvent* event)
{
Canvas& canvas = event->getCanvas();
Expand Down
8 changes: 6 additions & 2 deletions code/Ui/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class T_DLLCLASS Edit : public Widget

Edit();

virtual ~Edit();

bool create(Widget* parent, const std::wstring& text = L"", uint32_t style = WsNone, const EditValidator* validator = nullptr);

virtual void destroy() override;
Expand Down Expand Up @@ -81,8 +83,6 @@ class T_DLLCLASS Edit : public Widget
bool m_hover;
bool m_modal;

void eventFocus(FocusEvent* event);

void eventMouseTrack(MouseTrackEvent* event);

void eventButtonDown(MouseButtonDownEvent* event);
Expand All @@ -93,6 +93,10 @@ class T_DLLCLASS Edit : public Widget

void eventKey(KeyEvent* event);

void eventFocus(FocusEvent* event);

void eventShow(ShowEvent* event);

void eventPaint(PaintEvent* event);

void eventTimer(TimerEvent* event);
Expand Down

0 comments on commit d1104ab

Please sign in to comment.