Bug 1779627 - Migrate toolkit/components/mozintl/mozIntl.jsm to esm; r=nordzilla
[gecko.git] / widget / uikit / nsLookAndFeel.mm
blob6e36efe080178768e6051fcc87d5aba8a7526dd1
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 nsLookAndFeel::nsLookAndFeel() : mInitialized(false) {}
18 nsLookAndFeel::~nsLookAndFeel() {}
20 static nscolor GetColorFromUIColor(UIColor* aColor) {
21   CGColorRef cgColor = [aColor CGColor];
22   CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(cgColor));
23   const CGFloat* components = CGColorGetComponents(cgColor);
24   if (model == kCGColorSpaceModelRGB) {
25     return NS_RGB((unsigned int)(components[0] * 255.0), (unsigned int)(components[1] * 255.0),
26                   (unsigned int)(components[2] * 255.0));
27   } else if (model == kCGColorSpaceModelMonochrome) {
28     unsigned int val = (unsigned int)(components[0] * 255.0);
29     return NS_RGBA(val, val, val, (unsigned int)(components[1] * 255.0));
30   }
31   MOZ_ASSERT_UNREACHABLE("Unhandled color space!");
32   return 0;
35 void nsLookAndFeel::NativeInit() { EnsureInit(); }
37 void nsLookAndFeel::RefreshImpl() {
38   nsXPLookAndFeel::RefreshImpl();
40   mInitialized = false;
43 nsresult nsLookAndFeel::NativeGetColor(ColorID, ColorScheme, nscolor& aResult) {
44   EnsureInit();
46   nsresult res = NS_OK;
48   switch (aID) {
49     case ColorID::Highlight:
50       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
51       break;
52     case ColorID::MozMenuhover:
53       aResult = NS_RGB(0xee, 0xee, 0xee);
54       break;
55     case ColorID::Highlighttext:
56     case ColorID::MozMenuhovertext:
57       aResult = mColorTextSelectForeground;
58       break;
59     case ColorID::IMESelectedRawTextBackground:
60     case ColorID::IMESelectedConvertedTextBackground:
61     case ColorID::IMERawInputBackground:
62     case ColorID::IMEConvertedTextBackground:
63       aResult = NS_TRANSPARENT;
64       break;
65     case ColorID::IMESelectedRawTextForeground:
66     case ColorID::IMESelectedConvertedTextForeground:
67     case ColorID::IMERawInputForeground:
68     case ColorID::IMEConvertedTextForeground:
69       aResult = NS_SAME_AS_FOREGROUND_COLOR;
70       break;
71     case ColorID::IMERawInputUnderline:
72     case ColorID::IMEConvertedTextUnderline:
73       aResult = NS_40PERCENT_FOREGROUND_COLOR;
74       break;
75     case ColorID::IMESelectedRawTextUnderline:
76     case ColorID::IMESelectedConvertedTextUnderline:
77       aResult = NS_SAME_AS_FOREGROUND_COLOR;
78       break;
79     case ColorID::SpellCheckerUnderline:
80       aResult = NS_RGB(0xff, 0, 0);
81       break;
83     //
84     // css2 system colors http://www.w3.org/TR/REC-CSS2/ui.html#system-colors
85     //
86     case ColorID::Buttontext:
87     case ColorID::MozButtonhovertext:
88     case ColorID::Captiontext:
89     case ColorID::Menutext:
90     case ColorID::Infotext:
91     case ColorID::MozMenubartext:
92     case ColorID::Windowtext:
93       aResult = mColorDarkText;
94       break;
95     case ColorID::Activecaption:
96       aResult = NS_RGB(0xff, 0xff, 0xff);
97       break;
98     case ColorID::Activeborder:
99       aResult = NS_RGB(0x00, 0x00, 0x00);
100       break;
101     case ColorID::Appworkspace:
102       aResult = NS_RGB(0xFF, 0xFF, 0xFF);
103       break;
104     case ColorID::Background:
105       aResult = NS_RGB(0x63, 0x63, 0xCE);
106       break;
107     case ColorID::Buttonface:
108     case ColorID::MozButtonhoverface:
109       aResult = NS_RGB(0xF0, 0xF0, 0xF0);
110       break;
111     case ColorID::Buttonhighlight:
112       aResult = NS_RGB(0xFF, 0xFF, 0xFF);
113       break;
114     case ColorID::Buttonshadow:
115       aResult = NS_RGB(0xDC, 0xDC, 0xDC);
116       break;
117     case ColorID::Graytext:
118       aResult = NS_RGB(0x44, 0x44, 0x44);
119       break;
120     case ColorID::Inactiveborder:
121       aResult = NS_RGB(0xff, 0xff, 0xff);
122       break;
123     case ColorID::Inactivecaption:
124       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
125       break;
126     case ColorID::Inactivecaptiontext:
127       aResult = NS_RGB(0x45, 0x45, 0x45);
128       break;
129     case ColorID::Scrollbar:
130       aResult = NS_RGB(0, 0, 0);  // XXX
131       break;
132     case ColorID::Threeddarkshadow:
133       aResult = NS_RGB(0xDC, 0xDC, 0xDC);
134       break;
135     case ColorID::Threedshadow:
136       aResult = NS_RGB(0xE0, 0xE0, 0xE0);
137       break;
138     case ColorID::Threedface:
139       aResult = NS_RGB(0xF0, 0xF0, 0xF0);
140       break;
141     case ColorID::Threedhighlight:
142       aResult = NS_RGB(0xff, 0xff, 0xff);
143       break;
144     case ColorID::Threedlightshadow:
145       aResult = NS_RGB(0xDA, 0xDA, 0xDA);
146       break;
147     case ColorID::Menu:
148       aResult = NS_RGB(0xff, 0xff, 0xff);
149       break;
150     case ColorID::Infobackground:
151       aResult = NS_RGB(0xFF, 0xFF, 0xC7);
152       break;
153     case ColorID::Windowframe:
154       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
155       break;
156     case ColorID::Window:
157     case ColorID::MozField:
158     case ColorID::MozCombobox:
159       aResult = NS_RGB(0xff, 0xff, 0xff);
160       break;
161     case ColorID::MozFieldtext:
162     case ColorID::MozComboboxtext:
163       aResult = mColorDarkText;
164       break;
165     case ColorID::MozDialog:
166       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
167       break;
168     case ColorID::MozDialogtext:
169     case ColorID::MozCellhighlighttext:
170     case ColorID::Selecteditemtext:
171     case ColorID::MozColheadertext:
172     case ColorID::MozColheaderhovertext:
173       aResult = mColorDarkText;
174       break;
175     case ColorID::MozDragtargetzone:
176     case ColorID::MozMacChromeActive:
177     case ColorID::MozMacChromeInactive:
178       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
179       break;
180     case ColorID::MozMacFocusring:
181       aResult = NS_RGB(0x3F, 0x98, 0xDD);
182       break;
183     case ColorID::MozMacMenushadow:
184       aResult = NS_RGB(0xA3, 0xA3, 0xA3);
185       break;
186     case ColorID::MozMacMenutextdisable:
187       aResult = NS_RGB(0x88, 0x88, 0x88);
188       break;
189     case ColorID::MozMacMenutextselect:
190       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
191       break;
192     case ColorID::MozMacDisabledtoolbartext:
193       aResult = NS_RGB(0x3F, 0x3F, 0x3F);
194       break;
195     case ColorID::MozMacMenuselect:
196       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
197       break;
198     case ColorID::MozButtondefault:
199       aResult = NS_RGB(0xDC, 0xDC, 0xDC);
200       break;
201     case ColorID::MozCellhighlight:
202     case ColorID::Selecteditem:
203     case ColorID::MozMacSecondaryhighlight:
204       // For inactive list selection
205       aResult = NS_RGB(0xaa, 0xaa, 0xaa);
206       break;
207     case ColorID::MozEventreerow:
208       // Background color of even list rows.
209       aResult = NS_RGB(0xff, 0xff, 0xff);
210       break;
211     case ColorID::MozOddtreerow:
212       // Background color of odd list rows.
213       aResult = NS_TRANSPARENT;
214       break;
215     case ColorID::MozNativehyperlinktext:
216       // There appears to be no available system defined color. HARDCODING to the appropriate color.
217       aResult = NS_RGB(0x14, 0x4F, 0xAE);
218       break;
219     case ColorID::MozNativevisitedhyperlinktext:
220       // Safari defaults to the MacOS color implementation for visited links, which in turn uses
221       // systemPurpleColor, so we do the same here.
222       aResult = GetColorFromUIColor([UIColor systemPurpleColor]);
223       break;
224     default:
225       NS_WARNING("Someone asked nsILookAndFeel for a color I don't know about");
226       aResult = NS_RGB(0xff, 0xff, 0xff);
227       res = NS_ERROR_FAILURE;
228       break;
229   }
231   return res;
234 NS_IMETHODIMP
235 nsLookAndFeel::NativeGetInt(IntID aID, int32_t& aResult) {
236   nsresult res = NS_OK;
238   switch (aID) {
239     case IntID::ScrollButtonLeftMouseButtonAction:
240       aResult = 0;
241       break;
242     case IntID::ScrollButtonMiddleMouseButtonAction:
243     case IntID::ScrollButtonRightMouseButtonAction:
244       aResult = 3;
245       break;
246     case IntID::CaretBlinkTime:
247       aResult = 567;
248       break;
249     case IntID::CaretWidth:
250       aResult = 1;
251       break;
252     case IntID::ShowCaretDuringSelection:
253       aResult = 0;
254       break;
255     case IntID::SelectTextfieldsOnKeyFocus:
256       // Select textfield content when focused by kbd
257       // used by nsEventStateManager::sTextfieldSelectModel
258       aResult = 1;
259       break;
260     case IntID::SubmenuDelay:
261       aResult = 200;
262       break;
263     case IntID::MenusCanOverlapOSBar:
264       // xul popups are not allowed to overlap the menubar.
265       aResult = 0;
266       break;
267     case IntID::SkipNavigatingDisabledMenuItem:
268       aResult = 1;
269       break;
270     case IntID::DragThresholdX:
271     case IntID::DragThresholdY:
272       aResult = 4;
273       break;
274     case IntID::ScrollArrowStyle:
275       aResult = eScrollArrow_None;
276       break;
277     case IntID::TreeOpenDelay:
278       aResult = 1000;
279       break;
280     case IntID::TreeCloseDelay:
281       aResult = 1000;
282       break;
283     case IntID::TreeLazyScrollDelay:
284       aResult = 150;
285       break;
286     case IntID::TreeScrollDelay:
287       aResult = 100;
288       break;
289     case IntID::TreeScrollLinesMax:
290       aResult = 3;
291       break;
292     case IntID::DWMCompositor:
293     case IntID::WindowsClassic:
294     case IntID::WindowsDefaultTheme:
295       aResult = 0;
296       res = NS_ERROR_NOT_IMPLEMENTED;
297       break;
298     case IntID::MacGraphiteTheme:
299       aResult = 0;
300       break;
301     case IntID::TabFocusModel:
302       aResult = 1;  // default to just textboxes
303       break;
304     case IntID::ScrollToClick:
305       aResult = 0;
306       break;
307     case IntID::ChosenMenuItemsShouldBlink:
308       aResult = 1;
309       break;
310     case IntID::IMERawInputUnderlineStyle:
311     case IntID::IMEConvertedTextUnderlineStyle:
312     case IntID::IMESelectedRawTextUnderlineStyle:
313     case IntID::IMESelectedConvertedTextUnderline:
314       aResult = NS_STYLE_TEXT_DECORATION_STYLE_SOLID;
315       break;
316     case IntID::SpellCheckerUnderlineStyle:
317       aResult = NS_STYLE_TEXT_DECORATION_STYLE_DOTTED;
318       break;
319     case IntID::ContextMenuOffsetVertical:
320     case IntID::ContextMenuOffsetHorizontal:
321       aResult = 2;
322       break;
323     default:
324       aResult = 0;
325       res = NS_ERROR_FAILURE;
326   }
327   return res;
330 NS_IMETHODIMP
331 nsLookAndFeel::NativeGetFloat(FloatID aID, float& aResult) {
332   nsresult res = NS_OK;
334   switch (aID) {
335     case FloatID::IMEUnderlineRelativeSize:
336       aResult = 2.0f;
337       break;
338     case FloatID::SpellCheckerUnderlineRelativeSize:
339       aResult = 2.0f;
340       break;
341     default:
342       aResult = -1.0;
343       res = NS_ERROR_FAILURE;
344   }
346   return res;
349 bool nsLookAndFeel::NativeGetFont(FontID aID, nsString& aFontName, gfxFontStyle& aFontStyle) {
350   // hack for now
351   if (aID == FontID::Window || aID == FontID::Document) {
352     aFontStyle.style = FontSlantStyle::Normal();
353     aFontStyle.weight = FontWeight::Normal();
354     aFontStyle.stretch = FontStretch::Normal();
355     aFontStyle.size = 14;
356     aFontStyle.systemFont = true;
358     aFontName.AssignLiteral("sans-serif");
359     return true;
360   }
362   // TODO: implement more here?
363   return false;
366 void nsLookAndFeel::EnsureInit() {
367   if (mInitialized) {
368     return;
369   }
370   mInitialized = true;
372   nscolor color;
373   GetColor(ColorID::TextSelectBackground, color);
374   if (color == 0x000000) {
375     mColorTextSelectForeground = NS_RGB(0xff, 0xff, 0xff);
376   } else {
377     mColorTextSelectForeground = NS_SAME_AS_FOREGROUND_COLOR;
378   }
380   mColorDarkText = GetColorFromUIColor([UIColor darkTextColor]);
382   RecordTelemetry();