1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "HeadlessLookAndFeel.h"
8 #include "mozilla/FontPropertyTypes.h"
9 #include "nsIContent.h"
10 
11 namespace mozilla::widget {
12 
13 static const char16_t UNICODE_BULLET = 0x2022;
14 
HeadlessLookAndFeel()15 HeadlessLookAndFeel::HeadlessLookAndFeel() {}
16 
17 HeadlessLookAndFeel::~HeadlessLookAndFeel() = default;
18 
NativeGetColor(ColorID aID,ColorScheme,nscolor & aColor)19 nsresult HeadlessLookAndFeel::NativeGetColor(ColorID aID, ColorScheme,
20                                              nscolor& aColor) {
21   // For headless mode, we use GetStandinForNativeColor for everything we can,
22   // and hardcoded values for everything else.
23 
24   nsresult res = NS_OK;
25 
26   switch (aID) {
27     // Override the solid black that GetStandinForNativeColor provides for
28     // FieldText, to match our behavior under the real GTK.
29     case ColorID::Fieldtext:
30       aColor = NS_RGB(0x21, 0x21, 0x21);
31       break;
32 
33     // The rest are not provided by GetStandinForNativeColor.
34     case ColorID::IMESelectedRawTextBackground:
35     case ColorID::IMESelectedConvertedTextBackground:
36     case ColorID::IMERawInputBackground:
37     case ColorID::IMEConvertedTextBackground:
38       aColor = NS_TRANSPARENT;
39       break;
40     case ColorID::IMESelectedRawTextForeground:
41     case ColorID::IMESelectedConvertedTextForeground:
42     case ColorID::IMERawInputForeground:
43     case ColorID::IMEConvertedTextForeground:
44       aColor = NS_SAME_AS_FOREGROUND_COLOR;
45       break;
46     case ColorID::IMERawInputUnderline:
47     case ColorID::IMEConvertedTextUnderline:
48       aColor = NS_40PERCENT_FOREGROUND_COLOR;
49       break;
50     case ColorID::IMESelectedRawTextUnderline:
51     case ColorID::IMESelectedConvertedTextUnderline:
52       aColor = NS_SAME_AS_FOREGROUND_COLOR;
53       break;
54     case ColorID::MozEventreerow:
55       aColor = NS_RGB(0xff, 0xff, 0xff);
56       break;
57     case ColorID::MozMacButtonactivetext:
58     case ColorID::MozMacDefaultbuttontext:
59       aColor = NS_RGB(0xff, 0xff, 0xff);
60       break;
61     case ColorID::SpellCheckerUnderline:
62       aColor = NS_RGB(0xff, 0x00, 0x00);
63       break;
64     case ColorID::TextBackground:
65       aColor = NS_RGB(0xff, 0xff, 0xff);
66       break;
67     case ColorID::TextForeground:
68       aColor = NS_RGB(0x00, 0x00, 0x00);
69       break;
70     case ColorID::TextHighlightBackground:
71       aColor = NS_RGB(0xef, 0x0f, 0xff);
72       break;
73     case ColorID::TextHighlightForeground:
74       aColor = NS_RGB(0xff, 0xff, 0xff);
75       break;
76     case ColorID::TextSelectBackground:
77       aColor = NS_RGB(0xaa, 0xaa, 0xaa);
78       break;
79     case ColorID::TextSelectBackgroundAttention:
80       aColor = NS_TRANSPARENT;
81       break;
82     case ColorID::TextSelectBackgroundDisabled:
83       aColor = NS_RGB(0xaa, 0xaa, 0xaa);
84       break;
85     case ColorID::TextSelectForeground:
86       aColor = NS_SAME_AS_FOREGROUND_COLOR;
87       break;
88     case ColorID::Widget3DHighlight:
89       aColor = NS_RGB(0xa0, 0xa0, 0xa0);
90       break;
91     case ColorID::Widget3DShadow:
92       aColor = NS_RGB(0x40, 0x40, 0x40);
93       break;
94     case ColorID::WidgetBackground:
95       aColor = NS_RGB(0xdd, 0xdd, 0xdd);
96       break;
97     case ColorID::WidgetForeground:
98       aColor = NS_RGB(0x00, 0x00, 0x00);
99       break;
100     case ColorID::WidgetSelectBackground:
101       aColor = NS_RGB(0x80, 0x80, 0x80);
102       break;
103     case ColorID::WidgetSelectForeground:
104       aColor = NS_RGB(0x00, 0x00, 0x80);
105       break;
106     case ColorID::WindowBackground:
107       aColor = NS_RGB(0xff, 0xff, 0xff);
108       break;
109     case ColorID::WindowForeground:
110       aColor = NS_RGB(0x00, 0x00, 0x00);
111       break;
112     case ColorID::Highlight:
113     case ColorID::MozAccentColor:
114       aColor = NS_RGB(53, 132, 228);
115       break;
116     case ColorID::Highlighttext:
117     case ColorID::MozAccentColorForeground:
118       aColor = NS_RGB(0xff, 0xff, 0xff);
119       break;
120     default:
121       aColor = GetStandinForNativeColor(aID);
122       break;
123   }
124 
125   return res;
126 }
127 
NativeGetInt(IntID aID,int32_t & aResult)128 nsresult HeadlessLookAndFeel::NativeGetInt(IntID aID, int32_t& aResult) {
129   nsresult res = NS_OK;
130   // These values should be sane defaults for headless mode under GTK.
131   switch (aID) {
132     case IntID::CaretBlinkTime:
133       aResult = 567;
134       break;
135     case IntID::CaretWidth:
136       aResult = 1;
137       break;
138     case IntID::ShowCaretDuringSelection:
139       aResult = 0;
140       break;
141     case IntID::SelectTextfieldsOnKeyFocus:
142       aResult = 1;
143       break;
144     case IntID::SubmenuDelay:
145       aResult = 200;
146       break;
147     case IntID::MenusCanOverlapOSBar:
148       aResult = 0;
149       break;
150     case IntID::UseOverlayScrollbars:
151       aResult = 0;
152       break;
153     case IntID::AllowOverlayScrollbarsOverlap:
154       aResult = 0;
155       break;
156     case IntID::ShowHideScrollbars:
157       aResult = 0;
158       break;
159     case IntID::SkipNavigatingDisabledMenuItem:
160       aResult = 1;
161       break;
162     case IntID::DragThresholdX:
163     case IntID::DragThresholdY:
164       aResult = 4;
165       break;
166     case IntID::UseAccessibilityTheme:
167       aResult = 0;
168       break;
169     case IntID::ScrollArrowStyle:
170       aResult = eScrollArrow_None;
171       break;
172     case IntID::ScrollSliderStyle:
173       aResult = eScrollThumbStyle_Proportional;
174       break;
175     case IntID::ScrollButtonLeftMouseButtonAction:
176       aResult = 0;
177       return NS_OK;
178     case IntID::ScrollButtonMiddleMouseButtonAction:
179       aResult = 3;
180       return NS_OK;
181     case IntID::ScrollButtonRightMouseButtonAction:
182       aResult = 3;
183       return NS_OK;
184     case IntID::TreeOpenDelay:
185       aResult = 1000;
186       break;
187     case IntID::TreeCloseDelay:
188       aResult = 1000;
189       break;
190     case IntID::TreeLazyScrollDelay:
191       aResult = 150;
192       break;
193     case IntID::TreeScrollDelay:
194       aResult = 100;
195       break;
196     case IntID::TreeScrollLinesMax:
197       aResult = 3;
198       break;
199     case IntID::TabFocusModel:
200       aResult = nsIContent::eTabFocus_textControlsMask;
201       break;
202     case IntID::ChosenMenuItemsShouldBlink:
203       aResult = 1;
204       break;
205     case IntID::WindowsAccentColorInTitlebar:
206     case IntID::WindowsDefaultTheme:
207     case IntID::DWMCompositor:
208       aResult = 0;
209       res = NS_ERROR_NOT_IMPLEMENTED;
210       break;
211     case IntID::WindowsClassic:
212     case IntID::WindowsGlass:
213       aResult = 0;
214       res = NS_ERROR_FAILURE;
215       break;
216     case IntID::MacGraphiteTheme:
217     case IntID::MacBigSurTheme:
218       aResult = 0;
219       res = NS_ERROR_NOT_IMPLEMENTED;
220       break;
221     case IntID::AlertNotificationOrigin:
222       aResult = NS_ALERT_TOP;
223       break;
224     case IntID::ScrollToClick:
225       aResult = 0;
226       break;
227     case IntID::IMERawInputUnderlineStyle:
228     case IntID::IMESelectedRawTextUnderlineStyle:
229     case IntID::IMEConvertedTextUnderlineStyle:
230     case IntID::IMESelectedConvertedTextUnderline:
231       aResult = NS_STYLE_TEXT_DECORATION_STYLE_SOLID;
232       break;
233     case IntID::SpellCheckerUnderlineStyle:
234       aResult = NS_STYLE_TEXT_DECORATION_STYLE_DOTTED;
235       break;
236     case IntID::MenuBarDrag:
237       aResult = 0;
238       break;
239     case IntID::WindowsThemeIdentifier:
240     case IntID::OperatingSystemVersionIdentifier:
241       aResult = 0;
242       res = NS_ERROR_NOT_IMPLEMENTED;
243       break;
244     case IntID::ScrollbarButtonAutoRepeatBehavior:
245       aResult = 0;
246       break;
247     case IntID::TooltipDelay:
248       aResult = 500;
249       break;
250     case IntID::SwipeAnimationEnabled:
251       aResult = 0;
252       break;
253     case IntID::ScrollbarDisplayOnMouseMove:
254       aResult = 0;
255       break;
256     case IntID::ScrollbarFadeBeginDelay:
257       aResult = 0;
258       break;
259     case IntID::ScrollbarFadeDuration:
260       aResult = 0;
261       break;
262     case IntID::ContextMenuOffsetVertical:
263       aResult = -6;
264       break;
265     case IntID::ContextMenuOffsetHorizontal:
266       aResult = 1;
267       break;
268     case IntID::GTKCSDAvailable:
269     case IntID::GTKCSDHideTitlebarByDefault:
270     case IntID::GTKCSDTransparentBackground:
271       aResult = 0;
272       break;
273     case IntID::GTKCSDMinimizeButton:
274       aResult = 0;
275       break;
276     case IntID::GTKCSDMaximizeButton:
277       aResult = 0;
278       break;
279     case IntID::GTKCSDCloseButton:
280       aResult = 1;
281       break;
282     case IntID::GTKCSDReversedPlacement:
283       aResult = 0;
284       break;
285     case IntID::SystemUsesDarkTheme:
286       aResult = 0;
287       break;
288     case IntID::PrefersReducedMotion:
289       aResult = 0;
290       break;
291     case IntID::PrimaryPointerCapabilities:
292       aResult = 0;
293       break;
294     case IntID::AllPointerCapabilities:
295       aResult = 0;
296       break;
297     default:
298       NS_WARNING(
299           "HeadlessLookAndFeel::NativeGetInt called with an unrecognized aID");
300       aResult = 0;
301       res = NS_ERROR_FAILURE;
302       break;
303   }
304   return res;
305 }
306 
NativeGetFloat(FloatID aID,float & aResult)307 nsresult HeadlessLookAndFeel::NativeGetFloat(FloatID aID, float& aResult) {
308   nsresult res = NS_OK;
309 
310   // Hardcoded values for GTK.
311   switch (aID) {
312     case FloatID::IMEUnderlineRelativeSize:
313       aResult = 1.0f;
314       break;
315     case FloatID::SpellCheckerUnderlineRelativeSize:
316       aResult = 1.0f;
317       break;
318     case FloatID::CaretAspectRatio:
319       // Intentionally failing to quietly indicate lack of support.
320       aResult = -1.0;
321       res = NS_ERROR_FAILURE;
322       break;
323     default:
324       NS_WARNING(
325           "HeadlessLookAndFeel::NativeGetFloat called with an unrecognized "
326           "aID");
327       aResult = -1.0;
328       res = NS_ERROR_FAILURE;
329       break;
330   }
331 
332   return res;
333 }
334 
NativeGetFont(FontID aID,nsString & aFontName,gfxFontStyle & aFontStyle)335 bool HeadlessLookAndFeel::NativeGetFont(FontID aID, nsString& aFontName,
336                                         gfxFontStyle& aFontStyle) {
337   // Default to san-serif for everything.
338   aFontStyle.style = FontSlantStyle::Normal();
339   aFontStyle.weight = FontWeight::Normal();
340   aFontStyle.stretch = FontStretch::Normal();
341   aFontStyle.size = 14;
342   aFontStyle.systemFont = true;
343 
344   aFontName.AssignLiteral("sans-serif");
345   return true;
346 }
347 
GetPasswordCharacterImpl()348 char16_t HeadlessLookAndFeel::GetPasswordCharacterImpl() {
349   return UNICODE_BULLET;
350 }
351 
RefreshImpl()352 void HeadlessLookAndFeel::RefreshImpl() { nsXPLookAndFeel::RefreshImpl(); }
353 
GetEchoPasswordImpl()354 bool HeadlessLookAndFeel::GetEchoPasswordImpl() { return false; }
355 
356 }  // namespace mozilla::widget
357