OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/libgtkui/native_theme_gtk.h" | 5 #include "chrome/browser/ui/libgtkui/native_theme_gtk2.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "chrome/browser/ui/libgtkui/chrome_gtk_frame.h" | 9 #include "chrome/browser/ui/libgtkui/chrome_gtk_frame.h" |
10 #include "chrome/browser/ui/libgtkui/chrome_gtk_menu_subclasses.h" | 10 #include "chrome/browser/ui/libgtkui/chrome_gtk_menu_subclasses.h" |
11 #include "chrome/browser/ui/libgtkui/gtk_ui.h" | 11 #include "chrome/browser/ui/libgtkui/gtk_ui.h" |
12 #include "chrome/browser/ui/libgtkui/gtk_util.h" | 12 #include "chrome/browser/ui/libgtkui/gtk_util.h" |
13 #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h" | 13 #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h" |
14 #include "third_party/skia/include/core/SkColor.h" | 14 #include "third_party/skia/include/core/SkColor.h" |
15 #include "ui/gfx/color_palette.h" | 15 #include "ui/gfx/color_palette.h" |
16 #include "ui/gfx/color_utils.h" | 16 #include "ui/gfx/color_utils.h" |
17 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
18 #include "ui/gfx/geometry/size.h" | 18 #include "ui/gfx/geometry/size.h" |
19 #include "ui/gfx/path.h" | 19 #include "ui/gfx/path.h" |
20 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
21 #include "ui/native_theme/common_theme.h" | 21 #include "ui/native_theme/common_theme.h" |
22 #include "ui/native_theme/native_theme_aura.h" | 22 #include "ui/native_theme/native_theme_aura.h" |
23 #include "ui/native_theme/native_theme_dark_aura.h" | 23 #include "ui/native_theme/native_theme_dark_aura.h" |
24 | 24 |
25 namespace libgtkui { | 25 namespace libgtkui { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // Theme colors returned by GetSystemColor(). | |
30 const SkColor kInvalidColorIdColor = SkColorSetRGB(255, 0, 128); | |
31 const SkColor kURLTextColor = SkColorSetRGB(0x0b, 0x80, 0x43); | |
32 | |
33 // Generates the normal URL color, a green color used in unhighlighted URL | |
34 // text. It is a mix of |kURLTextColor| and the current text color. Unlike the | |
35 // selected text color, it is more important to match the qualities of the | |
36 // foreground typeface color instead of taking the background into account. | |
37 SkColor NormalURLColor(SkColor foreground) { | |
38 color_utils::HSL fg_hsl, hue_hsl; | |
39 color_utils::SkColorToHSL(foreground, &fg_hsl); | |
40 color_utils::SkColorToHSL(kURLTextColor, &hue_hsl); | |
41 | |
42 // Only allow colors that have a fair amount of saturation in them (color vs | |
43 // white). This means that our output color will always be fairly green. | |
44 double s = std::max(0.5, fg_hsl.s); | |
45 | |
46 // Make sure the luminance is at least as bright as the |kURLTextColor| green | |
47 // would be if we were to use that. | |
48 double l; | |
49 if (fg_hsl.l < hue_hsl.l) | |
50 l = hue_hsl.l; | |
51 else | |
52 l = (fg_hsl.l + hue_hsl.l) / 2; | |
53 | |
54 color_utils::HSL output = {hue_hsl.h, s, l}; | |
55 return color_utils::HSLToSkColor(output, 255); | |
56 } | |
57 | |
58 // Generates the selected URL color, a green color used on URL text in the | |
59 // currently highlighted entry in the autocomplete popup. It's a mix of | |
60 // |kURLTextColor|, the current text color, and the background color (the | |
61 // select highlight). It is more important to contrast with the background | |
62 // saturation than to look exactly like the foreground color. | |
63 SkColor SelectedURLColor(SkColor foreground, SkColor background) { | |
64 color_utils::HSL fg_hsl, bg_hsl, hue_hsl; | |
65 color_utils::SkColorToHSL(foreground, &fg_hsl); | |
66 color_utils::SkColorToHSL(background, &bg_hsl); | |
67 color_utils::SkColorToHSL(kURLTextColor, &hue_hsl); | |
68 | |
69 // The saturation of the text should be opposite of the background, clamped | |
70 // to 0.2-0.8. We make sure it's greater than 0.2 so there's some color, but | |
71 // less than 0.8 so it's not the oversaturated neon-color. | |
72 double opposite_s = 1 - bg_hsl.s; | |
73 double s = std::max(0.2, std::min(0.8, opposite_s)); | |
74 | |
75 // The luminance should match the luminance of the foreground text. Again, | |
76 // we clamp so as to have at some amount of color (green) in the text. | |
77 double opposite_l = fg_hsl.l; | |
78 double l = std::max(0.1, std::min(0.9, opposite_l)); | |
79 | |
80 color_utils::HSL output = {hue_hsl.h, s, l}; | |
81 return color_utils::HSLToSkColor(output, 255); | |
82 } | |
83 | |
84 enum WidgetState { | 29 enum WidgetState { |
85 NORMAL = 0, | 30 NORMAL = 0, |
86 ACTIVE = 1, | 31 ACTIVE = 1, |
87 PRELIGHT = 2, | 32 PRELIGHT = 2, |
88 SELECTED = 3, | 33 SELECTED = 3, |
89 INSENSITIVE = 4, | 34 INSENSITIVE = 4, |
90 }; | 35 }; |
91 | 36 |
92 #if GTK_MAJOR_VERSION == 2 | |
93 // Same order as enum WidgetState above | 37 // Same order as enum WidgetState above |
94 const GtkStateType stateMap[] = { | 38 const GtkStateType stateMap[] = { |
95 GTK_STATE_NORMAL, | 39 GTK_STATE_NORMAL, GTK_STATE_ACTIVE, GTK_STATE_PRELIGHT, |
96 GTK_STATE_ACTIVE, | 40 GTK_STATE_SELECTED, GTK_STATE_INSENSITIVE, |
97 GTK_STATE_PRELIGHT, | |
98 GTK_STATE_SELECTED, | |
99 GTK_STATE_INSENSITIVE, | |
100 }; | 41 }; |
101 | 42 |
102 SkColor GetFGColor(GtkWidget* widget, WidgetState state) { | 43 SkColor GetFGColor(GtkWidget* widget, WidgetState state) { |
103 return GdkColorToSkColor(gtk_rc_get_style(widget)->fg[stateMap[state]]); | 44 return GdkColorToSkColor(gtk_rc_get_style(widget)->fg[stateMap[state]]); |
104 } | 45 } |
105 SkColor GetBGColor(GtkWidget* widget, WidgetState state) { | 46 SkColor GetBGColor(GtkWidget* widget, WidgetState state) { |
106 return GdkColorToSkColor(gtk_rc_get_style(widget)->bg[stateMap[state]]); | 47 return GdkColorToSkColor(gtk_rc_get_style(widget)->bg[stateMap[state]]); |
107 } | 48 } |
108 | 49 |
109 SkColor GetTextColor(GtkWidget* widget, WidgetState state) { | 50 SkColor GetTextColor(GtkWidget* widget, WidgetState state) { |
110 return GdkColorToSkColor(gtk_rc_get_style(widget)->text[stateMap[state]]); | 51 return GdkColorToSkColor(gtk_rc_get_style(widget)->text[stateMap[state]]); |
111 } | 52 } |
112 SkColor GetTextAAColor(GtkWidget* widget, WidgetState state) { | 53 SkColor GetTextAAColor(GtkWidget* widget, WidgetState state) { |
113 return GdkColorToSkColor(gtk_rc_get_style(widget)->text_aa[stateMap[state]]); | 54 return GdkColorToSkColor(gtk_rc_get_style(widget)->text_aa[stateMap[state]]); |
114 } | 55 } |
115 SkColor GetBaseColor(GtkWidget* widget, WidgetState state) { | 56 SkColor GetBaseColor(GtkWidget* widget, WidgetState state) { |
116 return GdkColorToSkColor(gtk_rc_get_style(widget)->base[stateMap[state]]); | 57 return GdkColorToSkColor(gtk_rc_get_style(widget)->base[stateMap[state]]); |
117 } | 58 } |
118 | 59 |
119 #else | |
120 // Same order as enum WidgetState above | |
121 const GtkStateFlags stateMap[] = { | |
122 GTK_STATE_FLAG_NORMAL, GTK_STATE_FLAG_ACTIVE, | |
123 GTK_STATE_FLAG_PRELIGHT, GTK_STATE_FLAG_SELECTED, | |
124 GTK_STATE_FLAG_INSENSITIVE, | |
125 }; | |
126 | |
127 SkColor GetFGColor(GtkWidget* widget, WidgetState state) { | |
128 GdkRGBA color; | |
129 gtk_style_context_get_color(gtk_widget_get_style_context(widget), | |
130 stateMap[state], &color); | |
131 return SkColorSetRGB(color.red * 255, color.green * 255, color.blue * 255); | |
132 } | |
133 SkColor GetBGColor(GtkWidget* widget, WidgetState state) { | |
134 GdkRGBA color; | |
135 | |
136 G_GNUC_BEGIN_IGNORE_DEPRECATIONS | |
137 gtk_style_context_get_background_color(gtk_widget_get_style_context(widget), | |
138 stateMap[state], &color); | |
139 G_GNUC_END_IGNORE_DEPRECATIONS | |
140 | |
141 // Hack for default color | |
142 if (color.alpha == 0.0) | |
143 color = {1, 1, 1, 1}; | |
144 | |
145 return SkColorSetRGB(color.red * 255, color.green * 255, color.blue * 255); | |
146 } | |
147 | |
148 SkColor GetTextColor(GtkWidget* widget, WidgetState state) { | |
149 return GetFGColor(widget, state); | |
150 } | |
151 SkColor GetTextAAColor(GtkWidget* widget, WidgetState state) { | |
152 return GetFGColor(widget, state); | |
153 } | |
154 SkColor GetBaseColor(GtkWidget* widget, WidgetState state) { | |
155 return GetBGColor(widget, state); | |
156 } | |
157 | |
158 #endif | |
159 | |
160 } // namespace | 60 } // namespace |
161 | 61 |
162 // static | 62 // static |
163 NativeThemeGtk2* NativeThemeGtk2::instance() { | 63 NativeThemeGtk2* NativeThemeGtk2::instance() { |
164 CR_DEFINE_STATIC_LOCAL(NativeThemeGtk2, s_native_theme, ()); | 64 CR_DEFINE_STATIC_LOCAL(NativeThemeGtk2, s_native_theme, ()); |
165 return &s_native_theme; | 65 return &s_native_theme; |
166 } | 66 } |
167 | 67 |
168 // Constructors automatically called | 68 // Constructors automatically called |
169 NativeThemeGtk2::NativeThemeGtk2() {} | 69 NativeThemeGtk2::NativeThemeGtk2() {} |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 case kColorId_LabelTextSelectionColor: | 175 case kColorId_LabelTextSelectionColor: |
276 return GetTextColor(GetLabel(), SELECTED); | 176 return GetTextColor(GetLabel(), SELECTED); |
277 case kColorId_LabelTextSelectionBackgroundFocused: | 177 case kColorId_LabelTextSelectionBackgroundFocused: |
278 return GetBaseColor(GetLabel(), SELECTED); | 178 return GetBaseColor(GetLabel(), SELECTED); |
279 | 179 |
280 // Link | 180 // Link |
281 case kColorId_LinkDisabled: | 181 case kColorId_LinkDisabled: |
282 return SkColorSetA(GetSystemColor(kColorId_LinkEnabled), 0xBB); | 182 return SkColorSetA(GetSystemColor(kColorId_LinkEnabled), 0xBB); |
283 case kColorId_LinkEnabled: { | 183 case kColorId_LinkEnabled: { |
284 SkColor link_color = SK_ColorTRANSPARENT; | 184 SkColor link_color = SK_ColorTRANSPARENT; |
285 GetChromeStyleColor("link-color", &link_color); | 185 GdkColor* style_color = nullptr; |
| 186 gtk_widget_style_get(GetWindow(), "link-color", &style_color, nullptr); |
| 187 if (style_color) { |
| 188 link_color = GdkColorToSkColor(*style_color); |
| 189 gdk_color_free(style_color); |
| 190 } |
286 if (link_color != SK_ColorTRANSPARENT) | 191 if (link_color != SK_ColorTRANSPARENT) |
287 return link_color; | 192 return link_color; |
288 // Default color comes from gtklinkbutton.c. | 193 // Default color comes from gtklinkbutton.c. |
289 return SkColorSetRGB(0x00, 0x00, 0xEE); | 194 return SkColorSetRGB(0x00, 0x00, 0xEE); |
290 } | 195 } |
291 case kColorId_LinkPressed: | 196 case kColorId_LinkPressed: |
292 return SK_ColorRED; | 197 return SK_ColorRED; |
293 | 198 |
294 // Button | 199 // Button |
295 case kColorId_ButtonEnabledColor: | 200 case kColorId_ButtonEnabledColor: |
(...skipping 18 matching lines...) Expand all Loading... |
314 return GetTextColor(GetLabel(), SELECTED); | 219 return GetTextColor(GetLabel(), SELECTED); |
315 case kColorId_ButtonPressedShade: | 220 case kColorId_ButtonPressedShade: |
316 return SK_ColorTRANSPARENT; | 221 return SK_ColorTRANSPARENT; |
317 | 222 |
318 // Textfield | 223 // Textfield |
319 case kColorId_TextfieldDefaultColor: | 224 case kColorId_TextfieldDefaultColor: |
320 return GetTextColor(GetEntry(), NORMAL); | 225 return GetTextColor(GetEntry(), NORMAL); |
321 case kColorId_TextfieldDefaultBackground: | 226 case kColorId_TextfieldDefaultBackground: |
322 return GetBaseColor(GetEntry(), NORMAL); | 227 return GetBaseColor(GetEntry(), NORMAL); |
323 | 228 |
324 #if GTK_MAJOR_VERSION == 2 | |
325 case kColorId_TextfieldReadOnlyColor: | 229 case kColorId_TextfieldReadOnlyColor: |
326 return GetTextColor(GetEntry(), ACTIVE); | 230 return GetTextColor(GetEntry(), ACTIVE); |
327 case kColorId_TextfieldReadOnlyBackground: | 231 case kColorId_TextfieldReadOnlyBackground: |
328 return GetBaseColor(GetEntry(), ACTIVE); | 232 return GetBaseColor(GetEntry(), ACTIVE); |
329 case kColorId_TextfieldSelectionColor: | 233 case kColorId_TextfieldSelectionColor: |
330 return GetTextColor(GetEntry(), SELECTED); | 234 return GetTextColor(GetEntry(), SELECTED); |
331 case kColorId_TextfieldSelectionBackgroundFocused: | 235 case kColorId_TextfieldSelectionBackgroundFocused: |
332 return GetBaseColor(GetEntry(), SELECTED); | 236 return GetBaseColor(GetEntry(), SELECTED); |
333 #else | |
334 case kColorId_TextfieldReadOnlyColor: | |
335 return GetTextColor(GetEntry(), SELECTED); | |
336 case kColorId_TextfieldReadOnlyBackground: | |
337 return GetBaseColor(GetEntry(), SELECTED); | |
338 case kColorId_TextfieldSelectionColor: | |
339 return GetTextColor(GetLabel(), SELECTED); | |
340 case kColorId_TextfieldSelectionBackgroundFocused: | |
341 return GetBaseColor(GetLabel(), SELECTED); | |
342 #endif | |
343 | 237 |
344 // Tooltips | 238 // Tooltips |
345 case kColorId_TooltipBackground: | 239 case kColorId_TooltipBackground: |
346 return GetBGColor(GetTooltip(), NORMAL); | 240 return GetBGColor(GetTooltip(), NORMAL); |
347 case kColorId_TooltipText: | 241 case kColorId_TooltipText: |
348 return GetFGColor(GetTooltip(), NORMAL); | 242 return GetFGColor(GetTooltip(), NORMAL); |
349 | 243 |
350 // Trees and Tables (implemented on GTK using the same class) | 244 // Trees and Tables (implemented on GTK using the same class) |
351 case kColorId_TableBackground: | 245 case kColorId_TableBackground: |
352 case kColorId_TreeBackground: | 246 case kColorId_TreeBackground: |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 } | 343 } |
450 | 344 |
451 case kColorId_NumColors: | 345 case kColorId_NumColors: |
452 NOTREACHED(); | 346 NOTREACHED(); |
453 break; | 347 break; |
454 } | 348 } |
455 | 349 |
456 return kInvalidColorIdColor; | 350 return kInvalidColorIdColor; |
457 } | 351 } |
458 | 352 |
459 // Get ChromeGtkFrame theme colors. No-op in GTK3. | |
460 bool NativeThemeGtk2::GetChromeStyleColor(const char* style_property, | |
461 SkColor* ret_color) const { | |
462 #if GTK_MAJOR_VERSION == 2 | |
463 GdkColor* style_color = nullptr; | |
464 gtk_widget_style_get(GetWindow(), style_property, &style_color, nullptr); | |
465 if (style_color) { | |
466 *ret_color = GdkColorToSkColor(*style_color); | |
467 gdk_color_free(style_color); | |
468 return true; | |
469 } | |
470 #endif | |
471 | |
472 return false; | |
473 } | |
474 | |
475 GtkWidget* NativeThemeGtk2::GetWindow() const { | 353 GtkWidget* NativeThemeGtk2::GetWindow() const { |
476 static GtkWidget* fake_window = NULL; | 354 static GtkWidget* fake_window = NULL; |
477 | 355 |
478 if (!fake_window) { | 356 if (!fake_window) { |
479 fake_window = chrome_gtk_frame_new(); | 357 fake_window = chrome_gtk_frame_new(); |
480 gtk_widget_realize(fake_window); | 358 gtk_widget_realize(fake_window); |
481 } | 359 } |
482 | 360 |
483 return fake_window; | 361 return fake_window; |
484 } | 362 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 | 440 |
563 if (!fake_menu_item) { | 441 if (!fake_menu_item) { |
564 fake_menu_item = gtk_custom_menu_item_new(); | 442 fake_menu_item = gtk_custom_menu_item_new(); |
565 gtk_menu_shell_append(GTK_MENU_SHELL(GetMenu()), fake_menu_item); | 443 gtk_menu_shell_append(GTK_MENU_SHELL(GetMenu()), fake_menu_item); |
566 } | 444 } |
567 | 445 |
568 return fake_menu_item; | 446 return fake_menu_item; |
569 } | 447 } |
570 | 448 |
571 } // namespace libgtkui | 449 } // namespace libgtkui |
OLD | NEW |