Skip to content

Commit

Permalink
Add a message when not running as administrator, add setting to start…
Browse files Browse the repository at this point in the history
… with windows
  • Loading branch information
christianrondeau committed Jul 27, 2014
1 parent 4836452 commit 28c0b07
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 38 deletions.
28 changes: 7 additions & 21 deletions GoToWindow/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace GoToWindow
public partial class App : Application
{
private readonly IGoToWindowContext _context = new GoToWindowContext();
private InterceptAltTab _keyHandler;
private Mutex _mutex;

private void Application_Startup(object sender, StartupEventArgs e)
Expand All @@ -37,8 +36,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
trayIcon.DoubleClickCommand = new OpenMainWindowCommand(_context);
trayIcon.ContextMenu = CreateContextMenu();

if (GoToWindow.Properties.Settings.Default.HookAltTab)
_keyHandler = new InterceptAltTab(HandleAltTab);
_context.EnableAltTabHook(GoToWindow.Properties.Settings.Default.HookAltTab);
}

private ContextMenu CreateContextMenu()
Expand All @@ -47,7 +45,7 @@ private ContextMenu CreateContextMenu()
var showMenu = new MenuItem { Header = "Show", Command = new OpenMainWindowCommand(_context) };
contextMenu.Items.Add(showMenu);

var settingsMenu = new MenuItem { Header = "Settings", Command = new ShowSettingsCommand() };
var settingsMenu = new MenuItem { Header = "Settings", Command = new ShowSettingsCommand(_context) };
contextMenu.Items.Add(settingsMenu);

contextMenu.Items.Add(new Separator());
Expand All @@ -58,29 +56,17 @@ private ContextMenu CreateContextMenu()
return contextMenu;
}

delegate void CommandDelegate(object parameters);

private void HandleAltTab()
{
var cmd = new OpenMainWindowCommand(_context);
Application.Current.Dispatcher.BeginInvoke(
new CommandDelegate(cmd.Execute),
DispatcherPriority.Normal,
new Object[] { null });
}

private void Application_Exit(object sender, ExitEventArgs e)
{
if (_mutex != null)
if (_context != null)
{
_mutex.ReleaseMutex();
_mutex.Dispose();
_context.Dispose();
}

if (_keyHandler != null)
if (_mutex != null)
{
_keyHandler.Dispose();
_keyHandler = null;
_mutex.ReleaseMutex();
_mutex.Dispose();
}
}

Expand Down
11 changes: 11 additions & 0 deletions GoToWindow/CommandDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GoToWindow
{
class CommandDelegate
{
}
}
10 changes: 10 additions & 0 deletions GoToWindow/Commands/CommandDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GoToWindow.Commands
{
delegate void CommandDelegate(object parameters);
}
6 changes: 4 additions & 2 deletions GoToWindow/Commands/ShowSettingsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace GoToWindow.Commands
public class ShowSettingsCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private IGoToWindowContext _context;

public ShowSettingsCommand()
public ShowSettingsCommand(IGoToWindowContext context)
{
_context = context;
}

public bool CanExecute(object parameter)
Expand All @@ -25,7 +27,7 @@ public void Execute(object parameter)
if (Application.Current.Windows.OfType<SettingsWindow>().Count() > 0)
return;

new SettingsWindow().Show();
new SettingsWindow(_context).ShowDialog();
}
}
}
2 changes: 2 additions & 0 deletions GoToWindow/GoToWindow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="BindingExtensions\SettingsBindingExtension.cs" />
<Compile Include="CommandDelegate.cs" />
<Compile Include="Commands\CommandDelegate.cs" />
<Compile Include="Commands\ShowSettingsCommand.cs" />
<Compile Include="Converters\ExecutableToIconValueConverter.cs" />
<Compile Include="Commands\ExitCommand.cs" />
Expand Down
41 changes: 39 additions & 2 deletions GoToWindow/GoToWindowContext.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
using System;
using GoToWindow.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace GoToWindow
{
public interface IGoToWindowContext
public interface IGoToWindowContext : IDisposable
{
void Show();
void Hide();
void EnableAltTabHook(bool enabled);
}

public class GoToWindowContext : IGoToWindowContext
{
delegate void ActionDelegate();

private MainWindow _mainWindow;
private InterceptAltTab _hooks;

public void Show()
{
Expand All @@ -35,5 +42,35 @@ public void Hide()
if (_mainWindow != null && _mainWindow.IsLoaded)
_mainWindow.Close();
}

public void EnableAltTabHook(bool enabled)
{
if(_hooks == null && enabled)
{
_hooks = new InterceptAltTab(HandleAltTab);
}
else if (_hooks != null && !enabled)
{
_hooks.Dispose();
_hooks = null;
}
}

private void HandleAltTab()
{
Application.Current.Dispatcher.BeginInvoke(
new ActionDelegate(Show),
DispatcherPriority.Normal,
null);
}

public void Dispose()
{
if (_hooks != null)
{
_hooks.Dispose();
_hooks = null;
}
}
}
}
15 changes: 8 additions & 7 deletions GoToWindow/Windows/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gotowindowBindingExtensions="clr-namespace:GoToWindow.BindingExtensions"
Title="Go To Window Settings" Icon="/GoToWindow;component/Resources/AppIcon.ico" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" WindowStyle="ToolWindow" SizeToContent="WidthAndHeight">
<Grid Height="140" Width="380">
<CheckBox Content="Hook Alt+Tab" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top" IsChecked="{gotowindowBindingExtensions:SettingBinding Path=HookAltTab}"/>
<Button Name="okButton" IsDefault="True" Content="Apply" HorizontalAlignment="Left" Margin="295,110,0,0" VerticalAlignment="Top" Width="75" Click="okButton_Click"/>
<Button Name="cancelButton" IsCancel="True" Content="Cancel" HorizontalAlignment="Left" Margin="215,110,0,0" VerticalAlignment="Top" Width="75" Click="cancelButton_Click"/>
Title="Go To Window Settings" Icon="/GoToWindow;component/Resources/AppIcon.ico" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" WindowStyle="ToolWindow" SizeToContent="WidthAndHeight" Initialized="Window_Initialized">
<Grid Height="160" Width="380">
<Label Content="Settings will be applied after Go To Window is restarted" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Label Content="© Christian Rondeau 2014" HorizontalAlignment="Left" Margin="10,104,0,0" VerticalAlignment="Top" FontStyle="Italic"/>

