Skip to content

Commit

Permalink
Refactor, automatically adapt window size to screen size. Add Design …
Browse files Browse the repository at this point in the history
…Time data.
  • Loading branch information
Christian Rondeau committed Sep 1, 2014
1 parent b0e46ff commit 4b90f0e
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 61 deletions.
63 changes: 10 additions & 53 deletions GoToWindow/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using GoToWindow.Api;
using GoToWindow.Commands;
using Hardcodet.Wpf.TaskbarNotification;
using GoToWindow.Components;
using log4net;

namespace GoToWindow
Expand All @@ -14,8 +11,8 @@ public partial class App : Application
private static readonly ILog Log = LogManager.GetLogger(typeof(App).Assembly, "GoToWindow");

private IGoToWindowContext _context;
private GoToWindowTrayIcon _menu;
private Mutex _mutex;
private TaskbarIcon _trayIcon;

private void Application_Startup(object sender, StartupEventArgs e)
{
Expand All @@ -30,13 +27,7 @@ private void Application_Startup(object sender, StartupEventArgs e)

_context = new GoToWindowContext();

_trayIcon = new TaskbarIcon
{
Icon = GoToWindow.Properties.Resources.AppIcon,
ToolTipText = "Go To Window",
DoubleClickCommand = new OpenMainWindowCommand(_context),
ContextMenu = CreateContextMenu()
};
_menu = new GoToWindowTrayIcon(_context);

_context.EnableAltTabHook(
GoToWindow.Properties.Settings.Default.HookAltTab,
Expand All @@ -47,61 +38,27 @@ private void Application_Startup(object sender, StartupEventArgs e)

Log.Info("Application started.");

ShowStartupTooltip();
_menu.ShowStartupTooltip();
}

private void ShowStartupTooltip()
private void Application_Exit(object sender, ExitEventArgs e)
{
var shortcutPressesBeforeOpen = GoToWindow.Properties.Settings.Default.ShortcutPressesBeforeOpen;
var openShortcutDescription = shortcutPressesBeforeOpen == 1
? "Alt + Tab"
: "Alt + Tab + Tab";

var tooltipMessage = string.Format("Press {0} and start typing to find a window.", openShortcutDescription);

if (!WindowsRuntimeHelper.GetHasElevatedPrivileges())
if (_menu != null)
{
tooltipMessage += Environment.NewLine + Environment.NewLine + "NOTE: Not running with elevated privileges. Performance will be affected; Will not work in applications running as an administrator.";
_menu.Dispose();
_menu = null;
}

_trayIcon.ShowBalloonTip(
"Go To Window",
tooltipMessage,
BalloonIcon.Info);
}

private ContextMenu CreateContextMenu()
{
var contextMenu = new ContextMenu();
var showMenu = new MenuItem { Header = "_Show", Command = new OpenMainWindowCommand(_context) };
contextMenu.Items.Add(showMenu);

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

contextMenu.Items.Add(new Separator());

var exitMenuItem = new MenuItem { Header = "E_xit", Command = new ExitCommand() };
contextMenu.Items.Add(exitMenuItem);

return contextMenu;
}

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

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

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

Log.Info("Application exited.");
Expand Down
73 changes: 73 additions & 0 deletions GoToWindow/Components/GoToWindowTrayIcon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Windows.Controls;
using GoToWindow.Api;
using GoToWindow.Commands;
using Hardcodet.Wpf.TaskbarNotification;

namespace GoToWindow.Components
{
public class GoToWindowTrayIcon : IDisposable
{
private readonly IGoToWindowContext _context;
private TaskbarIcon _trayIcon;

public GoToWindowTrayIcon(IGoToWindowContext context)
{
_context = context;

_trayIcon = new TaskbarIcon
{
Icon = Properties.Resources.AppIcon,
ToolTipText = "Go To Window",
DoubleClickCommand = new OpenMainWindowCommand(_context),
ContextMenu = CreateContextMenu()
};
}

private ContextMenu CreateContextMenu()
{
var contextMenu = new ContextMenu();
var showMenu = new MenuItem { Header = "_Show", Command = new OpenMainWindowCommand(_context) };
contextMenu.Items.Add(showMenu);

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

contextMenu.Items.Add(new Separator());

var exitMenuItem = new MenuItem { Header = "E_xit", Command = new ExitCommand() };
contextMenu.Items.Add(exitMenuItem);

return contextMenu;
}

public void ShowStartupTooltip()
{
var shortcutPressesBeforeOpen = Properties.Settings.Default.ShortcutPressesBeforeOpen;
var openShortcutDescription = shortcutPressesBeforeOpen == 1
? "Alt + Tab"
: "Alt + Tab + Tab";

var tooltipMessage = string.Format("Press {0} and start typing to find a window.", openShortcutDescription);

if (!WindowsRuntimeHelper.GetHasElevatedPrivileges())
{
tooltipMessage += Environment.NewLine + Environment.NewLine + "NOTE: Not running with elevated privileges. Performance will be affected; Will not work in applications running as an administrator.";
}

_trayIcon.ShowBalloonTip(
"Go To Window",
tooltipMessage,
BalloonIcon.Info);
}

public void Dispose()
{
if (_trayIcon != null)
{
_trayIcon.Dispose();
_trayIcon = null;
}
}
}
}
2 changes: 2 additions & 0 deletions GoToWindow/GoToWindow.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
<Compile Include="ExitCodes.cs" />
<Compile Include="GoToWindowContext.cs" />
<Compile Include="Commands\OpenMainWindowCommand.cs" />
<Compile Include="Components\GoToWindowTrayIcon.cs" />
<Compile Include="GoToWindowPluginsContainer.cs" />
<Compile Include="ViewModels\DesignTimeMainViewModel.cs" />
<Compile Include="ViewModels\MainViewModel.cs" />
<Compile Include="ViewModels\SettingsPluginViewModel.cs" />
<Compile Include="ViewModels\SettingsViewModel.cs" />
Expand Down
26 changes: 25 additions & 1 deletion GoToWindow/GoToWindowContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class GoToWindowContext : IGoToWindowContext
{
private enum GoToWindowState
{
Undefined,
Showing,
ShowingThenHide,
Shown,
Expand Down Expand Up @@ -74,6 +73,9 @@ public void Show()
Log.Debug("Showing Main Window.");
_state = GoToWindowState.Showing;

SetAvailableWindowSize();
_mainWindow.Left = SystemParameters.VirtualScreenWidth/2f - _mainViewModel.AvailableWindowWidth/2f;
_mainWindow.Top = SystemParameters.VirtualScreenHeight / 2f - (_mainViewModel.AvailableWindowHeight + 56) / 2f;
_mainWindow.Show();

if (_mainWindowEntry == null)
Expand All @@ -91,6 +93,28 @@ public void Show()
}
}

private void SetAvailableWindowSize()
{
var width = SystemParameters.VirtualScreenWidth;
var height = SystemParameters.VirtualScreenHeight;

if (width > 1280)
{
_mainViewModel.AvailableWindowWidth = (int) (width*0.5f);
_mainViewModel.AvailableWindowHeight = (int) (height*0.66f);
}
else if (width < 800)
{
_mainViewModel.AvailableWindowWidth = (int)(width * 0.8f);
_mainViewModel.AvailableWindowHeight = (int)(height * 0.8f);
}
else
{
_mainViewModel.AvailableWindowWidth = 640;
_mainViewModel.AvailableWindowHeight = (int)(height * 0.6f);
}
}

public void Hide()
{
lock (_lock)
Expand Down
63 changes: 63 additions & 0 deletions GoToWindow/ViewModels/DesignTimeMainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using GoToWindow.Extensibility;
using GoToWindow.Extensibility.Controls;
using GoToWindow.Extensibility.ViewModel;

namespace GoToWindow.ViewModels
{
public class DesignTimeMainViewModel : MainViewModel
{
private class DesignTimeSearchResult : ISearchResult, IBasicSearchResult
{
public UserControl View { get; private set; }

public BitmapFrame Icon { get; private set; }
public string Title { get; private set; }
public string ProcessName { get; private set; }

public DesignTimeSearchResult(string processName, string title)
{
View = new BasicListEntry();
ProcessName = processName;
Title = title;
}

public void Select()
{
}

public bool IsShown(string searchQuery)
{
return true;
}
}

public DesignTimeMainViewModel()
{
IsEmpty = false;
AvailableWindowWidth = 640;
AvailableWindowHeight = 320;
//SearchText = "User Query...";
Windows = new CollectionViewSource
{
Source = new List<ISearchResult>
{
new DesignTimeSearchResult("process", "Window Title"),
new DesignTimeSearchResult("very long process name", "Very very long window title that should end up with ellipsis because it is so very long"),
new DesignTimeSearchResult("filler", "Some Window Title"),
new DesignTimeSearchResult("filler", "Some Window Title"),
new DesignTimeSearchResult("filler", "Some Window Title"),
new DesignTimeSearchResult("filler", "Some Window Title"),
new DesignTimeSearchResult("filler", "Some Window Title"),
new DesignTimeSearchResult("filler", "Some Window Title"),
new DesignTimeSearchResult("filler", "Some Window Title"),
new DesignTimeSearchResult("filler", "Some Window Title")
}
};
}
}
}
28 changes: 25 additions & 3 deletions GoToWindow/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@

namespace GoToWindow.ViewModels
{
public class MainViewModel : INotifyPropertyChanged
public class MainViewModel : INotifyPropertyChanged
{
private static readonly ILog Log = LogManager.GetLogger(typeof(MainViewModel).Assembly, "GoToWindow");

private string _searchText;
private bool _isEmpty;
private int _availableWindowWidth;
private int _availableWindowHeight;

public event PropertyChangedEventHandler PropertyChanged;

public CollectionViewSource Windows { get; private set; }
public CollectionViewSource Windows { get; protected set; }
public ISearchResult SelectedWindowEntry { get; set; }

public void Load(IEnumerable<IGoToWindowPlugin> plugins)
Expand Down Expand Up @@ -77,13 +79,33 @@ public string SearchText
public bool IsEmpty
{
get { return _isEmpty; }
private set
protected set
{
_isEmpty = value;
OnPropertyChanged("IsEmpty");
}
}

public int AvailableWindowWidth
{
get { return _availableWindowWidth; }
set
{
_availableWindowWidth = value;
OnPropertyChanged("AvailableWindowWidth");
}
}

public int AvailableWindowHeight
{
get { return _availableWindowHeight; }
set
{
_availableWindowHeight = value;
OnPropertyChanged("AvailableWindowHeight");
}
}

protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
Expand Down
10 changes: 6 additions & 4 deletions GoToWindow/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
xmlns:converters="clr-namespace:GoToWindow.Converters"
xmlns:model="clr-namespace:GoToWindow.Extensibility.ViewModel;assembly=GoToWindow.Extensibility"
mc:Ignorable="d"
Title="Go To Window" Height="Auto" Width="640"
WindowStartupLocation="CenterScreen"
Title="Go To Window"
Width="{Binding Path=AvailableWindowWidth}" Height="Auto"
SizeToContent="Height"
WindowStartupLocation="Manual"
AllowsTransparency="True"
Background="Transparent"
WindowStyle="None"
Expand All @@ -21,7 +23,7 @@
Deactivated="Window_Deactivated"
Activated="Window_Activated"
Loaded="Window_Loaded"
d:DataContext="{d:DesignInstance viewModels:MainViewModel}">
d:DataContext="{d:DesignInstance viewModels:DesignTimeMainViewModel, IsDesignTimeCreatable=True}">
<Window.Resources>
<Style x:Key="RoundButton" TargetType="{x:Type Button}">
<Setter Property="Template">
Expand Down Expand Up @@ -112,7 +114,7 @@
MouseDoubleClick="WindowsListView_MouseDoubleClick"
PreviewKeyDown="WindowsListView_PreviewKeyDown"
BorderBrush="Transparent"
MaxHeight="288"
MaxHeight="{Binding Path=AvailableWindowHeight}"
Visibility="{Binding IsEmpty, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InvertAndVisibilityConverter}}"
ItemsSource="{Binding Windows.View}"
SelectedItem="{Binding SelectedWindowEntry, Mode=TwoWay}">
Expand Down

0 comments on commit 4b90f0e

Please sign in to comment.