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

LibDraw+LibGUI: Allow changing individual colors in a Palette

Palette is now a value wrapper around a NonnullRefPtr<PaletteImpl>.
A new function, set_color(ColorRole, Color) implements a simple
copy-on-write mechanism so that we're sharing the PaletteImpl in the
common case, but allowing you to create custom palettes if you like,
by getting a GWidget's palette, modifying it, and then assigning the
modified palette to the widget via GWidget::set_palette().

Use this to make PaintBrush show its palette colors once again.

Fixes #943.
This commit is contained in:
Andreas Kling 2019-12-29 00:47:49 +01:00
parent 19d4f4c7b5
commit 7b2dd7e116
18 changed files with 98 additions and 40 deletions

View file

@ -1,26 +1,46 @@
#include <LibDraw/Palette.h>
NonnullRefPtr<Palette> Palette::create_with_shared_buffer(SharedBuffer& buffer)
NonnullRefPtr<PaletteImpl> PaletteImpl::create_with_shared_buffer(SharedBuffer& buffer)
{
return adopt(*new Palette(buffer));
return adopt(*new PaletteImpl(buffer));
}
Palette::Palette(SharedBuffer& buffer)
PaletteImpl::PaletteImpl(SharedBuffer& buffer)
: m_theme_buffer(buffer)
{
}
Palette::Palette(const PaletteImpl& impl)
: m_impl(impl)
{
}
Palette::~Palette()
{
}
const SystemTheme& Palette::theme() const
const SystemTheme& PaletteImpl::theme() const
{
return *(const SystemTheme*)m_theme_buffer->data();
}
Color Palette::color(ColorRole role) const
Color PaletteImpl::color(ColorRole role) const
{
ASSERT((int)role < (int)ColorRole::__Count);
return theme().color[(int)role];
}
NonnullRefPtr<PaletteImpl> PaletteImpl::clone() const
{
auto new_theme_buffer = SharedBuffer::create_with_size(m_theme_buffer->size());
memcpy(new_theme_buffer->data(), m_theme_buffer->data(), m_theme_buffer->size());
return adopt(*new PaletteImpl(*new_theme_buffer));
}
void Palette::set_color(ColorRole role, Color color)
{
if (m_impl->ref_count() != 1)
m_impl = m_impl->clone();
auto& theme = const_cast<SystemTheme&>(impl().theme());
theme.color[(int)role] = color;
}

View file

@ -1,13 +1,33 @@
#pragma once
#include <AK/Badge.h>
#include <AK/Noncopyable.h>
#include <LibDraw/SystemTheme.h>
class GApplication;
class Palette : public RefCounted<Palette> {
class PaletteImpl : public RefCounted<PaletteImpl> {
AK_MAKE_NONCOPYABLE(PaletteImpl)
AK_MAKE_NONMOVABLE(PaletteImpl)
public:
static NonnullRefPtr<Palette> create_with_shared_buffer(SharedBuffer&);
static NonnullRefPtr<PaletteImpl> create_with_shared_buffer(SharedBuffer&);
NonnullRefPtr<PaletteImpl> clone() const;
Color color(ColorRole) const;
const SystemTheme& theme() const;
void replace_internal_buffer(Badge<GApplication>, SharedBuffer& buffer) { m_theme_buffer = buffer; }
private:
explicit PaletteImpl(SharedBuffer&);
RefPtr<SharedBuffer> m_theme_buffer;
};
class Palette {
public:
explicit Palette(const PaletteImpl&);
~Palette();
Color window() const { return color(ColorRole::Window); }
@ -41,14 +61,14 @@ public:
Color threed_shadow2() const { return color(ColorRole::ThreedShadow2); }
Color hover_highlight() const { return color(ColorRole::ThreedHighlight); }
Color color(ColorRole) const;
Color color(ColorRole role) const { return m_impl->color(role); }
void set_color(ColorRole, Color);
const SystemTheme& theme() const;
const SystemTheme& theme() const { return m_impl->theme(); }
void replace_internal_buffer(Badge<GApplication>, SharedBuffer& buffer) { m_theme_buffer = buffer; }
PaletteImpl& impl() { return *m_impl; }
const PaletteImpl& impl() const { return *m_impl; }
private:
explicit Palette(SharedBuffer&);
RefPtr<SharedBuffer> m_theme_buffer;
NonnullRefPtr<PaletteImpl> m_impl;
};

View file

@ -39,6 +39,7 @@ enum class ColorRole {
__Count,
Background = Window,
DisabledText = ThreedShadow1,
};

View file

@ -147,7 +147,7 @@ void GApplication::did_delete_last_window(Badge<GWindow>)
void GApplication::set_system_palette(SharedBuffer& buffer)
{
if (!m_system_palette)
m_system_palette = Palette::create_with_shared_buffer(buffer);
m_system_palette = PaletteImpl::create_with_shared_buffer(buffer);
else
m_system_palette->replace_internal_buffer({}, buffer);
@ -157,5 +157,5 @@ void GApplication::set_system_palette(SharedBuffer& buffer)
void GApplication::set_palette(const Palette& palette)
{
m_palette = palette;
m_palette = palette.impl();
}

View file

@ -3,6 +3,7 @@
#include <AK/Badge.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <LibDraw/Palette.h>
#include <LibGUI/GShortcut.h>
class CEventLoop;
@ -42,7 +43,7 @@ public:
const String& invoked_as() const { return m_invoked_as; }
const Vector<String>& args() const { return m_args; }
const Palette& palette() const { return *m_palette; }
Palette palette() const { return Palette(*m_palette); }
void set_palette(const Palette&);
void set_system_palette(SharedBuffer&);
@ -50,8 +51,8 @@ public:
private:
OwnPtr<CEventLoop> m_event_loop;
OwnPtr<GMenuBar> m_menubar;
RefPtr<Palette> m_palette;
RefPtr<Palette> m_system_palette;
RefPtr<PaletteImpl> m_palette;
RefPtr<PaletteImpl> m_system_palette;
HashMap<GShortcut, GAction*> m_global_shortcut_actions;
class TooltipWindow;
TooltipWindow* m_tooltip_window { nullptr };

View file

@ -67,7 +67,7 @@ const GWidgetClassRegistration* GWidgetClassRegistration::find(const String& cla
GWidget::GWidget(GWidget* parent)
: CObject(parent, true)
, m_font(Font::default_font())
, m_palette(GApplication::the().palette())
, m_palette(GApplication::the().palette().impl())
{
}
@ -695,5 +695,5 @@ Vector<GWidget*> GWidget::child_widgets() const
void GWidget::set_palette(const Palette& palette)
{
m_palette = palette;
m_palette = palette.impl();
}

View file

@ -8,6 +8,7 @@
#include <LibDraw/Color.h>
#include <LibDraw/Font.h>
#include <LibDraw/Orientation.h>
#include <LibDraw/Palette.h>
#include <LibDraw/Rect.h>
#include <LibDraw/SystemTheme.h>
#include <LibGUI/GEvent.h>
@ -22,7 +23,6 @@ class GLayout;
class GMenu;
class GWindow;
class GraphicsBitmap;
class Palette;
enum class SizePolicy {
Fixed,
@ -237,7 +237,7 @@ public:
void do_layout();
const Palette& palette() const { return *m_palette; }
Palette palette() const { return Palette(*m_palette); }
void set_palette(const Palette&);
protected:
@ -301,7 +301,7 @@ private:
HashMap<GShortcut, GAction*> m_local_shortcut_actions;
NonnullRefPtr<Palette> m_palette;
NonnullRefPtr<PaletteImpl> m_palette;
};
template<>