using PrivaPub.ClientModels.Resources; using PrivaPub.ClientModels.User; using System.ComponentModel.DataAnnotations; namespace decePubClient.Models { public class PageSettings { [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(ErrorsResource)), Display(Name = "LanguageId", ResourceType = typeof(FieldsNameResource))] public string CurrentLanguageCode { get; set; } = "en"; [Range(0, 359, ErrorMessageResourceName = nameof(Range), ErrorMessageResourceType = typeof(ErrorsResource))] public short LightThemeIndexColour { get; set; } = 25; [Range(0, 359, ErrorMessageResourceName = nameof(Range), ErrorMessageResourceType = typeof(ErrorsResource))] public short DarkThemeIndexColour { get; set; } = 215; public bool PreferSystemTheming { get; set; } = true; public bool ThemeIsDarkMode { get; set; } = false; public bool ThemeIsLightGray { get; set; } = true; public bool ThemeIsDarkGray { get; set; } = false; // before publishing pictures without a description, the composer asks once public bool RemindAltText { get; set; } = true; // the theme is the login's (PrivaPub's user settings): taken at sign-in, saved there whenever it changes here public bool SyncWithLogin { get; set; } = true; // How the theme flags combine is in wwwroot/js/theme.js: with PreferSystemTheming the OS picks light or dark; // otherwise ThemeIsDarkGray, then ThemeIsLightGray, then ThemeIsDarkMode decide; either grey flag drops the // colour. These keep the flags consistent when the UI changes one aspect. public const short DefaultLightThemeIndexColour = 25; public const short DefaultDarkThemeIndexColour = 215; public bool IsGray() => ThemeIsLightGray || ThemeIsDarkGray; /// Leaves system theming and shows the light or dark variant, grey or coloured as before. public void SetDarkMode(bool isDark) { PreferSystemTheming = false; ThemeIsDarkMode = isDark; if (IsGray()) SetGray(true, isDark); } /// Switches grey on or off for the variant currently shown. public void SetGray(bool isGray, bool isDark) { ThemeIsDarkGray = isGray && isDark; ThemeIsLightGray = isGray && !isDark; } /// Sets the hue of the light or dark variant; picking a hue means colour, so grey goes off. public void SetHue(bool isDark, short hue) { if (isDark) DarkThemeIndexColour = hue; else LightThemeIndexColour = hue; SetGray(false, isDark); } /// /// These settings as the login keeps them: the language as the two letters of the culture the app runs in /// (ExtensionMethods.ToCulture: "it" is it-IT, anything else en-GB), the icons' hue at its default, since the client /// has no such setting. /// public ViewUserSettings ToLoginSettings() => new() { LanguageCode = CurrentLanguageCode is "it" ? "it" : "en", LightThemeIndexColour = LightThemeIndexColour, DarkThemeIndexColour = DarkThemeIndexColour, IconsThemeIndexColour = DefaultLightThemeIndexColour, PreferSystemTheming = PreferSystemTheming, ThemeIsDarkMode = ThemeIsDarkMode, ThemeIsLightGray = ThemeIsLightGray, ThemeIsDarkGray = ThemeIsDarkGray }; /// Takes the login's theme; its language stays this device's, since changing it reloads the app. public void TakeLoginTheme(ViewUserSettings settings) { LightThemeIndexColour = Math.Clamp(settings.LightThemeIndexColour, (short)0, (short)359); DarkThemeIndexColour = Math.Clamp(settings.DarkThemeIndexColour, (short)0, (short)359); PreferSystemTheming = settings.PreferSystemTheming; ThemeIsDarkMode = settings.ThemeIsDarkMode; ThemeIsLightGray = settings.ThemeIsLightGray; ThemeIsDarkGray = settings.ThemeIsDarkGray; } } }