Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(607)

Side by Side Diff: ui/views/background.cc

Issue 2816193002: Introduce a type of View background that stays in sync with its host (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/background.h ('k') | ui/views/bubble/tray_bubble_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/views/background.h" 5 #include "ui/views/background.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/scoped_observer.h"
9 #include "build/build_config.h" 10 #include "build/build_config.h"
10 #include "ui/gfx/canvas.h" 11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/color_palette.h"
11 #include "ui/gfx/color_utils.h" 13 #include "ui/gfx/color_utils.h"
12 #include "ui/views/painter.h" 14 #include "ui/views/painter.h"
13 #include "ui/views/view.h" 15 #include "ui/views/view.h"
16 #include "ui/views/view_observer.h"
14 17
15 #if defined(OS_WIN) 18 #if defined(OS_WIN)
16 #include "skia/ext/skia_utils_win.h" 19 #include "skia/ext/skia_utils_win.h"
17 #endif 20 #endif
18 21
19 namespace views { 22 namespace views {
20 23
21 // SolidBackground is a trivial Background implementation that fills the 24 // SolidBackground is a trivial Background implementation that fills the
22 // background in a solid color. 25 // background in a solid color.
23 class SolidBackground : public Background { 26 class SolidBackground : public Background {
24 public: 27 public:
25 explicit SolidBackground(SkColor color) { 28 explicit SolidBackground(SkColor color) {
26 SetNativeControlColor(color); 29 SetNativeControlColor(color);
27 } 30 }
28 31
29 void Paint(gfx::Canvas* canvas, View* view) const override { 32 void Paint(gfx::Canvas* canvas, View* view) const override {
30 // Fill the background. Note that we don't constrain to the bounds as 33 // Fill the background. Note that we don't constrain to the bounds as
31 // canvas is already clipped for us. 34 // canvas is already clipped for us.
32 canvas->DrawColor(get_color()); 35 canvas->DrawColor(get_color());
33 } 36 }
34 37
35 private: 38 private:
36 DISALLOW_COPY_AND_ASSIGN(SolidBackground); 39 DISALLOW_COPY_AND_ASSIGN(SolidBackground);
37 }; 40 };
38 41
42 // ThemedSolidBackground is a solid background that stays in sync with a view's
43 // native theme.
44 class ThemedSolidBackground : public SolidBackground, public ViewObserver {
45 public:
46 explicit ThemedSolidBackground(View* view, ui::NativeTheme::ColorId color_id)
47 : SolidBackground(gfx::kPlaceholderColor),
48 observer_(this),
49 color_id_(color_id) {
50 observer_.Add(view);
51 OnViewNativeThemeChanged(view);
52 }
53 ~ThemedSolidBackground() override {}
54
55 // ViewObserver:
56 void OnViewNativeThemeChanged(View* view) override {
57 SetNativeControlColor(view->GetNativeTheme()->GetSystemColor(color_id_));
58 view->SchedulePaint();
59 }
60
61 private:
62 ScopedObserver<View, ViewObserver> observer_;
63 ui::NativeTheme::ColorId color_id_;
64
65 DISALLOW_COPY_AND_ASSIGN(ThemedSolidBackground);
66 };
67
39 class BackgroundPainter : public Background { 68 class BackgroundPainter : public Background {
40 public: 69 public:
41 explicit BackgroundPainter(std::unique_ptr<Painter> painter) 70 explicit BackgroundPainter(std::unique_ptr<Painter> painter)
42 : painter_(std::move(painter)) { 71 : painter_(std::move(painter)) {
43 DCHECK(painter_); 72 DCHECK(painter_);
44 } 73 }
45 74
46 ~BackgroundPainter() override {} 75 ~BackgroundPainter() override {}
47 76
48 void Paint(gfx::Canvas* canvas, View* view) const override { 77 void Paint(gfx::Canvas* canvas, View* view) const override {
(...skipping 17 matching lines...) Expand all
66 void Background::SetNativeControlColor(SkColor color) { 95 void Background::SetNativeControlColor(SkColor color) {
67 color_ = color; 96 color_ = color;
68 } 97 }
69 98
70 // static 99 // static
71 Background* Background::CreateSolidBackground(SkColor color) { 100 Background* Background::CreateSolidBackground(SkColor color) {
72 return new SolidBackground(color); 101 return new SolidBackground(color);
73 } 102 }
74 103
75 // static 104 // static
105 Background* Background::CreateThemedSolidBackground(
106 View* view,
107 ui::NativeTheme::ColorId color_id) {
108 return new ThemedSolidBackground(view, color_id);
109 }
110
111 // static
76 Background* Background::CreateStandardPanelBackground() { 112 Background* Background::CreateStandardPanelBackground() {
77 // TODO(beng): Should be in NativeTheme. 113 // TODO(beng): Should be in NativeTheme.
78 return CreateSolidBackground(SK_ColorWHITE); 114 return CreateSolidBackground(SK_ColorWHITE);
79 } 115 }
80 116
81 // static 117 // static
82 Background* Background::CreateBackgroundPainter( 118 Background* Background::CreateBackgroundPainter(
83 std::unique_ptr<Painter> painter) { 119 std::unique_ptr<Painter> painter) {
84 return new BackgroundPainter(std::move(painter)); 120 return new BackgroundPainter(std::move(painter));
85 } 121 }
86 122
87 } // namespace views 123 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/background.h ('k') | ui/views/bubble/tray_bubble_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698