aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.WinForms/Applications/IconManager.cs
blob: 7bbeb3408d5b7cd0cecd94c7d8846b0bfacba75e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;
using System.Reflection;
using ShiftOS.WinForms.Tools;
using Newtonsoft.Json;

namespace ShiftOS.WinForms.Applications
{
    [RequiresUpgrade("icon_manager")]
    [Launcher("Icon Manager", true, "al_icon_manager", "Customization")]
    [DefaultTitle("Icon Manager")]
    [DefaultIcon("iconIconManager")]
    public partial class IconManager : UserControl, IShiftOSWindow
    {
        public IconManager()
        {
            InitializeComponent();
        }

        public void OnLoad()
        {
            LoadIconsFromEngine();
            SetupUI();
        }

        public void OnSkinLoad()
        {
            LoadIconsFromEngine();
            SetupUI();
        }

        public bool OnUnload()
        {
            Icons = null;
            return true;
        }

        private Dictionary<string, byte[]> Icons = null;

        private const int pageSize = 10;
        private int currentPage = 0;
        private int pageCount = 0;

        public Image GetIcon(string id)
        {
            if (!Icons.ContainsKey(id))
                Icons.Add(id, null);

            if (Icons[id] == null)
            {
                var img = SkinEngine.GetDefaultIcon(id);
                using (var mstr = new System.IO.MemoryStream())
                {
                    img.Save(mstr, System.Drawing.Imaging.ImageFormat.Png);
                    Icons[id] = mstr.ToArray();
                }
                return img;
            }
            else
            {
                using (var sr = new System.IO.MemoryStream(Icons[id]))
                {
                    return Image.FromStream(sr);
                }
            }
        }

        public void SetIcon(string key, byte[] raw)
        {
            if (!Icons.ContainsKey(key))
                Icons.Add(key, raw);
            Icons[key] = raw;
        }

        public void LoadIconsFromEngine()
        {
            //We have to serialize the engine icon list to JSON to break references with the data.
            string json = JsonConvert.SerializeObject(SkinEngine.LoadedSkin.AppIcons);
            //And deserialize to the local instance...essentially making a clone.
            Icons = JsonConvert.DeserializeObject<Dictionary<string, byte[]>>(json);
        }

        public void SetupUI()
        {
            flbody.Controls.Clear(); //Clear the icon list.

            Type[] types = Array.FindAll(ReflectMan.Types, x => x.GetCustomAttributes(false).FirstOrDefault(y => y is DefaultIconAttribute) != null);

            pageCount = types.GetPageCount(pageSize);

            foreach (var type in Array.FindAll(types.GetItemsOnPage(currentPage, pageSize), t => Shiftorium.UpgradeAttributesUnlocked(t)))
            {
                var pnl = new Panel();
                pnl.Height = 30;
                pnl.Width = flbody.Width - 15;
                flbody.Controls.Add(pnl);
                pnl.Show();
                var pic = new PictureBox();
                pic.SizeMode = PictureBoxSizeMode.StretchImage;
                pic.Size = new Size(24, 24);
                pic.Image = GetIcon(type.Name);
                pnl.Controls.Add(pic);
                pic.Left = 5;
                pic.Top = (pnl.Height - pic.Height) / 2;
                pic.Show();
                var lbl = new Label();
                lbl.Tag = "header3";
                lbl.AutoSize = true;
                lbl.Text = NameChangerBackend.GetNameRaw(type);
                ControlManager.SetupControl(lbl);
                pnl.Controls.Add(lbl);
                lbl.CenterParent();
                lbl.Show();
                var btn = new Button();
                btn.Text = "Change...";
                btn.AutoSize = true;
                btn.AutoSizeMode = AutoSizeMode.GrowAndShrink;
                pnl.Controls.Add(btn);
                btn.Left = (pnl.Width - btn.Width) - 5;
                btn.Top = (pnl.Height - btn.Height) / 2;
                btn.Click += (o, a) =>
                {
                    var gfp = new GraphicPicker(pic.Image, lbl.Text + " icon", ImageLayout.Stretch, (raw, img, layout) =>
                    {
                        pic.Image = img;
                        SetIcon(type.Name, raw);
                    });
                    AppearanceManager.SetupDialog(gfp);
                };
                btn.Show();
                ControlManager.SetupControls(pnl);
            }

            btnnext.Visible = (currentPage < pageCount - 1);
            btnprev.Visible = (currentPage > 0);

            lbcurrentpage.Text = "Page " + (currentPage + 1).ToString() + " of " + pageCount.ToString();
        }

        public void OnUpgrade()
        {
            LoadIconsFromEngine();
            SetupUI();
        }

        private void btnprev_Click(object sender, EventArgs e)
        {
            currentPage--;
            SetupUI();
        }

        public void ResetToDefaults()
        {
            currentPage = 0;
            foreach (var key in Icons.Keys)
            {
                var img = SkinEngine.GetDefaultIcon(key);
                using(var ms = new System.IO.MemoryStream())
                {
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                    Icons[key] = ms.ToArray();
                }
            }
            SetupUI();
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            currentPage++;
            SetupUI();
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            AppearanceManager.Close(this);
        }

        private void btnreset_Click(object sender, EventArgs e)
        {
            ResetToDefaults();
        }

        private void btnapply_Click(object sender, EventArgs e)
        {
            SkinEngine.LoadedSkin.AppIcons = Icons;
            SkinEngine.SaveSkin();
            SkinEngine.LoadSkin();
            Infobox.Show("Icons applied!", "The new icons have been applied to ShiftOS successfully!");
        }
    }

    public static class PaginationExtensions
    {
        public static int GetPageCount<T>(this IEnumerable<T> collection, int pageSize)
        {
            return (collection.Count() + pageSize - 1) / pageSize;
        }

        public static T[] GetItemsOnPage<T>(this T[] collection, int page, int pageSize)
        {
            List<T> obj = new List<T>();

            for (int i = pageSize * page; i <= pageSize + (pageSize * page) && i < collection.Count(); i++)
            {
                try
                {
                    obj.Add(collection[i]);
                }
                catch
                {
                }
            }
            return obj.ToArray();
        }
    }

}