Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: chrome/browser/ui/libgtk2ui/gtk2_ui.cc

Issue 2290053002: Remove some unused theming code for GTK. (Closed)
Patch Set: remove GtkThemeIconSource Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/libgtk2ui/gtk2_ui.h ('k') | ui/views/linux_ui/linux_ui.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/libgtk2ui/gtk2_ui.h" 5 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <pango/pango.h> 8 #include <pango/pango.h>
9 #include <X11/Xcursor/Xcursor.h> 9 #include <X11/Xcursor/Xcursor.h>
10 #include <set> 10 #include <set>
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 // linux/debian_wheezy_i386-sysroot/usr/include/linux/stddef.h redefines NULL 78 // linux/debian_wheezy_i386-sysroot/usr/include/linux/stddef.h redefines NULL
79 // to 0, which breaks -Wsentinel. Get back the normal definition of NULL. 79 // to 0, which breaks -Wsentinel. Get back the normal definition of NULL.
80 // TODO(thakis): Remove this once we update sysroots. 80 // TODO(thakis): Remove this once we update sysroots.
81 #define __need_NULL 81 #define __need_NULL
82 #include <stddef.h> 82 #include <stddef.h>
83 83
84 namespace libgtk2ui { 84 namespace libgtk2ui {
85 85
86 namespace { 86 namespace {
87 87
88 class GtkThemeIconSource : public gfx::ImageSkiaSource {
89 public:
90 GtkThemeIconSource(int id, const char* icon, bool enabled)
91 : id_(id), icon_(icon), enabled_(enabled) {}
92
93 ~GtkThemeIconSource() override {}
94
95 gfx::ImageSkiaRep GetImageForScale(float scale) override {
96 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
97 SkBitmap default_icon = rb.GetImageNamed(id_).AsBitmap();
98
99 int scalew = default_icon.width() * scale;
100 int scaleh = default_icon.height() * scale;
101
102 // Ask GTK to render the icon to a buffer, which we will steal from.
103 GtkIconTheme* icon_theme = gtk_icon_theme_get_default();
104 GdkPixbuf* gdk_icon = gtk_icon_theme_load_icon(
105 icon_theme, icon_, 20 * scale, (GtkIconLookupFlags)0, NULL);
106
107 // This can theoretically happen if an icon theme doesn't provide a
108 // specific image. This should realistically never happen, but I bet there
109 // are some theme authors who don't reliably provide all icons.
110 if (!gdk_icon)
111 return gfx::ImageSkiaRep();
112
113 #if GTK_MAJOR_VERSION == 2
114 GtkIconSource* icon_source = gtk_icon_source_new();
115 gtk_icon_source_set_pixbuf(icon_source, gdk_icon);
116
117 GdkPixbuf* temp = gtk_style_render_icon(
118 gtk_rc_get_style(NativeThemeGtk2::instance()->GetButton()), icon_source,
119 GTK_TEXT_DIR_NONE, enabled_ ? GTK_STATE_NORMAL : GTK_STATE_INSENSITIVE,
120 (GtkIconSize)-1, NativeThemeGtk2::instance()->GetButton(), NULL);
121
122 gtk_icon_source_free(icon_source);
123 g_object_unref(gdk_icon);
124
125 gdk_icon = temp;
126 #endif
127
128 SkBitmap retval;
129 retval.allocN32Pixels(scalew, scaleh);
130 retval.eraseColor(0);
131
132 const SkBitmap icon = GdkPixbufToImageSkia(gdk_icon);
133 g_object_unref(gdk_icon);
134
135 SkCanvas canvas(retval);
136 SkPaint paint;
137
138 #if GTK_MAJOR_VERSION > 2
139 if (!enabled_)
140 paint.setAlpha(128);
141 #endif
142
143 canvas.drawBitmap(icon, (scalew / 2) - (icon.width() / 2),
144 (scaleh / 2) - (icon.height() / 2), &paint);
145
146 return gfx::ImageSkiaRep(retval, scale);
147 }
148
149 private:
150 int id_;
151 const char* icon_;
152 bool enabled_;
153
154 DISALLOW_COPY_AND_ASSIGN(GtkThemeIconSource);
155 };
156
157 class GtkButtonImageSource : public gfx::ImageSkiaSource { 88 class GtkButtonImageSource : public gfx::ImageSkiaSource {
158 public: 89 public:
159 GtkButtonImageSource(const char* idr_string, gfx::Size size) 90 GtkButtonImageSource(const char* idr_string, gfx::Size size)
160 : width_(size.width()), height_(size.height()) { 91 : width_(size.width()), height_(size.height()) {
161 is_blue_ = !!strstr(idr_string, "IDR_BLUE"); 92 is_blue_ = !!strstr(idr_string, "IDR_BLUE");
162 focus_ = !!strstr(idr_string, "_FOCUSED_"); 93 focus_ = !!strstr(idr_string, "_FOCUSED_");
163 94
164 if (strstr(idr_string, "_DISABLED")) { 95 if (strstr(idr_string, "_DISABLED")) {
165 state_ = ui::NativeTheme::kDisabled; 96 state_ = ui::NativeTheme::kDisabled;
166 } else if (strstr(idr_string, "_HOVER")) { 97 } else if (strstr(idr_string, "_HOVER")) {
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 221
291 // Prefix for app indicator ids 222 // Prefix for app indicator ids
292 const char kAppIndicatorIdPrefix[] = "chrome_app_indicator_"; 223 const char kAppIndicatorIdPrefix[] = "chrome_app_indicator_";
293 224
294 // Number of app indicators used (used as part of app-indicator id). 225 // Number of app indicators used (used as part of app-indicator id).
295 int indicators_count; 226 int indicators_count;
296 227
297 // The unknown content type. 228 // The unknown content type.
298 const char* kUnknownContentType = "application/octet-stream"; 229 const char* kUnknownContentType = "application/octet-stream";
299 230
300 // The size of the rendered toolbar image.
301 const int kToolbarImageWidth = 64;
302 const int kToolbarImageHeight = 128;
303
304 // Values used as the new luminance and saturation values in the inactive tab 231 // Values used as the new luminance and saturation values in the inactive tab
305 // text color. 232 // text color.
306 const double kInactiveLuminance = 0.15; 233 const double kInactiveLuminance = 0.15;
307 const double kInactiveSaturation = 0.3; 234 const double kInactiveSaturation = 0.3;
308 235
309 // TODO(erg): ThemeService has a whole interface just for reading default 236 // TODO(erg): ThemeService has a whole interface just for reading default
310 // constants. Figure out what to do with that more long term; for now, just 237 // constants. Figure out what to do with that more long term; for now, just
311 // copy the constants themselves here. 238 // copy the constants themselves here.
312 // 239 //
313 // Default tints. 240 // Default tints.
314 const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f }; 241 const color_utils::HSL kDefaultTintFrameIncognito = { -1, 0.2f, 0.35f };
315 const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f }; 242 const color_utils::HSL kDefaultTintFrameIncognitoInactive = { -1, 0.3f, 0.6f };
316 const color_utils::HSL kDefaultTintBackgroundTab = { -1, 0.5, 0.75 };
317 243
318 #if GTK_MAJOR_VERSION == 3 244 #if GTK_MAJOR_VERSION == 3
319 const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f }; 245 const color_utils::HSL kDefaultTintFrameInactive = { -1, -1, 0.75f };
320 #endif 246 #endif
321 247
322 // Picks a button tint from a set of background colors. While 248 // Picks a button tint from a set of background colors. While
323 // |accent_color| will usually be the same color through a theme, this 249 // |accent_color| will usually be the same color through a theme, this
324 // function will get called with the normal GtkLabel |text_color|/GtkWindow 250 // function will get called with the normal GtkLabel |text_color|/GtkWindow
325 // |background_color| pair and the GtkEntry |text_color|/|background_color| 251 // |background_color| pair and the GtkEntry |text_color|/|background_color|
326 // pair. While 3/4 of the time the resulting tint will be the same, themes that 252 // pair. While 3/4 of the time the resulting tint will be the same, themes that
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 417
492 Gtk2UI::Gtk2UI() 418 Gtk2UI::Gtk2UI()
493 : default_font_size_pixels_(0), 419 : default_font_size_pixels_(0),
494 default_font_style_(gfx::Font::NORMAL), 420 default_font_style_(gfx::Font::NORMAL),
495 default_font_weight_(gfx::Font::Weight::NORMAL), 421 default_font_weight_(gfx::Font::Weight::NORMAL),
496 middle_click_action_(GetDefaultMiddleClickAction()), 422 middle_click_action_(GetDefaultMiddleClickAction()),
497 device_scale_factor_(1.0) { 423 device_scale_factor_(1.0) {
498 GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess()); 424 GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess());
499 } 425 }
500 426
501 427 Gtk2UI::~Gtk2UI() {}
502 428
503 void OnThemeChanged(GObject* obj, GParamSpec* param, Gtk2UI* gtkui) { 429 void OnThemeChanged(GObject* obj, GParamSpec* param, Gtk2UI* gtkui) {
504 gtkui->ResetStyle(); 430 gtkui->ResetStyle();
505 } 431 }
506 432
507 void Gtk2UI::Initialize() { 433 void Gtk2UI::Initialize() {
508 GtkSettings* settings = gtk_settings_get_default(); 434 GtkSettings* settings = gtk_settings_get_default();
509 g_signal_connect_after(settings, 435 g_signal_connect_after(settings,
510 "notify::gtk-theme-name", 436 "notify::gtk-theme-name",
511 G_CALLBACK(OnThemeChanged), 437 G_CALLBACK(OnThemeChanged),
(...skipping 22 matching lines...) Expand all
534 indicators_count = 0; 460 indicators_count = 0;
535 461
536 // Instantiate the singleton instance of Gtk2EventLoop. 462 // Instantiate the singleton instance of Gtk2EventLoop.
537 Gtk2EventLoop::GetInstance(); 463 Gtk2EventLoop::GetInstance();
538 } 464 }
539 465
540 void Gtk2UI::MaterialDesignControllerReady() { 466 void Gtk2UI::MaterialDesignControllerReady() {
541 UpdateMaterialDesignColors(); 467 UpdateMaterialDesignColors();
542 } 468 }
543 469
544 Gtk2UI::~Gtk2UI() {
545 ClearAllThemeData();
546 }
547
548 gfx::Image Gtk2UI::GetThemeImageNamed(int id) const {
549 // Try to get our cached version:
550 ImageCache::const_iterator it = gtk_images_.find(id);
551 if (it != gtk_images_.end())
552 return it->second;
553
554 gfx::Image image = GenerateGtkThemeImage(id);
555
556 if (image.IsEmpty()) {
557 SkBitmap bitmap = GenerateGtkThemeBitmap(id);
558 if (!bitmap.empty())
559 image = gfx::Image::CreateFrom1xBitmap(bitmap);
560 }
561
562 gtk_images_[id] = image;
563 return image;
564 }
565
566 bool Gtk2UI::GetTint(int id, color_utils::HSL* tint) const { 470 bool Gtk2UI::GetTint(int id, color_utils::HSL* tint) const {
567 switch (id) { 471 switch (id) {
568 // Tints for which the cross-platform default is fine. Before adding new 472 // Tints for which the cross-platform default is fine. Before adding new
569 // values here, specifically verify they work well on Linux. 473 // values here, specifically verify they work well on Linux.
570 case ThemeProperties::TINT_BACKGROUND_TAB: 474 case ThemeProperties::TINT_BACKGROUND_TAB:
571 // TODO(estade): Return something useful for TINT_BUTTONS so that chrome:// 475 // TODO(estade): Return something useful for TINT_BUTTONS so that chrome://
572 // page icons are colored appropriately. 476 // page icons are colored appropriately.
573 case ThemeProperties::TINT_BUTTONS: 477 case ThemeProperties::TINT_BUTTONS:
574 break; 478 break;
575 default: 479 default:
576 // Assume any tints not specifically verified on Linux aren't usable. 480 // Assume any tints not specifically verified on Linux aren't usable.
577 // TODO(pkasting): Try to remove values from |colors_| that could just be 481 // TODO(pkasting): Try to remove values from |colors_| that could just be
578 // added to the group above instead. 482 // added to the group above instead.
579 NOTREACHED(); 483 NOTREACHED();
580 } 484 }
581 return false; 485 return false;
582 } 486 }
583 487
584 bool Gtk2UI::GetColor(int id, SkColor* color) const { 488 bool Gtk2UI::GetColor(int id, SkColor* color) const {
585 ColorMap::const_iterator it = colors_.find(id); 489 ColorMap::const_iterator it = colors_.find(id);
586 if (it != colors_.end()) { 490 if (it != colors_.end()) {
587 *color = it->second; 491 *color = it->second;
588 return true; 492 return true;
589 } 493 }
590 494
591 return false; 495 return false;
592 } 496 }
593 497
594 bool Gtk2UI::HasCustomImage(int id) const {
595 return !GetThemeImageNamed(id).IsEmpty();
596 }
597
598 SkColor Gtk2UI::GetFocusRingColor() const { 498 SkColor Gtk2UI::GetFocusRingColor() const {
599 return focus_ring_color_; 499 return focus_ring_color_;
600 } 500 }
601 501
602 SkColor Gtk2UI::GetThumbActiveColor() const { 502 SkColor Gtk2UI::GetThumbActiveColor() const {
603 return thumb_active_color_; 503 return thumb_active_color_;
604 } 504 }
605 505
606 SkColor Gtk2UI::GetThumbInactiveColor() const { 506 SkColor Gtk2UI::GetThumbInactiveColor() const {
607 return thumb_inactive_color_; 507 return thumb_inactive_color_;
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
921 821
922 colors_[ThemeProperties::COLOR_TAB_TEXT] = label_color; 822 colors_[ThemeProperties::COLOR_TAB_TEXT] = label_color;
923 colors_[ThemeProperties::COLOR_BOOKMARK_TEXT] = label_color; 823 colors_[ThemeProperties::COLOR_BOOKMARK_TEXT] = label_color;
924 824
925 UpdateDefaultFont(); 825 UpdateDefaultFont();
926 826
927 // Build the various icon tints. 827 // Build the various icon tints.
928 GetNormalButtonTintHSL(&button_tint_); 828 GetNormalButtonTintHSL(&button_tint_);
929 GetNormalEntryForegroundHSL(&entry_tint_); 829 GetNormalEntryForegroundHSL(&entry_tint_);
930 GetSelectedEntryForegroundHSL(&selected_entry_tint_); 830 GetSelectedEntryForegroundHSL(&selected_entry_tint_);
931 SkColor frame_color = BuildFrameColors();
932 831
933 // The inactive frame color never occurs naturally in the theme, as it is a 832 // The inactive frame color never occurs naturally in the theme, as it is a
934 // tinted version of |frame_color|. We generate another color based on the 833 // tinted version of the normal frame color. We generate another color based
935 // background tab color, with the lightness and saturation moved in the 834 // on the background tab color, with the lightness and saturation moved in the
936 // opposite direction. (We don't touch the hue, since there should be subtle 835 // opposite direction. (We don't touch the hue, since there should be subtle
937 // hints of the color in the text.) 836 // hints of the color in the text.)
938 color_utils::HSL inactive_tab_text_hsl; 837 color_utils::HSL inactive_tab_text_hsl;
939 color_utils::SkColorToHSL( 838 color_utils::SkColorToHSL(
940 theme->GetSystemColor(ui::NativeTheme::kColorId_WindowBackground), 839 theme->GetSystemColor(ui::NativeTheme::kColorId_WindowBackground),
941 &inactive_tab_text_hsl); 840 &inactive_tab_text_hsl);
942 inactive_tab_text_hsl.s = kInactiveLuminance; 841 inactive_tab_text_hsl.s = kInactiveLuminance;
943 inactive_tab_text_hsl.l = kInactiveSaturation; 842 inactive_tab_text_hsl.l = kInactiveSaturation;
944 843
945 colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] = 844 colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] =
(...skipping 14 matching lines...) Expand all
960 859
961 colors_[ThemeProperties::COLOR_NTP_BACKGROUND] = ntp_background; 860 colors_[ThemeProperties::COLOR_NTP_BACKGROUND] = ntp_background;
962 colors_[ThemeProperties::COLOR_NTP_TEXT] = ntp_foreground; 861 colors_[ThemeProperties::COLOR_NTP_TEXT] = ntp_foreground;
963 862
964 // The NTP header is the color that surrounds the current active thumbnail on 863 // The NTP header is the color that surrounds the current active thumbnail on
965 // the NTP, and acts as the border of the "Recent Links" box. It would be 864 // the NTP, and acts as the border of the "Recent Links" box. It would be
966 // awesome if they were separated so we could use GetBorderColor() for the 865 // awesome if they were separated so we could use GetBorderColor() for the
967 // border around the "Recent Links" section, but matching the frame color is 866 // border around the "Recent Links" section, but matching the frame color is
968 // more important. 867 // more important.
969 868
869 BuildFrameColors();
870 SkColor frame_color = colors_[ThemeProperties::COLOR_FRAME];
970 colors_[ThemeProperties::COLOR_NTP_HEADER] = frame_color; 871 colors_[ThemeProperties::COLOR_NTP_HEADER] = frame_color;
971 colors_[ThemeProperties::COLOR_NTP_SECTION] = toolbar_color; 872 colors_[ThemeProperties::COLOR_NTP_SECTION] = toolbar_color;
972 colors_[ThemeProperties::COLOR_NTP_SECTION_TEXT] = label_color; 873 colors_[ThemeProperties::COLOR_NTP_SECTION_TEXT] = label_color;
973 874
974 SkColor link_color = 875 SkColor link_color =
975 theme->GetSystemColor(ui::NativeTheme::kColorId_LinkEnabled); 876 theme->GetSystemColor(ui::NativeTheme::kColorId_LinkEnabled);
976 colors_[ThemeProperties::COLOR_NTP_LINK] = link_color; 877 colors_[ThemeProperties::COLOR_NTP_LINK] = link_color;
977 colors_[ThemeProperties::COLOR_NTP_LINK_UNDERLINE] = link_color; 878 colors_[ThemeProperties::COLOR_NTP_LINK_UNDERLINE] = link_color;
978 colors_[ThemeProperties::COLOR_NTP_SECTION_LINK] = link_color; 879 colors_[ThemeProperties::COLOR_NTP_SECTION_LINK] = link_color;
979 colors_[ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE] = link_color; 880 colors_[ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE] = link_color;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1032 !ui::MaterialDesignController::IsModeMaterial()) { 933 !ui::MaterialDesignController::IsModeMaterial()) {
1033 return; 934 return;
1034 } 935 }
1035 NativeThemeGtk2* theme = NativeThemeGtk2::instance(); 936 NativeThemeGtk2* theme = NativeThemeGtk2::instance();
1036 SkColor label_color = 937 SkColor label_color =
1037 theme->GetSystemColor(ui::NativeTheme::kColorId_LabelEnabledColor); 938 theme->GetSystemColor(ui::NativeTheme::kColorId_LabelEnabledColor);
1038 colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] = 939 colors_[ThemeProperties::COLOR_BACKGROUND_TAB_TEXT] =
1039 color_utils::BlendTowardOppositeLuma(label_color, 50); 940 color_utils::BlendTowardOppositeLuma(label_color, 50);
1040 } 941 }
1041 942
1042 SkColor Gtk2UI::BuildFrameColors() { 943 void Gtk2UI::BuildFrameColors() {
944 #if GTK_MAJOR_VERSION == 2
1043 NativeThemeGtk2* theme = NativeThemeGtk2::instance(); 945 NativeThemeGtk2* theme = NativeThemeGtk2::instance();
946 color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 };
1044 SkColor frame_color = 947 SkColor frame_color =
1045 theme->GetSystemColor(ui::NativeTheme::kColorId_WindowBackground); 948 theme->GetSystemColor(ui::NativeTheme::kColorId_WindowBackground);
1046 SkColor temp_color;
1047
1048 #if GTK_MAJOR_VERSION == 2
1049 color_utils::HSL kDefaultFrameShift = { -1, -1, 0.4 };
1050 GtkStyle* style = gtk_rc_get_style(theme->GetWindow());
1051
1052 frame_color = color_utils::HSLShift(frame_color, kDefaultFrameShift); 949 frame_color = color_utils::HSLShift(frame_color, kDefaultFrameShift);
1053 theme->GetChromeStyleColor("frame-color", &frame_color); 950 theme->GetChromeStyleColor("frame-color", &frame_color);
951 colors_[ThemeProperties::COLOR_FRAME] = frame_color;
1054 952
1055 temp_color = frame_color; 953 GtkStyle* style = gtk_rc_get_style(theme->GetWindow());
1056 colors_[ThemeProperties::COLOR_FRAME] = temp_color; 954 SkColor temp_color = color_utils::HSLShift(
1057
1058 temp_color = color_utils::HSLShift(
1059 GdkColorToSkColor(style->bg[GTK_STATE_INSENSITIVE]), 955 GdkColorToSkColor(style->bg[GTK_STATE_INSENSITIVE]),
1060 kDefaultFrameShift); 956 kDefaultFrameShift);
1061 theme->GetChromeStyleColor("inactive-frame-color", &temp_color); 957 theme->GetChromeStyleColor("inactive-frame-color", &temp_color);
1062 colors_[ThemeProperties::COLOR_FRAME_INACTIVE] = temp_color; 958 colors_[ThemeProperties::COLOR_FRAME_INACTIVE] = temp_color;
1063 959
1064 temp_color = color_utils::HSLShift( 960 temp_color = color_utils::HSLShift(
1065 frame_color, 961 frame_color,
1066 kDefaultTintFrameIncognito); 962 kDefaultTintFrameIncognito);
1067 theme->GetChromeStyleColor("incognito-frame-color", &temp_color); 963 theme->GetChromeStyleColor("incognito-frame-color", &temp_color);
1068 colors_[ThemeProperties::COLOR_FRAME_INCOGNITO] = temp_color; 964 colors_[ThemeProperties::COLOR_FRAME_INCOGNITO] = temp_color;
1069 965
1070 temp_color = color_utils::HSLShift( 966 temp_color = color_utils::HSLShift(
1071 frame_color, 967 frame_color,
1072 kDefaultTintFrameIncognitoInactive); 968 kDefaultTintFrameIncognitoInactive);
1073 theme->GetChromeStyleColor("incognito-inactive-frame-color", &temp_color); 969 theme->GetChromeStyleColor("incognito-inactive-frame-color", &temp_color);
1074 colors_[ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE] = temp_color; 970 colors_[ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE] = temp_color;
1075 #else 971 #else
1076 const SkBitmap* bitmap; 972 auto set_frame_color = [this](int color_id) {
973 // Render a GtkHeaderBar as our title bar, cropping out any curved edges
974 // on the left and right sides. Also remove the bottom border for good
975 // measure.
976 SkBitmap bitmap;
977 bitmap.allocN32Pixels(kToolbarImageWidth, 40);
978 bitmap.eraseColor(0);
1077 979
1078 bitmap = GetThemeImageNamed(IDR_THEME_FRAME).ToSkBitmap(); 980 static GtkWidget* title = nullptr;
1079 bitmap->lockPixels(); 981 if (!title) {
1080 temp_color = bitmap->getColor(bitmap->width() / 2, bitmap->height() - 1); 982 title = gtk_header_bar_new();
1081 bitmap->unlockPixels(); 983 gtk_widget_set_size_request(title, kToolbarImageWidth * 2, 48);
1082 colors_[ThemeProperties::COLOR_FRAME] = temp_color;
1083 984
1084 bitmap = GetThemeImageNamed(IDR_THEME_FRAME_INACTIVE).ToSkBitmap(); 985 GtkWidget* window = gtk_offscreen_window_new();
1085 bitmap->lockPixels(); 986 gtk_container_add(GTK_CONTAINER(window), title);
1086 temp_color = bitmap->getColor(bitmap->width() / 2, bitmap->height() - 1);
1087 bitmap->unlockPixels();
1088 colors_[ThemeProperties::COLOR_FRAME_INACTIVE] = temp_color;
1089 987
1090 bitmap = GetThemeImageNamed(IDR_THEME_FRAME_INCOGNITO).ToSkBitmap(); 988 gtk_widget_show_all(window);
1091 bitmap->lockPixels();
1092 temp_color = bitmap->getColor(bitmap->width() / 2, bitmap->height() - 1);
1093 bitmap->unlockPixels();
1094 colors_[ThemeProperties::COLOR_FRAME_INCOGNITO] = temp_color;
1095
1096 bitmap = GetThemeImageNamed(IDR_THEME_FRAME_INCOGNITO_INACTIVE).ToSkBitmap();
1097 bitmap->lockPixels();
1098 temp_color = bitmap->getColor(bitmap->width() / 2, bitmap->height() - 1);
1099 bitmap->unlockPixels();
1100 colors_[ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE] = temp_color;
1101 #endif
1102
1103 return frame_color;
1104 }
1105
1106 gfx::Image Gtk2UI::GenerateGtkThemeImage(int id) const {
1107 gfx::ImageSkiaSource* source = NULL;
1108
1109 switch (id) {
1110 case IDR_BACK:
1111 source = new GtkThemeIconSource(id, "go-previous", true);
1112 break;
1113 case IDR_BACK_D:
1114 source = new GtkThemeIconSource(id, "go-previous", false);
1115 break;
1116
1117 case IDR_FORWARD:
1118 source = new GtkThemeIconSource(id, "go-next", true);
1119 break;
1120 case IDR_FORWARD_D:
1121 source = new GtkThemeIconSource(id, "go-next", false);
1122 break;
1123
1124 case IDR_HOME:
1125 source = new GtkThemeIconSource(id, "go-home", true);
1126 break;
1127
1128 case IDR_RELOAD:
1129 source = new GtkThemeIconSource(id, "view-refresh", true);
1130 break;
1131 case IDR_RELOAD_D:
1132 source = new GtkThemeIconSource(id, "view-refresh", false);
1133 break;
1134
1135 case IDR_STOP:
1136 source = new GtkThemeIconSource(id, "process-stop", true);
1137 break;
1138 case IDR_STOP_D:
1139 source = new GtkThemeIconSource(id, "process-stop", false);
1140 break;
1141
1142
1143 // The toolbar bezels don't seem to be in use anymore, remove at your
1144 // discretion
1145 case IDR_TOOLBAR_BEZEL_HOVER: {
1146 gfx::Size size = ResourceBundle::GetSharedInstance().
1147 GetImageSkiaNamed(id)->size();
1148 source = new GtkButtonImageSource("IDR_BUTTON_HOVER", size);
1149 break;
1150 }
1151 case IDR_TOOLBAR_BEZEL_PRESSED: {
1152 gfx::Size size = ResourceBundle::GetSharedInstance().
1153 GetImageSkiaNamed(id)->size();
1154 source = new GtkButtonImageSource("IDR_BUTTON_PRESSED", size);
1155 break;
1156 }
1157 }
1158
1159 if (source)
1160 return gfx::Image(gfx::ImageSkia(source, 1));
1161
1162 return gfx::Image();
1163 }
1164
1165 SkBitmap Gtk2UI::GenerateGtkThemeBitmap(int id) const {
1166 switch (id) {
1167 case IDR_THEME_TOOLBAR: {
1168 if (ui::MaterialDesignController::IsModeMaterial())
1169 break;
1170
1171 SkBitmap bitmap;
1172 bitmap.allocN32Pixels(kToolbarImageWidth, kToolbarImageHeight);
1173 bitmap.eraseColor(
1174 NativeThemeGtk2::instance()->GetSystemColor(
1175 ui::NativeTheme::kColorId_LabelBackgroundColor));
1176 return bitmap;
1177 } 989 }
1178 990
1179 case IDR_THEME_TAB_BACKGROUND: 991 cairo_surface_t* surface = cairo_image_surface_create_for_data(
1180 return GenerateTabImage(IDR_THEME_FRAME); 992 static_cast<unsigned char*>(bitmap.getAddr(0, 0)), CAIRO_FORMAT_ARGB32,
1181 case IDR_THEME_TAB_BACKGROUND_INCOGNITO: 993 bitmap.width(), bitmap.height(), bitmap.width() * 4);
1182 return GenerateTabImage(IDR_THEME_FRAME_INCOGNITO); 994 cairo_t* cr = cairo_create(surface);
1183 case IDR_FRAME: 995 cairo_translate(cr, kToolbarImageWidth / -2, 0);
1184 case IDR_THEME_FRAME: 996 gtk_widget_draw(title, cr);
1185 return GenerateFrameImage(ThemeProperties::COLOR_FRAME, 997 cairo_destroy(cr);
1186 "frame-gradient-color"); 998 cairo_surface_destroy(surface);
1187 case IDR_FRAME_INACTIVE: 999
1188 case IDR_THEME_FRAME_INACTIVE: 1000 switch (color_id) {
1189 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INACTIVE, 1001 case ThemeProperties::COLOR_FRAME_INACTIVE:
1190 "inactive-frame-gradient-color"); 1002 bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
1191 case IDR_THEME_FRAME_INCOGNITO: 1003 bitmap, kDefaultTintFrameInactive);
1192 return GenerateFrameImage(ThemeProperties::COLOR_FRAME_INCOGNITO, 1004 break;
1193 "incognito-frame-gradient-color"); 1005 case ThemeProperties::COLOR_FRAME_INCOGNITO:
1194 case IDR_THEME_FRAME_INCOGNITO_INACTIVE: { 1006 bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
1195 return GenerateFrameImage( 1007 bitmap, kDefaultTintFrameIncognito);
1196 ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE, 1008 break;
1197 "incognito-inactive-frame-gradient-color"); 1009 case ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE:
1198 } 1010 bitmap = SkBitmapOperations::CreateHSLShiftedBitmap(
1199 // Icons that sit inside the omnibox shouldn't receive TINT_BUTTONS and 1011 bitmap, kDefaultTintFrameIncognitoInactive);
1200 // instead should tint based on the foreground text entry color in GTK+ 1012 break;
1201 // mode because some themes that try to be dark *and* light have very
1202 // different colors between the omnibox and the normal background area.
1203 // TODO(erg): Decide what to do about other icons that appear in the
1204 // omnibox, e.g. content settings icons.
1205 case IDR_OMNIBOX_CALCULATOR:
1206 case IDR_OMNIBOX_EXTENSION_APP:
1207 case IDR_OMNIBOX_HTTP:
1208 case IDR_OMNIBOX_SEARCH:
1209 case IDR_OMNIBOX_STAR:
1210 case IDR_OMNIBOX_TTS: {
1211 return GenerateTintedIcon(id, entry_tint_);
1212 } 1013 }
1213 1014
1214 // TODO(erg): The dropdown arrow should be tinted because we're injecting 1015 bitmap.lockPixels();
1215 // various background GTK colors, but the code that accesses them needs to 1016 colors_[color_id] =
1216 // be modified so that they ask their ui::ThemeProvider instead of the 1017 bitmap.getColor(bitmap.width() / 2, bitmap.height() - 1);
1217 // ResourceBundle. (i.e. in a light on dark theme, the dropdown arrow will 1018 bitmap.unlockPixels();
1218 // be dark on dark) 1019 };
1219 case IDR_MENU_DROPARROW:
1220 return GenerateTintedIcon(id, button_tint_);
1221 }
1222 1020
1223 return SkBitmap(); 1021 set_frame_color(ThemeProperties::COLOR_FRAME);
1224 } 1022 set_frame_color(ThemeProperties::COLOR_FRAME_INACTIVE);
1225 1023 set_frame_color(ThemeProperties::COLOR_FRAME_INCOGNITO);
1226 SkBitmap Gtk2UI::GenerateFrameImage( 1024 set_frame_color(ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE);
1227 int color_id,
1228 const char* gradient_name) const {
1229 #if GTK_MAJOR_VERSION == 2
1230 if (ui::MaterialDesignController::IsModeMaterial())
1231 return SkBitmap();
1232
1233 ColorMap::const_iterator it = colors_.find(color_id);
1234 DCHECK(it != colors_.end());
1235 SkColor base = it->second;
1236
1237 // We use two colors: the main color (passed in) and a lightened version of
1238 // that color (which is supposed to match the light gradient at the top of
1239 // several GTK+ themes, such as Ambiance, Clearlooks or Bluebird).
1240 const color_utils::HSL kGtkFrameShift = { -1, -1, 0.58 };
1241 gfx::Canvas canvas(gfx::Size(kToolbarImageWidth, kToolbarImageHeight),
1242 1.0f, true);
1243
1244 SkColor gradient_top_color = color_utils::HSLShift(base, kGtkFrameShift);
1245 int gradient_size;
1246
1247 NativeThemeGtk2::instance()->GetChromeStyleColor(gradient_name,
1248 &gradient_top_color);
1249 gtk_widget_style_get(NativeThemeGtk2::instance()->GetWindow(),
1250 "frame-gradient-size", &gradient_size,
1251 NULL);
1252
1253 if (gradient_size) {
1254 SkPaint paint;
1255 paint.setStyle(SkPaint::kFill_Style);
1256 paint.setAntiAlias(true);
1257 paint.setShader(gfx::CreateGradientShader(
1258 0, gradient_size, gradient_top_color, base));
1259
1260 canvas.DrawRect(gfx::Rect(0, 0, kToolbarImageWidth, gradient_size), paint);
1261 }
1262
1263 canvas.FillRect(gfx::Rect(0, gradient_size, kToolbarImageWidth,
1264 kToolbarImageHeight - gradient_size), base);
1265 return canvas.ExtractImageRep().sk_bitmap();
1266 #else
1267 // Render a GtkHeaderBar as our title bar, cropping out any curved edges on
1268 // the left and right sides. Also remove the bottom border for good measure.
1269 SkBitmap bitmap;
1270 bitmap.allocN32Pixels(kToolbarImageWidth, 40);
1271 bitmap.eraseColor(0);
1272
1273 static GtkWidget* title = NULL;
1274 if (!title) {
1275 title = gtk_header_bar_new();
1276 gtk_widget_set_size_request(title, kToolbarImageWidth * 2, 48);
1277
1278 GtkWidget* window = gtk_offscreen_window_new();
1279 gtk_container_add(GTK_CONTAINER(window), title);
1280
1281 gtk_widget_show_all(window);
1282 }
1283
1284 cairo_surface_t* surface = cairo_image_surface_create_for_data(
1285 static_cast<unsigned char*>(bitmap.getAddr(0, 0)),
1286 CAIRO_FORMAT_ARGB32,
1287 bitmap.width(), bitmap.height(),
1288 bitmap.width() * 4);
1289 cairo_t* cr = cairo_create(surface);
1290 cairo_translate(cr, kToolbarImageWidth / -2, 0);
1291 gtk_widget_draw(title, cr);
1292 cairo_destroy(cr);
1293 cairo_surface_destroy(surface);
1294
1295 switch (color_id) {
1296 case ThemeProperties::COLOR_FRAME_INACTIVE:
1297 return SkBitmapOperations::CreateHSLShiftedBitmap(bitmap,
1298 kDefaultTintFrameInactive);
1299 case ThemeProperties::COLOR_FRAME_INCOGNITO:
1300 return SkBitmapOperations::CreateHSLShiftedBitmap(bitmap,
1301 kDefaultTintFrameIncognito);
1302 case ThemeProperties::COLOR_FRAME_INCOGNITO_INACTIVE:
1303 return SkBitmapOperations::CreateHSLShiftedBitmap(bitmap,
1304 kDefaultTintFrameIncognitoInactive);
1305 }
1306
1307 return bitmap;
1308 #endif 1025 #endif
1309 } 1026 }
1310 1027
1311 SkBitmap Gtk2UI::GenerateTabImage(int base_id) const {
1312 if (ui::MaterialDesignController::IsModeMaterial())
1313 return SkBitmap();
1314
1315 const SkBitmap* base_image = GetThemeImageNamed(base_id).ToSkBitmap();
1316 SkBitmap bg_tint = SkBitmapOperations::CreateHSLShiftedBitmap(
1317 *base_image, kDefaultTintBackgroundTab);
1318 return SkBitmapOperations::CreateTiledBitmap(
1319 bg_tint, 0, 0, bg_tint.width(), bg_tint.height());
1320 }
1321
1322 SkBitmap Gtk2UI::GenerateTintedIcon(
1323 int base_id,
1324 const color_utils::HSL& tint) const {
1325 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
1326 return SkBitmapOperations::CreateHSLShiftedBitmap(
1327 rb.GetImageNamed(base_id).AsBitmap(), tint);
1328 }
1329
1330 void Gtk2UI::GetNormalButtonTintHSL(color_utils::HSL* tint) const { 1028 void Gtk2UI::GetNormalButtonTintHSL(color_utils::HSL* tint) const {
1331 NativeThemeGtk2* theme = NativeThemeGtk2::instance(); 1029 NativeThemeGtk2* theme = NativeThemeGtk2::instance();
1332 1030
1333 SkColor accent_color = 1031 SkColor accent_color =
1334 theme->GetSystemColor( 1032 theme->GetSystemColor(
1335 ui::NativeTheme::kColorId_ButtonHighlightColor); 1033 ui::NativeTheme::kColorId_ButtonHighlightColor);
1336 SkColor text_color = 1034 SkColor text_color =
1337 theme->GetSystemColor( 1035 theme->GetSystemColor(
1338 ui::NativeTheme::kColorId_LabelEnabledColor); 1036 ui::NativeTheme::kColorId_LabelEnabledColor);
1339 SkColor base_color = 1037 SkColor base_color =
(...skipping 23 matching lines...) Expand all
1363 // The simplest of all the tints. We just use the selected text in the entry 1061 // The simplest of all the tints. We just use the selected text in the entry
1364 // since the icons tinted this way will only be displayed against 1062 // since the icons tinted this way will only be displayed against
1365 // base[GTK_STATE_SELECTED]. 1063 // base[GTK_STATE_SELECTED].
1366 SkColor color = 1064 SkColor color =
1367 NativeThemeGtk2::instance()->GetSystemColor( 1065 NativeThemeGtk2::instance()->GetSystemColor(
1368 ui::NativeTheme::kColorId_TextfieldSelectionColor); 1066 ui::NativeTheme::kColorId_TextfieldSelectionColor);
1369 1067
1370 color_utils::SkColorToHSL(color, tint); 1068 color_utils::SkColorToHSL(color, tint);
1371 } 1069 }
1372 1070
1373 void Gtk2UI::ClearAllThemeData() {
1374 gtk_images_.clear();
1375 }
1376
1377 void Gtk2UI::UpdateDefaultFont() { 1071 void Gtk2UI::UpdateDefaultFont() {
1378 PangoContext* pc = gtk_widget_get_pango_context( 1072 PangoContext* pc = gtk_widget_get_pango_context(
1379 NativeThemeGtk2::instance()->GetLabel()); 1073 NativeThemeGtk2::instance()->GetLabel());
1380 const PangoFontDescription* desc = pango_context_get_font_description(pc); 1074 const PangoFontDescription* desc = pango_context_get_font_description(pc);
1381 1075
1382 // Use gfx::FontRenderParams to select a family and determine the rendering 1076 // Use gfx::FontRenderParams to select a family and determine the rendering
1383 // settings. 1077 // settings.
1384 gfx::FontRenderParamsQuery query; 1078 gfx::FontRenderParamsQuery query;
1385 query.families = base::SplitString(pango_font_description_get_family(desc), 1079 query.families = base::SplitString(pango_font_description_get_family(desc),
1386 ",", base::TRIM_WHITESPACE, 1080 ",", base::TRIM_WHITESPACE,
(...skipping 21 matching lines...) Expand all
1408 // TODO(davemoore): What about PANGO_STYLE_OBLIQUE? 1102 // TODO(davemoore): What about PANGO_STYLE_OBLIQUE?
1409 if (pango_font_description_get_style(desc) == PANGO_STYLE_ITALIC) 1103 if (pango_font_description_get_style(desc) == PANGO_STYLE_ITALIC)
1410 query.style |= gfx::Font::ITALIC; 1104 query.style |= gfx::Font::ITALIC;
1411 1105
1412 default_font_render_params_ = 1106 default_font_render_params_ =
1413 gfx::GetFontRenderParams(query, &default_font_family_); 1107 gfx::GetFontRenderParams(query, &default_font_family_);
1414 default_font_style_ = query.style; 1108 default_font_style_ = query.style;
1415 } 1109 }
1416 1110
1417 void Gtk2UI::ResetStyle() { 1111 void Gtk2UI::ResetStyle() {
1418 ClearAllThemeData();
1419 LoadGtkValues(); 1112 LoadGtkValues();
1420 // TODO(varkha): There will be no need to call UpdateMaterialDesignColors() 1113 // TODO(varkha): There will be no need to call UpdateMaterialDesignColors()
1421 // once Material Design is on unconditionally. 1114 // once Material Design is on unconditionally.
1422 UpdateMaterialDesignColors(); 1115 UpdateMaterialDesignColors();
1423 NativeThemeGtk2::instance()->NotifyObservers(); 1116 NativeThemeGtk2::instance()->NotifyObservers();
1424 } 1117 }
1425 1118
1426 void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor) { 1119 void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor) {
1427 device_scale_factor_ = device_scale_factor; 1120 device_scale_factor_ = device_scale_factor;
1428 UpdateDefaultFont(); 1121 UpdateDefaultFont();
1429 } 1122 }
1430 1123
1431 float Gtk2UI::GetDeviceScaleFactor() const { 1124 float Gtk2UI::GetDeviceScaleFactor() const {
1432 if (display::Display::HasForceDeviceScaleFactor()) 1125 if (display::Display::HasForceDeviceScaleFactor())
1433 return display::Display::GetForcedDeviceScaleFactor(); 1126 return display::Display::GetForcedDeviceScaleFactor();
1434 const int kCSSDefaultDPI = 96; 1127 const int kCSSDefaultDPI = 96;
1435 const float scale = GetDPI() / kCSSDefaultDPI; 1128 const float scale = GetDPI() / kCSSDefaultDPI;
1436 1129
1437 // Blacklist scaling factors <130% (crbug.com/484400) and round 1130 // Blacklist scaling factors <130% (crbug.com/484400) and round
1438 // to 1 decimal to prevent rendering problems (crbug.com/485183). 1131 // to 1 decimal to prevent rendering problems (crbug.com/485183).
1439 return scale < 1.3f ? 1.0f : roundf(scale * 10) / 10; 1132 return scale < 1.3f ? 1.0f : roundf(scale * 10) / 10;
1440 } 1133 }
1441 1134
1442 } // namespace libgtk2ui 1135 } // namespace libgtk2ui
1443 1136
1444 views::LinuxUI* BuildGtk2UI() { 1137 views::LinuxUI* BuildGtk2UI() {
1445 return new libgtk2ui::Gtk2UI; 1138 return new libgtk2ui::Gtk2UI;
1446 } 1139 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/libgtk2ui/gtk2_ui.h ('k') | ui/views/linux_ui/linux_ui.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698