Commit 5729f982 authored by danakj's avatar danakj Committed by Commit bot

tabstrip: Use a PaintRecorder to access the Canvas for painting.

Don't use the canvas() from the PaintContext so that we can have the
PaintContext be backed by a DisplayItemList instead in the future.
Access to the Canvas should be done through a PaintRecorder.

For compositing effects (opacity) we use a CompositingRecorder to
provide a scoped opacity change.

R=pkasting
BUG=466426

Review URL: https://codereview.chromium.org/1062293003

Cr-Commit-Position: refs/heads/master@{#324173}
parent 5cc4fb59
......@@ -463,10 +463,8 @@ CardUnmaskPromptViews::FadeOutView::~FadeOutView() {
void CardUnmaskPromptViews::FadeOutView::PaintChildren(
const ui::PaintContext& context) {
if (opacity_ > 0.99)
return views::View::PaintChildren(context);
ui::CompositingRecorder recorder(context, opacity_);
uint8_t alpha = static_cast<uint8_t>(255 * opacity_);
ui::CompositingRecorder recorder(context, alpha);
views::View::PaintChildren(context);
}
......
......@@ -38,7 +38,8 @@
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/list_selection_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/paint_context.h"
#include "ui/compositor/compositing_recorder.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/gfx/animation/animation_container.h"
#include "ui/gfx/animation/throb_animation.h"
#include "ui/gfx/canvas.h"
......@@ -74,14 +75,14 @@ namespace {
static const int kTabStripAnimationVSlop = 40;
// Inactive tabs in a native frame are slightly transparent.
static const int kGlassFrameInactiveTabAlpha = 200;
static const uint8_t kGlassFrameInactiveTabAlpha = 200;
// If there are multiple tabs selected then make non-selected inactive tabs
// even more transparent.
static const int kGlassFrameInactiveTabAlphaMultiSelection = 150;
// Alpha applied to all elements save the selected tabs.
static const int kInactiveTabAndNewTabButtonAlphaAsh = 230;
static const int kInactiveTabAndNewTabButtonAlpha = 255;
static const uint8_t kInactiveTabAndNewTabButtonAlphaAsh = 230;
static const uint8_t kInactiveTabAndNewTabButtonAlpha = 255;
// Inverse ratio of the width of a tab edge to the width of the tab. When
// hovering over the left or right edge of a tab, the drop indicator will
......@@ -458,10 +459,11 @@ gfx::ImageSkia NewTabButton::GetImageForState(
canvas.DrawImageInt(GetBackgroundImage(state, scale), 0, 0);
// Draw the button border with a slight alpha.
const int kGlassFrameOverlayAlpha = 178;
const int kOpaqueFrameOverlayAlpha = 230;
uint8 alpha = ShouldWindowContentsBeTransparent() ?
kGlassFrameOverlayAlpha : kOpaqueFrameOverlayAlpha;
const uint8_t kGlassFrameOverlayAlpha = 178;
const uint8_t kOpaqueFrameOverlayAlpha = 230;
uint8_t alpha = ShouldWindowContentsBeTransparent()
? kGlassFrameOverlayAlpha
: kOpaqueFrameOverlayAlpha;
canvas.DrawImageInt(*overlay, 0, 0, alpha);
return gfx::ImageSkia(canvas.ExtractImageRep());
......@@ -1242,7 +1244,6 @@ void TabStrip::Layout() {
}
void TabStrip::PaintChildren(const ui::PaintContext& context) {
gfx::Canvas* canvas = context.canvas();
// The view order doesn't match the paint order (tabs_ contains the tab
// ordering). Additionally we need to paint the tabs that are closing in
// |tabs_closing_map_|.
......@@ -1255,64 +1256,65 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) {
const chrome::HostDesktopType host_desktop_type =
chrome::GetHostDesktopTypeForNativeView(GetWidget()->GetNativeView());
const int inactive_tab_alpha =
(host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH) ?
kInactiveTabAndNewTabButtonAlphaAsh : kInactiveTabAndNewTabButtonAlpha;
const uint8_t inactive_tab_alpha =
(host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH)
? kInactiveTabAndNewTabButtonAlphaAsh
: kInactiveTabAndNewTabButtonAlpha;
if (inactive_tab_alpha < 255)
canvas->SaveLayerAlpha(inactive_tab_alpha);
{
ui::CompositingRecorder opacity_recorder(context, inactive_tab_alpha);
PaintClosingTabs(tab_count(), context);
PaintClosingTabs(tab_count(), context);
for (int i = tab_count() - 1; i >= 0; --i) {
Tab* tab = tab_at(i);
if (tab->IsSelected())
selected_tab_count++;
if (tab->dragging() && !stacked_layout_) {
is_dragging = true;
if (tab->IsActive()) {
for (int i = tab_count() - 1; i >= 0; --i) {
Tab* tab = tab_at(i);
if (tab->IsSelected())
selected_tab_count++;
if (tab->dragging() && !stacked_layout_) {
is_dragging = true;
if (tab->IsActive()) {
active_tab = tab;
active_tab_index = i;
} else {
tabs_dragging.push_back(tab);
}
} else if (!tab->IsActive()) {
if (!tab->IsSelected()) {
if (!stacked_layout_)
tab->Paint(context);
} else {
selected_tabs.push_back(tab);
}
} else {
active_tab = tab;
active_tab_index = i;
} else {
tabs_dragging.push_back(tab);
}
} else if (!tab->IsActive()) {
if (!tab->IsSelected()) {
if (!stacked_layout_)
tab->Paint(context);
} else {
selected_tabs.push_back(tab);
}
} else {
active_tab = tab;
active_tab_index = i;
PaintClosingTabs(i, context);
}
PaintClosingTabs(i, context);
}
// Draw from the left and then the right if we're in touch mode.
if (stacked_layout_ && active_tab_index >= 0) {
for (int i = 0; i < active_tab_index; ++i) {
Tab* tab = tab_at(i);
tab->Paint(context);
}
// Draw from the left and then the right if we're in touch mode.
if (stacked_layout_ && active_tab_index >= 0) {
for (int i = 0; i < active_tab_index; ++i) {
Tab* tab = tab_at(i);
tab->Paint(context);
}
for (int i = tab_count() - 1; i > active_tab_index; --i) {
Tab* tab = tab_at(i);
tab->Paint(context);
for (int i = tab_count() - 1; i > active_tab_index; --i) {
Tab* tab = tab_at(i);
tab->Paint(context);
}
}
}
if (inactive_tab_alpha < 255)
canvas->Restore();
if (GetWidget()->ShouldWindowContentsBeTransparent()) {
ui::PaintRecorder recorder(context);
// Make sure non-active tabs are somewhat transparent.
SkPaint paint;
// If there are multiple tabs selected, fade non-selected tabs more to make
// the selected tabs more noticable.
int alpha = selected_tab_count > 1 ?
kGlassFrameInactiveTabAlphaMultiSelection :
kGlassFrameInactiveTabAlpha;
uint8_t alpha = selected_tab_count > 1
? kGlassFrameInactiveTabAlphaMultiSelection
: kGlassFrameInactiveTabAlpha;
paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
paint.setStyle(SkPaint::kFill_Style);
......@@ -1324,7 +1326,7 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) {
// overlap the avatar button, leading to visual artifacts.
const int kTopOffset = 4;
// The tabstrip area overlaps the toolbar area by 2 px.
canvas->DrawRect(
recorder.canvas()->DrawRect(
gfx::Rect(0, kTopOffset, width(), height() - kTopOffset - 2), paint);
}
......@@ -1338,11 +1340,10 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) {
active_tab->Paint(context);
// Paint the New Tab button.
if (inactive_tab_alpha < 255)
canvas->SaveLayerAlpha(inactive_tab_alpha);
newtab_button_->Paint(context);
if (inactive_tab_alpha < 255)
canvas->Restore();
{
ui::CompositingRecorder opacity_recorder(context, inactive_tab_alpha);
newtab_button_->Paint(context);
}
// And the dragged tabs.
for (size_t i = 0; i < tabs_dragging.size(); ++i)
......
......@@ -10,13 +10,15 @@
namespace ui {
CompositingRecorder::CompositingRecorder(const PaintContext& context,
float opacity)
: canvas_(context.canvas()) {
canvas_->SaveLayerAlpha(0xff * opacity);
uint8_t alpha)
: canvas_(context.canvas()), saved_(alpha < 255) {
if (saved_)
canvas_->SaveLayerAlpha(alpha);
}
CompositingRecorder::~CompositingRecorder() {
canvas_->Restore();
if (saved_)
canvas_->Restore();
}
} // namespace ui
......@@ -5,6 +5,7 @@
#ifndef UI_COMPOSITOR_COMPOSITING_RECORDER_H_
#define UI_COMPOSITOR_COMPOSITING_RECORDER_H_
#include "base/basictypes.h"
#include "base/macros.h"
#include "ui/compositor/compositor_export.h"
......@@ -22,11 +23,14 @@ class PaintContext;
// be filtered by the effect.
class COMPOSITOR_EXPORT CompositingRecorder {
public:
CompositingRecorder(const PaintContext& context, float opacity);
// |alpha| is a value between 0 and 255, where 0 is transparent and 255 is
// opaque.
CompositingRecorder(const PaintContext& context, uint8_t alpha);
~CompositingRecorder();
private:
gfx::Canvas* canvas_;
bool saved_;
DISALLOW_COPY_AND_ASSIGN(CompositingRecorder);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment