|
1 |
| -using System; |
2 |
| -using System.Runtime.ExceptionServices; |
3 |
| -using Avalonia; |
4 |
| -using Avalonia.Controls; |
5 |
| -using Avalonia.Data; |
6 |
| -using Avalonia.Input; |
7 |
| -using Avalonia.Threading; |
8 |
| - |
9 |
| -namespace WebViewControl { |
10 |
| - |
11 |
| - partial class WebView : BaseControl { |
12 |
| - |
13 |
| - private bool IsInDesignMode => false; |
14 |
| - |
15 |
| - public static readonly StyledProperty<string> AddressProperty = |
16 |
| - AvaloniaProperty.Register<WebView, string>(nameof(Address), defaultBindingMode: BindingMode.TwoWay); |
17 |
| - |
18 |
| - public string Address { |
19 |
| - get => GetValue(AddressProperty); |
20 |
| - set => SetValue(AddressProperty, value); |
21 |
| - } |
22 |
| - |
23 |
| - protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change) { |
24 |
| - base.OnPropertyChanged(change); |
25 |
| - |
26 |
| - if (change.Property == AddressProperty) { |
27 |
| - InternalAddress = Address; |
28 |
| - } |
29 |
| - } |
30 |
| - |
31 |
| - partial void ExtraInitialize() { |
32 |
| - VisualChildren.Add(chromium); |
33 |
| - chromium.AddressChanged += (o, address) => ExecuteInUI(() => Address = address); |
34 |
| - } |
35 |
| - |
36 |
| - protected override void OnKeyDown(KeyEventArgs e) { |
37 |
| - if (AllowDeveloperTools && e.Key == Key.F12) { |
38 |
| - ToggleDeveloperTools(); |
39 |
| - e.Handled = true; |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - protected override void OnGotFocus(GotFocusEventArgs e) { |
44 |
| - if (!e.Handled) { |
45 |
| - e.Handled = true; |
46 |
| - base.OnGotFocus(e); |
47 |
| - |
48 |
| - // use async call to avoid reentrancy, otherwise the webview will fight to get the focus |
49 |
| - Dispatcher.UIThread.Post(() => { |
50 |
| - if (IsFocused) { |
51 |
| - chromium.Focus(); |
52 |
| - } |
53 |
| - }, DispatcherPriority.Background); |
54 |
| - } |
55 |
| - } |
56 |
| - |
57 |
| - protected override void InternalDispose() => Dispose(); |
58 |
| - |
59 |
| - private void ForwardException(ExceptionDispatchInfo exceptionInfo) { |
60 |
| - // TODO |
61 |
| - } |
62 |
| - |
63 |
| - private T ExecuteInUI<T>(Func<T> action) { |
64 |
| - if (Dispatcher.UIThread.CheckAccess()) { |
65 |
| - return action(); |
66 |
| - } |
67 |
| - return Dispatcher.UIThread.InvokeAsync<T>(action).Result; |
68 |
| - } |
69 |
| - |
70 |
| - private void AsyncExecuteInUI(Action action) { |
71 |
| - if (isDisposing) { |
72 |
| - return; |
73 |
| - } |
74 |
| - // use async call to avoid dead-locks, otherwise if the source action tries to to evaluate js it would block |
75 |
| - Dispatcher.UIThread.InvokeAsync( |
76 |
| - () => { |
77 |
| - if (!isDisposing) { |
78 |
| - ExecuteWithAsyncErrorHandling(action); |
79 |
| - } |
80 |
| - }, |
81 |
| - DispatcherPriority.Normal); |
82 |
| - } |
83 |
| - |
84 |
| - internal void InitializeBrowser(int initialWidth, int initialHeight) { |
85 |
| - chromium.CreateBrowser(initialWidth, initialHeight); |
86 |
| - } |
87 |
| - |
88 |
| - /// <summary> |
89 |
| - /// Called when the webview is requesting focus. Return false to allow the |
90 |
| - /// focus to be set or true to cancel setting the focus. |
91 |
| - /// <paramref name="isSystemEvent">True if is a system focus event, or false if is a navigation</paramref> |
92 |
| - /// </summary> |
93 |
| - protected virtual bool OnSetFocus(bool isSystemEvent) { |
94 |
| - var focusedElement = KeyboardDevice.Instance.FocusedElement; |
95 |
| - return !(focusedElement == chromium || focusedElement == this); |
96 |
| - } |
97 |
| - } |
98 |
| -} |
| 1 | +using System; |
| 2 | +using System.Runtime.ExceptionServices; |
| 3 | +using Avalonia; |
| 4 | +using Avalonia.Controls; |
| 5 | +using Avalonia.Data; |
| 6 | +using Avalonia.Input; |
| 7 | +using Avalonia.Threading; |
| 8 | + |
| 9 | +namespace WebViewControl { |
| 10 | + |
| 11 | + partial class WebView : BaseControl { |
| 12 | + |
| 13 | + private bool IsInDesignMode => false; |
| 14 | + |
| 15 | + public static readonly StyledProperty<string> AddressProperty = |
| 16 | + AvaloniaProperty.Register<WebView, string>(nameof(Address), defaultBindingMode: BindingMode.TwoWay); |
| 17 | + |
| 18 | + public string Address { |
| 19 | + get => GetValue(AddressProperty); |
| 20 | + set => SetValue(AddressProperty, value); |
| 21 | + } |
| 22 | + |
| 23 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { |
| 24 | + base.OnPropertyChanged(change); |
| 25 | + |
| 26 | + if (change.Property == AddressProperty) { |
| 27 | + InternalAddress = Address; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + partial void ExtraInitialize() { |
| 32 | + VisualChildren.Add(chromium); |
| 33 | + chromium[!FocusableProperty] = this[!FocusableProperty]; |
| 34 | + chromium.AddressChanged += (o, address) => ExecuteInUI(() => Address = address); |
| 35 | + } |
| 36 | + |
| 37 | + protected override void OnKeyDown(KeyEventArgs e) { |
| 38 | + if (AllowDeveloperTools && e.Key == Key.F12) { |
| 39 | + ToggleDeveloperTools(); |
| 40 | + e.Handled = true; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + protected override void OnGotFocus(GotFocusEventArgs e) { |
| 45 | + if (!e.Handled) { |
| 46 | + e.Handled = true; |
| 47 | + base.OnGotFocus(e); |
| 48 | + |
| 49 | + // use async call to avoid reentrancy, otherwise the webview will fight to get the focus |
| 50 | + Dispatcher.UIThread.Post(() => { |
| 51 | + if (IsFocused) { |
| 52 | + chromium.Focus(); |
| 53 | + } |
| 54 | + }, DispatcherPriority.Background); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + protected override void InternalDispose() => Dispose(); |
| 59 | + |
| 60 | + private void ForwardException(ExceptionDispatchInfo exceptionInfo) { |
| 61 | + // TODO |
| 62 | + } |
| 63 | + |
| 64 | + private T ExecuteInUI<T>(Func<T> action) { |
| 65 | + if (Dispatcher.UIThread.CheckAccess()) { |
| 66 | + return action(); |
| 67 | + } |
| 68 | + return Dispatcher.UIThread.InvokeAsync<T>(action).Result; |
| 69 | + } |
| 70 | + |
| 71 | + private void AsyncExecuteInUI(Action action) { |
| 72 | + if (isDisposing) { |
| 73 | + return; |
| 74 | + } |
| 75 | + // use async call to avoid dead-locks, otherwise if the source action tries to to evaluate js it would block |
| 76 | + Dispatcher.UIThread.InvokeAsync( |
| 77 | + () => { |
| 78 | + if (!isDisposing) { |
| 79 | + ExecuteWithAsyncErrorHandling(action); |
| 80 | + } |
| 81 | + }, |
| 82 | + DispatcherPriority.Normal); |
| 83 | + } |
| 84 | + |
| 85 | + internal void InitializeBrowser(int initialWidth, int initialHeight) { |
| 86 | + chromium.CreateBrowser(initialWidth, initialHeight); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Called when the webview is requesting focus. Return false to allow the |
| 91 | + /// focus to be set or true to cancel setting the focus. |
| 92 | + /// <paramref name="isSystemEvent">True if is a system focus event, or false if is a navigation</paramref> |
| 93 | + /// </summary> |
| 94 | + protected virtual bool OnSetFocus(bool isSystemEvent) { |
| 95 | + // VisualRoot can be null when webview is not yet added to the Visual tree |
| 96 | + var focusedElement = TopLevel.GetTopLevel(this)?.FocusManager.GetFocusedElement(); |
| 97 | + return !(focusedElement == chromium || focusedElement == this); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments