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

Proposed Fix for Issue #69 - Better handling of Always-On-Top apps #70

Open
wants to merge 6 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
67 changes: 67 additions & 0 deletions Switcheroo/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ManagedWinapi.Windows;

namespace Switcheroo
{
public static class Extensions
{
public static bool IsPinnedToBottom(this AppWindowViewModel appWindow, IEnumerable<IntPtr> pinnedList)
{
return pinnedList.Any(ptr => (long)ptr == (long)appWindow.HWnd);
}

public static void PinToBottom(this AppWindowViewModel appWindow, List<IntPtr> pinnedList)
{
if (appWindow.IsPinnedToBottom(pinnedList))
return;

pinnedList.Add(appWindow.HWnd);
}

public static void UnpinFromBottom(this AppWindowViewModel appWindow, List<IntPtr> pinnedList)
{
if (!appWindow.IsPinnedToBottom(pinnedList))
return;

pinnedList.RemoveAll(ptr => (long) ptr == (long) appWindow.HWnd);
}

public static List<AppWindowViewModel> Sort(this IEnumerable<AppWindowViewModel> windowList,
SystemWindow foregroundWindow, IEnumerable<IntPtr> pinnedList)
{
var result = new List<AppWindowViewModel>();

var windowArray = windowList as AppWindowViewModel[] ?? windowList.ToArray();
result.AddRange(windowArray.Where(w => !w.IsPinnedToBottom(pinnedList)));
result.AddRange(windowArray.Where(w => w.IsPinnedToBottom(pinnedList)));

var firstWindow = result.FirstOrDefault();

if (firstWindow != null && firstWindow.AppWindow.IsRelatedTo(foregroundWindow))
{
result.RemoveAt(0);
result.Add(firstWindow);
}

return result;
}

public static bool IsRelatedTo(this SystemWindow window1, SystemWindow window2)
{
return window1.HasTheSameHandleAs(window2) || window1.HasTheSameProcessIdAs(window2);
}

public static bool HasTheSameHandleAs(this SystemWindow window1, SystemWindow window2)
{

return window1.HWnd == window2.HWnd;
}

public static bool HasTheSameProcessIdAs(this SystemWindow window1, SystemWindow window2)
{
return window1.Process.Id == window2.Process.Id;
}
}
}
15 changes: 15 additions & 0 deletions Switcheroo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
Executed="ScrollListUp" />
<CommandBinding Command="local:MainWindow.ScrollListDownCommand"
Executed="ScrollListDown" />
<CommandBinding Command="local:MainWindow.PinToBottomCommand"
Executed="PinCommand" />
<CommandBinding Command="local:MainWindow.UnpinFromBottomCommand"
Executed="UnpinCommand"/>
</Window.CommandBindings>

<Window.InputBindings>
Expand All @@ -30,6 +34,16 @@
<Window.Resources>
<local:WindowHandleToIconConverter x:Key="WindowHandleToIconConverter" />
<local:WindowHandleToCachedIconConverter x:Key="WindowHandleToCachedIconConverter" />
<ContextMenu x:Key="CmPinToBottom">
<MenuItem Command="local:MainWindow.PinToBottomCommand">
<MenuItem.Icon>
<Image Source="Resources/pin.png"></Image>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
<ContextMenu x:Key="CmUnpinFromBottom">
<MenuItem Command="local:MainWindow.UnpinFromBottomCommand"/>
</ContextMenu>
</Window.Resources>

<Border Padding="3" Background="White" BorderBrush="DarkGray" BorderThickness="1" Name="Border">
Expand Down Expand Up @@ -199,6 +213,7 @@
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="Padding" Value="0,5" />
<EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick" />
<EventSetter Event="MouseRightButtonUp" Handler="ListBoxItem_MouseRightClick" />
</Style>
</ListBox.ItemContainerStyle>
<!-- Changes inactive selection color to be same as selected -->
Expand Down
73 changes: 52 additions & 21 deletions Switcheroo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
using Switcheroo.Core.Matchers;
using Switcheroo.Properties;
using Application = System.Windows.Application;
using ContextMenu = System.Windows.Controls.ContextMenu;
using MenuItem = System.Windows.Forms.MenuItem;
using MessageBox = System.Windows.MessageBox;

Expand All @@ -56,10 +57,13 @@ 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();
public static readonly RoutedUICommand PinToBottomCommand = new RoutedUICommand("Pin To Bottom", "PinToBottom", typeof(AppWindowViewModel), new InputGestureCollection {new KeyGesture(Key.L, ModifierKeys.Control)});
public static readonly RoutedUICommand UnpinFromBottomCommand = new RoutedUICommand("Unpin From Bottom", "UnpinFromBottom", typeof(MainWindow), new InputGestureCollection {new KeyGesture(Key.H, ModifierKeys.Control)});
private OptionsWindow _optionsWindow;
private AboutWindow _aboutWindow;
private AltTabHook _altTabHook;
private SystemWindow _foregroundWindow;
private List<IntPtr> _pinnedToBottom = new List<IntPtr>();

