private void Update(TelegramTheme requested, ThemeAccentInfo info = null) { try { ThemeOutgoing.Update(requested, info?.Values); ThemeIncoming.Update(requested, info?.Values); var values = info?.Values; var shades = info?.Shades; var target = MergedDictionaries[0].ThemeDictionaries[requested == TelegramTheme.Light ? "Light" : "Dark"] as ResourceDictionary; var lookup = ThemeService.GetLookup(requested); if (shades != null && shades.TryGetValue(AccentShade.Default, out Color accentResource)) { target["Accent"] = accentResource; } else { target["Accent"] = ThemeInfoBase.Accents[TelegramThemeType.Day][AccentShade.Default]; } foreach (var item in lookup) { if (target.TryGet(item.Key, out SolidColorBrush brush)) { Color value; if (item.Value is AccentShade shade) { if (shades != null && shades.TryGetValue(shade, out Color accent)) { value = accent; } else { value = ThemeInfoBase.Accents[TelegramThemeType.Day][shade]; } } else if (values != null && values.TryGetValue(item.Key, out Color themed)) { value = themed; } else if (item.Value is Color color) { value = color; } if (brush.Color == value) { continue; } try { brush.Color = value; } catch (UnauthorizedAccessException) { // Some times access denied is thrown, // this seems to happen after the application // is resumed, but unfortunately I can't see // any fix to this. The exception is going // to be thrown any time - even minutes after // the resume - if the theme changes. // The exception MIGHT be related to StaticResources // but I'm not able to confirm this. } } } int GetColor(string key) { if (target.TryGet(key, out SolidColorBrush brush)) { return(brush.Color.ToValue()); } return(0); } Parameters = new ThemeParameters { BackgroundColor = GetColor("ContentDialogBackground"), TextColor = GetColor("ContentDialogForeground"), ButtonColor = GetColor("ButtonBackground"), ButtonTextColor = GetColor("ButtonForeground"), HintColor = GetColor("SystemControlDisabledChromeDisabledLowBrush"), LinkColor = GetColor("HyperlinkForeground") }; } catch { } }
// This is for chat specific theme public bool Update(ElementTheme elementTheme, ChatTheme theme) { var updated = false; var requested = elementTheme == ElementTheme.Dark ? TelegramTheme.Dark : TelegramTheme.Light; var settings = requested == TelegramTheme.Light ? theme?.LightSettings : theme?.DarkSettings; if (settings != null) { if (_lastAccent != settings.AccentColor) { _lastTheme = theme; var tint = SettingsService.Current.Appearance[requested].Type; if (tint == TelegramThemeType.Classic || (tint == TelegramThemeType.Custom && requested == TelegramTheme.Light)) { tint = TelegramThemeType.Day; } else if (tint == TelegramThemeType.Custom) { tint = TelegramThemeType.Tinted; } var accent = settings.AccentColor.ToColor(); var outgoing = settings.OutgoingMessageAccentColor.ToColor(); var info = ThemeAccentInfo.FromAccent(tint, accent, outgoing); ThemeOutgoing.Update(info.Parent, info.Values); ThemeIncoming.Update(info.Parent, info.Values); } if (_lastBackground != settings.Background?.Id) { updated = true; } _lastAccent = settings.AccentColor; _lastBackground = settings.Background?.Id; } else { if (_lastAccent != null) { _lastTheme = null; var options = SettingsService.Current.Appearance; if (options[requested].Type == TelegramThemeType.Custom && System.IO.File.Exists(options[requested].Custom)) { var info = ThemeCustomInfo.FromFile(options[requested].Custom); ThemeOutgoing.Update(info.Parent, info.Values); ThemeIncoming.Update(info.Parent, info.Values); } else if (ThemeAccentInfo.IsAccent(options[requested].Type)) { var info = ThemeAccentInfo.FromAccent(options[requested].Type, options.Accents[options[requested].Type]); ThemeOutgoing.Update(info.Parent, info.Values); ThemeIncoming.Update(info.Parent, info.Values); } else { ThemeOutgoing.Update(requested); ThemeIncoming.Update(requested); } } if (_lastBackground != null) { updated = true; } _lastAccent = null; _lastBackground = null; } return(updated); }