cwtch-ui/lib/themes/yamltheme.dart

126 lines
6.0 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'dart:ui';
import 'package:cwtch/themes/cwtch.dart';
import 'package:cwtch/themes/opaque.dart';
import 'package:flutter/services.dart';
import 'package:yaml/yaml.dart';
Future<Map<String, Map<String, OpaqueThemeType>>> loadYamlThemes() async {
final manifestJson = await rootBundle.loadString('AssetManifest.json');
final themesList = json.decode(manifestJson).keys.where((String key) => key.startsWith('assets/themes'));
Map<String, Map<String, OpaqueThemeType>> themes = Map();
for (String themefile in themesList) {
if (themefile.substring(themefile.length-4) != ".yml") {
continue;
}
try {
var data = await loadYamlTheme(themefile);
if (data != null) {
// remove "assets/themes" and ".yml" from name
themes[themefile.substring(14, themefile.length - 4)] = data;
}
} catch (e) {
print("Failed to load theme: $themefile with exception: $e");
}
}
return themes;
}
Future<YamlMap?> readAssetYamlTheme(String themefile) async {
print("loading theme: $themefile...");
final contents = await rootBundle.loadString(themefile);
return loadYaml(contents);
}
Future<Map<String, OpaqueThemeType>?> loadYamlTheme(String themefile) async {
final yml = await readAssetYamlTheme(themefile);
if (yml == null) {
print("failed to load theme: $themefile");
return null;
}
Map<String, OpaqueThemeType> subthemes = Map();
if ((yml["themes"] as YamlMap).containsKey(mode_dark)) {
subthemes[mode_dark] = YmlTheme(yml, yml["themes"]["name"], mode_dark);
}
if ((yml["themes"] as YamlMap).containsKey(mode_light)) {
subthemes[mode_light] = YmlTheme(yml, yml["themes"]["name"], mode_light);
}
return subthemes;
}
class YmlTheme extends OpaqueThemeType {
late YamlMap yml;
late String mode;
late String theme;
late OpaqueThemeType fallbackTheme;
YmlTheme(YamlMap yml, theme, mode) {
this.yml = yml;
this.theme = theme;
this.mode = mode;
if (mode == mode_dark) {
fallbackTheme = CwtchDark();
} else {
fallbackTheme = CwtchLight();
}
}
Color? getColor(String name) {
var val = yml["themes"][mode]["theme"][name];
if (! (val is int)) {
val = yml["themes"][mode]["theme"][val] ?? val;
}
if (! (val is int)) {
val = yml["themes"][mode]?["colors"][val] ?? val;
}
if (! (val is int)) {
val = yml["colors"]?[val];
}
if (! (val is int)) {
return null;
}
return Color(0xFF000000 + val as int);
}
get backgroundMainColor => getColor("backgroundMainColor") ?? fallbackTheme.backgroundMainColor;
get backgroundPaneColor => getColor("backgroundPaneColor") ?? fallbackTheme.backgroundPaneColor;
get topbarColor => getColor("topbarColor") ?? fallbackTheme.topbarColor;
get mainTextColor => getColor("mainTextColor") ?? fallbackTheme.mainTextColor;
get hilightElementColor => getColor("hilightElementColor") ?? fallbackTheme.hilightElementColor;
get backgroundHilightElementColor => getColor("backgroundHilightElementColor") ?? fallbackTheme.backgroundHilightElementColor;
get sendHintTextColor => getColor("sendHintTextColor") ?? fallbackTheme.sendHintTextColor;
get defaultButtonColor => getColor("defaultButtonColor") ?? fallbackTheme.defaultButtonColor;
get defaultButtonActiveColor => /*mode == mode_light ? darken(defaultButtonColor) :*/ lighten(getColor("defaultButtonColor") ?? fallbackTheme.defaultButtonColor);
get defaultButtonTextColor => getColor("defaultButtonTextColor") ?? fallbackTheme.defaultButtonTextColor;
get defaultButtonDisabledColor => getColor("defaultButtonDisabledColor") ?? fallbackTheme.defaultButtonDisabledColor;
get textfieldBackgroundColor => getColor("textfieldBackgroundColor") ?? fallbackTheme.textfieldBackgroundColor;
get textfieldBorderColor => getColor("textfieldBorderColor") ?? fallbackTheme.textfieldBorderColor;
get textfieldHintColor => getColor("textfieldHintColor") ?? fallbackTheme.textfieldHintColor;
get textfieldErrorColor => getColor("textfieldErrorColor") ?? fallbackTheme.textfieldErrorColor;
get scrollbarDefaultColor => getColor("scrollbarDefaultColor") ?? fallbackTheme.scrollbarDefaultColor;
get portraitBackgroundColor => getColor("portraitBackgroundColor") ?? fallbackTheme.portraitBackgroundColor;
get portraitOnlineBorderColor => getColor("portraitOnlineBorderColor") ?? fallbackTheme.portraitOnlineBorderColor;
get portraitOfflineBorderColor => getColor("portraitOfflineBorderColor") ?? fallbackTheme.portraitOfflineBorderColor;
get portraitBlockedBorderColor => getColor("portraitBlockedBorderColor") ?? fallbackTheme.portraitBlockedBorderColor;
get portraitBlockedTextColor => getColor("portraitBlockedTextColor") ?? fallbackTheme.portraitBlockedTextColor;
get portraitContactBadgeColor => getColor("portraitContactBadgeColor") ?? fallbackTheme.portraitContactBadgeColor;
get portraitContactBadgeTextColor => getColor("portraitContactBadgeTextColor") ?? fallbackTheme.portraitContactBadgeTextColor;
get portraitProfileBadgeColor => getColor("portraitProfileBadgeColor") ?? fallbackTheme.portraitProfileBadgeColor;
get portraitProfileBadgeTextColor => getColor("portraitProfileBadgeTextColor") ?? fallbackTheme.portraitProfileBadgeTextColor;
get portraitOnlineAwayColor => Color(0xFFFFF59D) ?? fallbackTheme.portraitOnlineAwayColor;
get portraitOnlineBusyColor => Color(0xFFEF9A9A) ?? fallbackTheme.portraitOnlineBusyColor;
get dropShadowColor => getColor("dropShadowColor") ?? fallbackTheme.dropShadowColor;
get toolbarIconColor => getColor("toolbarIconColor") ?? fallbackTheme.toolbarIconColor;
get messageFromMeBackgroundColor => getColor("messageFromMeBackgroundColor") ?? fallbackTheme.messageFromMeBackgroundColor;
get messageFromMeTextColor => getColor("messageFromMeTextColor") ?? fallbackTheme.messageFromMeTextColor;
get messageFromOtherBackgroundColor => getColor("messageFromOtherBackgroundColor") ?? fallbackTheme.messageFromOtherBackgroundColor;
get messageFromOtherTextColor => getColor("messageFromOtherTextColor") ?? fallbackTheme.messageFromOtherTextColor;
}