Пример #1
0
        public static bool Equals(ThemeCustomInfo x, ThemeCustomInfo y)
        {
            if (x.Parent != y.Parent)
            {
                return(false);
            }

            bool equal = false;

            if (x.Values.Count == y.Values.Count) // Require equal count.
            {
                equal = true;
                foreach (var pair in x.Values)
                {
                    if (y.Values.TryGetValue(pair.Key, out Color value))
                    {
                        // Require value be equal.
                        if (!Equals(value, pair.Value))
                        {
                            equal = false;
                            break;
                        }
                    }
                    else
                    {
                        // Require key be present.
                        equal = false;
                        break;
                    }
                }
            }

            return(equal);
        }
Пример #2
0
        public async Task SerializeAsync(StorageFile file, ThemeCustomInfo theme)
        {
            var lines = new StringBuilder();

            lines.AppendLine("!");
            lines.AppendLine($"name: {theme.Name}");
            lines.AppendLine($"parent: {(int)theme.Parent}");

            var lastbrush = false;

            foreach (var item in theme.Values)
            {
                if (item.Value is Color color)
                {
                    if (!lastbrush)
                    {
                        lines.AppendLine("#");
                    }

                    var hexValue = (color.A << 24) + (color.R << 16) + (color.G << 8) + (color.B & 0xff);

                    lastbrush = true;
                    lines.AppendLine(string.Format("{0}: #{1:X8}", item.Key, hexValue));
                }
            }

            await FileIO.WriteTextAsync(file, lines.ToString());
        }
Пример #3
0
        public async Task <ThemeCustomInfo> DeserializeAsync(StorageFile file)
        {
            var lines = await FileIO.ReadLinesAsync(file);

            var theme = ThemeCustomInfo.FromFile(file.Path, lines);

            return(theme);
        }
Пример #4
0
        public async Task CreateThemeAsync(ThemeInfoBase theme)
        {
            var confirm = await MessagePopup.ShowAsync(Strings.Resources.CreateNewThemeAlert, Strings.Resources.NewTheme, Strings.Resources.CreateTheme, Strings.Resources.Cancel);

            if (confirm != ContentDialogResult.Primary)
            {
                return;
            }

            var input = new InputPopup();

            input.Title  = Strings.Resources.NewTheme;
            input.Header = Strings.Resources.EnterThemeName;
            input.Text   = $"{theme.Name} #2";
            input.IsPrimaryButtonEnabled   = true;
            input.IsSecondaryButtonEnabled = true;
            input.PrimaryButtonText        = Strings.Resources.OK;
            input.SecondaryButtonText      = Strings.Resources.Cancel;

            confirm = await input.ShowQueuedAsync();

            if (confirm != ContentDialogResult.Primary)
            {
                return;
            }

            var preparing = new ThemeCustomInfo(theme.Parent, theme.AccentColor, input.Text);
            var fileName  = Client.Execute(new CleanFileName(theme.Name)) as Text;

            var lookup = ThemeService.GetLookup(theme.Parent);

            foreach (var value in lookup)
            {
                if (value.Value is Color color)
                {
                    preparing.Values[value.Key] = color;
                }
            }

            if (theme is ThemeCustomInfo custom)
            {
                foreach (var item in custom.Values)
                {
                    preparing.Values[item.Key] = item.Value;
                }
            }
            else if (theme is ThemeAccentInfo accent)
            {
                foreach (var item in accent.Values)
                {
                    preparing.Values[item.Key] = item.Value;
                }
            }

            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("themes\\" + fileName.TextValue + ".unigram-theme", CreationCollisionOption.GenerateUniqueName);

            await SerializeAsync(file, preparing);

            preparing.Path = file.Path;

            SetTheme(preparing, true);

            if (Window.Current.Content is Views.Host.RootPage root)
            {
                root.ShowEditor(preparing);
            }
        }
Пример #5
0
        public async Task InstallThemeAsync(StorageFile file)
        {
            var info = await DeserializeAsync(file);

            if (info == null)
            {
                return;
            }

            var installed = await GetCustomThemesAsync();

            var equals = installed.FirstOrDefault(x => x is ThemeCustomInfo custom && ThemeCustomInfo.Equals(custom, info));

            if (equals != null)
            {
                SetTheme(equals, true);
                return;
            }

            var folder = await ApplicationData.Current.LocalFolder.GetFolderAsync("themes");

            var result = await file.CopyAsync(folder, file.Name, NameCollisionOption.GenerateUniqueName);

            var theme = await DeserializeAsync(result);

            if (theme != null)
            {
                SetTheme(theme, true);
            }
        }