Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multimonitor - round 2 #88

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Core.UnitTests/Core.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<Name>Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
9 changes: 9 additions & 0 deletions Switcheroo/ICalculateWindowPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Windows.Forms;

namespace Switcheroo
{
public interface ICalculateWindowPosition
{
WindowPosition CalculateWindowPosition(Screen screen, double windowWidth, double windowHeight);
}
}
44 changes: 38 additions & 6 deletions Switcheroo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Reflection;
Expand All @@ -41,6 +42,7 @@
using Application = System.Windows.Application;
using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox;
using MouseCursor = System.Windows.Forms.Cursor;

namespace Switcheroo
{
Expand All @@ -56,6 +58,8 @@ public partial class MainWindow : Window
public static readonly RoutedUICommand SwitchToWindowCommand = new RoutedUICommand();
public static readonly RoutedUICommand ScrollListDownCommand = new RoutedUICommand();
public static readonly RoutedUICommand ScrollListUpCommand = new RoutedUICommand();

private static readonly ICalculateWindowPosition WindowPositionCalculator = new WindowPositionCalculator();
private OptionsWindow _optionsWindow;
private AboutWindow _aboutWindow;
private AltTabHook _altTabHook;
Expand Down Expand Up @@ -268,7 +272,7 @@ private void LoadData(InitialFocus focus)
var firstWindow = _unfilteredWindowList.FirstOrDefault();

var foregroundWindowMovedToBottom = false;

// Move first window to the bottom of the list if it's related to the foreground window
if (firstWindow != null && AreWindowsRelated(firstWindow.AppWindow, _foregroundWindow))
{
Expand All @@ -282,9 +286,9 @@ private void LoadData(InitialFocus focus)

foreach (var window in _unfilteredWindowList)
{
window.FormattedTitle = new XamlHighlighter().Highlight(new[] {new StringPart(window.AppWindow.Title)});
window.FormattedTitle = new XamlHighlighter().Highlight(new[] { new StringPart(window.AppWindow.Title) });
window.FormattedProcessTitle =
new XamlHighlighter().Highlight(new[] {new StringPart(window.AppWindow.ProcessTitle)});
new XamlHighlighter().Highlight(new[] { new StringPart(window.AppWindow.ProcessTitle) });
}

lb.DataContext = null;
Expand Down Expand Up @@ -333,9 +337,37 @@ private void CenterWindow()
SizeToContent = SizeToContent.Manual;
SizeToContent = SizeToContent.WidthAndHeight;

var windowPosition =
WindowPositionCalculator.CalculateWindowPosition(PickScreen(), ActualWidth, ActualHeight);

// Position the window in the center of the screen
Left = (SystemParameters.PrimaryScreenWidth/2) - (ActualWidth/2);
Top = (SystemParameters.PrimaryScreenHeight/2) - (ActualHeight/2);
Left = windowPosition.Left;
Top = windowPosition.Top;
}

/// <summary>
/// Decides which screen to pop Switcheroo up
/// </summary>
/// <returns></returns>
private Screen PickScreen()
{
Screen screen;
switch (Settings.Default.MultiMonitor)
{
case 0:
screen = Screen.PrimaryScreen;
break;
case 1:
screen = Screen.FromHandle(_foregroundWindow.HWnd);
break;
case 2:
screen = Screen.AllScreens.First(s => s.Bounds.Contains(MouseCursor.Position.X, MouseCursor.Position.Y));
break;
default:
screen = Screen.PrimaryScreen;
break;
}
return screen;
}

/// <summary>
Expand Down Expand Up @@ -606,7 +638,7 @@ private async void CloseWindow(object sender, ExecutedRoutedEventArgs e)
foreach (var win in windows)
{
bool isClosed = await _windowCloser.TryCloseAsync(win);
if(isClosed)
if (isClosed)
RemoveWindow(win);
}

Expand Down
8 changes: 8 additions & 0 deletions Switcheroo/OptionsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
<LineBreak />
<Italic>* Requires a restart of Switcheroo</Italic>
</TextBlock>
<Label Margin="25,0,5,0" Padding="0">Multi-Monitor Support</Label>
<ComboBox Margin="25,5,5,10" Name="MultiMonSelector"
Width="150"
HorizontalAlignment="Left">
<ComboBoxItem IsSelected="True">Default Monitor</ComboBoxItem>
<ComboBoxItem>Active Monitor</ComboBoxItem>
<ComboBoxItem>Mouse Location</ComboBoxItem>
</ComboBox>
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand Down
2 changes: 2 additions & 0 deletions Switcheroo/OptionsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public OptionsWindow()
AutoSwitch.IsChecked = Settings.Default.AutoSwitch;
AutoSwitch.IsEnabled = Settings.Default.AltTabHook;
RunAsAdministrator.IsChecked = Settings.Default.RunAsAdmin;
MultiMonSelector.SelectedIndex = Settings.Default.MultiMonitor;
}

