-
-
Notifications
You must be signed in to change notification settings - Fork 631
Support transparent window in Views framework (follow up issue #2315) #4086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,14 +74,14 @@ void InitCrashReporter() { | |
|
|
||
| #endif // BUILDFLAG(IS_WIN) | ||
|
|
||
| bool GetColor(const cef_color_t cef_in, bool is_windowless, SkColor* sk_out) { | ||
| // Windowed browser colors must be fully opaque. | ||
| if (!is_windowless && CefColorGetA(cef_in) != SK_AlphaOPAQUE) { | ||
| bool GetColor(const cef_color_t cef_in, bool is_transparent, SkColor* sk_out) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |is_transparent| variable name is misleading. |supports_transparency| (or similar) would be more accurate. |
||
| // transparent unsupported browser colors must be fully opaque. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment would be clearer as: "Fully opaque colors are required for browsers that don't support transparency." Same below. |
||
| if (!is_transparent && CefColorGetA(cef_in) != SK_AlphaOPAQUE) { | ||
| return false; | ||
| } | ||
|
|
||
| // Windowless browser colors may be fully transparent. | ||
| if (is_windowless && CefColorGetA(cef_in) == SK_AlphaTRANSPARENT) { | ||
| // transparent supported browser colors may be fully transparent. | ||
| if (is_transparent && CefColorGetA(cef_in) == SK_AlphaTRANSPARENT) { | ||
| *sk_out = SK_ColorTRANSPARENT; | ||
| return true; | ||
| } | ||
|
|
@@ -444,19 +444,19 @@ bool CefContext::OnInitThread() { | |
|
|
||
| SkColor CefContext::GetBackgroundColor( | ||
| const CefBrowserSettings* browser_settings, | ||
| cef_state_t windowless_state) const { | ||
| bool is_windowless = windowless_state == STATE_ENABLED | ||
| cef_state_t transparent_state) const { | ||
| bool is_transparent = transparent_state == STATE_ENABLED | ||
| ? true | ||
| : (windowless_state == STATE_DISABLED | ||
| : (transparent_state == STATE_DISABLED | ||
| ? false | ||
| : !!settings_.windowless_rendering_enabled); | ||
|
|
||
| // Default to opaque white if no acceptable color values are found. | ||
| SkColor sk_color = SK_ColorWHITE; | ||
|
|
||
| if (!browser_settings || | ||
| !GetColor(browser_settings->background_color, is_windowless, &sk_color)) { | ||
| GetColor(settings_.background_color, is_windowless, &sk_color); | ||
| !GetColor(browser_settings->background_color, is_transparent, &sk_color)) { | ||
| GetColor(settings_.background_color, is_transparent, &sk_color); | ||
| } | ||
| return sk_color; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,6 +110,17 @@ MainContextImpl::MainContextImpl(CefRefPtr<CefCommandLine> command_line, | |
| use_views_ = true; | ||
| } | ||
|
|
||
| // Whether transparent painting is used with windowless rendering. | ||
| const bool use_transparent_painting = | ||
| (use_windowless_rendering_ || use_views_) && | ||
| command_line_->HasSwitch(switches::kTransparentPaintingEnabled); | ||
| if (use_views_ && use_transparent_painting && | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |use_transparent_painting| will be true for windowless rendering, but then you exclude windowless rendering in this conditional. |
||
| command_line->HasSwitch(switches::kHideFrame) && | ||
| !command_line_->HasSwitch(switches::kUrl)) { | ||
| // Use the draggable regions test as the default URL for frameless windows. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is incorrect. |
||
| main_url_ = "http://tests/transparent_views"; | ||
| } | ||
|
|
||
| if (command_line_->HasSwitch(switches::kBackgroundColor)) { | ||
| // Parse the background color value. | ||
| background_color_ = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <html> | ||
| <head><title>Translucent Window Test</title> | ||
| <style> | ||
| #radius-window { | ||
| border-radius: 30px; | ||
| height: 400px; | ||
| width: 600px; | ||
| background: rgba(98, 98, 115); | ||
| opacity: 0.8; | ||
| } | ||
| #title-bar { | ||
| user-select: none; | ||
| margin-left: 30px; | ||
| margin-right: 30px; | ||
| display: flex; | ||
| background: rgb(88, 88, 105); | ||
| } | ||
| .draggable { | ||
| flex-grow: 2; | ||
| -webkit-app-region: drag; | ||
| height: 20px; | ||
| } | ||
| .controls { | ||
| flex-grow: 0; | ||
| font: 18px bold "Sans-serif"; | ||
| color: red; | ||
| height: 20px; | ||
| width: 20px; | ||
| cursor: hand; | ||
| } | ||
| </style> | ||
| <script> | ||
| function changeAlpha(e) { | ||
| let w = document.getElementById("radius-window"); | ||
| let opacity = w.style.opacity === "" ? 0.8 : parseFloat(w.style.opacity); | ||
| if (!e.shiftKey) { | ||
| opacity += 0.1; | ||
| } else { | ||
| opacity -= 0.1; | ||
| } | ||
| if (opacity <= 1 && opacity >= 0) { | ||
| w.style.opacity = opacity; | ||
| } | ||
| } | ||
| </script> | ||
| </head> | ||
| <body> | ||
| <div id="radius-window" onmousedown="changeAlpha(event);"> | ||
| <div id="title-bar"> | ||
| <div class="draggable"></div> | ||
| <div class="controls" onclick="window.close()">X</div> | ||
| </div> | ||
| Transparent window using Views framework can be enabled by setting the | ||
| alpha component of the CefSettings.background_color to fully transparent | ||
| (0x00) and returning true from IsFrameless() in CefWindowDelegate(). | ||
| The transparent effect will propagate to | ||
| <a href="#" onClick="window.open(location.href, 'Popup'); return false;">popped up window</a>. | ||
| <br/>Window can be closed using JavaScript <a href="#" onClick="window.close(); return false;">window.close()</a>. | ||
| </p> | ||
| Click: decrease alpha component of the background.<br/> | ||
| Shift-Click: increase alpha component of the background. | ||
| </div> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,12 @@ class SimpleWindowDelegate : public CefWindowDelegate { | |
| browser_view_ = nullptr; | ||
| } | ||
|
|
||
| bool IsFrameless(CefRefPtr<CefWindow> window) override { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method appears unused. |
||
| CefRefPtr<CefCommandLine> command_line = | ||
| CefCommandLine::GetGlobalCommandLine(); | ||
| return command_line->HasSwitch("hide-frame"); | ||
| } | ||
|
|
||
| bool CanClose(CefRefPtr<CefWindow> window) override { | ||
| // Allow the window to close if the browser says it's OK. | ||
| CefRefPtr<CefBrowser> browser = browser_view_->GetBrowser(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -567,6 +567,19 @@ void WindowAcceleratorImpl(CefRefPtr<CefWaitableEvent> event) { | |
| TestWindowDelegate::RunTest(event, std::move(config)); | ||
| } | ||
|
|
||
| void VerifyWindowTransparentBackground(CefRefPtr<CefWindow> window) { | ||
| // The transparent background color value from CefSettings.background_color | ||
| // by set in CefTestSuite::GetSettings() to enable transparent window | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change appears to be missing? |
||
| // in Views framework. | ||
| EXPECT_EQ(window->GetBackgroundColor(), 0u); | ||
| } | ||
|
|
||
| void WindowCreateTranslucentImpl(CefRefPtr<CefWaitableEvent> event) { | ||
| auto config = std::make_unique<TestWindowDelegate::Config>(); | ||
| config->on_window_created = base::BindOnce(VerifyWindowTransparentBackground); | ||
| TestWindowDelegate::RunTest(event, std::move(config)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // Test window functionality. This is primarily to exercise exposed CEF APIs | ||
|
|
@@ -591,6 +604,7 @@ WINDOW_TEST_ASYNC(WindowFullscreenFrameless) | |
| WINDOW_TEST_ASYNC(WindowIcon) | ||
| WINDOW_TEST_ASYNC(WindowIconFrameless) | ||
| WINDOW_TEST_ASYNC(WindowAccelerator) | ||
| WINDOW_TEST_ASYNC(WindowCreateTranslucent) | ||
|
|
||
| namespace { | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't look like you're checking if the window is frameless before enabling transparency. Also, enabling transparency for Views windows by default would be a pretty significant breaking change -- we probably don't want to do that.