positions are stored by window title

positions are stored persistently
This commit is contained in:
Ulf
2013-06-14 23:12:24 +02:00
parent a3a24146cc
commit 87fcda0b31
6 changed files with 96 additions and 38 deletions

View File

@@ -45,7 +45,7 @@ namespace PreviewToy
protected override void OnMove(EventArgs e) protected override void OnMove(EventArgs e)
{ {
base.OnMove(e); base.OnMove(e);
this.spawner.register_preview_position(this.Handle, this.Location); this.spawner.register_preview_position(this.Text, this.Location);
} }
protected void RefreshPreview() protected void RefreshPreview()

View File

@@ -9,6 +9,8 @@ using System.Runtime.InteropServices;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing.Text; using System.Drawing.Text;
using System.Windows.Threading; using System.Windows.Threading;
using System.Xml.Linq;
using System.Linq;
namespace PreviewToy namespace PreviewToy
{ {
@@ -17,13 +19,13 @@ namespace PreviewToy
private Dictionary<IntPtr, Preview> previews; private Dictionary<IntPtr, Preview> previews;
private DispatcherTimer dispatcherTimer; private DispatcherTimer dispatcherTimer;
private IntPtr active_client; private IntPtr active_client_handle = (IntPtr)0;
private String active_client_title = "";
private Dictionary<IntPtr, Dictionary<IntPtr, Point>> layouts; private Dictionary<String, Dictionary<String, Point>> layouts;
private bool is_initialized; private bool is_initialized;
private bool frames_were_hidden = false;
private Stopwatch ignoring_size_sync; private Stopwatch ignoring_size_sync;
public PreviewToyHandler() public PreviewToyHandler()
@@ -32,7 +34,7 @@ namespace PreviewToy
previews = new Dictionary<IntPtr, Preview>(); previews = new Dictionary<IntPtr, Preview>();
layouts = new Dictionary<IntPtr, Dictionary<IntPtr, Point>>(); layouts = new Dictionary<String, Dictionary<String, Point>>();
ignoring_size_sync = new Stopwatch(); ignoring_size_sync = new Stopwatch();
ignoring_size_sync.Start(); ignoring_size_sync.Start();
@@ -66,6 +68,8 @@ namespace PreviewToy
option_sync_size_x.Text = Properties.Settings.Default.sync_resize_x.ToString(); option_sync_size_x.Text = Properties.Settings.Default.sync_resize_x.ToString();
option_sync_size_y.Text = Properties.Settings.Default.sync_resize_y.ToString(); option_sync_size_y.Text = Properties.Settings.Default.sync_resize_y.ToString();
option_show_thumbnail_frames.Checked = Properties.Settings.Default.show_thumb_frames; option_show_thumbnail_frames.Checked = Properties.Settings.Default.show_thumb_frames;
load_layout();
} }
@@ -102,6 +106,12 @@ namespace PreviewToy
previews[process.MainWindowHandle].Text = "-> " + process.MainWindowTitle + " <-"; previews[process.MainWindowHandle].Text = "-> " + process.MainWindowTitle + " <-";
} }
if (process.MainWindowHandle == DwmApi.GetForegroundWindow())
{
active_client_handle = process.MainWindowHandle;
active_client_title = process.MainWindowTitle;
}
} }
@@ -120,31 +130,80 @@ namespace PreviewToy
{ {
previews[processHandle].Close(); previews[processHandle].Close();
previews.Remove(processHandle); previews.Remove(processHandle);
layouts.Remove(processHandle);
} }
} }
private void handle_unique_layout(Preview preview, IntPtr last_known_active_window) private void load_layout()
{ {
Dictionary<IntPtr, Point> layout; try
{
XElement rootElement = XElement.Load("config.xml");
foreach (var el in rootElement.Elements())
{
Dictionary<String, Point> inner = new Dictionary<String, Point>();
foreach (var inner_el in el.Elements())
{
inner["-> " + inner_el.Name.ToString().Replace("_", " ") + " <-"] = new Point(Convert.ToInt32(inner_el.Element("x").Value),
Convert.ToInt32(inner_el.Element("y").Value));
}
layouts[el.Name.ToString().Replace("_", " ")] = inner;
}
}
catch
{
// do nothing
}
}
private void store_layout()
{
XElement el = new XElement("layouts");
foreach (var client in layouts.Keys)
{
if (client == "")
{
continue;
}
XElement layout = new XElement(client.Replace(" ", "_"));
foreach (var thumbnail_ in layouts[client])
{
String thumbnail = thumbnail_.Key.Replace("-> ", "").Replace(" <-", "").Replace(" ", "_");
if (thumbnail == "")
{
continue;
}
XElement position = new XElement(thumbnail);
position.Add(new XElement("x", thumbnail_.Value.X));
position.Add(new XElement("y", thumbnail_.Value.Y));
layout.Add(position);
}
el.Add(layout);
}
el.Save("config.xml");
}
private void handle_unique_layout(Preview preview, String last_known_active_window)
{
Dictionary<String, Point> layout;
if (layouts.TryGetValue(last_known_active_window, out layout)) if (layouts.TryGetValue(last_known_active_window, out layout))
{ {
Point new_loc; Point new_loc;
if (layout.TryGetValue(preview.Handle, out new_loc)) if ( Properties.Settings.Default.unique_layout && layout.TryGetValue(preview.Text, out new_loc))
{ {
preview.Location = new_loc; preview.Location = new_loc;
} }
else else
{ {
// create inner dict // create inner dict
layout[preview.Handle] = preview.Location; layout[preview.Text] = preview.Location;
} }
} }
else if ((int)last_known_active_window != 0) else if (last_known_active_window != "")
{ {
// create outer dict // create outer dict
layouts[last_known_active_window] = new Dictionary<IntPtr, Point>(); layouts[last_known_active_window] = new Dictionary<String, Point>();
layouts[last_known_active_window][preview.Handle] = preview.Location; layouts[last_known_active_window][preview.Text] = preview.Location;
} }
} }
@@ -167,10 +226,6 @@ namespace PreviewToy
IntPtr active_window = DwmApi.GetForegroundWindow(); IntPtr active_window = DwmApi.GetForegroundWindow();
Preview poo;
if (previews.TryGetValue(active_window, out poo)){
active_client = active_window;}
// hide, show, resize and move // hide, show, resize and move
foreach (KeyValuePair<IntPtr, Preview> entry in previews) foreach (KeyValuePair<IntPtr, Preview> entry in previews)
{ {
@@ -178,18 +233,14 @@ namespace PreviewToy
{ {
entry.Value.Hide(); entry.Value.Hide();
} }
else if (entry.Key == active_client && Properties.Settings.Default.hide_active) else if (entry.Key == active_client_handle && Properties.Settings.Default.hide_active)
{ {
entry.Value.Hide(); entry.Value.Hide();
} }
else else
{ {
entry.Value.Show(); entry.Value.Show();
handle_unique_layout(entry.Value, active_client_title);
if (Properties.Settings.Default.unique_layout)
{
handle_unique_layout(entry.Value, active_client);
}
} }
} }
} }
@@ -221,18 +272,19 @@ namespace PreviewToy
} }
public void register_preview_position(IntPtr preview_handle, Point position) public void register_preview_position(String preview_handle, Point position)
{ {
Dictionary<IntPtr, Point> layout; Dictionary<String, Point> layout;
if (layouts.TryGetValue(active_client, out layout)) if (layouts.TryGetValue(active_client_title, out layout))
{ {
layout[preview_handle] = position; layout[preview_handle] = position;
} }
else if ((int)active_client != 0) else if (active_client_title == "")
{ {
layouts[active_client] = new Dictionary<IntPtr, Point>(); layouts[active_client_title] = new Dictionary<String, Point>();
layouts[active_client][preview_handle] = position; layouts[active_client_title][preview_handle] = position;
} }
store_layout();
} }

