Commit d20ec3c9 authored by thomasanderson's avatar thomasanderson Committed by Commit bot

LibGtkUi: Partition NativeThemeGtk into NativeThemeGtk2 and NativeThemeGtk3

When complete, the Gtk3 theming code will look completely different
from the Gtk2 code.  This CL splits out NativeThemeGtk in preparation
of this change.

The planned change for gtk3 is to use foreign drawing (see
gtk/demos/gtk-demo/foreigndrawing.c) to provide an implementation of
NativeTheme::GetPartSize and NativeTheme::Paint*.

The Gtk2 theme will continue to use the NativeThemeBase
implementations of these functions while just supplying basic colors.

BUG=132847
R=erg@chromium.org

Review-Url: https://codereview.chromium.org/2579683002
Cr-Commit-Position: refs/heads/master@{#439027}
parent ad541b1b
...@@ -46,8 +46,6 @@ common_sources = [ ...@@ -46,8 +46,6 @@ common_sources = [
"libgtkui_export.h", "libgtkui_export.h",
"menu_util.cc", "menu_util.cc",
"menu_util.h", "menu_util.h",
"native_theme_gtk.cc",
"native_theme_gtk.h",
"print_dialog_gtk.cc", "print_dialog_gtk.cc",
"print_dialog_gtk.h", "print_dialog_gtk.h",
"printing_gtk_util.cc", "printing_gtk_util.cc",
...@@ -129,7 +127,10 @@ group("libgtkui") { ...@@ -129,7 +127,10 @@ group("libgtkui") {
} }
component("libgtk2ui") { component("libgtk2ui") {
sources = common_sources sources = common_sources + [
"native_theme_gtk2.cc",
"native_theme_gtk2.h",
]
configs += common_configs configs += common_configs
defines = [ "LIBGTKUI_IMPLEMENTATION" ] defines = [ "LIBGTKUI_IMPLEMENTATION" ]
...@@ -152,7 +153,10 @@ component("libgtk2ui") { ...@@ -152,7 +153,10 @@ component("libgtk2ui") {
# This is compiled with "all" even when it's not referenced to ensure that # This is compiled with "all" even when it's not referenced to ensure that
# GTK3 continues to build. GTK3 is explicitly specified by some distros. # GTK3 continues to build. GTK3 is explicitly specified by some distros.
component("libgtk3ui") { component("libgtk3ui") {
sources = common_sources sources = common_sources + [
"native_theme_gtk3.cc",
"native_theme_gtk3.h",
]
configs += common_configs configs += common_configs
defines = [ "LIBGTKUI_IMPLEMENTATION" ] defines = [ "LIBGTKUI_IMPLEMENTATION" ]
......
This diff is collapsed.
...@@ -146,6 +146,15 @@ class Gtk2UI : public views::LinuxUI { ...@@ -146,6 +146,15 @@ class Gtk2UI : public views::LinuxUI {
// Updates |default_font_*|. // Updates |default_font_*|.
void UpdateDefaultFont(); void UpdateDefaultFont();
// Gets a ChromeGtkFrame theme color; returns true on success. No-op on gtk3.
bool GetChromeStyleColor(const char* sytle_property,
SkColor* ret_color) const;
ui::NativeTheme* native_theme_;
// A GtkWindow object with the class "ChromeGtkFrame".
GtkWidget* fake_window_;
// Colors calculated by LoadGtkValues() that are given to the // Colors calculated by LoadGtkValues() that are given to the
// caller while |use_gtk_| is true. // caller while |use_gtk_| is true.
ColorMap colors_; ColorMap colors_;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "ui/base/accelerators/accelerator.h" #include "ui/base/accelerators/accelerator.h"
#include "ui/events/event_constants.h" #include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_code_conversion_x.h" #include "ui/events/keycodes/keyboard_code_conversion_x.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
namespace { namespace {
...@@ -52,6 +53,52 @@ void CommonInitFromCommandLine(const base::CommandLine& command_line, ...@@ -52,6 +53,52 @@ void CommonInitFromCommandLine(const base::CommandLine& command_line,
namespace libgtkui { namespace libgtkui {
// Theme colors returned by GetSystemColor().
const SkColor kInvalidColorIdColor = SkColorSetRGB(255, 0, 128);
const SkColor kURLTextColor = SkColorSetRGB(0x0b, 0x80, 0x43);
SkColor NormalURLColor(SkColor foreground) {
color_utils::HSL fg_hsl, hue_hsl;
color_utils::SkColorToHSL(foreground, &fg_hsl);
color_utils::SkColorToHSL(kURLTextColor, &hue_hsl);
// Only allow colors that have a fair amount of saturation in them (color vs
// white). This means that our output color will always be fairly green.
double s = std::max(0.5, fg_hsl.s);
// Make sure the luminance is at least as bright as the |kURLTextColor| green
// would be if we were to use that.
double l;
if (fg_hsl.l < hue_hsl.l)
l = hue_hsl.l;
else
l = (fg_hsl.l + hue_hsl.l) / 2;
color_utils::HSL output = {hue_hsl.h, s, l};
return color_utils::HSLToSkColor(output, 255);
}
SkColor SelectedURLColor(SkColor foreground, SkColor background) {
color_utils::HSL fg_hsl, bg_hsl, hue_hsl;
color_utils::SkColorToHSL(foreground, &fg_hsl);
color_utils::SkColorToHSL(background, &bg_hsl);
color_utils::SkColorToHSL(kURLTextColor, &hue_hsl);
// The saturation of the text should be opposite of the background, clamped
// to 0.2-0.8. We make sure it's greater than 0.2 so there's some color, but
// less than 0.8 so it's not the oversaturated neon-color.
double opposite_s = 1 - bg_hsl.s;
double s = std::max(0.2, std::min(0.8, opposite_s));
// The luminance should match the luminance of the foreground text. Again,
// we clamp so as to have at some amount of color (green) in the text.
double opposite_l = fg_hsl.l;
double l = std::max(0.1, std::min(0.9, opposite_l));
color_utils::HSL output = {hue_hsl.h, s, l};
return color_utils::HSLToSkColor(output, 255);
}
void GtkInitFromCommandLine(const base::CommandLine& command_line) { void GtkInitFromCommandLine(const base::CommandLine& command_line) {
CommonInitFromCommandLine(command_line, gtk_init); CommonInitFromCommandLine(command_line, gtk_init);
} }
......
...@@ -25,6 +25,22 @@ class Accelerator; ...@@ -25,6 +25,22 @@ class Accelerator;
namespace libgtkui { namespace libgtkui {
extern const SkColor kInvalidColorIdColor;
extern const SkColor kURLTextColor;
// Generates the normal URL color, a green color used in unhighlighted URL
// text. It is a mix of |kURLTextColor| and the current text color. Unlike the
// selected text color, it is more important to match the qualities of the
// foreground typeface color instead of taking the background into account.
SkColor NormalURLColor(SkColor foreground);
// Generates the selected URL color, a green color used on URL text in the
// currently highlighted entry in the autocomplete popup. It's a mix of
// |kURLTextColor|, the current text color, and the background color (the
// select highlight). It is more important to contrast with the background
// saturation than to look exactly like the foreground color.
SkColor SelectedURLColor(SkColor foreground, SkColor background);
void GtkInitFromCommandLine(const base::CommandLine& command_line); void GtkInitFromCommandLine(const base::CommandLine& command_line);
// Returns the name of the ".desktop" file associated with our running process. // Returns the name of the ".desktop" file associated with our running process.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/ui/libgtkui/native_theme_gtk.h" #include "chrome/browser/ui/libgtkui/native_theme_gtk2.h"
#include <gtk/gtk.h> #include <gtk/gtk.h>
...@@ -26,61 +26,6 @@ namespace libgtkui { ...@@ -26,61 +26,6 @@ namespace libgtkui {
namespace { namespace {
// Theme colors returned by GetSystemColor().
const SkColor kInvalidColorIdColor = SkColorSetRGB(255, 0, 128);
const SkColor kURLTextColor = SkColorSetRGB(0x0b, 0x80, 0x43);
// Generates the normal URL color, a green color used in unhighlighted URL
// text. It is a mix of |kURLTextColor| and the current text color. Unlike the
// selected text color, it is more important to match the qualities of the
// foreground typeface color instead of taking the background into account.
SkColor NormalURLColor(SkColor foreground) {
color_utils::HSL fg_hsl, hue_hsl;
color_utils::SkColorToHSL(foreground, &fg_hsl);
color_utils::SkColorToHSL(kURLTextColor, &hue_hsl);
// Only allow colors that have a fair amount of saturation in them (color vs
// white). This means that our output color will always be fairly green.
double s = std::max(0.5, fg_hsl.s);
// Make sure the luminance is at least as bright as the |kURLTextColor| green
// would be if we were to use that.
double l;
if (fg_hsl.l < hue_hsl.l)
l = hue_hsl.l;
else
l = (fg_hsl.l + hue_hsl.l) / 2;
color_utils::HSL output = {hue_hsl.h, s, l};
return color_utils::HSLToSkColor(output, 255);
}
// Generates the selected URL color, a green color used on URL text in the
// currently highlighted entry in the autocomplete popup. It's a mix of
// |kURLTextColor|, the current text color, and the background color (the
// select highlight). It is more important to contrast with the background
// saturation than to look exactly like the foreground color.
SkColor SelectedURLColor(SkColor foreground, SkColor background) {
color_utils::HSL fg_hsl, bg_hsl, hue_hsl;
color_utils::SkColorToHSL(foreground, &fg_hsl);
color_utils::SkColorToHSL(background, &bg_hsl);
color_utils::SkColorToHSL(kURLTextColor, &hue_hsl);
// The saturation of the text should be opposite of the background, clamped
// to 0.2-0.8. We make sure it's greater than 0.2 so there's some color, but
// less than 0.8 so it's not the oversaturated neon-color.
double opposite_s = 1 - bg_hsl.s;
double s = std::max(0.2, std::min(0.8, opposite_s));
// The luminance should match the luminance of the foreground text. Again,
// we clamp so as to have at some amount of color (green) in the text.
double opposite_l = fg_hsl.l;
double l = std::max(0.1, std::min(0.9, opposite_l));
color_utils::HSL output = {hue_hsl.h, s, l};
return color_utils::HSLToSkColor(output, 255);
}
enum WidgetState { enum WidgetState {
NORMAL = 0, NORMAL = 0,
ACTIVE = 1, ACTIVE = 1,
...@@ -89,14 +34,10 @@ enum WidgetState { ...@@ -89,14 +34,10 @@ enum WidgetState {
INSENSITIVE = 4, INSENSITIVE = 4,
}; };
#if GTK_MAJOR_VERSION == 2
// Same order as enum WidgetState above // Same order as enum WidgetState above
const GtkStateType stateMap[] = { const GtkStateType stateMap[] = {
GTK_STATE_NORMAL, GTK_STATE_NORMAL, GTK_STATE_ACTIVE, GTK_STATE_PRELIGHT,
GTK_STATE_ACTIVE, GTK_STATE_SELECTED, GTK_STATE_INSENSITIVE,
GTK_STATE_PRELIGHT,
GTK_STATE_SELECTED,
GTK_STATE_INSENSITIVE,
}; };
SkColor GetFGColor(GtkWidget* widget, WidgetState state) { SkColor GetFGColor(GtkWidget* widget, WidgetState state) {
...@@ -116,47 +57,6 @@ SkColor GetBaseColor(GtkWidget* widget, WidgetState state) { ...@@ -116,47 +57,6 @@ SkColor GetBaseColor(GtkWidget* widget, WidgetState state) {
return GdkColorToSkColor(gtk_rc_get_style(widget)->base[stateMap[state]]); return GdkColorToSkColor(gtk_rc_get_style(widget)->base[stateMap[state]]);
} }
#else
// Same order as enum WidgetState above
const GtkStateFlags stateMap[] = {
GTK_STATE_FLAG_NORMAL, GTK_STATE_FLAG_ACTIVE,
GTK_STATE_FLAG_PRELIGHT, GTK_STATE_FLAG_SELECTED,
GTK_STATE_FLAG_INSENSITIVE,
};
SkColor GetFGColor(GtkWidget* widget, WidgetState state) {
GdkRGBA color;
gtk_style_context_get_color(gtk_widget_get_style_context(widget),
stateMap[state], &color);
return SkColorSetRGB(color.red * 255, color.green * 255, color.blue * 255);
}
SkColor GetBGColor(GtkWidget* widget, WidgetState state) {
GdkRGBA color;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gtk_style_context_get_background_color(gtk_widget_get_style_context(widget),
stateMap[state], &color);
G_GNUC_END_IGNORE_DEPRECATIONS
// Hack for default color
if (color.alpha == 0.0)
color = {1, 1, 1, 1};
return SkColorSetRGB(color.red * 255, color.green * 255, color.blue * 255);
}
SkColor GetTextColor(GtkWidget* widget, WidgetState state) {
return GetFGColor(widget, state);
}
SkColor GetTextAAColor(GtkWidget* widget, WidgetState state) {
return GetFGColor(widget, state);
}
SkColor GetBaseColor(GtkWidget* widget, WidgetState state) {
return GetBGColor(widget, state);
}
#endif
} // namespace } // namespace
// static // static
...@@ -282,7 +182,12 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const { ...@@ -282,7 +182,12 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const {
return SkColorSetA(GetSystemColor(kColorId_LinkEnabled), 0xBB); return SkColorSetA(GetSystemColor(kColorId_LinkEnabled), 0xBB);
case kColorId_LinkEnabled: { case kColorId_LinkEnabled: {
SkColor link_color = SK_ColorTRANSPARENT; SkColor link_color = SK_ColorTRANSPARENT;
GetChromeStyleColor("link-color", &link_color); GdkColor* style_color = nullptr;
gtk_widget_style_get(GetWindow(), "link-color", &style_color, nullptr);
if (style_color) {
link_color = GdkColorToSkColor(*style_color);
gdk_color_free(style_color);
}
if (link_color != SK_ColorTRANSPARENT) if (link_color != SK_ColorTRANSPARENT)
return link_color; return link_color;
// Default color comes from gtklinkbutton.c. // Default color comes from gtklinkbutton.c.
...@@ -321,7 +226,6 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const { ...@@ -321,7 +226,6 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const {
case kColorId_TextfieldDefaultBackground: case kColorId_TextfieldDefaultBackground:
return GetBaseColor(GetEntry(), NORMAL); return GetBaseColor(GetEntry(), NORMAL);
#if GTK_MAJOR_VERSION == 2
case kColorId_TextfieldReadOnlyColor: case kColorId_TextfieldReadOnlyColor:
return GetTextColor(GetEntry(), ACTIVE); return GetTextColor(GetEntry(), ACTIVE);
case kColorId_TextfieldReadOnlyBackground: case kColorId_TextfieldReadOnlyBackground:
...@@ -330,16 +234,6 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const { ...@@ -330,16 +234,6 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const {
return GetTextColor(GetEntry(), SELECTED); return GetTextColor(GetEntry(), SELECTED);
case kColorId_TextfieldSelectionBackgroundFocused: case kColorId_TextfieldSelectionBackgroundFocused:
return GetBaseColor(GetEntry(), SELECTED); return GetBaseColor(GetEntry(), SELECTED);
#else
case kColorId_TextfieldReadOnlyColor:
return GetTextColor(GetEntry(), SELECTED);
case kColorId_TextfieldReadOnlyBackground:
return GetBaseColor(GetEntry(), SELECTED);
case kColorId_TextfieldSelectionColor:
return GetTextColor(GetLabel(), SELECTED);
case kColorId_TextfieldSelectionBackgroundFocused:
return GetBaseColor(GetLabel(), SELECTED);
#endif
// Tooltips // Tooltips
case kColorId_TooltipBackground: case kColorId_TooltipBackground:
...@@ -456,22 +350,6 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const { ...@@ -456,22 +350,6 @@ SkColor NativeThemeGtk2::GetSystemColor(ColorId color_id) const {
return kInvalidColorIdColor; return kInvalidColorIdColor;
} }
// Get ChromeGtkFrame theme colors. No-op in GTK3.
bool NativeThemeGtk2::GetChromeStyleColor(const char* style_property,
SkColor* ret_color) const {
#if GTK_MAJOR_VERSION == 2
GdkColor* style_color = nullptr;
gtk_widget_style_get(GetWindow(), style_property, &style_color, nullptr);
if (style_color) {
*ret_color = GdkColorToSkColor(*style_color);
gdk_color_free(style_color);
return true;
}
#endif
return false;
}
GtkWidget* NativeThemeGtk2::GetWindow() const { GtkWidget* NativeThemeGtk2::GetWindow() const {
static GtkWidget* fake_window = NULL; static GtkWidget* fake_window = NULL;
......
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK_H_ #ifndef CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK2_H_
#define CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK_H_ #define CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK2_H_
#include <gtk/gtk.h>
#include "base/macros.h" #include "base/macros.h"
#include "ui/native_theme/native_theme_base.h" #include "ui/native_theme/native_theme_base.h"
typedef struct _GtkWidget GtkWidget;
namespace libgtkui { namespace libgtkui {
// A version of NativeTheme that uses GTK2 supplied colours instead of the // A version of NativeTheme that uses GTK2 supplied colours instead of the
...@@ -31,10 +31,9 @@ class NativeThemeGtk2 : public ui::NativeThemeBase { ...@@ -31,10 +31,9 @@ class NativeThemeGtk2 : public ui::NativeThemeBase {
const gfx::Rect& rect, const gfx::Rect& rect,
const MenuItemExtraParams& menu_item) const override; const MenuItemExtraParams& menu_item) const override;
// Gets a ChromeGtkFrame theme color; returns true on success. Always returns private:
// false in GTK3. NativeThemeGtk2();
bool GetChromeStyleColor(const char* style_property, ~NativeThemeGtk2() override;
SkColor* ret_color) const;
// Returns various widgets for theming use. // Returns various widgets for theming use.
GtkWidget* GetWindow() const; GtkWidget* GetWindow() const;
...@@ -47,13 +46,9 @@ class NativeThemeGtk2 : public ui::NativeThemeBase { ...@@ -47,13 +46,9 @@ class NativeThemeGtk2 : public ui::NativeThemeBase {
GtkWidget* GetMenu() const; GtkWidget* GetMenu() const;
GtkWidget* GetMenuItem() const; GtkWidget* GetMenuItem() const;
private:
NativeThemeGtk2();
~NativeThemeGtk2() override;
DISALLOW_COPY_AND_ASSIGN(NativeThemeGtk2); DISALLOW_COPY_AND_ASSIGN(NativeThemeGtk2);
}; };
} // namespace libgtkui } // namespace libgtkui
#endif // CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK_H_ #endif // CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK2_H_
This diff is collapsed.
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK3_H_
#define CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK3_H_
#include "base/macros.h"
#include "ui/native_theme/native_theme_base.h"
typedef struct _GtkWidget GtkWidget;
namespace libgtkui {
// A version of NativeTheme that uses GTK3-rendered widgets.
class NativeThemeGtk3 : public ui::NativeThemeBase {
public:
static NativeThemeGtk3* instance();
// Overridden from ui::NativeThemeBase:
SkColor GetSystemColor(ColorId color_id) const override;
private:
NativeThemeGtk3();
~NativeThemeGtk3() override;
// Returns various widgets for theming use.
// TODO(thomasanderson): Remove all of these.
GtkWidget* GetWindow() const;
GtkWidget* GetEntry() const;
GtkWidget* GetLabel() const;
GtkWidget* GetButton() const;
GtkWidget* GetBlueButton() const;
GtkWidget* GetTree() const;
GtkWidget* GetTooltip() const;
GtkWidget* GetMenu() const;
GtkWidget* GetMenuItem() const;
DISALLOW_COPY_AND_ASSIGN(NativeThemeGtk3);
};
} // namespace libgtkui
#endif // CHROME_BROWSER_UI_LIBGTKUI_NATIVE_THEME_GTK3_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment