Store widget locations in file

This commit is contained in:
Michael 2017-04-16 14:18:02 -04:00
parent 538f99faf7
commit 5cb49f3328
4 changed files with 96 additions and 1 deletions

View file

@ -17,5 +17,10 @@ namespace ShiftOS.WinForms
Name = n;
Description = desc;
}
public override string ToString()
{
return this.Name + "_" + Description;
}
}
}

View file

@ -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<DesktopWidgetAttribute, Type> GetAllWidgetTypes()
{
Dictionary<DesktopWidgetAttribute, Type> types = new Dictionary<WinForms.DesktopWidgetAttribute, Type>();
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<string, Point>();
var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is DesktopWidgetAttribute) as DesktopWidgetAttribute;
try
{
dict = JsonConvert.DeserializeObject<Dictionary<string, Point>>(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<string, Point>();
var attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x is DesktopWidgetAttribute) as DesktopWidgetAttribute;
try
{
dict = JsonConvert.DeserializeObject<Dictionary<string, Point>>(Utils.ReadAllText(Paths.GetPath("widgets.dat")));
return dict[attrib.ToString()];
}
catch
{
return new Point(-1, -1);
}
finally
{
}
}
}
}

View file

@ -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)

View file

@ -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");