Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / widget / uikit / nsLookAndFeel.mm
blob522ce225678a70528b63007c749ce860e361da63
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #import <UIKit/UIColor.h>
7 #import <UIKit/UIInterface.h>
9 #include "nsLookAndFeel.h"
11 #include "mozilla/FontPropertyTypes.h"
12 #include "nsStyleConsts.h"
13 #include "gfxFont.h"
14 #include "gfxFontConstants.h"
16 using namespace mozilla;
18 nsLookAndFeel::nsLookAndFeel() : mInitialized(false) {}
20 nsLookAndFeel::~nsLookAndFeel() {}
22 static nscolor GetColorFromUIColor(UIColor* aColor) {
23   CGColorRef cgColor = [aColor CGColor];
24   CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(cgColor));
25   const CGFloat* components = CGColorGetComponents(cgColor);
26   if (model == kCGColorSpaceModelRGB) {
27     return NS_RGB((unsigned int)(components[0] * 255.0),
28                   (unsigned int)(components[1] * 255.0),
29                   (unsigned int)(components[2] * 255.0));
30   } else if (model == kCGColorSpaceModelMonochrome) {
31     unsigned int val = (unsigned int)(components[0] * 255.0);
32     return NS_RGBA(val, val, val, (unsigned int)(components[1] * 255.0));
33   }
34   MOZ_ASSERT_UNREACHABLE("Unhandled color space!");
35   return 0;
38 void nsLookAndFeel::NativeInit() { EnsureInit(); }
40 void nsLookAndFeel::RefreshImpl() {
41   nsXPLookAndFeel::RefreshImpl();
43   mInitialized = false;
46 nsresult nsLookAndFeel::NativeGetColor(ColorID aID, ColorScheme aColorScheme,
47                                        nscolor& aResult) {
48   EnsureInit();
50   nsresult res = NS_OK;
52   switch (aID) {
53     case ColorID::Highlight:
54       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
55       break;
56     case ColorID::MozMenuhover:
57       aResult = NS_RGB(0xee, 0xee, 0xee);
58       break;
59     case ColorID::Highlighttext:
60     case ColorID::MozMenuhovertext:
61       aResult = NS_SAME_AS_FOREGROUND_COLOR;
62       break;
63     case ColorID::IMESelectedRawTextBackground:
64     case ColorID::IMESelectedConvertedTextBackground:
65     case ColorID::IMERawInputBackground:
66     case ColorID::IMEConvertedTextBackground:
67       aResult = NS_TRANSPARENT;
68       break;
69     case ColorID::IMESelectedRawTextForeground:
70     case ColorID::IMESelectedConvertedTextForeground:
71     case ColorID::IMERawInputForeground:
72     case ColorID::IMEConvertedTextForeground:
73       aResult = NS_SAME_AS_FOREGROUND_COLOR;
74       break;
75     case ColorID::IMERawInputUnderline:
76     case ColorID::IMEConvertedTextUnderline:
77       aResult = NS_40PERCENT_FOREGROUND_COLOR;
78       break;
79     case ColorID::IMESelectedRawTextUnderline:
80     case ColorID::IMESelectedConvertedTextUnderline:
81       aResult = NS_SAME_AS_FOREGROUND_COLOR;
82       break;
83     case ColorID::SpellCheckerUnderline:
84       aResult = NS_RGB(0xff, 0, 0);
85       break;
87     //
88     // css2 system colors http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
89     //
90     case ColorID::Buttontext:
91     case ColorID::MozButtonhovertext:
92     case ColorID::Captiontext:
93     case ColorID::Menutext:
94     case ColorID::Infotext:
95     case ColorID::Windowtext:
96       aResult = mColorDarkText;
97       break;
98     case ColorID::Activecaption:
99       aResult = NS_RGB(0xff, 0xff, 0xff);
100       break;
101     case ColorID::Activeborder:
102       aResult = NS_RGB(0x00, 0x00, 0x00);
103       break;
104     case ColorID::Appworkspace:
105       aResult = NS_RGB(0xFF, 0xFF, 0xFF);
106       break;
107     case ColorID::Background:
108       aResult = NS_RGB(0x63, 0x63, 0xCE);
109       break;
110     case ColorID::Buttonface:
111     case ColorID::MozButtonhoverface:
112       aResult = NS_RGB(0xF0, 0xF0, 0xF0);
113       break;
114     case ColorID::Buttonhighlight:
115       aResult = NS_RGB(0xFF, 0xFF, 0xFF);
116       break;
117     case ColorID::Buttonshadow:
118       aResult = NS_RGB(0xDC, 0xDC, 0xDC);
119       break;
120     case ColorID::Graytext:
121       aResult = NS_RGB(0x44, 0x44, 0x44);
122       break;
123     case ColorID::Inactiveborder:
124       aResult = NS_RGB(0xff, 0xff, 0xff);
125       break;
126     case ColorID::Inactivecaption:
127       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
128       break;
129     case ColorID::Inactivecaptiontext:
130       aResult = NS_RGB(0x45, 0x45, 0x45);
131       break;
132     case ColorID::Scrollbar:
133       aResult = NS_RGB(0, 0, 0);  // XXX
134       break;
135     case ColorID::Threeddarkshadow:
136       aResult = NS_RGB(0xDC, 0xDC, 0xDC);
137       break;
138     case ColorID::Threedshadow:
139       aResult = NS_RGB(0xE0, 0xE0, 0xE0);
140       break;
141     case ColorID::Threedface:
142       aResult = NS_RGB(0xF0, 0xF0, 0xF0);
143       break;
144     case ColorID::Threedhighlight:
145       aResult = NS_RGB(0xff, 0xff, 0xff);
146       break;
147     case ColorID::Threedlightshadow:
148       aResult = NS_RGB(0xDA, 0xDA, 0xDA);
149       break;
150     case ColorID::Menu:
151       aResult = NS_RGB(0xff, 0xff, 0xff);
152       break;
153     case ColorID::Infobackground:
154       aResult = NS_RGB(0xFF, 0xFF, 0xC7);
155       break;
156     case ColorID::Windowframe:
157       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
158       break;
159     case ColorID::Window:
160     case ColorID::Field:
161     case ColorID::MozCombobox:
162       aResult = NS_RGB(0xff, 0xff, 0xff);
163       break;
164     case ColorID::Fieldtext:
165     case ColorID::MozComboboxtext:
166       aResult = mColorDarkText;
167       break;
168     case ColorID::MozDialog:
169       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
170       break;
171     case ColorID::MozDialogtext:
172     case ColorID::MozCellhighlighttext:
173     case ColorID::Selecteditemtext:
174     case ColorID::MozColheadertext:
175     case ColorID::MozColheaderhovertext:
176       aResult = mColorDarkText;
177       break;
178     case ColorID::MozMacFocusring:
179       aResult = NS_RGB(0x3F, 0x98, 0xDD);
180       break;
181     case ColorID::MozMacDisabledtoolbartext:
182       aResult = NS_RGB(0x3F, 0x3F, 0x3F);
183       break;
184     case ColorID::MozCellhighlight:
185     case ColorID::Selecteditem:
186       // For inactive list selection
187       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
188       break;
189     case ColorID::MozEventreerow:
190       // Background color of even list rows.
191       aResult = NS_RGB(0xff, 0xff, 0xff);
192       break;
193     case ColorID::MozOddtreerow:
194       // Background color of odd list rows.
195       aResult = NS_TRANSPARENT;
196       break;
197     case ColorID::MozNativehyperlinktext:
198       // There appears to be no available system defined color. HARDCODING to
199       // the appropriate color.
200       aResult = NS_RGB(0x14, 0x4F, 0xAE);
201       break;
202     case ColorID::MozNativevisitedhyperlinktext:
203       // Safari defaults to the MacOS color implementation for visited links,
204       // which in turn uses systemPurpleColor, so we do the same here.
205       aResult = GetColorFromUIColor([UIColor systemPurpleColor]);
206       break;
207     case ColorID::TargetTextBackground:
208     case ColorID::TargetTextForeground:
209       aResult = GetStandinForNativeColor(aID, aColorScheme);
210       break;
211     default:
212       NS_WARNING("Someone asked nsILookAndFeel for a color I don't know about");
213       aResult = NS_RGB(0xff, 0xff, 0xff);
214       res = NS_ERROR_FAILURE;
215       break;
216   }
218   return res;
221 NS_IMETHODIMP
222 nsLookAndFeel::NativeGetInt(IntID aID, int32_t& aResult) {
223   nsresult res = NS_OK;
225   switch (aID) {
226     case IntID::ScrollButtonLeftMouseButtonAction:
227       aResult = 0;
228       break;
229     case IntID::ScrollButtonMiddleMouseButtonAction:
230     case IntID::ScrollButtonRightMouseButtonAction:
231       aResult = 3;
232       break;
233     case IntID::CaretBlinkTime:
234       aResult = 567;
235       break;
236     case IntID::CaretWidth:
237       aResult = 1;
238       break;
239     case IntID::SelectTextfieldsOnKeyFocus:
240       // Select textfield content when focused by kbd
241       // used by nsEventStateManager::sTextfieldSelectModel
242       aResult = 1;
243       break;
244     case IntID::SubmenuDelay:
245       aResult = 200;
246       break;
247     case IntID::MenusCanOverlapOSBar:
248       // xul popups are not allowed to overlap the menubar.
249       aResult = 0;
250       break;
251     case IntID::SkipNavigatingDisabledMenuItem:
252       aResult = 1;
253       break;
254     case IntID::DragThresholdX:
255     case IntID::DragThresholdY:
256       aResult = 4;
257       break;
258     case IntID::ScrollArrowStyle:
259       aResult = eScrollArrow_None;
260       break;
261     case IntID::UseOverlayScrollbars:
262     case IntID::AllowOverlayScrollbarsOverlap:
263       aResult = 1;
264       break;
265     case IntID::ScrollbarDisplayOnMouseMove:
266       aResult = 0;
267       break;
268     case IntID::ScrollbarFadeBeginDelay:
269       aResult = 450;
270       break;
271     case IntID::ScrollbarFadeDuration:
272       aResult = 200;
273       break;
274     case IntID::TreeOpenDelay:
275       aResult = 1000;
276       break;
277     case IntID::TreeCloseDelay:
278       aResult = 1000;
279       break;
280     case IntID::TreeLazyScrollDelay:
281       aResult = 150;
282       break;
283     case IntID::TreeScrollDelay:
284       aResult = 100;
285       break;
286     case IntID::TreeScrollLinesMax:
287       aResult = 3;
288       break;
289     case IntID::ScrollToClick:
290       aResult = 0;
291       break;
292     case IntID::ChosenMenuItemsShouldBlink:
293       aResult = 1;
294       break;
295     case IntID::IMERawInputUnderlineStyle:
296     case IntID::IMEConvertedTextUnderlineStyle:
297     case IntID::IMESelectedRawTextUnderlineStyle:
298     case IntID::IMESelectedConvertedTextUnderline:
299       aResult = static_cast<int32_t>(StyleTextDecorationStyle::Solid);
300       break;
301     case IntID::SpellCheckerUnderlineStyle:
302       aResult = static_cast<int32_t>(StyleTextDecorationStyle::Dotted);
303       break;
304     case IntID::ContextMenuOffsetVertical:
305     case IntID::ContextMenuOffsetHorizontal:
306       aResult = 2;
307       break;
308     default:
309       aResult = 0;
310       res = NS_ERROR_FAILURE;
311   }
312   return res;
315 NS_IMETHODIMP
316 nsLookAndFeel::NativeGetFloat(FloatID aID, float& aResult) {
317   nsresult res = NS_OK;
319   switch (aID) {
320     case FloatID::IMEUnderlineRelativeSize:
321       aResult = 2.0f;
322       break;
323     case FloatID::SpellCheckerUnderlineRelativeSize:
324       aResult = 2.0f;
325       break;
326     default:
327       aResult = -1.0;
328       res = NS_ERROR_FAILURE;
329   }
331   return res;
334 bool nsLookAndFeel::NativeGetFont(FontID aID, nsString& aFontName,
335                                   gfxFontStyle& aFontStyle) {
336   // hack for now
337   if (aID == FontID::Caption || aID == FontID::Menu) {
338     aFontStyle.style = FontSlantStyle::NORMAL;
339     aFontStyle.weight = FontWeight::NORMAL;
340     aFontStyle.stretch = FontStretch::NORMAL;
341     aFontStyle.size = 14;
342     aFontStyle.systemFont = true;
344     aFontName.AssignLiteral("sans-serif");
345     return true;
346   }
348   // TODO: implement more here?
349   return false;
352 void nsLookAndFeel::EnsureInit() {
353   if (mInitialized) {
354     return;
355   }
356   mInitialized = true;
358   mColorDarkText = GetColorFromUIColor([UIColor darkTextColor]);
360   RecordTelemetry();