Skip to content

Commit 2a32330

Browse files
oriko1010awesomekling
authored andcommitted
LibGUI: Add a ThemeChange event
1 parent 2b162ef commit 2a32330

File tree

8 files changed

+53
-1
lines changed

8 files changed

+53
-1
lines changed

Libraries/LibGUI/Event.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
#include <Kernel/KeyCode.h>
3030
#include <LibCore/Event.h>
31+
#include <LibGUI/WindowType.h>
3132
#include <LibGfx/Point.h>
3233
#include <LibGfx/Rect.h>
33-
#include <LibGUI/WindowType.h>
3434

3535
namespace GUI {
3636

@@ -62,6 +62,7 @@ class Event : public Core::Event {
6262
EnabledChange,
6363
DragMove,
6464
Drop,
65+
ThemeChange,
6566

6667
__Begin_WM_Events,
6768
WM_WindowRemoved,
@@ -340,4 +341,12 @@ class DropEvent final : public Event {
340341
NonnullRefPtr<Core::MimeData> m_mime_data;
341342
};
342343

344+
class ThemeChangeEvent final : public Event {
345+
public:
346+
ThemeChangeEvent()
347+
: Event(Type::ThemeChange)
348+
{
349+
}
350+
};
351+
343352
}

Libraries/LibGUI/TextEditor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,13 @@ void TextEditor::resize_event(ResizeEvent& event)
12791279
recompute_all_visual_lines();
12801280
}
12811281

1282+
void TextEditor::theme_change_event(ThemeChangeEvent& event)
1283+
{
1284+
ScrollableWidget::theme_change_event(event);
1285+
if (m_highlighter)
1286+
m_highlighter->rehighlight(palette());
1287+
}
1288+
12821289
void TextEditor::set_selection(const TextRange& selection)
12831290
{
12841291
if (m_selection == selection)

Libraries/LibGUI/TextEditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class TextEditor
148148
virtual void leave_event(Core::Event&) override;
149149
virtual void context_menu_event(ContextMenuEvent&) override;
150150
virtual void resize_event(ResizeEvent&) override;
151+
virtual void theme_change_event(ThemeChangeEvent&) override;
151152
virtual void cursor_did_change() {}
152153

153154
TextPosition text_position_at(const Gfx::Point&) const;

Libraries/LibGUI/Widget.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ void Widget::event(Core::Event& event)
206206
return drag_move_event(static_cast<DragEvent&>(event));
207207
case Event::Drop:
208208
return drop_event(static_cast<DropEvent&>(event));
209+
case Event::ThemeChange:
210+
return theme_change_event(static_cast<ThemeChangeEvent&>(event));
209211
case Event::Enter:
210212
return handle_enter_event(event);
211213
case Event::Leave:
@@ -417,6 +419,10 @@ void Widget::drop_event(DropEvent& event)
417419
event.ignore();
418420
}
419421

422+
void Widget::theme_change_event(ThemeChangeEvent&)
423+
{
424+
}
425+
420426
void Widget::update()
421427
{
422428
if (rect().is_empty())

Libraries/LibGUI/Widget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ class Widget : public Core::Object {
292292
virtual void change_event(Event&);
293293
virtual void drag_move_event(DragEvent&);
294294
virtual void drop_event(DropEvent&);
295+
virtual void theme_change_event(ThemeChangeEvent&);
295296

296297
virtual void did_begin_inspection() override;
297298
virtual void did_end_inspection() override;

Libraries/LibGUI/Window.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,22 @@ void Window::event(Core::Event& event)
334334
return result.widget->dispatch_event(*local_event, this);
335335
}
336336

337+
if (event.type() == Event::ThemeChange) {
338+
if (!m_main_widget)
339+
return;
340+
auto theme_event = static_cast<ThemeChangeEvent&>(event);
341+
auto dispatch_theme_change = [&](auto& widget, auto recursive) {
342+
widget.dispatch_event(theme_event, this);
343+
widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
344+
widget.dispatch_event(theme_event, this);
345+
recursive(widget, recursive);
346+
return IterationDecision::Continue;
347+
});
348+
};
349+
dispatch_theme_change(*m_main_widget.ptr(), dispatch_theme_change);
350+
return;
351+
}
352+
337353
Core::Object::event(event);
338354
}
339355

@@ -635,6 +651,14 @@ void Window::schedule_relayout()
635651
});
636652
}
637653

654+
void Window::for_each_window(Badge<WindowServerConnection>, Function<void(Window&)> callback)
655+
{
656+
for (auto& e : *reified_windows) {
657+
ASSERT(e.value);
658+
callback(*e.value);
659+
}
660+
}
661+
638662
void Window::update_all_windows(Badge<WindowServerConnection>)
639663
{
640664
for (auto& e : *reified_windows) {

Libraries/LibGUI/Window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class Window : public Core::Object {
172172

173173
void schedule_relayout();
174174

175+
static void for_each_window(Badge<WindowServerConnection>, Function<void(Window&)>);
175176
static void update_all_windows(Badge<WindowServerConnection>);
176177
void notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded);
177178

Libraries/LibGUI/WindowServerConnection.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ void WindowServerConnection::handle(const Messages::WindowClient::UpdateSystemTh
7373
{
7474
set_system_theme_from_shbuf_id(message.system_theme_buffer_id());
7575
Window::update_all_windows({});
76+
Window::for_each_window({}, [](auto& window) {
77+
Core::EventLoop::current().post_event(window, make<ThemeChangeEvent>());
78+
});
7679
}
7780

7881
void WindowServerConnection::handle(const Messages::WindowClient::Paint& message)

0 commit comments

Comments
 (0)