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

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 , m_indexToSetOnClose(-1)
58 {
59 }
60
61 PopupMenuImpl::~PopupMenuImpl()
62 {
63 closePopup();
64 }
65
66 IntSize PopupMenuImpl::contentSize()
67 {
68 return IntSize();
69 }
70
71 void PopupMenuImpl::writeDocument(SharedBuffer* data)
72 {
73 IntRect anchorRectInScreen = m_chromeClient->rootViewToScreen(m_client->elem entRectRelativeToRootView());
74
75 PagePopupClient::addString("<!DOCTYPE html><head><meta charset='UTF-8'><styl e>\n", data);
76 data->append(Platform::current()->loadResource("pickerCommon.css"));
77 data->append(Platform::current()->loadResource("listPicker.css"));
78 PagePopupClient::addString("</style></head><body><div id=main>Loading...</di v><script>\n"
79 "window.dialogArguments = {\n", data);
80 addProperty("selectedIndex", m_client->selectedIndex(), data);
81 PagePopupClient::addString("children: [\n", data);
82 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(ownerElement()) ) {
83 if (isHTMLOptionElement(child))
84 addOption(toHTMLOptionElement(child), data);
85 if (isHTMLOptGroupElement(child))
86 addOptGroup(toHTMLOptGroupElement(child), data);
87 if (isHTMLHRElement(child))
88 addSeparator(toHTMLHRElement(child), data);
89 }
90 PagePopupClient::addString("],\n", data);
91 addProperty("anchorRectInScreen", anchorRectInScreen, data);
92 PagePopupClient::addString("};\n", data);
93 data->append(Platform::current()->loadResource("pickerCommon.js"));
94 data->append(Platform::current()->loadResource("listPicker.js"));
95 PagePopupClient::addString("</script></body>\n", data);
96 }
97
98 const char* fontWeightToString(FontWeight weight)
99 {
100 switch (weight) {
101 case FontWeight100:
102 return "100";
103 case FontWeight200:
104 return "200";
105 case FontWeight300:
106 return "300";
107 case FontWeight400:
108 return "400";
109 case FontWeight500:
110 return "500";
111 case FontWeight600:
112 return "600";
113 case FontWeight700:
114 return "700";
115 case FontWeight800:
116 return "800";
117 case FontWeight900:
118 return "900";
119 }
120 }
121
122 void PopupMenuImpl::addElementStyle(HTMLElement& element, SharedBuffer* data)
123 {
124 RenderStyle* style = m_client->renderStyleForItem(element);
125 ASSERT(style);
126 PagePopupClient::addString("style: {\n", data);
127 addProperty("color", style->visitedDependentColor(CSSPropertyColor).serializ ed(), data);
128 addProperty("backgroundColor", style->visitedDependentColor(CSSPropertyBackg roundColor).serialized(), data);
129 const FontDescription& fontDescription = style->font().fontDescription();
130 addProperty("fontSize", fontDescription.computedPixelSize(), data);
131 addProperty("fontWeight", String(fontWeightToString(fontDescription.weight() )), data);
132 PagePopupClient::addString("fontFamily: [\n", data);
133 for (const FontFamily* f = &fontDescription.family(); f; f = f->next()) {
134 addJavaScriptString(f->family().string(), data);
135 if (f->next())
136 PagePopupClient::addString(",\n", data);
137 }
138 PagePopupClient::addString("],\n", data);
139 addProperty("visibility", String(style->visibility() == HIDDEN ? "hidden" : "visible"), data);
140 addProperty("display", String(style->display() == NONE ? "none" : "block"), data);
141 addProperty("direction", String(style->direction() == RTL ? "rtl" : "ltr"), data);
142 addProperty("unicodeBidi", String(isOverride(style->unicodeBidi()) ? "bidi-o verride" : "normal"), data);
143 PagePopupClient::addString("},\n", data);
144 }
145
146 void PopupMenuImpl::addOption(HTMLOptionElement& element, SharedBuffer* data)
147 {
148 PagePopupClient::addString("{\n", data);
149 PagePopupClient::addString("type: \"option\",\n", data);
150 addProperty("label", element.text(), data);
151 addProperty("title", element.title(), data);
152 addProperty("value", element.listIndex(), data);
153 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr) , data);
154 addProperty("disabled", element.isDisabledFormControl(), data);
155 addElementStyle(element, data);
156 PagePopupClient::addString("},\n", data);
157 }
158
159 void PopupMenuImpl::addOptGroup(HTMLOptGroupElement& element, SharedBuffer* data )
160 {
161 PagePopupClient::addString("{\n", data);
162 PagePopupClient::addString("type: \"optgroup\",\n", data);
163 addProperty("label", element.groupLabelText(), data);
164 addProperty("title", element.title(), data);
165 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr) , data);
166 addProperty("disabled", element.isDisabledFormControl(), data);
167 addElementStyle(element, data);
168 PagePopupClient::addString("children: [", data);
169 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(element)) {
170 if (isHTMLOptionElement(child))
171 addOption(toHTMLOptionElement(child), data);
172 if (isHTMLOptGroupElement(child))
173 addOptGroup(toHTMLOptGroupElement(child), data);
174 if (isHTMLHRElement(child))
175 addSeparator(toHTMLHRElement(child), data);
176 }
177 PagePopupClient::addString("],\n", data);
178 PagePopupClient::addString("},\n", data);
179 }
180
181 void PopupMenuImpl::addSeparator(HTMLHRElement& element, SharedBuffer* data)
182 {
183 PagePopupClient::addString("{\n", data);
184 PagePopupClient::addString("type: \"separator\",\n", data);
185 addProperty("title", element.title(), data);
186 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr) , data);
187 addProperty("disabled", element.isDisabledFormControl(), data);
188 addElementStyle(element, data);
189 PagePopupClient::addString("},\n", data);
190 }
191
192 void PopupMenuImpl::didWriteDocument(Document& document)
193 {
194 Document& ownerDocument = ownerElement().document();
195 document.styleEngine()->setFontSelector(PopupMenuCSSFontSelector::create(&do cument, ownerDocument.styleEngine()->fontSelector()));
196 }
197
198 void PopupMenuImpl::setValueAndClosePopup(int numValue, const String& stringValu e)
199 {
200 ASSERT(m_popup);
201 ASSERT(m_client);
202 bool success;
203 int listIndex = stringValue.toInt(&success);
204 ASSERT(success);
205 m_client->selectionChanged(listIndex);
206 m_client->valueChanged(listIndex);
207 m_indexToSetOnClose = -1;
208 closePopup();
209 }
210
211 void PopupMenuImpl::setValue(const String& value)
212 {
213 ASSERT(m_client);
214 bool success;
215 int listIndex = value.toInt(&success);
216 ASSERT(success);
217 m_client->setTextFromItem(listIndex);
218 m_indexToSetOnClose = listIndex;
219 }
220
221 void PopupMenuImpl::didClosePopup()
222 {
223 if (m_indexToSetOnClose >= 0)
224 m_client->valueChanged(m_indexToSetOnClose);
225 m_indexToSetOnClose = -1;
226 m_popup = nullptr;
227 m_client->popupDidHide();
228 }
229
230 Element& PopupMenuImpl::ownerElement()
231 {
232 return m_client->ownerElement();
233 }
234
235 Locale& PopupMenuImpl::locale()
236 {
237 return Locale::defaultLocale();
238 }
239
240 void PopupMenuImpl::closePopup()
241 {
242 if (m_popup)
243 m_chromeClient->closePagePopup(m_popup);
244 }
245
246 void PopupMenuImpl::dispose()
247 {
248 closePopup();
249 }
250
251 void PopupMenuImpl::show(const FloatQuad& /*controlPosition*/, const IntSize& /* controlSize*/, int /*index*/)
252 {
253 ASSERT(!m_popup);
254 m_popup = m_chromeClient->openPagePopup(this, m_client->elementRectRelativeT oRootView());
255 }
256
257 void PopupMenuImpl::hide()
258 {
259 if (m_popup)
260 m_chromeClient->closePagePopup(m_popup);
261 }
262
263 void PopupMenuImpl::updateFromElement()
264 {
265 RefPtr<SharedBuffer> data = SharedBuffer::create();
266 PagePopupClient::addString("window.updateData = {\n", data.get());
267 PagePopupClient::addString("type: \"update\",\n", data.get());
268 PagePopupClient::addString("children: [", data.get());
269 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(ownerElement()) ) {
270 if (isHTMLOptionElement(child))
271 addOption(toHTMLOptionElement(child), data.get());
272 if (isHTMLOptGroupElement(child))
273 addOptGroup(toHTMLOptGroupElement(child), data.get());
274 if (isHTMLHRElement(child))
275 addSeparator(toHTMLHRElement(child), data.get());
276 }
277 PagePopupClient::addString("],\n", data.get());
278 PagePopupClient::addString("}\n", data.get());
279 m_popup->postMessage(String(data->data(), data->size()));
280 }
281
282
283 void PopupMenuImpl::disconnectClient()
284 {
285 m_client = nullptr;
286 #if ENABLE(OILPAN)
287 // Cannot be done during finalization, so instead done when the
288 // render object is destroyed and disconnected.
289 //
290 // FIXME: do this always, regardless of ENABLE(OILPAN).
291 dispose();
292 #endif
293 }
294
295 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698