OLD | NEW |
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 void OnViewIsDeleting(View* view) override { observer_.Remove(view); } |
| 61 |
| 62 private: |
| 63 ScopedObserver<View, ViewObserver> observer_; |
| 64 ui::NativeTheme::ColorId color_id_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(ThemedSolidBackground); |
| 67 }; |
| 68 |
39 class BackgroundPainter : public Background { | 69 class BackgroundPainter : public Background { |
40 public: | 70 public: |
41 explicit BackgroundPainter(std::unique_ptr<Painter> painter) | 71 explicit BackgroundPainter(std::unique_ptr<Painter> painter) |
42 : painter_(std::move(painter)) { | 72 : painter_(std::move(painter)) { |
43 DCHECK(painter_); | 73 DCHECK(painter_); |
44 } | 74 } |
45 | 75 |
46 ~BackgroundPainter() override {} | 76 ~BackgroundPainter() override {} |
47 | 77 |
48 void Paint(gfx::Canvas* canvas, View* view) const override { | 78 void Paint(gfx::Canvas* canvas, View* view) const override { |
(...skipping 17 matching lines...) Expand all Loading... |
66 void Background::SetNativeControlColor(SkColor color) { | 96 void Background::SetNativeControlColor(SkColor color) { |
67 color_ = color; | 97 color_ = color; |
68 } | 98 } |
69 | 99 |
70 // static | 100 // static |
71 Background* Background::CreateSolidBackground(SkColor color) { | 101 Background* Background::CreateSolidBackground(SkColor color) { |
72 return new SolidBackground(color); | 102 return new SolidBackground(color); |
73 } | 103 } |
74 | 104 |
75 // static | 105 // static |
| 106 Background* Background::CreateThemedSolidBackground( |
| 107 View* view, |
| 108 ui::NativeTheme::ColorId color_id) { |
| 109 return new ThemedSolidBackground(view, color_id); |
| 110 } |
| 111 |
| 112 // static |
76 Background* Background::CreateStandardPanelBackground() { | 113 Background* Background::CreateStandardPanelBackground() { |
77 // TODO(beng): Should be in NativeTheme. | 114 // TODO(beng): Should be in NativeTheme. |
78 return CreateSolidBackground(SK_ColorWHITE); | 115 return CreateSolidBackground(SK_ColorWHITE); |
79 } | 116 } |
80 | 117 |
81 // static | 118 // static |
82 Background* Background::CreateBackgroundPainter( | 119 Background* Background::CreateBackgroundPainter( |
83 std::unique_ptr<Painter> painter) { | 120 std::unique_ptr<Painter> painter) { |
84 return new BackgroundPainter(std::move(painter)); | 121 return new BackgroundPainter(std::move(painter)); |
85 } | 122 } |
86 | 123 |
87 } // namespace views | 124 } // namespace views |
OLD | NEW |