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
ApplyBackgroundTo(CefRefPtr<CefView> view)52 void ApplyBackgroundTo(CefRefPtr<CefView> view) {
53 if (!IsSet())
54 return;
55
56 view->SetBackgroundColor(g_background_color);
57 }
58
ApplyTo(CefRefPtr<CefPanel> panel)59 void ApplyTo(CefRefPtr<CefPanel> panel) {
60 if (!IsSet())
61 return;
62
63 panel->SetBackgroundColor(g_background_color);
64 }
65
ApplyTo(CefRefPtr<CefLabelButton> label_button)66 void ApplyTo(CefRefPtr<CefLabelButton> label_button) {
67 if (!IsSet())
68 return;
69
70 // All text except disabled gets the same color.
71 label_button->SetEnabledTextColors(g_text_color);
72 label_button->SetTextColor(CEF_BUTTON_STATE_DISABLED,
73 g_background_hover_color);
74
75 label_button->SetBackgroundColor(g_background_color);
76 }
77
ApplyTo(CefRefPtr<CefTextfield> textfield)78 void ApplyTo(CefRefPtr<CefTextfield> textfield) {
79 if (!IsSet())
80 return;
81
82 textfield->SetBackgroundColor(g_background_color);
83 textfield->SetTextColor(g_text_color);
84 }
85
ApplyTo(CefRefPtr<CefMenuModel> menu_model)86 void ApplyTo(CefRefPtr<CefMenuModel> menu_model) {
87 if (!IsSet())
88 return;
89
90 // All text except non-hovered accelerator gets the same color.
91 menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT, g_text_color);
92 menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_HOVERED, g_text_color);
93 menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_ACCELERATOR,
94 g_background_hover_color);
95 menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_ACCELERATOR_HOVERED,
96 g_text_color);
97
98 menu_model->SetColorAt(-1, CEF_MENU_COLOR_BACKGROUND, g_background_color);
99 menu_model->SetColorAt(-1, CEF_MENU_COLOR_BACKGROUND_HOVERED,
100 g_background_hover_color);
101
102 // Recursively color sub-menus.
103 for (int i = 0; i < menu_model->GetCount(); ++i) {
104 if (menu_model->GetTypeAt(i) == MENUITEMTYPE_SUBMENU)
105 ApplyTo(menu_model->GetSubMenuAt(i));
106 }
107 }
108
109 } // namespace views_style
110
111 } // namespace client
112