persistant storage of non-unique layout

This commit is contained in:
Ulf
2013-06-17 00:16:26 +02:00
parent 2aab26c905
commit 3a1a98d286
2 changed files with 93 additions and 41 deletions

View File

@@ -22,7 +22,8 @@ namespace PreviewToy
private IntPtr active_client_handle = (IntPtr)0; private IntPtr active_client_handle = (IntPtr)0;
private String active_client_title = ""; private String active_client_title = "";
private Dictionary<String, Dictionary<String, Point>> layouts; private Dictionary<String, Dictionary<String, Point>> unique_layouts;
private Dictionary<String, Point> flat_layout;
private bool is_initialized; private bool is_initialized;
@@ -34,7 +35,8 @@ namespace PreviewToy
previews = new Dictionary<IntPtr, Preview>(); previews = new Dictionary<IntPtr, Preview>();
layouts = new Dictionary<String, Dictionary<String, Point>>(); unique_layouts = new Dictionary<String, Dictionary<String, Point>>();
flat_layout = new Dictionary<String, Point>();
ignoring_size_sync = new Stopwatch(); ignoring_size_sync = new Stopwatch();
ignoring_size_sync.Start(); ignoring_size_sync.Start();
@@ -116,7 +118,6 @@ namespace PreviewToy
} }
// clean up old previews // clean up old previews
List<IntPtr> to_be_pruned = new List<IntPtr>(); List<IntPtr> to_be_pruned = new List<IntPtr>();
@@ -148,18 +149,33 @@ namespace PreviewToy
inner["-> " + inner_el.Name.ToString().Replace("_", " ") + " <-"] = new Point(Convert.ToInt32(inner_el.Element("x").Value), inner["-> " + inner_el.Name.ToString().Replace("_", " ") + " <-"] = new Point(Convert.ToInt32(inner_el.Element("x").Value),
Convert.ToInt32(inner_el.Element("y").Value)); Convert.ToInt32(inner_el.Element("y").Value));
} }
layouts[el.Name.ToString().Replace("_", " ")] = inner; unique_layouts[el.Name.ToString().Replace("_", " ")] = inner;
} }
} }
catch catch
{ {
// do nothing // do nothing
} }
try
{
XElement rootElement = XElement.Load("flat_layout.xml");
foreach (var el in rootElement.Elements())
{
flat_layout["-> " + el.Name.ToString().Replace("_", " ") + " <-"] = new Point(Convert.ToInt32(el.Element("x").Value),
Convert.ToInt32(el.Element("y").Value));
}
}
catch
{
// do nothing
}
} }
public void preview_did_switch() public void preview_did_switch()
{ {
store_layout(); store_layout(); //todo: check if it actually changed ...
foreach (KeyValuePair<IntPtr, Preview> entry in previews) foreach (KeyValuePair<IntPtr, Preview> entry in previews)
{ {
entry.Value.TopMost = Properties.Settings.Default.always_on_top; entry.Value.TopMost = Properties.Settings.Default.always_on_top;
@@ -167,22 +183,19 @@ namespace PreviewToy
} }
private void store_layout() private void store_layout()
{
//todo: check if it actually changed ...
try
{ {
XElement el = new XElement("layouts"); XElement el = new XElement("layouts");
foreach (var client in layouts.Keys) foreach (var client in unique_layouts.Keys)
{ {
if (client == "") if (client == "")
{ {
continue; continue;
} }
XElement layout = new XElement(client.Replace(" ", "_")); XElement layout = new XElement(client.Replace(" ", "_"));
foreach (var thumbnail_ in layouts[client]) foreach (var thumbnail_ in unique_layouts[client])
{ {
String thumbnail = thumbnail_.Key.Replace("-> ", "").Replace(" <-", "").Replace(" ", "_"); String thumbnail = thumbnail_.Key.Replace("-> ", "").Replace(" <-", "").Replace(" ", "_");
if (thumbnail == "") if (thumbnail == "" || thumbnail == "...")
{ {
continue; continue;
} }
@@ -195,17 +208,28 @@ namespace PreviewToy
} }
el.Save("layout.xml"); el.Save("layout.xml");
}
catch XElement el2 = new XElement("flat_layout");
foreach (var clientKV in flat_layout)
{ {
// if (clientKV.Key == "" || clientKV.Key == "..." )
{
continue;
} }
XElement layout = new XElement(clientKV.Key.Replace("-> ", "").Replace(" <-", "").Replace(" ", "_"));
layout.Add(new XElement("x", clientKV.Value.X));
layout.Add(new XElement("y", clientKV.Value.Y));
el2.Add(layout);
}
el2.Save("flat_layout.xml");
} }
private void handle_unique_layout(Preview preview, String last_known_active_window) private void handle_unique_layout(Preview preview, String last_known_active_window)
{ {
Dictionary<String, Point> layout; Dictionary<String, Point> layout;
if (layouts.TryGetValue(last_known_active_window, out layout)) if (unique_layouts.TryGetValue(last_known_active_window, out layout))
{ {
Point new_loc; Point new_loc;
if ( Properties.Settings.Default.unique_layout && layout.TryGetValue(preview.Text, out new_loc)) if ( Properties.Settings.Default.unique_layout && layout.TryGetValue(preview.Text, out new_loc))
@@ -221,8 +245,21 @@ namespace PreviewToy
else if (last_known_active_window != "") else if (last_known_active_window != "")
{ {
// create outer dict // create outer dict
layouts[last_known_active_window] = new Dictionary<String, Point>(); unique_layouts[last_known_active_window] = new Dictionary<String, Point>();
layouts[last_known_active_window][preview.Text] = preview.Location; unique_layouts[last_known_active_window][preview.Text] = preview.Location;
}
}
private void handle_flat_layout(Preview preview)
{
Point layout;
if (flat_layout.TryGetValue(preview.Text, out layout))
{
preview.Location = layout;
}
else if (preview.Text != "")
{
flat_layout[preview.Text] = preview.Location;
} }
} }
@@ -259,9 +296,15 @@ namespace PreviewToy
else else
{ {
entry.Value.Show(); entry.Value.Show();
if (Properties.Settings.Default.unique_layout)
{
handle_unique_layout(entry.Value, active_client_title); handle_unique_layout(entry.Value, active_client_title);
} }
else
{
handle_flat_layout(entry.Value);
}
}
entry.Value.hover_zoom = Properties.Settings.Default.zoom_on_hover; entry.Value.hover_zoom = Properties.Settings.Default.zoom_on_hover;
entry.Value.show_overlay = Properties.Settings.Default.show_overlay; entry.Value.show_overlay = Properties.Settings.Default.show_overlay;
} }
@@ -294,19 +337,28 @@ namespace PreviewToy
} }
public void register_preview_position(String preview_handle, Point position) public void register_preview_position(String preview_title, Point position)
{
if (Properties.Settings.Default.unique_layout)
{ {
Dictionary<String, Point> layout; Dictionary<String, Point> layout;
if (layouts.TryGetValue(active_client_title, out layout)) if (unique_layouts.TryGetValue(active_client_title, out layout))
{ {
layout[preview_handle] = position; layout[preview_title] = position;
} }
else if (active_client_title == "") else if (active_client_title == "")
{ {
layouts[active_client_title] = new Dictionary<String, Point>(); unique_layouts[active_client_title] = new Dictionary<String, Point>();
layouts[active_client_title][preview_handle] = position; unique_layouts[active_client_title][preview_title] = position;
} }
} }
else
{
flat_layout[preview_title] = position;
}
}
private void dispatcherTimer_Tick(object sender, EventArgs e) private void dispatcherTimer_Tick(object sender, EventArgs e)

Binary file not shown.