1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:57:35 +00:00

Move LibGUI/GStyle to SharedGraphics/StylePainter.

I want to paint some buttons in WindowServer where we don't have LibGUI.
This commit is contained in:
Andreas Kling 2019-03-28 17:32:38 +01:00
parent d12b6b8677
commit c7ab643883
13 changed files with 47 additions and 46 deletions

View file

@ -1,6 +1,6 @@
#include "GButton.h"
#include <LibGUI/GPainter.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/StylePainter.h>
//#define GBUTTON_DEBUG
@ -26,7 +26,7 @@ void GButton::paint_event(GPaintEvent& event)
GPainter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);
StylePainter::the().paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);
if (!caption().is_empty() || m_icon) {
auto content_rect = rect();

View file

@ -1,7 +1,7 @@
#pragma once
#include <LibGUI/GWidget.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/StylePainter.h>
#include <AK/AKString.h>
#include <AK/Function.h>
#include <SharedGraphics/GraphicsBitmap.h>
@ -20,8 +20,8 @@ public:
Function<void(GButton&)> on_click;
void set_button_style(GButtonStyle style) { m_button_style = style; }
GButtonStyle button_style() const { return m_button_style; }
void set_button_style(ButtonStyle style) { m_button_style = style; }
ButtonStyle button_style() const { return m_button_style; }
void click();
@ -37,7 +37,7 @@ private:
String m_caption;
RetainPtr<GraphicsBitmap> m_icon;
GButtonStyle m_button_style { GButtonStyle::Normal };
ButtonStyle m_button_style { ButtonStyle::Normal };
bool m_being_pressed { false };
bool m_hovered { false };
};

View file

@ -1,5 +1,5 @@
#include <LibGUI/GFrame.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/StylePainter.h>
#include <LibGUI/GPainter.h>
GFrame::GFrame(GWidget* parent)

View file

@ -1,5 +1,5 @@
#include <LibGUI/GScrollBar.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/StylePainter.h>
#include <SharedGraphics/CharacterBitmap.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <LibGUI/GPainter.h>
@ -190,14 +190,14 @@ void GScrollBar::paint_event(GPaintEvent& event)
painter.fill_rect(rect(), Color::from_rgb(0xd6d2ce));
GStyle::the().paint_button(painter, up_button_rect(), GButtonStyle::Normal, false);
StylePainter::the().paint_button(painter, up_button_rect(), ButtonStyle::Normal, false);
painter.draw_bitmap(up_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray);
GStyle::the().paint_button(painter, down_button_rect(), GButtonStyle::Normal, false);
StylePainter::the().paint_button(painter, down_button_rect(), ButtonStyle::Normal, false);
painter.draw_bitmap(down_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray);
if (has_scrubber())
GStyle::the().paint_button(painter, scrubber_rect(), GButtonStyle::Normal, false);
StylePainter::the().paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false);
}
void GScrollBar::mousedown_event(GMouseEvent& event)

View file

@ -1,7 +1,7 @@
#include <LibGUI/GStatusBar.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/StylePainter.h>
#include <LibGUI/GPainter.h>
GStatusBar::GStatusBar(GWidget* parent)
@ -36,5 +36,5 @@ void GStatusBar::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_surface(painter, rect());
StylePainter::the().paint_surface(painter, rect());
}

View file

@ -1,106 +0,0 @@
#include <LibGUI/GStyle.h>
#include <LibGUI/GPainter.h>
static GStyle* s_the;
GStyle& GStyle::the()
{
if (!s_the)
s_the = new GStyle;
return *s_the;
}
GStyle::GStyle()
{
}
static void paint_button_new(Painter& painter, const Rect& rect, bool pressed)
{
Color button_color = Color::from_rgb(0xc0c0c0);
Color highlight_color1 = Color::from_rgb(0xffffff);
Color highlight_color2 = Color::from_rgb(0xdfdfdf);
Color shadow_color1 = Color::from_rgb(0x808080);
Color shadow_color2 = Color::from_rgb(0x404040);
PainterStateSaver saver(painter);
painter.translate(rect.location());
if (pressed) {
// Base
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
painter.draw_rect(rect, shadow_color2);
// Sunken shadow
painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color1);
painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color1);
} else {
// Base
painter.fill_rect({ 2, 2, rect.width() - 4, rect.height() - 4 }, button_color);
// Outer highlight
painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, highlight_color2);
painter.draw_line({ 0, 1 }, { 0, rect.height() - 2 }, highlight_color2);
// Inner highlight
painter.draw_line({ 1, 1 }, { rect.width() - 3, 1 }, highlight_color1);
painter.draw_line({ 1, 2 }, { 1, rect.height() - 3 }, highlight_color1);
// Outer shadow
painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2);
painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, shadow_color2);
// Inner shadow
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color1);
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1);
}
}
void GStyle::paint_button(Painter& painter, const Rect& rect, GButtonStyle button_style, bool pressed, bool hovered)
{
if (button_style == GButtonStyle::Normal)
return paint_button_new(painter, rect, pressed);
Color button_color = Color::LightGray;
Color highlight_color = Color::White;
Color shadow_color = Color(96, 96, 96);
if (button_style == GButtonStyle::OldNormal)
painter.draw_rect(rect, Color::Black);
PainterStateSaver saver(painter);
painter.translate(rect.location());
if (pressed) {
// Base
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
// Sunken shadow
painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color);
painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color);
// Bottom highlight
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color);
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, highlight_color);
} else if (button_style == GButtonStyle::OldNormal || (button_style == GButtonStyle::CoolBar && hovered)) {
// Base
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
// White highlight
painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, highlight_color);
// Gray shadow
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color);
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color);
}
}
void GStyle::paint_surface(Painter& painter, const Rect& rect)
{
painter.fill_rect({ rect.x(), rect.y() + 1, rect.width(), rect.height() - 2 }, Color::LightGray);
painter.draw_line(rect.top_left(), rect.top_right(), Color::White);
painter.draw_line(rect.bottom_left(), rect.bottom_right(), Color::MidGray);
painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), Color::White);
painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), Color::MidGray);
}

View file

@ -1,17 +0,0 @@
#pragma once
class Painter;
class Rect;
enum class GButtonStyle { Normal, CoolBar, OldNormal };
class GStyle {
public:
static GStyle& the();
void paint_button(Painter&, const Rect&, GButtonStyle, bool pressed, bool hovered = false);
void paint_surface(Painter&, const Rect&);
private:
GStyle();
};

View file

@ -34,7 +34,7 @@ void GToolBar::add_action(Retained<GAction>&& action)
raw_action_ptr->activate();
};
button->set_button_style(GButtonStyle::CoolBar);
button->set_button_style(ButtonStyle::CoolBar);
button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
ASSERT(button->size_policy(Orientation::Horizontal) == SizePolicy::Fixed);
ASSERT(button->size_policy(Orientation::Vertical) == SizePolicy::Fixed);
@ -79,5 +79,5 @@ void GToolBar::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_surface(painter, rect());
StylePainter::the().paint_surface(painter, rect());
}

View file

@ -1,5 +1,6 @@
SHAREDGRAPHICS_OBJS = \
../SharedGraphics/Painter.o \
../SharedGraphics/StylePainter.o \
../SharedGraphics/Font.o \
../SharedGraphics/Rect.o \
../SharedGraphics/GraphicsBitmap.o \
@ -22,7 +23,6 @@ LIBGUI_OBJS = \
GScrollBar.o \
GStatusBar.o \
GWidget.o \
GStyle.o \
GLayout.o \
GBoxLayout.o \
GMenuBar.o \