Skip to content

Commit daaf589

Browse files
AtkinsSJawesomekling
authored andcommitted
ThemeEditor: Display window shadows in preview :^)
1 parent 885ca2f commit daaf589

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

Userland/Applications/ThemeEditor/PreviewWidget.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3+
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
34
*
45
* SPDX-License-Identifier: BSD-2-Clause
56
*/
@@ -84,6 +85,8 @@ PreviewWidget::PreviewWidget(const Gfx::Palette& preview_palette)
8485
m_maximize_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/upward-triangle.png");
8586
m_minimize_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/downward-triangle.png");
8687

88+
load_theme_bitmaps();
89+
8790
m_gallery = add<MiniWidgetGallery>();
8891
set_greedy_for_hits(true);
8992
}
@@ -92,10 +95,33 @@ PreviewWidget::~PreviewWidget()
9295
{
9396
}
9497

98+
void PreviewWidget::load_theme_bitmaps()
99+
{
100+
auto load_bitmap = [](String const& path, String& last_path, RefPtr<Gfx::Bitmap>& bitmap) {
101+
if (path.is_empty()) {
102+
last_path = String::empty();
103+
bitmap = nullptr;
104+
} else if (last_path != path) {
105+
bitmap = Gfx::Bitmap::try_load_from_file(path);
106+
if (bitmap)
107+
last_path = path;
108+
else
109+
last_path = String::empty();
110+
}
111+
};
112+
113+
load_bitmap(m_preview_palette.active_window_shadow_path(), m_last_active_window_shadow_path, m_active_window_shadow);
114+
load_bitmap(m_preview_palette.inactive_window_shadow_path(), m_last_inactive_window_shadow_path, m_inactive_window_shadow);
115+
load_bitmap(m_preview_palette.menu_shadow_path(), m_last_menu_shadow_path, m_menu_shadow);
116+
load_bitmap(m_preview_palette.taskbar_shadow_path(), m_last_taskbar_shadow_path, m_taskbar_shadow);
117+
load_bitmap(m_preview_palette.tooltip_shadow_path(), m_last_tooltip_shadow_path, m_tooltip_shadow);
118+
}
119+
95120
void PreviewWidget::set_preview_palette(const Gfx::Palette& palette)
96121
{
97122
m_preview_palette = palette;
98123
m_gallery->set_preview_palette(palette);
124+
load_theme_bitmaps();
99125
update();
100126
}
101127

@@ -139,14 +165,27 @@ void PreviewWidget::paint_event(GUI::PaintEvent& event)
139165

140166
for (auto& button : buttons) {
141167
pos -= window_button_width;
142-
Gfx::IntRect rect { pos, 0, window_button_width, window_button_height };
143-
rect.center_vertically_within(titlebar_text_rect);
144-
button.rect = rect;
168+
Gfx::IntRect button_rect { pos, 0, window_button_width, window_button_height };
169+
button_rect.center_vertically_within(titlebar_text_rect);
170+
button.rect = button_rect;
145171
}
146172

147173
painter.fill_rect(rect, m_preview_palette.window());
148174

149175
auto frame_rect = Gfx::WindowTheme::current().frame_rect_for_window(Gfx::WindowTheme::WindowType::Normal, rect, m_preview_palette, 0);
176+
177+
auto paint_shadow = [](Gfx::Painter& painter, Gfx::IntRect& frame_rect, Gfx::Bitmap const& shadow_bitmap) {
178+
auto total_shadow_size = shadow_bitmap.height();
179+
auto shadow_rect = frame_rect.inflated(total_shadow_size, total_shadow_size);
180+
Gfx::StylePainter::paint_simple_rect_shadow(painter, shadow_rect, shadow_bitmap);
181+
};
182+
183+
if (state == Gfx::WindowTheme::WindowState::Active && m_active_window_shadow) {
184+
paint_shadow(painter, frame_rect, *m_active_window_shadow);
185+
} else if (state == Gfx::WindowTheme::WindowState::Inactive && m_inactive_window_shadow) {
186+
paint_shadow(painter, frame_rect, *m_inactive_window_shadow);
187+
}
188+
150189
Gfx::PainterStateSaver saver(painter);
151190
painter.translate(frame_rect.location());
152191
Gfx::WindowTheme::current().paint_normal_frame(painter, state, rect, title, icon, m_preview_palette, buttons.last().rect, 0, false);

Userland/Applications/ThemeEditor/PreviewWidget.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#pragma once
88

99
#include <LibGUI/Frame.h>
10+
#include <LibGfx/Bitmap.h>
1011
#include <LibGfx/Palette.h>
1112

1213
namespace ThemeEditor {
@@ -28,6 +29,8 @@ class PreviewWidget final : public GUI::Frame {
2829
private:
2930
explicit PreviewWidget(const Gfx::Palette&);
3031

32+
void load_theme_bitmaps();
33+
3134
virtual void paint_event(GUI::PaintEvent&) override;
3235
virtual void resize_event(GUI::ResizeEvent&) override;
3336
virtual void drop_event(GUI::DropEvent&) override;
@@ -42,6 +45,17 @@ class PreviewWidget final : public GUI::Frame {
4245
RefPtr<Gfx::Bitmap> m_close_bitmap;
4346
RefPtr<Gfx::Bitmap> m_maximize_bitmap;
4447
RefPtr<Gfx::Bitmap> m_minimize_bitmap;
48+
49+
RefPtr<Gfx::Bitmap> m_active_window_shadow;
50+
RefPtr<Gfx::Bitmap> m_inactive_window_shadow;
51+
RefPtr<Gfx::Bitmap> m_menu_shadow;
52+
RefPtr<Gfx::Bitmap> m_taskbar_shadow;
53+
RefPtr<Gfx::Bitmap> m_tooltip_shadow;
54+
String m_last_active_window_shadow_path;
55+
String m_last_inactive_window_shadow_path;
56+
String m_last_menu_shadow_path;
57+
String m_last_taskbar_shadow_path;
58+
String m_last_tooltip_shadow_path;
4559
};
4660

4761
}

0 commit comments

Comments
 (0)