Skip to content

Commit 530a143

Browse files
committed
WPF - Allow setting Focus before the underlying browser has been initialized.
- Remove hacks from the ViewModel as Focus can now be set on Go(). - Creating a new viewmodel also sets focus Changing tabs doesn't set focus currently.
1 parent 8683702 commit 530a143

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

CefSharp.Wpf.Example/ViewModels/BrowserTabViewModel.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,6 @@ private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
187187
{
188188
WebBrowser.ConsoleMessage += OnWebBrowserConsoleMessage;
189189
WebBrowser.StatusMessage += OnWebBrowserStatusMessage;
190-
191-
// TODO: This is a bit of a hack. It would be nicer/cleaner to give the webBrowser focus in the Go()
192-
// TODO: method, but it seems like "something" gets messed up (= doesn't work correctly) if we give it
193-
// TODO: focus "too early" in the loading process...
194-
WebBrowser.FrameLoadEnd += (s, args) =>
195-
{
196-
//Sender is the ChromiumWebBrowser object
197-
var browser = s as ChromiumWebBrowser;
198-
if (browser != null && !browser.IsDisposed)
199-
{
200-
browser.Dispatcher.BeginInvoke((Action)(() => browser.Focus()));
201-
}
202-
};
203190
}
204191

205192
break;
@@ -220,8 +207,7 @@ private void Go()
220207
{
221208
Address = AddressEditable;
222209

223-
// Part of the Focus hack further described in the OnPropertyChanged() method...
224-
Keyboard.ClearFocus();
210+
Keyboard.Focus((IInputElement)WebBrowser);
225211
}
226212

227213
public void LoadCustomRequestExample()

CefSharp.Wpf.Example/Views/BrowserTabView.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
mc:Ignorable="d"
1010
d:DesignWidth="640"
1111
d:DesignHeight="480"
12+
FocusManager.FocusedElement="{Binding ElementName=browser}"
1213
d:DataContext="{d:DesignInstance local:BrowserTabViewModel}">
1314
<UserControl.Resources>
1415
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public partial class ChromiumWebBrowser : Control, IRenderWebBrowser, IWpfWebBro
147147
/// </summary>
148148
private int disposeSignaled;
149149

150+
/// <summary>
151+
/// This flag is set when the browser gets focus before the underlying CEF browser
152+
/// has been initialized.
153+
/// </summary>
154+
private bool initialFocus;
155+
150156
/// <summary>
151157
/// Hack to work around issue https://github.com/cefsharp/CefSharp/issues/2779
152158
/// Enabled by default
@@ -1124,6 +1130,11 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
11241130
}
11251131
}
11261132
});
1133+
1134+
if(initialFocus)
1135+
{
1136+
browser.GetHost()?.SendFocusEvent(true);
1137+
}
11271138
}
11281139

11291140
#region CanGoBack dependency property
@@ -2162,7 +2173,11 @@ private void UpdateTooltip(string text)
21622173
/// <param name="e">The <see cref="KeyboardFocusChangedEventArgs"/> instance containing the event data.</param>
21632174
private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
21642175
{
2165-
if (browser != null)
2176+
if (browser == null)
2177+
{
2178+
initialFocus = true;
2179+
}
2180+
else
21662181
{
21672182
browser.GetHost().SendFocusEvent(true);
21682183
}
@@ -2175,7 +2190,11 @@ private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
21752190
/// <param name="e">The <see cref="KeyboardFocusChangedEventArgs"/> instance containing the event data.</param>
21762191
private void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
21772192
{
2178-
if (browser != null)
2193+
if (browser == null)
2194+
{
2195+
initialFocus = false;
2196+
}
2197+
else
21792198
{
21802199
browser.GetHost().SendFocusEvent(false);
21812200
}

0 commit comments

Comments
 (0)