[macOS] Select element doesn't show popup if select element had lost focus while...
[WebKit-https.git] / Source / WebKitLegacy / mac / WebCoreSupport / PopupMenuMac.mm
1 /*
2  * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #import "PopupMenuMac.h"
22
23 #import "WebDelegateImplementationCaching.h"
24 #import "WebFrameInternal.h"
25 #import "WebUIDelegatePrivate.h"
26 #import <WebCore/IntRect.h>
27 #import <WebCore/AXObjectCache.h>
28 #import <WebCore/Chrome.h>
29 #import <WebCore/ChromeClient.h>
30 #import <WebCore/EventHandler.h>
31 #import <WebCore/Font.h>
32 #import <WebCore/Frame.h>
33 #import <WebCore/FrameView.h>
34 #import <WebCore/Page.h>
35 #import <WebCore/PopupMenuClient.h>
36 #import <pal/system/mac/PopupMenu.h>
37 #import <wtf/BlockObjCExceptions.h>
38
39 using namespace WebCore;
40
41 PopupMenuMac::PopupMenuMac(PopupMenuClient* client)
42     : m_client(client)
43 {
44 }
45
46 PopupMenuMac::~PopupMenuMac()
47 {
48     [m_popup setControlView:nil];
49 }
50
51 void PopupMenuMac::clear()
52 {
53     [m_popup removeAllItems];
54 }
55
56 void PopupMenuMac::populate()
57 {
58     if (m_popup)
59         clear();
60     else {
61         m_popup = adoptNS([[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:!m_client->shouldPopOver()]);
62         [m_popup setUsesItemFromMenu:NO];
63         [m_popup setAutoenablesItems:NO];
64     }
65     
66     // For pullDown menus the first item is hidden.
67     if (!m_client->shouldPopOver())
68         [m_popup addItemWithTitle:@""];
69
70     TextDirection menuTextDirection = m_client->menuStyle().textDirection();
71     [m_popup setUserInterfaceLayoutDirection:menuTextDirection == TextDirection::LTR ? NSUserInterfaceLayoutDirectionLeftToRight : NSUserInterfaceLayoutDirectionRightToLeft];
72
73     ASSERT(m_client);
74     int size = m_client->listSize();
75
76     for (int i = 0; i < size; i++) {
77         if (m_client->itemIsSeparator(i)) {
78             [[m_popup menu] addItem:[NSMenuItem separatorItem]];
79             continue;
80         }
81
82         PopupMenuStyle style = m_client->itemStyle(i);
83         RetainPtr<NSMutableDictionary> attributes = adoptNS([[NSMutableDictionary alloc] init]);
84         if (style.font() != FontCascade()) {
85             RetainPtr<CTFontRef> font = style.font().primaryFont().getCTFont();
86             if (!font) {
87                 CGFloat size = style.font().primaryFont().platformData().size();
88                 font = adoptCF(CTFontCreateUIFontForLanguage(isFontWeightBold(style.font().weight()) ? kCTFontUIFontEmphasizedSystem : kCTFontUIFontSystem, size, nullptr));
89             }
90             [attributes setObject:toNSFont(font.get()) forKey:NSFontAttributeName];
91         }
92
93         RetainPtr<NSMutableParagraphStyle> paragraphStyle = adoptNS([[NSParagraphStyle defaultParagraphStyle] mutableCopy]);
94         [paragraphStyle setAlignment:menuTextDirection == TextDirection::LTR ? NSTextAlignmentLeft : NSTextAlignmentRight];
95         NSWritingDirection writingDirection = style.textDirection() == TextDirection::LTR ? NSWritingDirectionLeftToRight : NSWritingDirectionRightToLeft;
96         [paragraphStyle setBaseWritingDirection:writingDirection];
97         if (style.hasTextDirectionOverride()) {
98             RetainPtr<NSNumber> writingDirectionValue = adoptNS([[NSNumber alloc] initWithInteger:writingDirection + NSWritingDirectionOverride]);
99             RetainPtr<NSArray> writingDirectionArray = adoptNS([[NSArray alloc] initWithObjects:writingDirectionValue.get(), nil]);
100             [attributes setObject:writingDirectionArray.get() forKey:NSWritingDirectionAttributeName];
101         }
102         [attributes setObject:paragraphStyle.get() forKey:NSParagraphStyleAttributeName];
103
104         // FIXME: Add support for styling the foreground and background colors.
105         // FIXME: Find a way to customize text color when an item is highlighted.
106         RetainPtr<NSAttributedString> string = adoptNS([[NSAttributedString alloc] initWithString:m_client->itemText(i) attributes:attributes.get()]);
107
108         [m_popup addItemWithTitle:@""];
109         NSMenuItem *menuItem = [m_popup lastItem];
110         [menuItem setAttributedTitle:string.get()];
111         // We set the title as well as the attributed title here. The attributed title will be displayed in the menu,
112         // but typeahead will use the non-attributed string that doesn't contain any leading or trailing whitespace.
113         [menuItem setTitle:[[string string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
114         [menuItem setEnabled:m_client->itemIsEnabled(i)];
115         [menuItem setToolTip:m_client->itemToolTip(i)];
116
117         ALLOW_DEPRECATED_DECLARATIONS_BEGIN
118         // Allow the accessible text of the item to be overridden if necessary.
119         if (AXObjectCache::accessibilityEnabled()) {
120             NSString *accessibilityOverride = m_client->itemAccessibilityText(i);
121             if ([accessibilityOverride length])
122                 [menuItem accessibilitySetOverrideValue:accessibilityOverride forAttribute:NSAccessibilityDescriptionAttribute];
123         }
124         ALLOW_DEPRECATED_DECLARATIONS_END
125     }
126 }
127
128 void PopupMenuMac::show(const IntRect& r, FrameView* v, int index)
129 {
130     populate();
131     int numItems = [m_popup numberOfItems];
132     if (numItems <= 0) {
133         if (m_client)
134             m_client->popupDidHide();
135         return;
136     }
137     ASSERT(numItems > index);
138
139     // Workaround for crazy bug where a selected index of -1 for a menu with only 1 item will cause a blank menu.
140     if (index == -1 && numItems == 2 && !m_client->shouldPopOver() && ![[m_popup itemAtIndex:1] isEnabled])
141         index = 0;
142
143     NSView* view = v->documentView();
144
145     TextDirection textDirection = m_client->menuStyle().textDirection();
146
147     [m_popup attachPopUpWithFrame:r inView:view];
148     [m_popup selectItemAtIndex:index];
149     [m_popup setUserInterfaceLayoutDirection:textDirection == TextDirection::LTR ? NSUserInterfaceLayoutDirectionLeftToRight : NSUserInterfaceLayoutDirectionRightToLeft];
150
151     NSMenu *menu = [m_popup menu];
152     [menu setUserInterfaceLayoutDirection:textDirection == TextDirection::LTR ? NSUserInterfaceLayoutDirectionLeftToRight : NSUserInterfaceLayoutDirectionRightToLeft];
153
154     NSPoint location;
155     CTFontRef font = m_client->menuStyle().font().primaryFont().getCTFont();
156
157     // These values were borrowed from AppKit to match their placement of the menu.
158     const int popOverHorizontalAdjust = -13;
159     const int popUnderHorizontalAdjust = 6;
160     const int popUnderVerticalAdjust = 6;
161     if (m_client->shouldPopOver()) {
162         NSRect titleFrame = [m_popup titleRectForBounds:r];
163         if (titleFrame.size.width <= 0 || titleFrame.size.height <= 0)
164             titleFrame = r;
165         float vertOffset = roundf((NSMaxY(r) - NSMaxY(titleFrame)) + NSHeight(titleFrame));
166         // Adjust for fonts other than the system font.
167         auto defaultFont = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, CTFontGetSize(font), nil));
168         vertOffset += CTFontGetDescent(font) - CTFontGetDescent(defaultFont.get());
169         vertOffset = fminf(NSHeight(r), vertOffset);
170         if (textDirection == TextDirection::LTR)
171             location = NSMakePoint(NSMinX(r) + popOverHorizontalAdjust, NSMaxY(r) - vertOffset);
172         else
173             location = NSMakePoint(NSMaxX(r) - popOverHorizontalAdjust, NSMaxY(r) - vertOffset);
174     } else {
175         if (textDirection == TextDirection::LTR)
176             location = NSMakePoint(NSMinX(r) + popUnderHorizontalAdjust, NSMaxY(r) + popUnderVerticalAdjust);
177         else
178             location = NSMakePoint(NSMaxX(r) - popUnderHorizontalAdjust, NSMaxY(r) + popUnderVerticalAdjust);
179     }
180     // Save the current event that triggered the popup, so we can clean up our event
181     // state after the NSMenu goes away.
182     Ref<Frame> frame(v->frame());
183     RetainPtr<NSEvent> event = frame->eventHandler().currentNSEvent();
184     
185     Ref<PopupMenuMac> protector(*this);
186
187     RetainPtr<NSView> dummyView = adoptNS([[NSView alloc] initWithFrame:r]);
188     [dummyView.get() setUserInterfaceLayoutDirection:textDirection == TextDirection::LTR ? NSUserInterfaceLayoutDirectionLeftToRight : NSUserInterfaceLayoutDirectionRightToLeft];
189     [view addSubview:dummyView.get()];
190     location = [dummyView convertPoint:location fromView:view];
191     
192     if (Page* page = frame->page()) {
193         WebView* webView = kit(page);
194         BEGIN_BLOCK_OBJC_EXCEPTIONS;
195         CallUIDelegate(webView, @selector(webView:willPopupMenu:), menu);
196         END_BLOCK_OBJC_EXCEPTIONS;
197     }
198
199     NSControlSize controlSize;
200     switch (m_client->menuStyle().menuSize()) {
201     case PopupMenuStyle::PopupMenuSizeNormal:
202         controlSize = NSControlSizeRegular;
203         break;
204     case PopupMenuStyle::PopupMenuSizeSmall:
205         controlSize = NSControlSizeSmall;
206         break;
207     case PopupMenuStyle::PopupMenuSizeMini:
208         controlSize = NSControlSizeMini;
209         break;
210     }
211
212     PAL::popUpMenu(menu, location, roundf(NSWidth(r)), dummyView.get(), index, toNSFont(font), controlSize, !m_client->menuStyle().hasDefaultAppearance());
213
214     [m_popup dismissPopUp];
215     [dummyView removeFromSuperview];
216
217     if (!m_client)
218         return;
219
220     int newIndex = [m_popup indexOfSelectedItem];
221     m_client->popupDidHide();
222
223     // Adjust newIndex for hidden first item.
224     if (!m_client->shouldPopOver())
225         newIndex--;
226
227     if (index != newIndex && newIndex >= 0)
228         m_client->valueChanged(newIndex);
229
230     // Give the frame a chance to fix up its event state, since the popup eats all the
231     // events during tracking.
232     frame->eventHandler().sendFakeEventsAfterWidgetTracking(event.get());
233 }
234
235 void PopupMenuMac::hide()
236 {
237     [[m_popup menu] cancelTracking];
238     if (m_client)
239         m_client->popupDidHide();
240 }
241
242 void PopupMenuMac::updateFromElement()
243 {
244 }
245
246 void PopupMenuMac::disconnectClient()
247 {
248     m_client = 0;
249 }