From 5cb49f332856ac312e8840ec04c7869b892d4dd4 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 16 Apr 2017 14:18:02 -0400 Subject: [PATCH] Store widget locations in file --- ShiftOS.WinForms/DesktopWidgetAttribute.cs | 5 +++ ShiftOS.WinForms/WidgetManager.cs | 45 ++++++++++++++++++++- ShiftOS.WinForms/WinformsDesktop.cs | 46 ++++++++++++++++++++++ ShiftOS_TheReturn/Paths.cs | 1 + 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/ShiftOS.WinForms/DesktopWidgetAttribute.cs b/ShiftOS.WinForms/DesktopWidgetAttribute.cs index 28d50ac..8d3706f 100644 --- a/ShiftOS.WinForms/DesktopWidgetAttribute.cs +++ b/ShiftOS.WinForms/DesktopWidgetAttribute.cs @@ -17,5 +17,10 @@ namespace ShiftOS.WinForms Name = n; Description = desc; } + + public override string ToString() + { + return this.Name + "_" + Description; + } } } diff --git a/ShiftOS.WinForms/WidgetManager.cs b/ShiftOS.WinForms/WidgetManager.cs index 125c804..15e2076 100644 --- a/ShiftOS.WinForms/WidgetManager.cs +++ b/ShiftOS.WinForms/WidgetManager.cs @@ -1,11 +1,14 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; using ShiftOS.Engine; +using ShiftOS.Objects.ShiftFS; namespace ShiftOS.WinForms { @@ -14,7 +17,7 @@ namespace ShiftOS.WinForms public static Dictionary GetAllWidgetTypes() { Dictionary types = new Dictionary(); - foreach(var exe in Directory.GetFiles(Environment.CurrentDirectory)) + foreach(var exe in System.IO.Directory.GetFiles(Environment.CurrentDirectory)) { if(exe.EndsWith(".exe") || exe.EndsWith(".dll")) { @@ -45,6 +48,46 @@ namespace ShiftOS.WinForms return types; } + internal static void SaveLocation(Type type, Point location) + { + var dict = new Dictionary(); + var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is DesktopWidgetAttribute) as DesktopWidgetAttribute; + try + { + dict = JsonConvert.DeserializeObject>(Utils.ReadAllText(Paths.GetPath("widgets.dat"))); + dict[attrib.ToString()] = location; + } + catch + { + dict.Add(attrib.ToString(), location); + } + finally + { + Utils.WriteAllText(Paths.GetPath("widgets.dat"), JsonConvert.SerializeObject(dict)); + } + + } + + internal static Point LoadLocation(Type type) + { + var dict = new Dictionary(); + var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is DesktopWidgetAttribute) as DesktopWidgetAttribute; + try + { + dict = JsonConvert.DeserializeObject>(Utils.ReadAllText(Paths.GetPath("widgets.dat"))); + + return dict[attrib.ToString()]; + + } + catch + { + return new Point(-1, -1); + } + finally + { + } + + } } } diff --git a/ShiftOS.WinForms/WinformsDesktop.cs b/ShiftOS.WinForms/WinformsDesktop.cs index 3050cdf..ee0f34d 100644 --- a/ShiftOS.WinForms/WinformsDesktop.cs +++ b/ShiftOS.WinForms/WinformsDesktop.cs @@ -452,7 +452,11 @@ namespace ShiftOS.WinForms foreach(var widget in WidgetManager.GetAllWidgetTypes()) { UserControl w = (UserControl)Activator.CreateInstance(widget.Value, null); + + w.Location = WidgetManager.LoadLocation(w.GetType()); + pnlwidgetlayer.Controls.Add(w); + MakeWidgetMovable(w); Widgets.Add(w as IDesktopWidget); } } @@ -481,6 +485,48 @@ namespace ShiftOS.WinForms PopulatePanelButtons(); } + public void MakeWidgetMovable(Control w, Control startCtrl = null) + { + if (startCtrl == null) + startCtrl = w; + + bool moving = false; + + w.MouseDown += (o, a) => + { + moving = true; + }; + + w.MouseMove += (o, a) => + { + if (moving == true) + { + var mPos = Cursor.Position; + int mY = mPos.Y - desktoppanel.Height; + int mX = mPos.X; + + int ctrlHeight = startCtrl.Height / 2; + int ctrlWidth = startCtrl.Width / 2; + + startCtrl.Location = new Point( + mX - ctrlWidth, + mY - ctrlHeight + ); + + } + }; + + w.MouseUp += (o, a) => + { + moving = false; + WidgetManager.SaveLocation(startCtrl.GetType(), w.Location); + }; + + foreach (Control c in w.Controls) + MakeWidgetMovable(c, startCtrl); + + } + public ToolStripMenuItem GetALCategoryWithName(string text) { foreach (ToolStripMenuItem menuitem in apps.DropDownItems) diff --git a/ShiftOS_TheReturn/Paths.cs b/ShiftOS_TheReturn/Paths.cs index 4f535d6..2fc55fd 100644 --- a/ShiftOS_TheReturn/Paths.cs +++ b/ShiftOS_TheReturn/Paths.cs @@ -61,6 +61,7 @@ namespace ShiftOS.Engine AddPath("data", "user.dat"); AddPath("data", "notifications.dat"); AddPath("data", "skin"); + AddPath("skin", "widgets.dat"); AddPath("system", "programs"); AddPath("system", "kernel.sft"); AddPath("system", "conf.sft");