WindowServer+LibGfx: Automatic "modified" markers in window titles

You can now add the string "[*]" to a window title and it will be
replaced with " (*)" if the window is modified, and with "" otherwise.
This commit is contained in:
Andreas Kling 2021-05-01 19:01:43 +02:00
parent e9c40b0243
commit 20dd5735ce
Notes: sideshowbarker 2024-07-18 18:48:39 +09:00
7 changed files with 13 additions and 10 deletions

View file

@ -128,7 +128,7 @@ void PreviewWidget::paint_event(GUI::PaintEvent& event)
auto frame_rect = Gfx::WindowTheme::current().frame_rect_for_window(Gfx::WindowTheme::WindowType::Normal, rect, m_preview_palette, 0);
Gfx::PainterStateSaver saver(painter);
painter.translate(frame_rect.location());
Gfx::WindowTheme::current().paint_normal_frame(painter, state, rect, title, icon, m_preview_palette, buttons.last().rect, 0);
Gfx::WindowTheme::current().paint_normal_frame(painter, state, rect, title, icon, m_preview_palette, buttons.last().rect, 0, false);
for (auto& button : buttons) {
Gfx::StylePainter::paint_button(painter, button.rect, m_preview_palette, Gfx::ButtonStyle::Normal, false);

View file

@ -76,7 +76,7 @@ void Canvas::paint_event(GUI::PaintEvent& event)
void Canvas::draw(Gfx::Painter& painter)
{
auto active_window_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
Gfx::WindowTheme::current().paint_normal_frame(painter, Gfx::WindowTheme::WindowState::Active, { 4, 18, WIDTH - 8, HEIGHT - 29 }, "Well hello friends 🐞", *active_window_icon, palette(), { WIDTH - 20, 6, 16, 16 }, 0);
Gfx::WindowTheme::current().paint_normal_frame(painter, Gfx::WindowTheme::WindowState::Active, { 4, 18, WIDTH - 8, HEIGHT - 29 }, "Well hello friends 🐞", *active_window_icon, palette(), { WIDTH - 20, 6, 16, 16 }, 0, false);
painter.draw_rect({ 20, 34, WIDTH - 40, HEIGHT - 45 }, palette().color(Gfx::ColorRole::Selection), true);
painter.draw_rect({ 24, 38, WIDTH - 48, HEIGHT - 53 }, palette().color(Gfx::ColorRole::Selection));

View file

@ -52,8 +52,11 @@ Gfx::IntRect ClassicWindowTheme::titlebar_text_rect(WindowType window_type, cons
};
}
void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window_state, const IntRect& window_rect, const StringView& title_text, const Bitmap& icon, const Palette& palette, const IntRect& leftmost_button_rect, int menu_row_count) const
void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window_state, const IntRect& window_rect, const StringView& window_title, const Bitmap& icon, const Palette& palette, const IntRect& leftmost_button_rect, int menu_row_count, bool window_modified) const
{
String final_title = window_title;
final_title.replace("[*]", window_modified ? " (*)" : "");
auto frame_rect = frame_rect_for_window(WindowType::Normal, window_rect, palette, menu_row_count);
frame_rect.set_location({ 0, 0 });
Gfx::StylePainter::paint_window_frame(painter, frame_rect, palette);
@ -64,7 +67,7 @@ void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window
auto titlebar_icon_rect = this->titlebar_icon_rect(WindowType::Normal, window_rect, palette);
auto titlebar_inner_rect = titlebar_text_rect(WindowType::Normal, window_rect, palette);
auto titlebar_title_rect = titlebar_inner_rect;
titlebar_title_rect.set_width(FontDatabase::default_bold_font().width(title_text));
titlebar_title_rect.set_width(FontDatabase::default_bold_font().width(final_title));
auto [title_color, border_color, border_color2, stripes_color, shadow_color] = compute_frame_colors(window_state, palette);
@ -86,9 +89,9 @@ void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window
auto clipped_title_rect = titlebar_title_rect;
clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
if (!clipped_title_rect.is_empty()) {
painter.draw_text(clipped_title_rect.translated(1, 2), title_text, title_font, Gfx::TextAlignment::CenterLeft, shadow_color, Gfx::TextElision::Right);
painter.draw_text(clipped_title_rect.translated(1, 2), final_title, title_font, Gfx::TextAlignment::CenterLeft, shadow_color, Gfx::TextElision::Right);
// FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
painter.draw_text(clipped_title_rect.translated(0, 1), title_text, title_font, Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
painter.draw_text(clipped_title_rect.translated(0, 1), final_title, title_font, Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
}
painter.draw_scaled_bitmap(titlebar_icon_rect, icon, icon.rect());

View file

@ -16,7 +16,7 @@ public:
ClassicWindowTheme();
virtual ~ClassicWindowTheme() override;
virtual void paint_normal_frame(Painter&, WindowState, const IntRect& window_rect, const StringView& title, const Bitmap& icon, const Palette&, const IntRect& leftmost_button_rect, int menu_row_count) const override;
virtual void paint_normal_frame(Painter& painter, WindowState window_state, const IntRect& window_rect, const StringView& window_title, const Bitmap& icon, const Palette& palette, const IntRect& leftmost_button_rect, int menu_row_count, bool window_modified) const override;
virtual void paint_tool_window_frame(Painter&, WindowState, const IntRect& window_rect, const StringView& title, const Palette&, const IntRect& leftmost_button_rect) const override;
virtual void paint_notification_frame(Painter&, const IntRect& window_rect, const Palette&, const IntRect& close_button_rect) const override;

View file

@ -31,7 +31,7 @@ public:
static WindowTheme& current();
virtual void paint_normal_frame(Painter&, WindowState, const IntRect& window_rect, const StringView& title, const Bitmap& icon, const Palette&, const IntRect& leftmost_button_rect, int menu_row_count) const = 0;
virtual void paint_normal_frame(Painter&, WindowState, const IntRect& window_rect, const StringView& title, const Bitmap& icon, const Palette&, const IntRect& leftmost_button_rect, int menu_row_count, bool window_modified) const = 0;
virtual void paint_tool_window_frame(Painter&, WindowState, const IntRect& window_rect, const StringView& title, const Palette&, const IntRect& leftmost_button_rect) const = 0;
virtual void paint_notification_frame(Painter&, const IntRect& window_rect, const Palette&, const IntRect& close_button_rect) const = 0;

View file

@ -998,7 +998,7 @@ void Window::set_modified(bool modified)
return;
m_modified = modified;
invalidate();
frame().invalidate_titlebar();
}
}

View file

@ -306,7 +306,7 @@ void WindowFrame::paint_normal_frame(Gfx::Painter& painter)
{
auto palette = WindowManager::the().palette();
auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::IntRect() : m_buttons.last().relative_rect();
Gfx::WindowTheme::current().paint_normal_frame(painter, window_state_for_theme(), m_window.rect(), compute_title_text(), m_window.icon(), palette, leftmost_button_rect, menu_row_count());
Gfx::WindowTheme::current().paint_normal_frame(painter, window_state_for_theme(), m_window.rect(), compute_title_text(), m_window.icon(), palette, leftmost_button_rect, menu_row_count(), m_window.is_modified());
if (m_window.menubar() && m_window.should_show_menubar())
paint_menubar(painter);