From 21c647dd7500bcc193cdc5934c26a845fcad1e97 Mon Sep 17 00:00:00 2001 From: MacDue Date: Sun, 1 May 2022 15:45:14 +0100 Subject: [PATCH] LibGUI: Add center_window_group_within() to AbstractThemePreview This method will center a group of window rects, within some bounds, accounting for the properties of the currently selected theme (i.e. border width, title height, etc). --- .../Libraries/LibGUI/AbstractThemePreview.cpp | 27 +++++++++++++++++++ .../Libraries/LibGUI/AbstractThemePreview.h | 5 ++++ 2 files changed, 32 insertions(+) diff --git a/Userland/Libraries/LibGUI/AbstractThemePreview.cpp b/Userland/Libraries/LibGUI/AbstractThemePreview.cpp index 96ae9f2351..18a4e2c11c 100644 --- a/Userland/Libraries/LibGUI/AbstractThemePreview.cpp +++ b/Userland/Libraries/LibGUI/AbstractThemePreview.cpp @@ -158,4 +158,31 @@ void AbstractThemePreview::paint_event(GUI::PaintEvent& event) paint_preview(event); } +void AbstractThemePreview::center_window_group_within(Span windows, Gfx::IntRect const& bounds) +{ + VERIFY(windows.size() >= 1); + + auto to_frame_rect = [&](Gfx::IntRect const& rect) { + return Gfx::WindowTheme::current().frame_rect_for_window( + Gfx::WindowTheme::WindowType::Normal, rect, m_preview_palette, 0); + }; + + auto leftmost_x_value = windows[0].rect.x(); + auto topmost_y_value = windows[0].rect.y(); + auto combind_frame_rect = to_frame_rect(windows[0].rect); + for (auto& window : windows.slice(1)) { + if (window.rect.x() < leftmost_x_value) + leftmost_x_value = window.rect.x(); + if (window.rect.y() < topmost_y_value) + topmost_y_value = window.rect.y(); + combind_frame_rect = combind_frame_rect.united(to_frame_rect(window.rect)); + } + + combind_frame_rect.center_within(bounds); + auto frame_offset = to_frame_rect({}).location(); + for (auto& window : windows) { + window.rect.set_left(combind_frame_rect.left() + (window.rect.x() - leftmost_x_value) - frame_offset.x()); + window.rect.set_top(combind_frame_rect.top() + (window.rect.y() - topmost_y_value) - frame_offset.y()); + } +} } diff --git a/Userland/Libraries/LibGUI/AbstractThemePreview.h b/Userland/Libraries/LibGUI/AbstractThemePreview.h index fc1fbbbc1a..7345a5471d 100644 --- a/Userland/Libraries/LibGUI/AbstractThemePreview.h +++ b/Userland/Libraries/LibGUI/AbstractThemePreview.h @@ -31,6 +31,11 @@ public: Function on_theme_load_from_file; Function on_palette_change; + struct Window { + Gfx::IntRect& rect; + }; + void center_window_group_within(Span windows, Gfx::IntRect const& bounds); + protected: explicit AbstractThemePreview(Gfx::Palette const&);