1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 12:15:07 +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

@ -39,7 +39,7 @@ public:
: GButton(parent) : GButton(parent)
, m_executable_path(exec_path) , m_executable_path(exec_path)
{ {
set_button_style(GButtonStyle::CoolBar); set_button_style(ButtonStyle::CoolBar);
set_icon(GraphicsBitmap::load_from_file(icon_path)); set_icon(GraphicsBitmap::load_from_file(icon_path));
set_preferred_size({ 50, 50 }); set_preferred_size({ 50, 50 });
set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);

View file

@ -2,7 +2,7 @@
#include <LibGUI/GPainter.h> #include <LibGUI/GPainter.h>
#include <LibGUI/GBoxLayout.h> #include <LibGUI/GBoxLayout.h>
#include <LibGUI/GLabel.h> #include <LibGUI/GLabel.h>
#include <LibGUI/GStyle.h> #include <SharedGraphics/StylePainter.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -106,5 +106,5 @@ void MemoryStatsWidget::paint_event(GPaintEvent& event)
{ {
GPainter painter(*this); GPainter painter(*this);
painter.set_clip_rect(event.rect()); painter.set_clip_rect(event.rect());
GStyle::the().paint_surface(painter, rect()); StylePainter::the().paint_surface(painter, rect());
} }

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
#include <LibGUI/GScrollBar.h> #include <LibGUI/GScrollBar.h>
#include <LibGUI/GStyle.h> #include <SharedGraphics/StylePainter.h>
#include <SharedGraphics/CharacterBitmap.h> #include <SharedGraphics/CharacterBitmap.h>
#include <SharedGraphics/GraphicsBitmap.h> #include <SharedGraphics/GraphicsBitmap.h>
#include <LibGUI/GPainter.h> #include <LibGUI/GPainter.h>
@ -190,14 +190,14 @@ void GScrollBar::paint_event(GPaintEvent& event)
painter.fill_rect(rect(), Color::from_rgb(0xd6d2ce)); 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); 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); 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()) 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) void GScrollBar::mousedown_event(GMouseEvent& event)

View file

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

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(); raw_action_ptr->activate();
}; };
button->set_button_style(GButtonStyle::CoolBar); button->set_button_style(ButtonStyle::CoolBar);
button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
ASSERT(button->size_policy(Orientation::Horizontal) == SizePolicy::Fixed); ASSERT(button->size_policy(Orientation::Horizontal) == SizePolicy::Fixed);
ASSERT(button->size_policy(Orientation::Vertical) == SizePolicy::Fixed); ASSERT(button->size_policy(Orientation::Vertical) == SizePolicy::Fixed);
@ -79,5 +79,5 @@ void GToolBar::paint_event(GPaintEvent& event)
{ {
GPainter painter(*this); GPainter painter(*this);
painter.set_clip_rect(event.rect()); 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_OBJS = \
../SharedGraphics/Painter.o \ ../SharedGraphics/Painter.o \
../SharedGraphics/StylePainter.o \
../SharedGraphics/Font.o \ ../SharedGraphics/Font.o \
../SharedGraphics/Rect.o \ ../SharedGraphics/Rect.o \
../SharedGraphics/GraphicsBitmap.o \ ../SharedGraphics/GraphicsBitmap.o \
@ -22,7 +23,6 @@ LIBGUI_OBJS = \
GScrollBar.o \ GScrollBar.o \
GStatusBar.o \ GStatusBar.o \
GWidget.o \ GWidget.o \
GStyle.o \
GLayout.o \ GLayout.o \
GBoxLayout.o \ GBoxLayout.o \
GMenuBar.o \ GMenuBar.o \

View file

@ -1,5 +1,6 @@
SHAREDGRAPHICS_OBJS = \ SHAREDGRAPHICS_OBJS = \
../../SharedGraphics/Painter.o \ ../../SharedGraphics/Painter.o \
../../SharedGraphics/StylePainter.o \
../../SharedGraphics/Font.o \ ../../SharedGraphics/Font.o \
../../SharedGraphics/Rect.o \ ../../SharedGraphics/Rect.o \
../../SharedGraphics/GraphicsBitmap.o \ ../../SharedGraphics/GraphicsBitmap.o \

View file

@ -1,16 +1,16 @@
#include <LibGUI/GStyle.h> #include <SharedGraphics/StylePainter.h>
#include <LibGUI/GPainter.h> #include <LibGUI/GPainter.h>
static GStyle* s_the; static StylePainter* s_the;
GStyle& GStyle::the() StylePainter& StylePainter::the()
{ {
if (!s_the) if (!s_the)
s_the = new GStyle; s_the = new StylePainter;
return *s_the; return *s_the;
} }
GStyle::GStyle() StylePainter::StylePainter()
{ {
} }
@ -56,16 +56,16 @@ static void paint_button_new(Painter& painter, const Rect& rect, bool pressed)
} }
} }
void GStyle::paint_button(Painter& painter, const Rect& rect, GButtonStyle button_style, bool pressed, bool hovered) void StylePainter::paint_button(Painter& painter, const Rect& rect, ButtonStyle button_style, bool pressed, bool hovered)
{ {
if (button_style == GButtonStyle::Normal) if (button_style == ButtonStyle::Normal)
return paint_button_new(painter, rect, pressed); return paint_button_new(painter, rect, pressed);
Color button_color = Color::LightGray; Color button_color = Color::LightGray;
Color highlight_color = Color::White; Color highlight_color = Color::White;
Color shadow_color = Color(96, 96, 96); Color shadow_color = Color(96, 96, 96);
if (button_style == GButtonStyle::OldNormal) if (button_style == ButtonStyle::OldNormal)
painter.draw_rect(rect, Color::Black); painter.draw_rect(rect, Color::Black);
PainterStateSaver saver(painter); PainterStateSaver saver(painter);
@ -82,7 +82,7 @@ void GStyle::paint_button(Painter& painter, const Rect& rect, GButtonStyle butto
// Bottom highlight // Bottom highlight
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color); 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); 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)) { } else if (button_style == ButtonStyle::OldNormal || (button_style == ButtonStyle::CoolBar && hovered)) {
// Base // Base
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color); painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
@ -96,7 +96,7 @@ void GStyle::paint_button(Painter& painter, const Rect& rect, GButtonStyle butto
} }
} }
void GStyle::paint_surface(Painter& painter, const Rect& rect) void StylePainter::paint_surface(Painter& painter, const Rect& rect)
{ {
painter.fill_rect({ rect.x(), rect.y() + 1, rect.width(), rect.height() - 2 }, Color::LightGray); 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.top_left(), rect.top_right(), Color::White);

View file

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