Skip to content

Commit

Permalink
#45: Keep the main window opened when focus is loft (optional setting)
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrondeau committed Dec 21, 2016
1 parent 87ee800 commit 3b9969a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 17 deletions.
3 changes: 3 additions & 0 deletions GoToWindow/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<setting name="OpenShortcut" serializeAs="String">
<value>5B+09:2</value>
</setting>
<setting name="KeepOpenOnLostFocus" serializeAs="String">
<value>False</value>
</setting>
</GoToWindow.Properties.Settings>
</userSettings>
<runtime>
Expand Down
2 changes: 1 addition & 1 deletion GoToWindow/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void Application_Deactivated(object sender, EventArgs e)
Log.Debug("Application deactivated.");

if (_context != null)
_context.Hide();
_context.Hide(false);
}

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
Expand Down
18 changes: 11 additions & 7 deletions GoToWindow/GoToWindowContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows.Interop;
using System.Windows.Threading;
using GoToWindow.Api;
using GoToWindow.Properties;
using GoToWindow.ViewModels;
using GoToWindow.Windows;
using log4net;
Expand All @@ -18,7 +19,7 @@ public interface IGoToWindowContext : IDisposable
IGoToWindowPluginsContainer PluginsContainer { get; }
void Init();
void Show();
void Hide();
void Hide(bool requested);
void EnableKeyboardHook(KeyboardShortcut shortcut);
void ShowSettings();
void UpdateAvailable(string version);
Expand Down Expand Up @@ -129,13 +130,16 @@ private void SetAvailableWindowSize(double screenWidth, double screenHeight)
}
}

public void Hide()
public void Hide(bool requested)
{
Hide(false);
Hide(false, requested);
}

public void Hide(bool hideIfPending)
public void Hide(bool hideIfPending, bool requested)
{
if (!requested && Settings.Default.KeepOpenOnLostFocus)
return;

lock (_lock)
{
if (_state == GoToWindowState.Showing)
Expand Down Expand Up @@ -256,7 +260,7 @@ private bool HideWindowIfNotForeground()
Log.DebugFormat("Window does not have focus when initialization is complete. Current foreground window is {0} (Process '{1}')", foregroundWindow.HWnd, foregroundWindow.ProcessName);
#endif

Hide();
Hide(false);
return true;
}

Expand All @@ -276,9 +280,9 @@ private void HandleShortcut()
Application.Current.Dispatcher.InvokeAsync(Show, DispatcherPriority.Normal);
}

private void _mainViewModel_Hide(object sender, EventArgs e)
private void _mainViewModel_Hide(object sender, CloseEventArgs e)
{
Hide();
Hide(false, e.Requested);
}

public void Dispose()
Expand Down
14 changes: 13 additions & 1 deletion GoToWindow/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 GoToWindow/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
<Setting Name="OpenShortcut" Type="System.String" Scope="User">
<Value Profile="(Default)">5B+09:2</Value>
</Setting>
<Setting Name="KeepOpenOnLostFocus" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
20 changes: 16 additions & 4 deletions GoToWindow/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public bool UpdateAvailable

public ICommand GoToWindowEntryShortcut { get; private set; }

public event EventHandler Close;
public event CloseEventHandler Close;

public MainViewModel()
{
Expand Down Expand Up @@ -140,10 +140,10 @@ public void Empty()
IsRowIndexVisible = false;
}

public void AskClose()
public void AskClose(bool requested)
{
if (Close != null)
Close(this, new EventArgs());
Close(this, new CloseEventArgs(requested));
}

private ISearchResult GetEntryAt(int index)
Expand All @@ -162,7 +162,19 @@ private ISearchResult GetEntryAt(int index)
private void GoToWindowEntryShortcutCommand_Executed(object sender, EventArgs e)
{
if (Close != null)
Close(this, new EventArgs());
Close(this, new CloseEventArgs(true));
}
}

public delegate void CloseEventHandler(object sender, CloseEventArgs args);

public class CloseEventArgs : EventArgs
{
public bool Requested { get; private set; }

public CloseEventArgs(bool requested)
{
Requested = requested;
}
}
}
3 changes: 3 additions & 0 deletions GoToWindow/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public SettingsViewModel(IGoToWindowContext context)
}

public bool WindowListSingleClick { get; set; }
public bool KeepOpenOnLostFocus { get; set; }
public bool NoElevatedPrivilegesWarning { get; set; }
public string Version { get; set; }
public List<SettingsPluginViewModel> Plugins { get; protected set; }
Expand Down Expand Up @@ -151,6 +152,7 @@ public void Load()
{
// Settings
WindowListSingleClick = Settings.Default.WindowListSingleClick;
KeepOpenOnLostFocus = Settings.Default.KeepOpenOnLostFocus;

// Shortcut
var shortcut = KeyboardShortcut.FromString(Settings.Default.OpenShortcut);
Expand Down Expand Up @@ -190,6 +192,7 @@ public void Apply()

// Settings
Settings.Default.WindowListSingleClick = WindowListSingleClick;
Settings.Default.KeepOpenOnLostFocus = KeepOpenOnLostFocus;

// Plugins
var disabledPlugins = new StringCollection();
Expand Down
8 changes: 4 additions & 4 deletions GoToWindow/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void FocusSelectedWindowItem()

windowEntry.Select();

ViewModel.AskClose();
ViewModel.AskClose(true);
}

private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
Expand Down Expand Up @@ -185,7 +185,7 @@ private void WindowsListView_PreviewKeyDown(object sender, KeyEventArgs e)

private void ClearSearchButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.AskClose();
ViewModel.AskClose(true);
}

private void Window_Activated(object sender, EventArgs e)
Expand All @@ -196,7 +196,7 @@ private void Window_Activated(object sender, EventArgs e)
private void Window_Deactivated(object sender, EventArgs e)
{
Log.Debug("Window deactivated.");
ViewModel.AskClose();
ViewModel.AskClose(false);
}

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
Expand All @@ -205,7 +205,7 @@ private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
ViewModel.IsRowIndexVisible = true;

if (e.Key == Key.Escape)
ViewModel.AskClose();
ViewModel.AskClose(true);
}

private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
Expand Down
1 change: 1 addition & 0 deletions GoToWindow/Windows/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
</StackPanel>

<CheckBox x:Name="WindowListSingleClick" Content="Click once to select an item in the windows list" IsChecked="{Binding WindowListSingleClick}" HorizontalAlignment="Left" Margin="2" />
<CheckBox x:Name="KeepOpenWhenLostFocus" Content="Keep the GoToWindow search form opened when focus is lost" IsChecked="{Binding KeepOpenOnLostFocus}" HorizontalAlignment="Left" Margin="2" />
</StackPanel>
</Grid>
</TabItem>
Expand Down

0 comments on commit 3b9969a

Please sign in to comment.