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" | |
10 #include "build/build_config.h" | 9 #include "build/build_config.h" |
11 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
12 #include "ui/gfx/color_palette.h" | |
13 #include "ui/gfx/color_utils.h" | 11 #include "ui/gfx/color_utils.h" |
14 #include "ui/views/painter.h" | 12 #include "ui/views/painter.h" |
15 #include "ui/views/view.h" | 13 #include "ui/views/view.h" |
16 #include "ui/views/view_observer.h" | |
17 | 14 |
18 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
19 #include "skia/ext/skia_utils_win.h" | 16 #include "skia/ext/skia_utils_win.h" |
20 #endif | 17 #endif |
21 | 18 |
22 namespace views { | 19 namespace views { |
23 | 20 |
24 // SolidBackground is a trivial Background implementation that fills the | 21 // SolidBackground is a trivial Background implementation that fills the |
25 // background in a solid color. | 22 // background in a solid color. |
26 class SolidBackground : public Background { | 23 class SolidBackground : public Background { |
27 public: | 24 public: |
28 explicit SolidBackground(SkColor color) { | 25 explicit SolidBackground(SkColor color) { |
29 SetNativeControlColor(color); | 26 SetNativeControlColor(color); |
30 } | 27 } |
31 | 28 |
32 void Paint(gfx::Canvas* canvas, View* view) const override { | 29 void Paint(gfx::Canvas* canvas, View* view) const override { |
33 // Fill the background. Note that we don't constrain to the bounds as | 30 // Fill the background. Note that we don't constrain to the bounds as |
34 // canvas is already clipped for us. | 31 // canvas is already clipped for us. |
35 canvas->DrawColor(get_color()); | 32 canvas->DrawColor(get_color()); |
36 } | 33 } |
37 | 34 |
38 private: | 35 private: |
39 DISALLOW_COPY_AND_ASSIGN(SolidBackground); | 36 DISALLOW_COPY_AND_ASSIGN(SolidBackground); |
40 }; | 37 }; |
41 | 38 |
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 | |
68 class BackgroundPainter : public Background { | 39 class BackgroundPainter : public Background { |
69 public: | 40 public: |
70 explicit BackgroundPainter(std::unique_ptr<Painter> painter) | 41 explicit BackgroundPainter(std::unique_ptr<Painter> painter) |
71 : painter_(std::move(painter)) { | 42 : painter_(std::move(painter)) { |
72 DCHECK(painter_); | 43 DCHECK(painter_); |
73 } | 44 } |
74 | 45 |
75 ~BackgroundPainter() override {} | 46 ~BackgroundPainter() override {} |
76 | 47 |
77 void Paint(gfx::Canvas* canvas, View* view) const override { | 48 void Paint(gfx::Canvas* canvas, View* view) const override { |
(...skipping 17 matching lines...) Expand all Loading... |
95 void Background::SetNativeControlColor(SkColor color) { | 66 void Background::SetNativeControlColor(SkColor color) { |
96 color_ = color; | 67 color_ = color; |
97 } | 68 } |
98 | 69 |
99 // static | 70 // static |
100 Background* Background::CreateSolidBackground(SkColor color) { | 71 Background* Background::CreateSolidBackground(SkColor color) { |
101 return new SolidBackground(color); | 72 return new SolidBackground(color); |
102 } | 73 } |
103 | 74 |
104 // static | 75 // 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 | |
112 Background* Background::CreateStandardPanelBackground() { | 76 Background* Background::CreateStandardPanelBackground() { |
113 // TODO(beng): Should be in NativeTheme. | 77 // TODO(beng): Should be in NativeTheme. |
114 return CreateSolidBackground(SK_ColorWHITE); | 78 return CreateSolidBackground(SK_ColorWHITE); |
115 } | 79 } |
116 | 80 |
117 // static | 81 // static |
118 Background* Background::CreateBackgroundPainter( | 82 Background* Background::CreateBackgroundPainter( |
119 std::unique_ptr<Painter> painter) { | 83 std::unique_ptr<Painter> painter) { |
120 return new BackgroundPainter(std::move(painter)); | 84 return new BackgroundPainter(std::move(painter)); |
121 } | 85 } |
122 | 86 |
123 } // namespace views | 87 } // namespace views |
OLD | NEW |