• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "tests/cefclient/browser/views_style.h"
6 
7 #include "tests/cefclient/browser/main_context.h"
8 
9 namespace client {
10 
11 namespace views_style {
12 
13 namespace {
14 
15 cef_color_t g_background_color = 0;
16 cef_color_t g_background_hover_color = 0;
17 cef_color_t g_text_color = 0;
18 
GetShade(int component)19 int GetShade(int component) {
20   return (component < 127) ? component + 75 : component - 75;
21 }
22 
MaybeInitialize()23 void MaybeInitialize() {
24   static bool initialized = false;
25   if (initialized)
26     return;
27 
28   g_background_color = MainContext::Get()->GetBackgroundColor();
29   if (g_background_color != 0) {
30     // Use a slightly modified shade of the background color for hover.
31     g_background_hover_color =
32         CefColorSetARGB(255, GetShade(CefColorGetR(g_background_color)),
33                         GetShade(CefColorGetG(g_background_color)),
34                         GetShade(CefColorGetB(g_background_color)));
35 
36     // Invert the background color for text.
37     g_text_color = CefColorSetARGB(255, 255 - CefColorGetR(g_background_color),
38                                    255 - CefColorGetG(g_background_color),
39                                    255 - CefColorGetB(g_background_color));
40   }
41 
42   initialized = true;
43 }
44 
45 }  // namespace
46 
IsSet()47 bool IsSet() {
48   MaybeInitialize();
49   return g_background_color != 0;
50 }
51 
ApplyTo(CefRefPtr<CefPanel> panel)52 void ApplyTo(CefRefPtr<CefPanel> panel) {
53   if (!IsSet())
54     return;
55 
56   panel->SetBackgroundColor(g_background_color);
57 }
58 
ApplyTo(CefRefPtr<CefLabelButton> label_button)59 void ApplyTo(CefRefPtr<CefLabelButton> label_button) {
60   if (!IsSet())
61     return;
62 
63   // All text except disabled gets the same color.
64   label_button->SetEnabledTextColors(g_text_color);
65   label_button->SetTextColor(CEF_BUTTON_STATE_DISABLED,
66                              g_background_hover_color);
67 
68   label_button->SetBackgroundColor(g_background_color);
69 }
70 
ApplyTo(CefRefPtr<CefTextfield> textfield)71 void ApplyTo(CefRefPtr<CefTextfield> textfield) {
72   if (!IsSet())
73     return;
74 
75   textfield->SetBackgroundColor(g_background_color);
76   textfield->SetTextColor(g_text_color);
77 }
78 
ApplyTo(CefRefPtr<CefMenuModel> menu_model)79 void ApplyTo(CefRefPtr<CefMenuModel> menu_model) {
80   if (!IsSet())
81     return;
82 
83   // All text except non-hovered accelerator gets the same color.
84   menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT, g_text_color);
85   menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_HOVERED, g_text_color);
86   menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_ACCELERATOR,
87                          g_background_hover_color);
88   menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_ACCELERATOR_HOVERED,
89                          g_text_color);
90 
91   menu_model->SetColorAt(-1, CEF_MENU_COLOR_BACKGROUND, g_background_color);
92   menu_model->SetColorAt(-1, CEF_MENU_COLOR_BACKGROUND_HOVERED,
93                          g_background_hover_color);
94 
95   // Recursively color sub-menus.
96   for (int i = 0; i < menu_model->GetCount(); ++i) {
97     if (menu_model->GetTypeAt(i) == MENUITEMTYPE_SUBMENU)
98       ApplyTo(menu_model->GetSubMenuAt(i));
99   }
100 }
101 
102 }  // namespace views_style
103 
104 }  // namespace client
105