Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: Source/web/PopupMenuImpl.cpp

Issue 736883002: Implement <select> Popup Menu using PagePopup (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014 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 "config.h"
6 #include "web/PopupMenuImpl.h"
7
8 #include "core/HTMLNames.h"
9 #include "core/css/CSSFontSelector.h"
10 #include "core/dom/StyleEngine.h"
11 #include "core/frame/FrameView.h"
12 #include "core/html/HTMLHRElement.h"
13 #include "core/html/HTMLOptGroupElement.h"
14 #include "core/html/HTMLOptionElement.h"
15 #include "core/page/PagePopup.h"
16 #include "platform/geometry/IntRect.h"
17 #include "platform/text/PlatformLocale.h"
18 #include "public/platform/Platform.h"
19 #include "public/web/WebColorChooser.h"
20 #include "web/ChromeClientImpl.h"
21 #include "web/WebViewImpl.h"
22
23 namespace blink {
24
25 class PopupMenuCSSFontSelector : public CSSFontSelector {
26 public:
27 static PassRefPtrWillBeRawPtr<PopupMenuCSSFontSelector> create(Document* doc ument, CSSFontSelector* ownerFontSelector)
28 {
29 return adoptRefWillBeNoop(new PopupMenuCSSFontSelector(document, ownerFo ntSelector));
30 }
31
32 // We don't override willUseFontData() for now because the old PopupListBox
33 // only worked with fonts loaded when opening the popup.
34 virtual PassRefPtr<FontData> getFontData(const FontDescription&, const Atomi cString&) override;
35
36 private:
37 PopupMenuCSSFontSelector(Document* document, CSSFontSelector* ownerFontSelec tor)
38 : CSSFontSelector(document)
39 , m_ownerFontSelector(ownerFontSelector) { }
40 RefPtrWillBeMember<CSSFontSelector> m_ownerFontSelector;
41 };
42
43 PassRefPtr<FontData> PopupMenuCSSFontSelector::getFontData(const FontDescription & description, const AtomicString& name)
44 {
45 return m_ownerFontSelector->getFontData(description, name);
46 }
47
48 PassRefPtrWillBeRawPtr<PopupMenuImpl> PopupMenuImpl::create(ChromeClientImpl* ch romeClient, PopupMenuClient* client)
49 {
50 return adoptRefWillBeNoop(new PopupMenuImpl(chromeClient, client));
51 }
52
53 PopupMenuImpl::PopupMenuImpl(ChromeClientImpl* chromeClient, PopupMenuClient* cl ient)
54 : m_chromeClient(chromeClient)
55 , m_client(client)
56 , m_popup(nullptr)
57 {
58 }
59
60 PopupMenuImpl::~PopupMenuImpl()
61 {
62 closePopup();
63 }
64
65 IntSize PopupMenuImpl::contentSize()
66 {
67 return IntSize();
68 }
69
70 void PopupMenuImpl::writeDocument(SharedBuffer* data)
71 {
72 IntRect anchorRectInScreen = m_chromeClient->rootViewToScreen(m_client->elem entRectRelativeToRootView());
73
74 PagePopupClient::addString("<!DOCTYPE html><head><meta charset='UTF-8'><styl e>\n", data);
75 data->append(Platform::current()->loadResource("pickerCommon.css"));
76 data->append(Platform::current()->loadResource("listPicker.css"));
77 PagePopupClient::addString("</style></head><body><div id=main>Loading...</di v><script>\n"
78 "window.dialogArguments = {\n", data);
79 addProperty("selectedIndex", m_client->selectedIndex(), data);
80 PagePopupClient::addString("children: [\n", data);
81 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(ownerElement()) ) {
82 if (isHTMLOptionElement(child))
83 addOption(toHTMLOptionElement(child), data);
84 if (isHTMLOptGroupElement(child))
85 addOptGroup(toHTMLOptGroupElement(child), data);
86 if (isHTMLHRElement(child))
87 addSeparator(toHTMLHRElement(child), data);
88 }
89 PagePopupClient::addString("],\n", data);
90 addProperty("anchorRectInScreen", anchorRectInScreen, data);
91 PagePopupClient::addString("};\n", data);
92 data->append(Platform::current()->loadResource("pickerCommon.js"));
93 data->append(Platform::current()->loadResource("listPicker.js"));
94 PagePopupClient::addString("</script></body>\n", data);
95 }
96
97 const char* fontWeightToString(FontWeight weight)
98 {
99 switch (weight) {
100 case FontWeight100:
101 return "100";
102 case FontWeight200:
103 return "200";
104 case FontWeight300:
105 return "300";
106 case FontWeight400:
107 return "400";
108 case FontWeight500:
109 return "500";
110 case FontWeight600:
111 return "600";
112 case FontWeight700:
113 return "700";
114 case FontWeight800:
115 return "800";
116 case FontWeight900:
117 return "900";
118 }
119 }
120
121 void PopupMenuImpl::addElementStyle(HTMLElement& element, SharedBuffer* data)
122 {
123 RenderStyle* style = m_client->renderStyleForItem(element);
124 ASSERT(style);
125 PagePopupClient::addString("style: {\n", data);
126 addProperty("color", style->visitedDependentColor(CSSPropertyColor).serializ ed(), data);
127 addProperty("backgroundColor", style->visitedDependentColor(CSSPropertyBackg roundColor).serialized(), data);
128 const FontDescription& fontDescription = style->font().fontDescription();
129 addProperty("fontSize", fontDescription.computedPixelSize(), data);
130 addProperty("fontWeight", String(fontWeightToString(fontDescription.weight() )), data);
131 PagePopupClient::addString("fontFamily: [\n", data);
132 for (const FontFamily* f = &fontDescription.family(); f; f = f->next()) {
133 addJavaScriptString(f->family().string(), data);
134 if (f->next())
135 PagePopupClient::addString(",\n", data);
136 }
137 PagePopupClient::addString("],\n", data);
138 addProperty("visibility", String(style->visibility() == HIDDEN ? "hidden" : "visible"), data);
139 addProperty("display", String(style->display() == NONE ? "none" : "block"), data);
140 addProperty("direction", String(style->direction() == RTL ? "rtl" : "ltr"), data);
141 addProperty("unicodeBidi", String(isOverride(style->unicodeBidi()) ? "bidi-o verride" : "normal"), data);
142 PagePopupClient::addString("},\n", data);
143 }
144
145 void PopupMenuImpl::addOption(HTMLOptionElement& element, SharedBuffer* data)
146 {
147 PagePopupClient::addString("{\n", data);
148 PagePopupClient::addString("type: \"option\",\n", data);
149 addProperty("label", element.text(), data);
150 addProperty("title", element.title(), data);
151 addProperty("value", element.listIndex(), data);
152 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr) , data);
153 addProperty("disabled", element.isDisabledFormControl(), data);
154 addElementStyle(element, data);
155 PagePopupClient::addString("},\n", data);
156 }
157
158 void PopupMenuImpl::addOptGroup(HTMLOptGroupElement& element, SharedBuffer* data )
159 {
160 PagePopupClient::addString("{\n", data);
161 PagePopupClient::addString("type: \"optgroup\",\n", data);
162 addProperty("label", element.groupLabelText(), data);
163 addProperty("title", element.title(), data);
164 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr) , data);
165 addProperty("disabled", element.isDisabledFormControl(), data);
166 addElementStyle(element, data);
167 PagePopupClient::addString("children: [", data);
168 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(element)) {
169 if (isHTMLOptionElement(child))
170 addOption(toHTMLOptionElement(child), data);
171 if (isHTMLOptGroupElement(child))
172 addOptGroup(toHTMLOptGroupElement(child), data);
173 if (isHTMLHRElement(child))
174 addSeparator(toHTMLHRElement(child), data);
175 }
176 PagePopupClient::addString("],\n", data);
177 PagePopupClient::addString("},\n", data);
178 }
179
180 void PopupMenuImpl::addSeparator(HTMLHRElement& element, SharedBuffer* data)
181 {
182 PagePopupClient::addString("{\n", data);
183 PagePopupClient::addString("type: \"separator\",\n", data);
184 addProperty("title", element.title(), data);
185 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr) , data);
186 addProperty("disabled", element.isDisabledFormControl(), data);
187 addElementStyle(element, data);
188 PagePopupClient::addString("},\n", data);
189 }
190
191 void PopupMenuImpl::didWriteDocument(Document* document)
192 {
193 Document& ownerDocument = ownerElement().document();
194 document->styleEngine()->setFontSelector(PopupMenuCSSFontSelector::create(do cument, ownerDocument.styleEngine()->fontSelector()));
195 }
196
197 void PopupMenuImpl::setValueAndClosePopup(int numValue, const String& stringValu e)
198 {
199 ASSERT(m_popup);
200 ASSERT(m_client);
201 bool success;
202 int listIndex = stringValue.toInt(&success);
203 ASSERT(success);
204 m_client->selectionChanged(listIndex);
205 m_client->valueChanged(listIndex);
206 closePopup();
207 }
208
209 void PopupMenuImpl::setValue(const String& value)
210 {
211 ASSERT(m_client);
212 bool success;
213 int listIndex = value.toInt(&success);
214 ASSERT(success);
215 m_client->setTextFromItem(listIndex);
216 // FIXME: Wrong menu list text when switching from keyboard to mouse. We nee d something like PopupListBox::m_originalIndex.
217 }
218
219 void PopupMenuImpl::didClosePopup()
220 {
221 m_popup = nullptr;
222 m_client->popupDidHide();
223 }
224
225 Element& PopupMenuImpl::ownerElement()
226 {
227 return m_client->ownerElement();
228 }
229
230 Locale& PopupMenuImpl::locale()
231 {
232 return Locale::defaultLocale();
233 }
234
235 void PopupMenuImpl::closePopup()
236 {
237 if (m_popup)
238 m_chromeClient->closePagePopup(m_popup);
239 }
240
241 void PopupMenuImpl::dispose()
242 {
243 closePopup();
244 }
245
246 void PopupMenuImpl::show(const FloatQuad& /*controlPosition*/, const IntSize& /* controlSize*/, int /*index*/)
247 {
248 ASSERT(!m_popup);
249 m_popup = m_chromeClient->openPagePopup(this, m_client->elementRectRelativeT oRootView());
250 }
251
252 void PopupMenuImpl::hide()
253 {
254 if (m_popup)
255 m_chromeClient->closePagePopup(m_popup);
256 }
257
258 void PopupMenuImpl::updateFromElement()
259 {
260 RefPtr<SharedBuffer> data = SharedBuffer::create();
261 PagePopupClient::addString("window.updateData = {\n", data.get());
262 PagePopupClient::addString("type: \"update\",\n", data.get());
263 PagePopupClient::addString("children: [", data.get());
264 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(ownerElement()) ) {
265 if (isHTMLOptionElement(child))
266 addOption(toHTMLOptionElement(child), data.get());
267 if (isHTMLOptGroupElement(child))
268 addOptGroup(toHTMLOptGroupElement(child), data.get());
269 if (isHTMLHRElement(child))
270 addSeparator(toHTMLHRElement(child), data.get());
271 }
272 PagePopupClient::addString("],\n", data.get());
273 PagePopupClient::addString("}\n", data.get());
274 m_popup->postMessage(String(data->data(), data->size()));
275 }
276
277
278 void PopupMenuImpl::disconnectClient()
279 {
280 m_client = nullptr;
281 #if ENABLE(OILPAN)
282 // Cannot be done during finalization, so instead done when the
283 // render object is destroyed and disconnected.
284 //
285 // FIXME: do this always, regardless of ENABLE(OILPAN).
286 dispose();
287 #endif
288 }
289
290 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698