View File

@@ -2,11 +2,11 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
[assembly: AssemblyTitle("DWM")] [assembly: AssemblyTitle("PreviewToy")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")] [assembly: AssemblyCompany("?")]
[assembly: AssemblyProduct("DWM")] [assembly: AssemblyProduct("PreviewToy")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2007")] [assembly: AssemblyCopyright("Copyright © Microsoft 2007")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]

View File

@@ -1,11 +1,11 @@
<?xml version="1.0"?> <?xml version="1.0"?>
<configuration> <configuration>
<configSections> <configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="PreviewToy.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> <section name="PreviewToy.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup> </sectionGroup>
</configSections> </configSections>
<startup><supportedRuntime version="v2.0.50727"/></startup><userSettings> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup><userSettings>
<PreviewToy.Properties.Settings> <PreviewToy.Properties.Settings>
<setting name="hide_active" serializeAs="String"> <setting name="hide_active" serializeAs="String">
<value>False</value> <value>False</value>

View File

@@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PreviewToy</RootNamespace> <RootNamespace>PreviewToy</RootNamespace>
<AssemblyName>eve-o preview</AssemblyName> <AssemblyName>eve-o preview</AssemblyName>
<TargetFrameworkVersion>v3.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileUpgradeFlags> <FileUpgradeFlags>
</FileUpgradeFlags> </FileUpgradeFlags>
<OldToolsVersion>2.0</OldToolsVersion> <OldToolsVersion>2.0</OldToolsVersion>
@@ -41,6 +41,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
@@ -50,6 +51,7 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess> <UseVSHostingProcess>false</UseVSHostingProcess>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<ApplicationIcon>icon.ico</ApplicationIcon> <ApplicationIcon>icon.ico</ApplicationIcon>
@@ -73,8 +75,9 @@
<PropertyGroup> <PropertyGroup>
<TargetZone>LocalIntranet</TargetZone> <TargetZone>LocalIntranet</TargetZone>
</PropertyGroup> </PropertyGroup>
<PropertyGroup />
<PropertyGroup> <PropertyGroup>
<ApplicationManifest>Properties\app.manifest</ApplicationManifest> <NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
@@ -83,6 +86,9 @@
<Reference Include="System.Deployment" /> <Reference Include="System.Deployment" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.XML" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Xml.Serialization" />
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

Binary file not shown.