Skip to content

Commit

Permalink
LibGUI: Fix re-paint issue when adding new widgets when window is alr…
Browse files Browse the repository at this point in the history
…eady shown
  • Loading branch information
kperdlich committed Sep 28, 2024
1 parent 2768a27 commit dc2196f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 35 deletions.
25 changes: 6 additions & 19 deletions Userland/LibGUI/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void Widget::event(Event& event)
updateLayout();
return onResizeEvent(static_cast<ResizeEvent&>(event));
case Event::Type::Paint: {
m_isDirty = false;
PaintEvent& paintEvent = static_cast<PaintEvent&>(event);
Painter painter(*this);
painter.setClipRect(paintEvent.rect());
Expand Down Expand Up @@ -114,8 +113,10 @@ void Widget::updateLayout()
if (!m_isVisible)
return;

if (m_layout)
if (m_layout) {
m_layout->activate();
update();
}
}

void Widget::onShowEvent(Event& event)
Expand Down Expand Up @@ -162,26 +163,15 @@ void Widget::setRelativeRect(const Rect& rect)
update();
}

void Widget::setWindow(Window* window)
{
m_window = window;
for (auto& child : m_children) {
if (child->isWidgetType()) {
Widget* childWidget = static_cast<Widget*>(child);
childWidget->setWindow(window);
}
}
}

bool Widget::hasFocus() const
{
return m_window && m_window->focusedWidget() == this && m_window->isActive();
return window() && window()->focusedWidget() == this && window()->isActive();
}

void Widget::setFocus(FocusReason reason)
{
ASSERT(m_window != nullptr);
m_window->setFocusedWidget(this, reason);
ASSERT(window() != nullptr);
window()->setFocusedWidget(this, reason);
}

bool Widget::isWidgetType() const
Expand Down Expand Up @@ -304,9 +294,6 @@ void Widget::update()

void Widget::update(const Rect& rect)
{
if (m_isDirty)
return;
m_isDirty = true;
if (Window* widgetWindow = window()) {
Rect widgetRelativeRect = rect;
widgetRelativeRect.moveBy(windowRelativeRect().position());
Expand Down
17 changes: 13 additions & 4 deletions Userland/LibGUI/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ class Widget : public CObject {
virtual void event(Event& event) override;
virtual bool isWidgetType() const override;

void setWindow(Window* window);
[[nodiscard]] Window* window() { return m_window; }
[[nodiscard]] const Window* window() const { return m_window; }
void setWindow(Window* window) { m_window = window; }
[[nodiscard]] Window* window()
{
if (Widget* const parent = parentWidget())
return parent->window();
return m_window;
}
[[nodiscard]] const Window* window() const
{
if (const Widget* const parent = parentWidget())
return parent->window();
return m_window;
}

[[nodiscard]] Rect windowRelativeRect() const;

Expand Down Expand Up @@ -111,7 +121,6 @@ class Widget : public CObject {
SizePolicy m_verticalSizePolicy { SizePolicy::Automatic };
SizePolicy m_horizontalSizePolicy { SizePolicy::Automatic };
bool m_isVisible { true };
bool m_isDirty { false };
};

} // GUI
2 changes: 0 additions & 2 deletions Userland/LibGUI/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ void Window::setCentralWidget(Widget& widget)
return;

m_centralWidget = &widget;
m_centralWidget->setParent(this);
m_centralWidget->setWindow(this);

Rect newRect = rect();
Expand All @@ -133,7 +132,6 @@ void Window::setCentralWidget(Widget& widget)
newRect.setWidth(m_centralWidget->preferredSizeHint().width());

resize(newRect.size());
m_centralWidget->resize(newRect.width(), newRect.height());
}

void Window::show()
Expand Down
3 changes: 2 additions & 1 deletion Userland/LibGUI/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,14 @@ void WindowManager::flushPainting()
if (!bigDirtyRect.intersects(windowOuterFrameRect(window))) {
return IteratorResult::Continue;
}

paintWindowFrame(window);
Bitmap* const windowBitmap = window.backBuffer();
if (!windowBitmap) {
std::cout << "[compose] no bitmap for window: " << window.title() << std::endl;
return IteratorResult::Continue;
}

paintWindowFrame(window);
Painter painter(*m_backBuffer);
// FIXME: Only blit pixels for updated rect
painter.blit(window.position(), *windowBitmap);
Expand Down
16 changes: 7 additions & 9 deletions Userland/LibGUI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ int main()
GUI::Widget* buttonListLayoutWidget = new GUI::Widget();
GUI::VBoxLayout* buttonList = new GUI::VBoxLayout(buttonListLayoutWidget);
buttonList->setSpacing(10);
buttonList->addWidget(*new GUI::Button("Btn1"));
buttonList->addWidget(*new GUI::Button("Btn2"));
buttonList->addWidget(*new GUI::Button("Btn3"));
buttonList->addWidget(*new GUI::Button("Btn4"));
buttonList->addWidget(*new GUI::Button("Btn5"));

static int buttonCounter = 5;
for (int i = 0; i < buttonCounter; ++i)
buttonList->addWidget(*new GUI::Button("Btn" + ADS::String::fromInt(i)));
buttonListLayoutWidget->setLayout(buttonList);

GUI::ScrollArea* scrollArea = new GUI::ScrollArea();
Expand All @@ -33,7 +32,7 @@ int main()
GUI::Widget* rootWidget = new GUI::Widget();
GUI::VBoxLayout* rootLayout = new GUI::VBoxLayout(rootWidget);
GUI::Button* button = new GUI::Button(rootWidget);
button->setText("Resize and translate");
button->setText("Add to list");
rootLayout->addWidget(*button);
rootLayout->addWidget(*scrollArea);
rootWidget->setLayout(rootLayout);
Expand All @@ -42,10 +41,9 @@ int main()
win1.resize(640, 480);
win1.setCentralWidget(*rootWidget);
win1.setTitle("Window 1");
button->onClick = [&win1]() {
button->onClick = [buttonList]() {
std::cout << "Button clicked" << std::endl;
win1.resize(720, 480);
win1.setPosition(100, 100);
buttonList->addWidget(*new GUI::Button("Btn" + ADS::String::fromInt(buttonCounter++)));
};
win1.show();

Expand Down

0 comments on commit dc2196f

Please sign in to comment.