Minor change in ui::Widget to check current manager

This commit is contained in:
David Capello 2021-01-15 13:16:40 -03:00
parent 045e3ad473
commit b1b201a22c

View File

@ -1,5 +1,5 @@
// Aseprite UI Library // Aseprite UI Library
// Copyright (C) 2018-2020 Igara Studio S.A. // Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello // Copyright (C) 2001-2018 David Capello
// //
// This file is released under the terms of the MIT license. // This file is released under the terms of the MIT license.
@ -106,7 +106,11 @@ Widget::~Widget()
void Widget::deferDelete() void Widget::deferDelete()
{ {
manager()->addToGarbage(this); if (auto man = manager())
man->addToGarbage(this);
else {
ASSERT(false);
}
} }
void Widget::initTheme() void Widget::initTheme()
@ -219,7 +223,8 @@ void Widget::setVisible(bool state)
} }
else { else {
if (!hasFlags(HIDDEN)) { if (!hasFlags(HIDDEN)) {
manager()->freeWidget(this); // Free from manager if (auto man = manager())
man->freeWidget(this); // Free from manager
enableFlags(HIDDEN); enableFlags(HIDDEN);
onVisible(false); onVisible(false);
@ -241,7 +246,8 @@ void Widget::setEnabled(bool state)
} }
else { else {
if (!hasFlags(DISABLED)) { if (!hasFlags(DISABLED)) {
manager()->freeWidget(this); // Free from the manager if (auto man = manager())
man->freeWidget(this); // Free from the manager
enableFlags(DISABLED); enableFlags(DISABLED);
invalidate(); invalidate();
@ -541,9 +547,8 @@ void Widget::removeChild(WidgetsList::iterator& it)
m_children.erase(it); m_children.erase(it);
// Free from manager // Free from manager
Manager* manager = this->manager(); if (auto man = manager())
if (manager) man->freeWidget(child);
manager->freeWidget(child);
child->m_parent = nullptr; child->m_parent = nullptr;
} }
@ -617,7 +622,8 @@ void Widget::layout()
void Widget::loadLayout() void Widget::loadLayout()
{ {
if (!m_id.empty()) { if (!m_id.empty()) {
LayoutIO* io = manager()->getLayoutIO(); auto man = manager();
LayoutIO* io = (man ? man->getLayoutIO(): nullptr);
if (io) { if (io) {
std::string layout = io->loadLayout(this); std::string layout = io->loadLayout(this);
if (!layout.empty()) { if (!layout.empty()) {
@ -636,7 +642,8 @@ void Widget::loadLayout()
void Widget::saveLayout() void Widget::saveLayout()
{ {
if (!m_id.empty()) { if (!m_id.empty()) {
LayoutIO* io = manager()->getLayoutIO(); auto man = manager();
LayoutIO* io = (man ? man->getLayoutIO(): nullptr);
if (io) { if (io) {
std::stringstream s; std::stringstream s;
SaveLayoutEvent ev(this, s); SaveLayoutEvent ev(this, s);
@ -690,8 +697,8 @@ void Widget::setBoundsQuietly(const gfx::Rect& rc)
m_bounds = rc; m_bounds = rc;
// Remove all paint messages for this widget. // Remove all paint messages for this widget.
if (Manager* manager = this->manager()) if (Manager* man = manager())
manager->removeMessagesFor(this, kPaintMessage); man->removeMessagesFor(this, kPaintMessage);
} }
// TODO Test moving this inside the if (m_bounds != rc) { ... } // TODO Test moving this inside the if (m_bounds != rc) { ... }
@ -975,6 +982,8 @@ void Widget::flushRedraw()
Manager* manager = this->manager(); Manager* manager = this->manager();
ASSERT(manager); ASSERT(manager);
if (!manager)
return;
while (!processing.empty()) { while (!processing.empty()) {
Widget* widget = processing.front(); Widget* widget = processing.front();
@ -1299,36 +1308,44 @@ void Widget::resetSizeHint()
void Widget::requestFocus() void Widget::requestFocus()
{ {
manager()->setFocus(this); if (auto man = manager())
man->setFocus(this);
} }
void Widget::releaseFocus() void Widget::releaseFocus()
{ {
if (hasFocus()) if (hasFocus()) {
manager()->freeFocus(); if (auto man = manager())
man->freeFocus();
}
} }
// Captures the mouse to send all the future mouse messsages to the // Captures the mouse to send all the future mouse messsages to the
// specified widget (included the kMouseMoveMessage and kSetCursorMessage). // specified widget (included the kMouseMoveMessage and kSetCursorMessage).
void Widget::captureMouse() void Widget::captureMouse()
{ {
if (!manager()->getCapture()) { if (auto man = manager()) {
manager()->setCapture(this); if (!man->getCapture()) {
man->setCapture(this);
}
} }
} }
// Releases the capture of the mouse events. // Releases the capture of the mouse events.
void Widget::releaseMouse() void Widget::releaseMouse()
{ {
if (manager()->getCapture() == this) { if (auto man = manager()) {
manager()->freeCapture(); if (man->getCapture() == this) {
man->freeCapture();
}
} }
} }
bool Widget::offerCapture(ui::MouseMessage* mouseMsg, int widget_type) bool Widget::offerCapture(ui::MouseMessage* mouseMsg, int widget_type)
{ {
if (hasCapture()) { if (hasCapture()) {
Widget* pick = manager()->pick(mouseMsg->position()); auto man = manager();
Widget* pick = (man ? man->pick(mouseMsg->position()): nullptr);
if (pick && pick != this && pick->type() == widget_type) { if (pick && pick != this && pick->type() == widget_type) {
releaseMouse(); releaseMouse();
@ -1339,7 +1356,7 @@ bool Widget::offerCapture(ui::MouseMessage* mouseMsg, int widget_type)
mouseMsg->modifiers(), mouseMsg->modifiers(),
mouseMsg->position()); mouseMsg->position());
mouseMsg2->setRecipient(pick); mouseMsg2->setRecipient(pick);
manager()->enqueueMessage(mouseMsg2); man->enqueueMessage(mouseMsg2);
return true; return true;
} }
} }
@ -1600,8 +1617,8 @@ void Widget::offsetWidgets(int dx, int dy)
m_bounds.offset(dx, dy); m_bounds.offset(dx, dy);
// Remove all paint messages for this widget. // Remove all paint messages for this widget.
if (Manager* manager = this->manager()) if (auto man = manager())
manager->removeMessagesFor(this, kPaintMessage); man->removeMessagesFor(this, kPaintMessage);
for (auto child : m_children) for (auto child : m_children)
child->offsetWidgets(dx, dy); child->offsetWidgets(dx, dy);