1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "xwalk/runtime/browser/ui/native_app_window_views.h"
7 #include "content/public/browser/notification_service.h"
8 #include "content/public/browser/web_contents.h"
9 #include "ui/gfx/screen.h"
10 #include "ui/views/controls/webview/webview.h"
11 #include "ui/views/widget/desktop_aura/desktop_screen.h"
12 #include "ui/views/widget/widget.h"
13 #include "xwalk/runtime/browser/ui/top_view_layout_views.h"
14 #include "xwalk/runtime/browser/ui/xwalk_views_delegate.h"
15 #include "xwalk/runtime/common/xwalk_notification_types.h"
18 #include "ui/views/window/native_frame_view.h"
22 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
27 NativeAppWindowViews::NativeAppWindowViews(
28 const NativeAppWindow::CreateParams& create_params)
29 : create_params_(create_params),
30 delegate_(create_params.delegate),
31 web_contents_(create_params.web_contents),
34 is_fullscreen_(false),
35 minimum_size_(create_params.minimum_size),
36 maximum_size_(create_params.maximum_size),
37 resizable_(create_params.resizable) {}
39 NativeAppWindowViews::~NativeAppWindowViews() {}
41 // Two-step initialization is needed here so that calls done by views::Widget to
42 // its delegate during views::Widget::Init() reach the correct implementation --
43 // e.g. ViewHierarchyChanged().
44 void NativeAppWindowViews::Initialize() {
46 window_ = new views::Widget;
48 views::Widget::InitParams params;
49 params.delegate = this;
50 params.remove_standard_frame = false;
51 params.use_system_default_icon = true;
52 params.top_level = true;
53 params.show_state = create_params_.state;
54 params.parent = create_params_.parent;
55 #if defined(OS_TIZEN_MOBILE)
56 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
57 // On Tizen apps are sized to the work area.
59 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().work_area();
60 params.bounds = bounds;
62 params.type = views::Widget::InitParams::TYPE_WINDOW;
63 params.bounds = create_params_.bounds;
65 params.net_wm_pid = create_params_.net_wm_pid;
67 window_->Init(params);
69 #if defined(OS_TIZEN_MOBILE)
70 // Set the bounds manually to avoid inset.
71 window_->SetBounds(bounds);
72 #elif !defined(USE_OZONE)
73 window_->CenterWindow(create_params_.bounds.size());
76 if (create_params_.state == ui::SHOW_STATE_FULLSCREEN)
79 // TODO(hmin): Need to configure the maximum and minimum size of this window.
80 window_->AddObserver(this);
83 gfx::NativeWindow NativeAppWindowViews::GetNativeWindow() const {
84 return window_->GetNativeWindow();
87 void NativeAppWindowViews::UpdateIcon(const gfx::Image& icon) {
89 window_->UpdateWindowIcon();
92 void NativeAppWindowViews::UpdateTitle(const base::string16& title) {
94 window_->UpdateWindowTitle();
97 gfx::Rect NativeAppWindowViews::GetRestoredBounds() const {
98 return window_->GetRestoredBounds();
101 gfx::Rect NativeAppWindowViews::GetBounds() const {
102 return window_->GetWindowBoundsInScreen();
105 void NativeAppWindowViews::SetBounds(const gfx::Rect& bounds) {
106 window_->SetBounds(bounds);
109 void NativeAppWindowViews::Focus() {
113 void NativeAppWindowViews::Show() {
117 void NativeAppWindowViews::Hide() {
121 void NativeAppWindowViews::Maximize() {
125 void NativeAppWindowViews::Minimize() {
129 void NativeAppWindowViews::SetFullscreen(bool fullscreen) {
130 if (is_fullscreen_ == fullscreen)
132 is_fullscreen_ = fullscreen;
133 window_->SetFullscreen(is_fullscreen_);
135 content::NotificationService::current()->Notify(
136 xwalk::NOTIFICATION_FULLSCREEN_CHANGED,
137 content::Source<NativeAppWindow>(this),
138 content::NotificationService::NoDetails());
141 void NativeAppWindowViews::Restore() {
145 void NativeAppWindowViews::FlashFrame(bool flash) {
146 window_->FlashFrame(flash);
149 void NativeAppWindowViews::Close() {
153 bool NativeAppWindowViews::IsActive() const {
154 return window_->IsActive();
157 bool NativeAppWindowViews::IsMaximized() const {
158 return window_->IsMaximized();
161 bool NativeAppWindowViews::IsMinimized() const {
162 return window_->IsMinimized();
165 bool NativeAppWindowViews::IsFullscreen() const {
166 return is_fullscreen_;
169 TopViewLayout* NativeAppWindowViews::top_view_layout() {
170 return static_cast<TopViewLayout*>(GetLayoutManager());
173 ////////////////////////////////////////////////////////////
174 // WidgetDelegate implementation
175 ////////////////////////////////////////////////////////////
176 views::View* NativeAppWindowViews::GetInitiallyFocusedView() {
180 views::View* NativeAppWindowViews::GetContentsView() {
184 views::Widget* NativeAppWindowViews::GetWidget() {
188 const views::Widget* NativeAppWindowViews::GetWidget() const {
192 base::string16 NativeAppWindowViews::GetWindowTitle() const {
196 void NativeAppWindowViews::DeleteDelegate() {
197 window_->RemoveObserver(this);
198 delegate_->OnWindowDestroyed();
201 gfx::ImageSkia NativeAppWindowViews::GetWindowAppIcon() {
202 return GetWindowIcon();
205 gfx::ImageSkia NativeAppWindowViews::GetWindowIcon() {
206 return *icon_.ToImageSkia();
209 bool NativeAppWindowViews::ShouldShowWindowTitle() const {
213 void NativeAppWindowViews::SaveWindowPlacement(const gfx::Rect& bounds,
214 ui::WindowShowState show_state) {
215 // TODO(hmin): views::WidgetDelegate::SaveWindowPlacement(bounds, show_state);
218 bool NativeAppWindowViews::GetSavedWindowPlacement(const views::Widget* widget,
219 gfx::Rect* bounds, ui::WindowShowState* show_state) const {
220 // TODO(hmin): Get the saved window placement.
224 bool NativeAppWindowViews::CanResize() const {
226 (maximum_size_.IsEmpty() || minimum_size_ != maximum_size_);
229 bool NativeAppWindowViews::CanMaximize() const {
230 return resizable_ && maximum_size_.IsEmpty();
234 views::NonClientFrameView* NativeAppWindowViews::CreateNonClientFrameView(
235 views::Widget* widget) {
236 return new views::NativeFrameView(widget);
240 ////////////////////////////////////////////////////////////
241 // views::View implementation
242 ////////////////////////////////////////////////////////////
244 void NativeAppWindowViews::ChildPreferredSizeChanged(views::View* child) {
245 // We only re-layout when the top view changes its preferred size (and notify
246 // us by calling its own function PreferredSizeChanged()). We don't react to
247 // changes in preferred size for the content view since we are currently
248 // setting its bounds to the maximum size available.
249 if (child == top_view_layout()->top_view())
253 void NativeAppWindowViews::ViewHierarchyChanged(
254 const ViewHierarchyChangedDetails& details) {
255 if (details.is_add && details.child == this) {
256 TopViewLayout* layout = new TopViewLayout();
257 SetLayoutManager(layout);
259 web_view_ = new views::WebView(NULL);
260 web_view_->SetWebContents(web_contents_);
261 AddChildView(web_view_);
262 layout->set_content_view(web_view_);
266 void NativeAppWindowViews::OnFocus() {
267 web_view_->RequestFocus();
270 gfx::Size NativeAppWindowViews::GetMaximumSize() {
271 return maximum_size_;
274 gfx::Size NativeAppWindowViews::GetMinimumSize() {
275 return minimum_size_;
278 ////////////////////////////////////////////////////////////
279 // views::WidgetObserver implementation
280 ////////////////////////////////////////////////////////////
281 void NativeAppWindowViews::OnWidgetClosing(views::Widget* widget) {
283 void NativeAppWindowViews::OnWidgetCreated(views::Widget* widget) {
285 void NativeAppWindowViews::OnWidgetDestroying(views::Widget* widget) {
287 void NativeAppWindowViews::OnWidgetDestroyed(views::Widget* widget) {
289 void NativeAppWindowViews::OnWidgetBoundsChanged(views::Widget* widget,
290 const gfx::Rect& new_bounds) {
294 NativeAppWindow* NativeAppWindow::Create(
295 const NativeAppWindow::CreateParams& create_params) {
296 NativeAppWindowViews* window;
297 #if defined(OS_TIZEN)
298 window = new NativeAppWindowTizen(create_params);
300 window = new NativeAppWindowViews(create_params);
302 window->Initialize();
307 void NativeAppWindow::Initialize() {
308 CHECK(!views::ViewsDelegate::views_delegate);
309 gfx::Screen::SetScreenInstance(
310 gfx::SCREEN_TYPE_NATIVE, views::CreateDesktopScreen());
311 views::ViewsDelegate::views_delegate = new XWalkViewsDelegate();