public MainWindow()
{
Expand Down Expand Up @@ -247,19 +251,7 @@ private static async Task<Version> GetLatestVersion()
/// </summary>
private void LoadData(InitialFocus focus)
{
_unfilteredWindowList = new WindowFinder().GetWindows().Select(window => new AppWindowViewModel(window)).ToList();

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))
{
_unfilteredWindowList.RemoveAt(0);
_unfilteredWindowList.Add(firstWindow);
foregroundWindowMovedToBottom = true;
}
_unfilteredWindowList = new WindowFinder().GetWindows().Select(window => new AppWindowViewModel(window)).Sort(_foregroundWindow, _pinnedToBottom);

_filteredWindowList = new ObservableCollection<AppWindowViewModel>(_unfilteredWindowList);
_windowCloser = new WindowCloser();
Expand All @@ -274,25 +266,21 @@ private void LoadData(InitialFocus focus)
lb.DataContext = null;
lb.DataContext = _filteredWindowList;

FocusItemInList(focus, foregroundWindowMovedToBottom);
FocusItemInList(focus, _foregroundWindow);

tb.Clear();
tb.Focus();
CenterWindow();
ScrollSelectedItemIntoView();
}

private static bool AreWindowsRelated(SystemWindow window1, SystemWindow window2)
{
return window1.HWnd == window2.HWnd || window1.Process.Id == window2.Process.Id;
}

private void FocusItemInList(InitialFocus focus, bool foregroundWindowMovedToBottom)
private void FocusItemInList(InitialFocus focus, SystemWindow foregroundWindow)
{
if (focus == InitialFocus.PreviousItem)
{
var previousItemIndex = lb.Items.Count - 1;
if (foregroundWindowMovedToBottom)
lb.SelectedIndex = previousItemIndex;
if ((lb.SelectedItem as AppWindowViewModel).AppWindow.HasTheSameHandleAs(foregroundWindow))
{
previousItemIndex--;
}
Expand Down Expand Up @@ -673,6 +661,49 @@ private void ShowHelpTextBlock_OnPreviewMouseDown(object sender, MouseButtonEven
HelpPanel.BeginAnimation(HeightProperty, new DoubleAnimation(HelpPanel.Height, newHeight, duration));
}

private void ListBoxItem_MouseRightClick(object sender, MouseButtonEventArgs e)
{
var appWindow = (sender as ListBoxItem).Content as AppWindowViewModel;

if (appWindow.IsPinnedToBottom(_pinnedToBottom))
{
var cmUnpinFromBottom = this.FindResource("CmUnpinFromBottom") as ContextMenu;
cmUnpinFromBottom.PlacementTarget = sender as ListBoxItem;
cmUnpinFromBottom.IsOpen = true;
return;
}

var cmPinToBottom = this.FindResource("CmPinToBottom") as ContextMenu;
cmPinToBottom.PlacementTarget = sender as ListBoxItem;
cmPinToBottom.IsOpen = true;
}

private void PinCommand(object sender, ExecutedRoutedEventArgs e)
{
var appWindow = lb.SelectedItem as AppWindowViewModel;

appWindow.PinToBottom(_pinnedToBottom);

ReorderList();
}

private void UnpinCommand(object sender, ExecutedRoutedEventArgs e)
{
var appWindow = lb.SelectedItem as AppWindowViewModel;

appWindow.UnpinFromBottom(_pinnedToBottom);

ReorderList();
}

private void ReorderList()
{
var windows = lb.Items.Cast<AppWindowViewModel>().Sort(_foregroundWindow, _pinnedToBottom);

lb.DataContext = windows;
lb.SelectedIndex = 0;
}

#endregion

private enum InitialFocus
Expand Down
12 changes: 11 additions & 1 deletion Switcheroo/Properties/Resources.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/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,7 @@
<data name="icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="pin" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\pin.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added Switcheroo/Resources/pin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions Switcheroo/Switcheroo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="AppWindowViewModel.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="IconToBitmapConverter.cs" />
<Compile Include="BoolToWhateverConverter.cs" />
<Compile Include="WindowCloser.cs" />
Expand Down Expand Up @@ -212,6 +213,9 @@
<ItemGroup>
<Resource Include="icon.ico" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\pin.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!--<Import Project="$(ProgramFiles)\MSBuild\Microsoft\StyleCop\v4.3\Microsoft.StyleCop.targets" />-->
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Binary file added pin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.