private void Cancel_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -107,6 +108,7 @@ private void Ok_Click(object sender, RoutedEventArgs e)
Settings.Default.AltTabHook = AltTabCheckBox.IsChecked.GetValueOrDefault();
Settings.Default.AutoSwitch = AutoSwitch.IsChecked.GetValueOrDefault();
Settings.Default.RunAsAdmin = RunAsAdministrator.IsChecked.GetValueOrDefault();
Settings.Default.MultiMonitor = MultiMonSelector.SelectedIndex;
Settings.Default.Save();

if (closeOptionsWindow)
Expand Down
12 changes: 12 additions & 0 deletions Switcheroo/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Switcheroo/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
<Setting Name="EnableHotKey" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="MultiMonitor" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>
3 changes: 3 additions & 0 deletions Switcheroo/Switcheroo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="AppWindowViewModel.cs" />
<Compile Include="ICalculateWindowPosition.cs" />
<Compile Include="IconToBitmapConverter.cs" />
<Compile Include="BoolToWhateverConverter.cs" />
<Compile Include="WindowCloser.cs" />
<Compile Include="WindowHandleToCachedIconConverter.cs" />
<Compile Include="WindowHandleToIconConverter.cs" />
<Compile Include="WindowPosition.cs" />
<Compile Include="WindowPositionCalculator.cs" />
<Page Include="AboutWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
8 changes: 8 additions & 0 deletions Switcheroo/WindowPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Switcheroo
{
public class WindowPosition
{
public double Top { get; set; }
public double Left { get; set; }
}
}
17 changes: 17 additions & 0 deletions Switcheroo/WindowPositionCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Windows.Forms;

namespace Switcheroo
{
public class WindowPositionCalculator : ICalculateWindowPosition
{
public WindowPosition CalculateWindowPosition(Screen screen, double windowWidth, double windowHeight)
{
return new WindowPosition
{
Left = Math.Round(screen.Bounds.X + (((double)screen.Bounds.Width / 2) - (windowWidth / 2))),
Top = Math.Round(screen.Bounds.Y + (((double)screen.Bounds.Height / 2) - (windowHeight / 2)))
};
}
}
}
91 changes: 47 additions & 44 deletions Switcheroo/app.config
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<configSections>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Switcheroo.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Switcheroo.Properties.Settings>
<setting name="Ctrl" serializeAs="String">
<value>False</value>
</setting>
<setting name="Shift" serializeAs="String">
<value>False</value>
</setting>
<setting name="WindowsKey" serializeAs="String">
<value>False</value>
</setting>
<setting name="HotKey" serializeAs="String">
<value>32</value>
</setting>
<setting name="Alt" serializeAs="String">
<value>True</value>
</setting>
<setting name="FirstRun" serializeAs="String">
<value>True</value>
</setting>
<setting name="AltTabHook" serializeAs="String">
<value>False</value>
</setting>
<setting name="RunAsAdmin" serializeAs="String">
<value>False</value>
</setting>
<setting name="AutoSwitch" serializeAs="String">
<value>False</value>
</setting>
<setting name="EnableHotKey" serializeAs="String">
<value>True</value>
</setting>
</Switcheroo.Properties.Settings>
</userSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<setting name="Ctrl" serializeAs="String">
<value>False</value>
</setting>
<setting name="Shift" serializeAs="String">
<value>False</value>
</setting>
<setting name="WindowsKey" serializeAs="String">
<value>False</value>
</setting>
<setting name="HotKey" serializeAs="String">
<value>32</value>
</setting>
<setting name="Alt" serializeAs="String">
<value>True</value>
</setting>
<setting name="FirstRun" serializeAs="String">
<value>True</value>
</setting>
<setting name="AltTabHook" serializeAs="String">
<value>False</value>
</setting>
<setting name="RunAsAdmin" serializeAs="String">
<value>False</value>
</setting>
<setting name="AutoSwitch" serializeAs="String">
<value>False</value>
</setting>
<setting name="EnableHotKey" serializeAs="String">
<value>True</value>
</setting>
<setting name="MultiMonitor" serializeAs="String">
<value>0</value>
</setting>
</Switcheroo.Properties.Settings>
</userSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>