|
42 | 42 | import com.axelor.script.CompositeScriptHelper;
|
43 | 43 | import com.axelor.script.ScriptBindings;
|
44 | 44 | import com.axelor.script.ScriptHelper;
|
| 45 | +import java.lang.reflect.Method; |
45 | 46 | import java.util.Collection;
|
46 | 47 | import java.util.HashMap;
|
47 | 48 | import java.util.List;
|
48 | 49 | import java.util.Map;
|
49 | 50 | import java.util.Optional;
|
50 | 51 | import java.util.Set;
|
| 52 | +import java.util.function.Supplier; |
51 | 53 | import java.util.stream.Collectors;
|
52 | 54 | import javax.inject.Inject;
|
53 | 55 | import javax.servlet.http.HttpServletRequest;
|
@@ -95,8 +97,6 @@ private Map<String, Object> appInfo() {
|
95 | 97 | SETTINGS.format(
|
96 | 98 | I18n.get(SETTINGS.getProperties().get(AvailableAppSettings.APPLICATION_COPYRIGHT))));
|
97 | 99 | map.put("theme", SETTINGS.get(AvailableAppSettings.APPLICATION_THEME, null));
|
98 |
| - map.put("logo", getLogoLink()); |
99 |
| - map.put("icon", getIconLink()); |
100 | 100 | map.put("lang", AppFilter.getLocale().toLanguageTag());
|
101 | 101 |
|
102 | 102 | final Map<String, Object> signIn = signInInfo();
|
@@ -318,59 +318,96 @@ private Object featuresInfo() {
|
318 | 318 | *
|
319 | 319 | * <p>The returned image can be a resource path string, a URL string or a MetaFile
|
320 | 320 | *
|
| 321 | + * @param mode light or dark |
321 | 322 | * @return user specific logo
|
322 | 323 | */
|
323 |
| - public Object getLogo() { |
324 |
| - final String logo = SETTINGS.get(AvailableAppSettings.APPLICATION_LOGO, "img/axelor.png"); |
325 |
| - if (SETTINGS.get(AvailableAppSettings.CONTEXT_APP_LOGO) != null) { |
326 |
| - final ScriptBindings bindings = new ScriptBindings(new HashMap<>()); |
327 |
| - final ScriptHelper helper = new CompositeScriptHelper(bindings); |
328 |
| - try { |
329 |
| - return Optional.ofNullable(helper.eval("__config__.appLogo")).orElse(logo); |
330 |
| - } catch (Exception e) { |
331 |
| - // Ignore |
332 |
| - } |
333 |
| - } |
334 |
| - return logo; |
335 |
| - } |
336 |
| - |
337 |
| - /** |
338 |
| - * Gets user specific application logo link, or falls back to default application logo. |
339 |
| - * |
340 |
| - * @return user specific logo link |
341 |
| - */ |
342 |
| - public String getLogoLink() { |
343 |
| - return getLink(getLogo()); |
| 324 | + public Object getLogo(String mode) { |
| 325 | + return getImage( |
| 326 | + SETTINGS.get(AvailableAppSettings.CONTEXT_APP_LOGO), |
| 327 | + mode, |
| 328 | + "appLogo", |
| 329 | + () -> |
| 330 | + isDark(mode) |
| 331 | + ? SETTINGS.get( |
| 332 | + AvailableAppSettings.APPLICATION_LOGO_DARK, |
| 333 | + SETTINGS.get(AvailableAppSettings.APPLICATION_LOGO, "img/axelor-dark.png")) |
| 334 | + : SETTINGS.get(AvailableAppSettings.APPLICATION_LOGO, "img/axelor.png")); |
344 | 335 | }
|
345 | 336 |
|
346 | 337 | /**
|
347 | 338 | * Gets user specific application icon, or falls back to default application icon.
|
348 | 339 | *
|
349 | 340 | * <p>The returned image can be a resource path string, a URL string or a MetaFile
|
350 | 341 | *
|
| 342 | + * @param mode light or dark |
351 | 343 | * @return user specific application icon
|
352 | 344 | */
|
353 |
| - public Object getIcon() { |
354 |
| - final String icon = SETTINGS.get(AvailableAppSettings.APPLICATION_ICON, "ico/favicon.ico"); |
355 |
| - if (SETTINGS.get(AvailableAppSettings.CONTEXT_APP_ICON) != null) { |
356 |
| - final ScriptBindings bindings = new ScriptBindings(new HashMap<>()); |
357 |
| - final ScriptHelper helper = new CompositeScriptHelper(bindings); |
358 |
| - try { |
359 |
| - return Optional.ofNullable(helper.eval("__config__.appIcon")).orElse(icon); |
360 |
| - } catch (Exception e) { |
361 |
| - // Ignore |
| 345 | + public Object getIcon(String mode) { |
| 346 | + return getImage( |
| 347 | + SETTINGS.get(AvailableAppSettings.CONTEXT_APP_ICON), |
| 348 | + mode, |
| 349 | + "appIcon", |
| 350 | + () -> |
| 351 | + isDark(mode) |
| 352 | + ? SETTINGS.get( |
| 353 | + AvailableAppSettings.APPLICATION_ICON_DARK, |
| 354 | + SETTINGS.get(AvailableAppSettings.APPLICATION_ICON, "ico/favicon.ico")) |
| 355 | + : SETTINGS.get(AvailableAppSettings.APPLICATION_ICON, "ico/favicon.ico")); |
| 356 | + } |
| 357 | + |
| 358 | + private Object getImage( |
| 359 | + String contextImage, String mode, String config, Supplier<String> defaultValue) { |
| 360 | + Object result; |
| 361 | + |
| 362 | + try { |
| 363 | + result = getImage(contextImage, mode); |
| 364 | + if (ObjectUtils.notEmpty(result)) { |
| 365 | + return result; |
362 | 366 | }
|
| 367 | + return defaultValue.get(); |
| 368 | + } catch (Exception e) { |
| 369 | + // Ignore |
363 | 370 | }
|
364 |
| - return icon; |
| 371 | + |
| 372 | + try { |
| 373 | + result = getConfigImage(config); |
| 374 | + if (ObjectUtils.notEmpty(result)) { |
| 375 | + return result; |
| 376 | + } |
| 377 | + } catch (Exception e) { |
| 378 | + // Ignore |
| 379 | + } |
| 380 | + |
| 381 | + return defaultValue.get(); |
365 | 382 | }
|
366 | 383 |
|
367 |
| - /** |
368 |
| - * Gets user specific application icon link, or falls back to default application icon. |
369 |
| - * |
370 |
| - * @return user specific application icon link |
371 |
| - */ |
372 |
| - public String getIconLink() { |
373 |
| - return getLink(getIcon()); |
| 384 | + private Object getImage(String imageCall, String mode) throws Exception { |
| 385 | + final String[] parts = imageCall.split("\\:", 2); |
| 386 | + |
| 387 | + if (parts.length != 2) { |
| 388 | + return null; |
| 389 | + } |
| 390 | + |
| 391 | + final String className = parts[0]; |
| 392 | + final String methodName = parts[1]; |
| 393 | + |
| 394 | + final Class<?> klass = Class.forName(className); |
| 395 | + final Method method = klass.getMethod(methodName, String.class); |
| 396 | + final Object bean = Beans.get(klass); |
| 397 | + |
| 398 | + return method.invoke(bean, mode); |
| 399 | + } |
| 400 | + |
| 401 | + // Legacy way to retrieve context image without passing mode |
| 402 | + @Deprecated(forRemoval = true) |
| 403 | + private Object getConfigImage(String name) { |
| 404 | + final ScriptBindings bindings = new ScriptBindings(new HashMap<>()); |
| 405 | + final ScriptHelper scriptHelper = new CompositeScriptHelper(bindings); |
| 406 | + return scriptHelper.eval("__config__." + name); |
| 407 | + } |
| 408 | + |
| 409 | + private boolean isDark(String mode) { |
| 410 | + return "dark".equalsIgnoreCase(mode); |
374 | 411 | }
|
375 | 412 |
|
376 | 413 | public String getLink(Object value) {
|
|
0 commit comments