<CheckBox Name="hookAltTabCheckbox" Content="Hook Alt+Tab" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top" IsChecked="{gotowindowBindingExtensions:SettingBinding Path=HookAltTab}"/>
<CheckBox Name="startWithWindowsCheckbox" Content="Start with Windows" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,61,0,0"/>
<TextBlock Name="noElevatedPrivilegesWarning" Foreground="Red" HorizontalAlignment="Left" Margin="10,81,0,0" VerticalAlignment="Top" TextWrapping="Wrap" FontWeight="Bold">Unless you restart with elevated privileges, you will not be able to switch to applications with elevated permissions.</TextBlock>
<Label Content="© Christian Rondeau 2014" HorizontalAlignment="Left" Margin="10,124,0,0" VerticalAlignment="Top" FontStyle="Italic"/>
<Button Name="okButton" IsDefault="True" Content="Apply" HorizontalAlignment="Left" Margin="295,130,0,0" VerticalAlignment="Top" Width="75" Click="okButton_Click"/>
<Button Name="cancelButton" IsCancel="True" Content="Close" HorizontalAlignment="Left" Margin="215,130,0,0" VerticalAlignment="Top" Width="75" Click="cancelButton_Click"/>
</Grid>
</Window>
72 changes: 66 additions & 6 deletions GoToWindow/Windows/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using System;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -14,28 +20,82 @@

namespace GoToWindow.Windows
{
/// <summary>
/// Interaction logic for SettingsWindow.xaml
/// </summary>
public partial class SettingsWindow : Window
{
public SettingsWindow()
private bool _originalStartWithWindowsIsChecked;
private readonly IGoToWindowContext _context;

public SettingsWindow(IGoToWindowContext context)
{
_context = context;

InitializeComponent();
}

private void okButton_Click(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.Save();

MessageBox.Show("Settings will be applied on restart", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
if(_originalStartWithWindowsIsChecked != startWithWindowsCheckbox.IsChecked)
{
UpdateStartWithWindows(startWithWindowsCheckbox.IsChecked == true);
}

_context.EnableAltTabHook(Properties.Settings.Default.HookAltTab);

Close();
}

private void UpdateStartWithWindows(bool active)
{
if (active)
{
var executablePath = Assembly.GetExecutingAssembly().Location;

var process = new Process();
process.StartInfo = new ProcessStartInfo
{
FileName = "reg.exe",
Arguments = "add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v \"GoToWindow\" /t REG_SZ /d \"" + executablePath + "\" /f",
Verb = "runas",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden

};
process.Start();
process.WaitForExit();
}
else
{
var executablePath = Assembly.GetExecutingAssembly().Location;

var process = new Process();
process.StartInfo = new ProcessStartInfo
{
FileName = "reg.exe",
Arguments = "delete \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\" /v \"GoToWindow\" /f",
Verb = "runas",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
process.Start();
process.WaitForExit();
}
}

private void cancelButton_Click(object sender, RoutedEventArgs e)
{
Close();
}

private void Window_Initialized(object sender, EventArgs e)
{
RegistryKey runList = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
var executablePath = Assembly.GetExecutingAssembly().Location;
startWithWindowsCheckbox.IsChecked = _originalStartWithWindowsIsChecked = ((string)runList.GetValue("GoToWindow") == executablePath);

var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
noElevatedPrivilegesWarning.Visibility = (principal.IsInRole(WindowsBuiltInRole.Administrator) || principal.IsInRole(0x200)) ? Visibility.Hidden : Visibility.Visible;
}
}
}

0 comments on commit 28c0b07

Please sign in to comment.