OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mash/wm/user_window_controller_impl.h" | 5 #include "mash/wm/user_window_controller_impl.h" |
6 | 6 |
7 #include "components/mus/public/cpp/property_type_converters.h" | 7 #include "components/mus/public/cpp/property_type_converters.h" |
8 #include "components/mus/public/cpp/window.h" | 8 #include "components/mus/public/cpp/window.h" |
9 #include "components/mus/public/cpp/window_property.h" | 9 #include "components/mus/public/cpp/window_property.h" |
10 #include "components/mus/public/cpp/window_tree_connection.h" | 10 #include "components/mus/public/cpp/window_tree_connection.h" |
11 #include "mash/wm/public/interfaces/container.mojom.h" | 11 #include "mash/wm/public/interfaces/container.mojom.h" |
12 #include "mash/wm/root_window_controller.h" | 12 #include "mash/wm/root_window_controller.h" |
13 #include "mojo/common/common_type_converters.h" | 13 #include "mojo/common/common_type_converters.h" |
14 #include "ui/resources/grit/ui_resources.h" | |
14 | 15 |
15 namespace mash { | 16 namespace mash { |
16 namespace wm { | 17 namespace wm { |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Get the title property from a mus::Window. | 20 // Get the title property from a mus::Window. |
20 mojo::String GetWindowTitle(mus::Window* window) { | 21 mojo::String GetWindowTitle(mus::Window* window) { |
21 if (window->HasSharedProperty( | 22 if (window->HasSharedProperty( |
22 mus::mojom::WindowManager::kWindowTitle_Property)) { | 23 mus::mojom::WindowManager::kWindowTitle_Property)) { |
23 return mojo::String::From(window->GetSharedProperty<base::string16>( | 24 return mojo::String::From(window->GetSharedProperty<base::string16>( |
24 mus::mojom::WindowManager::kWindowTitle_Property)); | 25 mus::mojom::WindowManager::kWindowTitle_Property)); |
25 } | 26 } |
26 return mojo::String(std::string()); | 27 return mojo::String(std::string()); |
27 } | 28 } |
28 | 29 |
30 // Get the serialized app icon bitmap from a mus::Window. | |
31 mojo::Array<uint8_t> GetWindowAppIcon(mus::Window* window) { | |
32 if (window->HasSharedProperty( | |
33 mus::mojom::WindowManager::kWindowAppIcon_Property)) { | |
34 return mojo::Array<uint8_t>::From( | |
35 window->GetSharedProperty<const std::vector<uint8_t>>( | |
36 mus::mojom::WindowManager::kWindowAppIcon_Property)); | |
37 } | |
38 return mojo::Array<uint8_t>(); | |
39 } | |
40 | |
29 // Returns |window|, or an ancestor thereof, parented to |container|, or null. | 41 // Returns |window|, or an ancestor thereof, parented to |container|, or null. |
30 mus::Window* GetTopLevelWindow(mus::Window* window, mus::Window* container) { | 42 mus::Window* GetTopLevelWindow(mus::Window* window, mus::Window* container) { |
31 while (window && window->parent() != container) | 43 while (window && window->parent() != container) |
32 window = window->parent(); | 44 window = window->parent(); |
33 return window; | 45 return window; |
34 } | 46 } |
35 | 47 |
36 // Get a UserWindow struct from a mus::Window. | 48 // Get a UserWindow struct from a mus::Window. |
37 mojom::UserWindowPtr GetUserWindow(mus::Window* window) { | 49 mojom::UserWindowPtr GetUserWindow(mus::Window* window) { |
38 mojom::UserWindowPtr user_window(mojom::UserWindow::New()); | 50 mojom::UserWindowPtr user_window(mojom::UserWindow::New()); |
39 user_window->window_id = window->id(); | 51 user_window->window_id = window->id(); |
40 user_window->window_title = GetWindowTitle(window); | 52 user_window->window_title = GetWindowTitle(window); |
53 user_window->window_app_icon = GetWindowAppIcon(window); | |
41 mus::Window* focused = window->connection()->GetFocusedWindow(); | 54 mus::Window* focused = window->connection()->GetFocusedWindow(); |
42 focused = GetTopLevelWindow(focused, window->parent()); | 55 focused = GetTopLevelWindow(focused, window->parent()); |
43 user_window->window_has_focus = focused == window; | 56 user_window->window_has_focus = focused == window; |
44 return user_window; | 57 return user_window; |
45 } | 58 } |
46 | 59 |
47 } // namespace | 60 } // namespace |
48 | 61 |
49 // Observes title changes on user windows. UserWindowControllerImpl uses this | 62 // Observes property changes on user windows. UserWindowControllerImpl uses |
50 // separate observer to avoid observing duplicate tree change notifications. | 63 // this separate observer to avoid observing duplicate tree change |
51 class WindowTitleObserver : public mus::WindowObserver { | 64 // notifications. |
65 class WindowPropertyObserver : public mus::WindowObserver { | |
52 public: | 66 public: |
53 explicit WindowTitleObserver(UserWindowControllerImpl* controller) | 67 explicit WindowPropertyObserver(UserWindowControllerImpl* controller) |
54 : controller_(controller) {} | 68 : controller_(controller) {} |
55 ~WindowTitleObserver() override {} | 69 ~WindowPropertyObserver() override {} |
56 | 70 |
57 private: | 71 private: |
58 // mus::WindowObserver: | 72 // mus::WindowObserver: |
59 void OnWindowSharedPropertyChanged( | 73 void OnWindowSharedPropertyChanged( |
60 mus::Window* window, | 74 mus::Window* window, |
61 const std::string& name, | 75 const std::string& name, |
62 const std::vector<uint8_t>* old_data, | 76 const std::vector<uint8_t>* old_data, |
63 const std::vector<uint8_t>* new_data) override { | 77 const std::vector<uint8_t>* new_data) override { |
64 if (controller_->user_window_observer() && | 78 if (!controller_->user_window_observer()) |
65 name == mus::mojom::WindowManager::kWindowTitle_Property) { | 79 return; |
80 if (name == mus::mojom::WindowManager::kWindowTitle_Property) { | |
66 controller_->user_window_observer()->OnUserWindowTitleChanged( | 81 controller_->user_window_observer()->OnUserWindowTitleChanged( |
67 window->id(), GetWindowTitle(window)); | 82 window->id(), GetWindowTitle(window)); |
83 } else if (name == mus::mojom::WindowManager::kWindowAppIcon_Property) { | |
84 controller_->user_window_observer()->OnUserWindowAppIconChanged( | |
85 window->id(), new_data ? mojo::Array<uint8_t>::From(*new_data) | |
msw
2016/03/24 17:40:54
Too bad we need to do another copy here...
James Cook
2016/03/25 15:39:28
Acknowledged.
| |
86 : mojo::Array<uint8_t>()); | |
68 } | 87 } |
69 } | 88 } |
70 | 89 |
71 UserWindowControllerImpl* controller_; | 90 UserWindowControllerImpl* controller_; |
72 DISALLOW_COPY_AND_ASSIGN(WindowTitleObserver); | 91 DISALLOW_COPY_AND_ASSIGN(WindowPropertyObserver); |
73 }; | 92 }; |
74 | 93 |
75 UserWindowControllerImpl::UserWindowControllerImpl() | 94 UserWindowControllerImpl::UserWindowControllerImpl() |
76 : root_controller_(nullptr) {} | 95 : root_controller_(nullptr) {} |
77 | 96 |
78 UserWindowControllerImpl::~UserWindowControllerImpl() { | 97 UserWindowControllerImpl::~UserWindowControllerImpl() { |
79 if (!root_controller_) | 98 if (!root_controller_) |
80 return; | 99 return; |
81 | 100 |
82 // TODO(msw): should really listen for user window container being destroyed | 101 // TODO(msw): should really listen for user window container being destroyed |
83 // and cleanup there. | 102 // and cleanup there. |
84 mus::Window* user_container = GetUserWindowContainer(); | 103 mus::Window* user_container = GetUserWindowContainer(); |
85 if (!user_container) | 104 if (!user_container) |
86 return; | 105 return; |
87 | 106 |
88 user_container->RemoveObserver(this); | 107 user_container->RemoveObserver(this); |
89 for (auto iter : user_container->children()) | 108 for (auto iter : user_container->children()) |
90 iter->RemoveObserver(window_title_observer_.get()); | 109 iter->RemoveObserver(window_property_observer_.get()); |
91 } | 110 } |
92 | 111 |
93 void UserWindowControllerImpl::Initialize( | 112 void UserWindowControllerImpl::Initialize( |
94 RootWindowController* root_controller) { | 113 RootWindowController* root_controller) { |
95 DCHECK(root_controller); | 114 DCHECK(root_controller); |
96 DCHECK(!root_controller_); | 115 DCHECK(!root_controller_); |
97 root_controller_ = root_controller; | 116 root_controller_ = root_controller; |
98 GetUserWindowContainer()->AddObserver(this); | 117 GetUserWindowContainer()->AddObserver(this); |
99 GetUserWindowContainer()->connection()->AddObserver(this); | 118 GetUserWindowContainer()->connection()->AddObserver(this); |
100 window_title_observer_.reset(new WindowTitleObserver(this)); | 119 window_property_observer_.reset(new WindowPropertyObserver(this)); |
101 for (auto iter : GetUserWindowContainer()->children()) | 120 for (auto iter : GetUserWindowContainer()->children()) |
102 iter->AddObserver(window_title_observer_.get()); | 121 iter->AddObserver(window_property_observer_.get()); |
103 } | 122 } |
104 | 123 |
105 mus::Window* UserWindowControllerImpl::GetUserWindowContainer() const { | 124 mus::Window* UserWindowControllerImpl::GetUserWindowContainer() const { |
106 return root_controller_->GetWindowForContainer( | 125 return root_controller_->GetWindowForContainer( |
107 mojom::Container::USER_WINDOWS); | 126 mojom::Container::USER_WINDOWS); |
108 } | 127 } |
109 | 128 |
110 void UserWindowControllerImpl::OnTreeChanging(const TreeChangeParams& params) { | 129 void UserWindowControllerImpl::OnTreeChanging(const TreeChangeParams& params) { |
111 DCHECK(root_controller_); | 130 DCHECK(root_controller_); |
112 if (params.new_parent == GetUserWindowContainer()) { | 131 if (params.new_parent == GetUserWindowContainer()) { |
113 params.target->AddObserver(window_title_observer_.get()); | 132 params.target->AddObserver(window_property_observer_.get()); |
114 if (user_window_observer_) | 133 if (user_window_observer_) |
115 user_window_observer_->OnUserWindowAdded(GetUserWindow(params.target)); | 134 user_window_observer_->OnUserWindowAdded(GetUserWindow(params.target)); |
116 } else if (params.old_parent == GetUserWindowContainer()) { | 135 } else if (params.old_parent == GetUserWindowContainer()) { |
117 params.target->RemoveObserver(window_title_observer_.get()); | 136 params.target->RemoveObserver(window_property_observer_.get()); |
118 if (user_window_observer_) | 137 if (user_window_observer_) |
119 user_window_observer_->OnUserWindowRemoved(params.target->id()); | 138 user_window_observer_->OnUserWindowRemoved(params.target->id()); |
120 } | 139 } |
121 } | 140 } |
122 | 141 |
123 void UserWindowControllerImpl::OnWindowTreeFocusChanged( | 142 void UserWindowControllerImpl::OnWindowTreeFocusChanged( |
124 mus::Window* gained_focus, | 143 mus::Window* gained_focus, |
125 mus::Window* lost_focus) { | 144 mus::Window* lost_focus) { |
126 if (!user_window_observer_) | 145 if (!user_window_observer_) |
127 return; | 146 return; |
(...skipping 24 matching lines...) Expand all Loading... | |
152 } | 171 } |
153 | 172 |
154 void UserWindowControllerImpl::FocusUserWindow(uint32_t window_id) { | 173 void UserWindowControllerImpl::FocusUserWindow(uint32_t window_id) { |
155 mus::Window* window = GetUserWindowContainer()->GetChildById(window_id); | 174 mus::Window* window = GetUserWindowContainer()->GetChildById(window_id); |
156 if (window) | 175 if (window) |
157 window->SetFocus(); | 176 window->SetFocus(); |
158 } | 177 } |
159 | 178 |
160 } // namespace wm | 179 } // namespace wm |
161 } // namespace mash | 180 } // namespace mash |
OLD | NEW |