OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. 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. |
| 4 |
| 5 #include "chrome/browser/ui/touch/frame/touch_browser_frame_view.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/renderer_host/site_instance.h" |
| 11 #include "chrome/browser/tab_contents/tab_contents.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/views/dom_view.h" |
| 14 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 15 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/notification_type.h" |
| 17 #include "chrome/common/url_constants.h" |
| 18 #include "gfx/rect.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 const int kKeyboardHeight = 300; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 /////////////////////////////////////////////////////////////////////////////// |
| 27 // TouchBrowserFrameView, public: |
| 28 |
| 29 TouchBrowserFrameView::TouchBrowserFrameView(BrowserFrame* frame, |
| 30 BrowserView* browser_view) |
| 31 : OpaqueBrowserFrameView(frame, browser_view), |
| 32 keyboard_(NULL) { |
| 33 registrar_.Add(this, |
| 34 NotificationType::NAV_ENTRY_COMMITTED, |
| 35 NotificationService::AllSources()); |
| 36 registrar_.Add(this, |
| 37 NotificationType::FOCUS_CHANGED_IN_PAGE, |
| 38 NotificationService::AllSources()); |
| 39 } |
| 40 |
| 41 TouchBrowserFrameView::~TouchBrowserFrameView() { |
| 42 } |
| 43 |
| 44 /////////////////////////////////////////////////////////////////////////////// |
| 45 // TouchBrowserFrameView, protected: |
| 46 int TouchBrowserFrameView::GetReservedHeight() const { |
| 47 if (keyboard_ && keyboard_->IsVisible()) { |
| 48 return kKeyboardHeight; |
| 49 } |
| 50 return 0; |
| 51 } |
| 52 |
| 53 /////////////////////////////////////////////////////////////////////////////// |
| 54 // TouchBrowserFrameView, private: |
| 55 |
| 56 void TouchBrowserFrameView::InitVirtualKeyboard() { |
| 57 if (keyboard_) |
| 58 return; |
| 59 |
| 60 keyboard_ = new DOMView; |
| 61 |
| 62 Profile* keyboard_profile = browser_view()->browser()->profile(); |
| 63 DCHECK(keyboard_profile) << "Profile required for virtual keyboard."; |
| 64 |
| 65 GURL keyboard_url(chrome::kChromeUIKeyboardURL); |
| 66 keyboard_->Init(keyboard_profile, |
| 67 SiteInstance::CreateSiteInstanceForURL(keyboard_profile, keyboard_url)); |
| 68 keyboard_->LoadURL(keyboard_url); |
| 69 |
| 70 keyboard_->SetVisible(false); |
| 71 AddChildView(keyboard_); |
| 72 } |
| 73 |
| 74 void TouchBrowserFrameView::UpdateKeyboardAndLayout(bool should_show_keyboard) { |
| 75 if (should_show_keyboard) |
| 76 InitVirtualKeyboard(); |
| 77 |
| 78 // Don't do any extra work until we have shown the keyboard. |
| 79 if (!keyboard_) |
| 80 return; |
| 81 |
| 82 if (should_show_keyboard == keyboard_->IsVisible()) |
| 83 return; |
| 84 |
| 85 keyboard_->SetVisible(should_show_keyboard); |
| 86 |
| 87 // Because the NonClientFrameView is a sibling of the ClientView, |
| 88 // we rely on the parent to properly resize the ClientView. |
| 89 GetParent()->Layout(); |
| 90 } |
| 91 |
| 92 void TouchBrowserFrameView::Observe(NotificationType type, |
| 93 const NotificationSource& source, |
| 94 const NotificationDetails& details) { |
| 95 if (type == NotificationType::FOCUS_CHANGED_IN_PAGE) { |
| 96 // Only modify the keyboard state if the notification is coming from |
| 97 // a source within the same Browser. |
| 98 Browser* browser = browser_view()->browser(); |
| 99 Source<RenderViewHost> specific_source(source); |
| 100 for (int i = 0; i < browser->tab_count(); ++i) { |
| 101 if (browser->GetTabContentsAt(i)->render_view_host() == |
| 102 specific_source.ptr()) { |
| 103 UpdateKeyboardAndLayout(*Details<const bool>(details).ptr()); |
| 104 break; |
| 105 } |
| 106 } |
| 107 } else { |
| 108 DCHECK(type == NotificationType::NAV_ENTRY_COMMITTED); |
| 109 |
| 110 // TODO(bryeung): this is hiding the keyboard for the very first page |
| 111 // load after browser startup when the page has an input element focused |
| 112 // to start with. It would be nice to fix, but not critical. |
| 113 |
| 114 // Everything else we have registered for should hide the keyboard. |
| 115 UpdateKeyboardAndLayout(false); |
| 116 } |
| 117 } |
OLD | NEW |