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

Multiple window closer - round 2 #90

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Core/AppWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public AppWindow Owner
return new AppWindow(ownerHandle);
}
}

public new static IEnumerable<AppWindow> AllToplevelWindows
public new static IEnumerable<AppWindow> AllToplevelWindows
{
get
{
Expand Down Expand Up @@ -250,4 +250,4 @@ private static string GetExecutablePath(int processId)
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
}
}
8 changes: 4 additions & 4 deletions Switcheroo.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30723.0
# Visual Studio 14
VisualStudioVersion = 14.0.22823.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Switcheroo", "Switcheroo\Switcheroo.csproj", "{B28E183B-0E38-48A8-8A4E-B4AB7B23CE93}"
ProjectSection(ProjectDependencies) = postProject
Expand All @@ -27,8 +27,8 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B28E183B-0E38-48A8-8A4E-B4AB7B23CE93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B28E183B-0E38-48A8-8A4E-B4AB7B23CE93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B28E183B-0E38-48A8-8A4E-B4AB7B23CE93}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{B28E183B-0E38-48A8-8A4E-B4AB7B23CE93}.Debug|Any CPU.Build.0 = Release|Any CPU
{B28E183B-0E38-48A8-8A4E-B4AB7B23CE93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B28E183B-0E38-48A8-8A4E-B4AB7B23CE93}.Release|Any CPU.Build.0 = Release|Any CPU
{FBD3EC1E-47E2-4D2D-81C9-D6506125A09A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down
10 changes: 9 additions & 1 deletion Switcheroo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<Window.CommandBindings>
<CommandBinding Command="local:MainWindow.CloseWindowCommand"
Executed="CloseWindow" />
<CommandBinding Command="local:MainWindow.CloseProcessesCommand"
Executed="CloseWindows" />
<CommandBinding Command="local:MainWindow.SwitchToWindowCommand"
Executed="OnEnterPressed" />
<CommandBinding Command="local:MainWindow.ScrollListUpCommand"
Expand All @@ -23,6 +25,7 @@
<Window.InputBindings>
<KeyBinding Command="local:MainWindow.CloseWindowCommand" Key="W" Modifiers="Ctrl" />
<KeyBinding Command="local:MainWindow.CloseWindowCommand" Key="X" Modifiers="Alt" />
<KeyBinding Command="local:MainWindow.CloseProcessesCommand" Gesture="Ctrl+Shift+W"/>
<KeyBinding Command="local:MainWindow.ScrollListUpCommand" Key="Up" />
<KeyBinding Command="local:MainWindow.ScrollListDownCommand" Key="Down" />
<KeyBinding Command="local:MainWindow.ScrollListUpCommand" Key="K" Modifiers="Alt" />
Expand Down Expand Up @@ -67,6 +70,11 @@
</TextBlock>
</StackPanel>
</DockPanel>
<DockPanel Margin="4,0,4,0" Name="ConfirmPanel" Background="#fff3cd" Height="0" DockPanel.Dock="Top">
<TextBlock FontSize="10" DockPanel.Dock="Left" Foreground="#856404">
press <Bold>ctrl + shift + w</Bold> again to close all windows in the list
</TextBlock>
</DockPanel>
<Grid DockPanel.Dock="Top">
<TextBox Name="tb" Padding="5" VerticalAlignment="Top" TextChanged="TextChanged" FontSize="15"
VerticalContentAlignment="Center">
Expand Down Expand Up @@ -211,4 +219,4 @@
</ListBox>
</DockPanel>
</Border>
</Window>
</Window>
66 changes: 56 additions & 10 deletions Switcheroo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ 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 CloseProcessesCommand = new RoutedUICommand();
private OptionsWindow _optionsWindow;
private AboutWindow _aboutWindow;
private AltTabHook _altTabHook;
Expand Down Expand Up @@ -268,7 +269,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 +283,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 @@ -334,8 +335,8 @@ private void CenterWindow()
SizeToContent = SizeToContent.WidthAndHeight;

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

/// <summary>
Expand Down Expand Up @@ -603,15 +604,42 @@ private void ListBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
private async void CloseWindow(object sender, ExecutedRoutedEventArgs e)
{
var windows = lb.SelectedItems.Cast<AppWindowViewModel>().ToList();
foreach (var win in windows)
foreach (var window in windows)
{
bool isClosed = await _windowCloser.TryCloseAsync(win);
if(isClosed)
RemoveWindow(win);
var isClosed = await _windowCloser.TryCloseAsync(window);
if (isClosed)
{
RemoveWindow(window);
}
}

if (lb.Items.Count == 0)
{
HideWindow();
}

e.Handled = true;
}

private async void CloseWindows(object sender, ExecutedRoutedEventArgs e)
{
if (ConfirmPanel.Height == 0)
{
ShowConfirmPanel();
TimedAction.ExecuteWithDelay(HideConfirmPanel, TimeSpan.FromSeconds(3));
return;
}

var windows = lb.Items.Cast<AppWindowViewModel>().ToList();

foreach (var window in windows)
{
var isClosed = await _windowCloser.TryCloseAsync(window);
if (isClosed)
{
RemoveWindow(window);
}
}

e.Handled = true;
}
Expand Down Expand Up @@ -716,6 +744,24 @@ private void ShowHelpTextBlock_OnPreviewMouseDown(object sender, MouseButtonEven
HelpPanel.BeginAnimation(HeightProperty, new DoubleAnimation(HelpPanel.Height, newHeight, duration));
}

private void ShowConfirmPanel()
{
var duration = new Duration(TimeSpan.FromSeconds(0.150));
var newHeight = 17;
ConfirmPanel.BeginAnimation(HeightProperty, new DoubleAnimation(ConfirmPanel.Height, newHeight, duration));
}

private void HideConfirmPanel()
{
if (ConfirmPanel.Height == 0)
{
return;
}

var duration = new Duration(TimeSpan.FromSeconds(0.150));
var newHeight = 0;
ConfirmPanel.BeginAnimation(HeightProperty, new DoubleAnimation(ConfirmPanel.Height, newHeight, duration));
}
#endregion

private enum InitialFocus
Expand All @@ -724,4 +770,4 @@ private enum InitialFocus
PreviousItem
}
}
}
}
1 change: 1 addition & 0 deletions Switcheroo/Switcheroo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="AppWindowViewModel.cs" />
<Compile Include="IconToBitmapConverter.cs" />
<Compile Include="BoolToWhateverConverter.cs" />
<Compile Include="TimedAction.cs" />
<Compile Include="WindowCloser.cs" />
<Compile Include="WindowHandleToCachedIconConverter.cs" />
<Compile Include="WindowHandleToIconConverter.cs" />
Expand Down
29 changes: 29 additions & 0 deletions Switcheroo/TimedAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Windows.Threading;

namespace Switcheroo
{
public static class TimedAction
{
public static void ExecuteWithDelay(Action action, TimeSpan delay)
{
var timer = new DispatcherTimer
{
Interval = delay,
Tag = action
};

timer.Tick += TimerOnTick;
timer.Start();
}

private static void TimerOnTick(object sender, EventArgs e)
{
var timer = (DispatcherTimer) sender;
var action = (Action) timer.Tag;

action.Invoke();
timer.Stop();
}
}
}