Browse Source

Remember focus and restore it when a workbench window or pad gets activated.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5208 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 16 years ago
parent
commit
3e64285db9
  1. 6
      src/Libraries/AvalonDock/ManagedContent.cs
  2. 4
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  3. 91
      src/Main/Base/Project/Src/Gui/CustomFocusManager.cs
  4. 11
      src/Main/Base/Project/Src/Gui/Workbench/Layouts/AvalonPadContent.cs
  5. 12
      src/Main/Base/Project/Src/Gui/Workbench/Layouts/AvalonWorkbenchWindow.cs

6
src/Libraries/AvalonDock/ManagedContent.cs

@ -435,11 +435,15 @@ namespace AvalonDock @@ -435,11 +435,15 @@ namespace AvalonDock
if (ContainerPane != null && Manager != null)// && Manager.ActiveContent != this)
{
ContainerPane.SelectedItem = this;
ContainerPane.Focus();
FocusContent();
if (Manager != null)
Manager.ActiveContent = this;
}
}
protected virtual void FocusContent()
{
}
bool _isActiveDocument = false;

4
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@ -145,6 +146,7 @@ @@ -145,6 +146,7 @@
</Compile>
<Compile Include="Src\Gui\Components\LocalizedPropertyGrid\StringParserPropertyContainer.cs" />
<Compile Include="Src\Gui\Components\SideBar\TextEditorSideBar.cs" />
<Compile Include="Src\Gui\CustomFocusManager.cs" />
<Compile Include="Src\Gui\Dialogs\AddOpenWithEntryDialog.cs" />
<Compile Include="Src\Gui\Dialogs\AddOpenWithEntryDialog.Designer.cs">
<DependentUpon>AddOpenWithEntryDialog.cs</DependentUpon>

91
src/Main/Base/Project/Src/Gui/CustomFocusManager.cs

@ -0,0 +1,91 @@ @@ -0,0 +1,91 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Windows;
using System.Windows.Input;
using ICSharpCode.Core;
namespace ICSharpCode.SharpDevelop.Gui
{
/// <summary>
/// Custom focus scope implementation.
/// See http://www.codeproject.com/KB/WPF/EnhancedFocusScope.aspx for a description of the problems with the normal WPF FocusScope.
/// </summary>
public static class CustomFocusManager
{
// DP for attached behavior, toggles remembering on or off
public static readonly DependencyProperty RememberFocusedChildProperty =
DependencyProperty.RegisterAttached("RememberFocusedChild", typeof(bool), typeof(CustomFocusManager),
new FrameworkPropertyMetadata(false, OnRememberFocusedChildChanged));
// This property is used to remember the focused child.
// We are using WeakReferences because a visual tree may change while it is not visible, and we don't
// want to keep parts of the old visual tree alive.
static readonly DependencyProperty FocusedChildProperty =
DependencyProperty.RegisterAttached("FocusedChild", typeof(WeakReference), typeof(CustomFocusManager));
public static bool GetRememberFocusedChild(UIElement element)
{
if (element == null)
throw new ArgumentNullException("element");
return (bool)element.GetValue(RememberFocusedChildProperty);
}
public static void SetRememberFocusedChild(UIElement element, bool value)
{
if (element == null)
throw new ArgumentNullException("element");
element.SetValue(RememberFocusedChildProperty, value);
}
public static IInputElement GetFocusedChild(UIElement element)
{
if (element == null)
throw new ArgumentNullException("element");
WeakReference r = (WeakReference)element.GetValue(FocusedChildProperty);
if (r != null)
return (IInputElement)r.Target;
else
return null;
}
public static void SetFocusToRememberedChild(UIElement element)
{
IInputElement focusedChild = GetFocusedChild(element);
LoggingService.Debug("Restoring focus for " + element + " to " + focusedChild);
if (focusedChild != null)
Keyboard.Focus(focusedChild);
}
static void OnRememberFocusedChildChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = d as UIElement;
if (element != null) {
if ((bool)e.OldValue)
element.RemoveHandler(UIElement.GotFocusEvent, onGotFocusEventHandler);
if ((bool)e.NewValue)
element.AddHandler(UIElement.GotFocusEvent, onGotFocusEventHandler, true);
}
}
static readonly RoutedEventHandler onGotFocusEventHandler = OnGotFocus;
static void OnGotFocus(object sender, RoutedEventArgs e)
{
UIElement element = (UIElement)sender;
IInputElement focusedElement = e.OriginalSource as IInputElement;
WeakReference r = (WeakReference)element.GetValue(FocusedChildProperty);
if (r != null) {
r.Target = focusedElement;
} else {
element.SetValue(FocusedChildProperty, new WeakReference(focusedElement));
}
}
}
}

11
src/Main/Base/Project/Src/Gui/Workbench/Layouts/AvalonPadContent.cs

@ -31,6 +31,7 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -31,6 +31,7 @@ namespace ICSharpCode.SharpDevelop.Gui
this.descriptor = descriptor;
this.layout = layout;
CustomFocusManager.SetRememberFocusedChild(this, true);
this.Name = descriptor.Class.Replace('.', '_');
this.SetValueToExtension(TitleProperty, new StringParseExtension(descriptor.Title));
placeholder = new TextBlock { Text = this.Title };
@ -40,6 +41,16 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -40,6 +41,16 @@ namespace ICSharpCode.SharpDevelop.Gui
placeholder.IsVisibleChanged += AvalonPadContent_IsVisibleChanged;
}
protected override void FocusContent()
{
IInputElement activeChild = CustomFocusManager.GetFocusedChild(this);
if (activeChild != null) {
LoggingService.Debug("Will move focus to: " + activeChild);
Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(delegate { Keyboard.Focus(activeChild); }));
}
}
public void ShowInDefaultPosition()
{
AnchorStyle style;

12
src/Main/Base/Project/Src/Gui/Workbench/Layouts/AvalonWorkbenchWindow.cs

@ -15,6 +15,7 @@ using System.Windows; @@ -15,6 +15,7 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using AvalonDock;
using ICSharpCode.Core;
@ -33,6 +34,7 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -33,6 +34,7 @@ namespace ICSharpCode.SharpDevelop.Gui
if (dockLayout == null)
throw new ArgumentNullException("dockLayout");
CustomFocusManager.SetRememberFocusedChild(this, true);
this.IsFloatingAllowed = true;
this.dockLayout = dockLayout;
viewContents = new ViewContentCollection(this);
@ -41,6 +43,16 @@ namespace ICSharpCode.SharpDevelop.Gui @@ -41,6 +43,16 @@ namespace ICSharpCode.SharpDevelop.Gui
OnTitleNameChanged(this, EventArgs.Empty);
}
protected override void FocusContent()
{
IInputElement activeChild = CustomFocusManager.GetFocusedChild(this);
if (activeChild != null) {
LoggingService.Debug("Will move focus to: " + activeChild);
Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(delegate { Keyboard.Focus(activeChild); }));
}
}
public bool IsDisposed { get { return false; } }
#region IOwnerState

Loading…
Cancel
Save