| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_gtk3.h" | 5 #include "chrome/browser/ui/libgtkui/native_theme_gtk3.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_util.h" | 11 #include "chrome/browser/ui/libgtkui/gtk_util.h" |
| 12 #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h" | 12 #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h" |
| 13 #include "ui/gfx/color_palette.h" | 13 #include "ui/gfx/color_palette.h" |
| 14 #include "ui/gfx/color_utils.h" | 14 #include "ui/gfx/color_utils.h" |
| 15 #include "ui/gfx/geometry/rect.h" | 15 #include "ui/gfx/geometry/rect.h" |
| 16 #include "ui/gfx/skbitmap_operations.h" |
| 16 #include "ui/native_theme/native_theme_dark_aura.h" | 17 #include "ui/native_theme/native_theme_dark_aura.h" |
| 17 | 18 |
| 18 namespace libgtkui { | 19 namespace libgtkui { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 void PaintWidget(SkCanvas* canvas, | 23 SkBitmap GetWidgetBitmap(const gfx::Size& size, |
| 23 const gfx::Rect& rect, | 24 GtkStyleContext* context) { |
| 24 const char* css_selector, | |
| 25 GtkStateFlags state) { | |
| 26 SkBitmap bitmap; | 25 SkBitmap bitmap; |
| 27 bitmap.allocN32Pixels(rect.width(), rect.height()); | 26 bitmap.allocN32Pixels(size.width(), size.height()); |
| 28 bitmap.eraseColor(0); | 27 bitmap.eraseColor(0); |
| 29 | 28 |
| 30 cairo_surface_t* surface = cairo_image_surface_create_for_data( | 29 cairo_surface_t* surface = cairo_image_surface_create_for_data( |
| 31 static_cast<unsigned char*>(bitmap.getAddr(0, 0)), CAIRO_FORMAT_ARGB32, | 30 static_cast<unsigned char*>(bitmap.getAddr(0, 0)), CAIRO_FORMAT_ARGB32, |
| 32 rect.width(), rect.height(), | 31 size.width(), size.height(), |
| 33 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, rect.width())); | 32 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, size.width())); |
| 34 cairo_t* cr = cairo_create(surface); | 33 cairo_t* cr = cairo_create(surface); |
| 35 | 34 |
| 36 auto context = GetStyleContextFromCss(css_selector); | 35 RenderBackground(size, cr, context); |
| 37 gtk_style_context_set_state(context, state); | 36 gtk_render_frame(context, cr, 0, 0, size.width(), size.height()); |
| 38 | |
| 39 gtk_render_background(context, cr, 0, 0, rect.width(), rect.height()); | |
| 40 gtk_render_frame(context, cr, 0, 0, rect.width(), rect.height()); | |
| 41 cairo_destroy(cr); | 37 cairo_destroy(cr); |
| 42 cairo_surface_destroy(surface); | 38 cairo_surface_destroy(surface); |
| 43 canvas->drawBitmap(bitmap, rect.x(), rect.y()); | 39 return bitmap; |
| 40 } |
| 41 |
| 42 void PaintWidget(SkCanvas* canvas, |
| 43 const gfx::Rect& rect, |
| 44 GtkStyleContext* context) { |
| 45 canvas->drawBitmap(GetWidgetBitmap(rect.size(), context), rect.x(), rect.y()); |
| 44 } | 46 } |
| 45 | 47 |
| 46 GtkStateFlags StateToStateFlags(NativeThemeGtk3::State state) { | 48 GtkStateFlags StateToStateFlags(NativeThemeGtk3::State state) { |
| 47 switch (state) { | 49 switch (state) { |
| 48 case NativeThemeGtk3::kDisabled: | 50 case NativeThemeGtk3::kDisabled: |
| 49 return GTK_STATE_FLAG_INSENSITIVE; | 51 return GTK_STATE_FLAG_INSENSITIVE; |
| 50 case NativeThemeGtk3::kHovered: | 52 case NativeThemeGtk3::kHovered: |
| 51 return GTK_STATE_FLAG_PRELIGHT; | 53 return GTK_STATE_FLAG_PRELIGHT; |
| 52 case NativeThemeGtk3::kNormal: | 54 case NativeThemeGtk3::kNormal: |
| 53 return GTK_STATE_FLAG_NORMAL; | 55 return GTK_STATE_FLAG_NORMAL; |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 g_type_class_unref(g_type_class_ref(gtk_link_button_get_type())); | 307 g_type_class_unref(g_type_class_ref(gtk_link_button_get_type())); |
| 306 g_type_class_unref(g_type_class_ref(gtk_spinner_get_type())); | 308 g_type_class_unref(g_type_class_ref(gtk_spinner_get_type())); |
| 307 g_type_class_unref(g_type_class_ref(gtk_menu_get_type())); | 309 g_type_class_unref(g_type_class_ref(gtk_menu_get_type())); |
| 308 g_type_class_unref(g_type_class_ref(gtk_menu_item_get_type())); | 310 g_type_class_unref(g_type_class_ref(gtk_menu_item_get_type())); |
| 309 g_type_class_unref(g_type_class_ref(gtk_entry_get_type())); | 311 g_type_class_unref(g_type_class_ref(gtk_entry_get_type())); |
| 310 g_type_class_unref(g_type_class_ref(gtk_info_bar_get_type())); | 312 g_type_class_unref(g_type_class_ref(gtk_info_bar_get_type())); |
| 311 g_type_class_unref(g_type_class_ref(gtk_tooltip_get_type())); | 313 g_type_class_unref(g_type_class_ref(gtk_tooltip_get_type())); |
| 312 g_type_class_unref(g_type_class_ref(gtk_scrollbar_get_type())); | 314 g_type_class_unref(g_type_class_ref(gtk_scrollbar_get_type())); |
| 313 g_type_class_unref(g_type_class_ref(gtk_toolbar_get_type())); | 315 g_type_class_unref(g_type_class_ref(gtk_toolbar_get_type())); |
| 314 g_type_class_unref(g_type_class_ref(gtk_text_view_get_type())); | 316 g_type_class_unref(g_type_class_ref(gtk_text_view_get_type())); |
| 317 g_type_class_unref(g_type_class_ref(gtk_menu_bar_get_type())); |
| 315 | 318 |
| 316 g_signal_connect_after(gtk_settings_get_default(), "notify::gtk-theme-name", | 319 g_signal_connect_after(gtk_settings_get_default(), "notify::gtk-theme-name", |
| 317 G_CALLBACK(OnThemeChanged), this); | 320 G_CALLBACK(OnThemeChanged), this); |
| 318 } | 321 } |
| 319 | 322 |
| 320 // This doesn't actually get called | 323 // This doesn't actually get called |
| 321 NativeThemeGtk3::~NativeThemeGtk3() {} | 324 NativeThemeGtk3::~NativeThemeGtk3() {} |
| 322 | 325 |
| 323 void NativeThemeGtk3::ResetColorCache() { | 326 void NativeThemeGtk3::ResetColorCache() { |
| 324 for (auto& color : color_cache_) | 327 for (auto& color : color_cache_) |
| 325 color = base::nullopt; | 328 color = base::nullopt; |
| 326 } | 329 } |
| 327 | 330 |
| 328 SkColor NativeThemeGtk3::GetSystemColor(ColorId color_id) const { | 331 SkColor NativeThemeGtk3::GetSystemColor(ColorId color_id) const { |
| 329 if (color_cache_[color_id]) | 332 if (color_cache_[color_id]) |
| 330 return color_cache_[color_id].value(); | 333 return color_cache_[color_id].value(); |
| 331 | 334 |
| 332 SkColor color = SkColorFromColorId(color_id); | 335 SkColor color = SkColorFromColorId(color_id); |
| 333 color_cache_[color_id] = color; | 336 color_cache_[color_id] = color; |
| 334 return color; | 337 return color; |
| 335 } | 338 } |
| 336 | 339 |
| 337 void NativeThemeGtk3::PaintMenuPopupBackground( | 340 void NativeThemeGtk3::PaintMenuPopupBackground( |
| 338 SkCanvas* canvas, | 341 SkCanvas* canvas, |
| 339 const gfx::Size& size, | 342 const gfx::Size& size, |
| 340 const MenuBackgroundExtraParams& menu_background) const { | 343 const MenuBackgroundExtraParams& menu_background) const { |
| 341 PaintWidget(canvas, gfx::Rect(size), "GtkMenu#menu", GTK_STATE_FLAG_NORMAL); | 344 PaintWidget(canvas, gfx::Rect(size), GetStyleContextFromCss("GtkMenu#menu")); |
| 342 } | 345 } |
| 343 | 346 |
| 344 void NativeThemeGtk3::PaintMenuItemBackground( | 347 void NativeThemeGtk3::PaintMenuItemBackground( |
| 345 SkCanvas* canvas, | 348 SkCanvas* canvas, |
| 346 State state, | 349 State state, |
| 347 const gfx::Rect& rect, | 350 const gfx::Rect& rect, |
| 348 const MenuItemExtraParams& menu_item) const { | 351 const MenuItemExtraParams& menu_item) const { |
| 349 PaintWidget(canvas, rect, "GtkMenu#menu GtkMenuItem#menuitem", | 352 auto context = GetStyleContextFromCss("GtkMenu#menu GtkMenuItem#menuitem"); |
| 350 StateToStateFlags(state)); | 353 gtk_style_context_set_state(context, StateToStateFlags(state)); |
| 354 PaintWidget(canvas, rect, context); |
| 355 } |
| 356 |
| 357 void NativeThemeGtk3::PaintFrameTopArea( |
| 358 SkCanvas* canvas, |
| 359 State state, |
| 360 const gfx::Rect& rect, |
| 361 const FrameTopAreaExtraParams& frame_top_area) const { |
| 362 auto context = GetStyleContextFromCss(frame_top_area.use_custom_frame |
| 363 ? "#headerbar.header-bar.titlebar" |
| 364 : "GtkMenuBar#menubar"); |
| 365 RemoveBorders(context); |
| 366 gtk_style_context_set_state(context, |
| 367 frame_top_area.is_active |
| 368 ? GTK_STATE_FLAG_NORMAL |
| 369 : GTK_STATE_FLAG_BACKDROP); |
| 370 |
| 371 SkBitmap bitmap = GetWidgetBitmap(rect.size(), context); |
| 372 |
| 373 if (frame_top_area.incognito) { |
| 374 bitmap = SkBitmapOperations::CreateHSLShiftedBitmap( |
| 375 bitmap, kDefaultTintFrameIncognito); |
| 376 } |
| 377 |
| 378 canvas->drawBitmap(bitmap, rect.x(), rect.y()); |
| 351 } | 379 } |
| 352 | 380 |
| 353 } // namespace libgtkui | 381 } // namespace libgtkui |
| OLD | NEW |