OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/touch/frame/touch_browser_frame_view.h" | 5 #include "chrome/browser/ui/touch/frame/touch_browser_frame_view.h" |
6 | 6 |
7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/renderer_host/render_view_host.h" | 8 #include "chrome/browser/renderer_host/render_view_host.h" |
9 #include "chrome/browser/tab_contents/navigation_controller.h" | 9 #include "chrome/browser/tab_contents/navigation_controller.h" |
10 #include "chrome/browser/tab_contents/tab_contents.h" | 10 #include "chrome/browser/tab_contents/tab_contents.h" |
11 #include "chrome/browser/tabs/tab_strip_model.h" | |
11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
12 #include "chrome/browser/ui/touch/frame/keyboard_container_view.h" | 14 #include "chrome/browser/ui/touch/frame/keyboard_container_view.h" |
13 #include "chrome/browser/ui/views/frame/browser_view.h" | 15 #include "chrome/browser/ui/views/frame/browser_view.h" |
14 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
15 #include "chrome/common/notification_type.h" | 17 #include "chrome/common/notification_type.h" |
16 #include "gfx/rect.h" | 18 #include "gfx/rect.h" |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 const int kKeyboardHeight = 300; | 22 const int kKeyboardHeight = 300; |
21 | 23 |
24 PropertyAccessor<bool>* GetFocusedStateAccessor() { | |
25 static PropertyAccessor<bool> state; | |
26 return &state; | |
27 } | |
28 | |
22 } // namespace | 29 } // namespace |
23 | 30 |
24 /////////////////////////////////////////////////////////////////////////////// | 31 /////////////////////////////////////////////////////////////////////////////// |
25 // TouchBrowserFrameView, public: | 32 // TouchBrowserFrameView, public: |
26 | 33 |
27 TouchBrowserFrameView::TouchBrowserFrameView(BrowserFrame* frame, | 34 TouchBrowserFrameView::TouchBrowserFrameView(BrowserFrame* frame, |
28 BrowserView* browser_view) | 35 BrowserView* browser_view) |
29 : OpaqueBrowserFrameView(frame, browser_view), | 36 : OpaqueBrowserFrameView(frame, browser_view), |
30 keyboard_showing_(false), | 37 keyboard_showing_(false), |
31 keyboard_(NULL) { | 38 keyboard_(NULL) { |
32 registrar_.Add(this, | 39 registrar_.Add(this, |
33 NotificationType::NAV_ENTRY_COMMITTED, | 40 NotificationType::NAV_ENTRY_COMMITTED, |
34 NotificationService::AllSources()); | 41 NotificationService::AllSources()); |
35 registrar_.Add(this, | 42 registrar_.Add(this, |
36 NotificationType::FOCUS_CHANGED_IN_PAGE, | 43 NotificationType::FOCUS_CHANGED_IN_PAGE, |
37 NotificationService::AllSources()); | 44 NotificationService::AllSources()); |
45 registrar_.Add(this, | |
46 NotificationType::TAB_CONTENTS_DESTROYED, | |
47 NotificationService::AllSources()); | |
48 | |
49 browser_view->browser()->tabstrip_model()->AddObserver(this); | |
38 } | 50 } |
39 | 51 |
40 TouchBrowserFrameView::~TouchBrowserFrameView() { | 52 TouchBrowserFrameView::~TouchBrowserFrameView() { |
53 browser_view()->browser()->tabstrip_model()->RemoveObserver(this); | |
41 } | 54 } |
42 | 55 |
43 void TouchBrowserFrameView::Layout() { | 56 void TouchBrowserFrameView::Layout() { |
44 OpaqueBrowserFrameView::Layout(); | 57 OpaqueBrowserFrameView::Layout(); |
45 | 58 |
46 if (!keyboard_) | 59 if (!keyboard_) |
47 return; | 60 return; |
48 | 61 |
49 keyboard_->SetVisible(keyboard_showing_); | 62 keyboard_->SetVisible(keyboard_showing_); |
50 keyboard_->SetBounds(GetBoundsForReservedArea()); | 63 keyboard_->SetBounds(GetBoundsForReservedArea()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 | 104 |
92 // The keyboard that pops up may end up hiding the text entry. So make sure | 105 // The keyboard that pops up may end up hiding the text entry. So make sure |
93 // the renderer scrolls when necessary to keep the textfield visible. | 106 // the renderer scrolls when necessary to keep the textfield visible. |
94 if (keyboard_showing_) { | 107 if (keyboard_showing_) { |
95 RenderViewHost* host = | 108 RenderViewHost* host = |
96 browser_view()->browser()->GetSelectedTabContents()->render_view_host(); | 109 browser_view()->browser()->GetSelectedTabContents()->render_view_host(); |
97 host->ScrollFocusedEditableNodeIntoView(); | 110 host->ScrollFocusedEditableNodeIntoView(); |
98 } | 111 } |
99 } | 112 } |
100 | 113 |
114 void TouchBrowserFrameView::TabSelectedAt(TabContentsWrapper* old_contents, | |
115 TabContentsWrapper* new_contents, | |
116 int index, | |
117 bool user_gesture) { | |
118 TabContents* contents = new_contents->tab_contents(); | |
119 bool* editable = GetFocusedStateAccessor()->GetProperty( | |
120 contents->property_bag()); | |
121 UpdateKeyboardAndLayout(editable ? *editable : false); | |
122 } | |
123 | |
124 | |
101 void TouchBrowserFrameView::Observe(NotificationType type, | 125 void TouchBrowserFrameView::Observe(NotificationType type, |
102 const NotificationSource& source, | 126 const NotificationSource& source, |
103 const NotificationDetails& details) { | 127 const NotificationDetails& details) { |
104 Browser* browser = browser_view()->browser(); | 128 Browser* browser = browser_view()->browser(); |
105 if (type == NotificationType::FOCUS_CHANGED_IN_PAGE) { | 129 if (type == NotificationType::FOCUS_CHANGED_IN_PAGE) { |
106 // Only modify the keyboard state if the currently active tab sent the | 130 // Only modify the keyboard state if the currently active tab sent the |
107 // notification. | 131 // notification. |
108 const TabContents* tab_contents = browser->GetSelectedTabContents(); | 132 const TabContents* tab_contents = browser->GetSelectedTabContents(); |
109 if (tab_contents && | 133 RenderViewHost* host = Source<RenderViewHost>(source).ptr(); |
110 tab_contents->render_view_host() == | 134 const bool editable = *Details<const bool>(details).ptr(); |
111 Source<RenderViewHost>(source).ptr()) | 135 |
112 UpdateKeyboardAndLayout(*Details<const bool>(details).ptr()); | 136 if (tab_contents && tab_contents->render_view_host() == host) { |
137 UpdateKeyboardAndLayout(editable); | |
138 } | |
139 | |
140 // Save the state of the focused field so that the keyboard visibility | |
141 // can be determined after tab switching. | |
142 TabContents* source_tab = static_cast<TabContents*>(host->delegate()); | |
sky
2011/01/26 18:07:43
Casts like this are dangerous. Could we change FOC
brettw
2011/01/26 20:18:07
I agree, we should never have this type of cast.
| |
143 GetFocusedStateAccessor()->SetProperty( | |
144 source_tab->property_bag(), editable); | |
113 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { | 145 } else if (type == NotificationType::NAV_ENTRY_COMMITTED) { |
114 Browser* source_browser = Browser::GetBrowserForController( | 146 Browser* source_browser = Browser::GetBrowserForController( |
115 Source<NavigationController>(source).ptr(), NULL); | 147 Source<NavigationController>(source).ptr(), NULL); |
116 // If the Browser for the keyboard has navigated, hide the keyboard. | 148 // If the Browser for the keyboard has navigated, hide the keyboard. |
117 if (source_browser == browser) | 149 if (source_browser == browser) |
118 UpdateKeyboardAndLayout(false); | 150 UpdateKeyboardAndLayout(false); |
151 } else if (type == NotificationType::TAB_CONTENTS_DESTROYED) { | |
152 GetFocusedStateAccessor()->DeleteProperty( | |
153 Source<TabContents>(source).ptr()->property_bag()); | |
sadrul
2011/01/26 17:43:10
Or should this be done from TabDetachedAt and TabR
sky
2011/01/26 18:07:43
This is better.
| |
119 } | 154 } |
120 } | 155 } |
OLD | NEW |