Skip to content

Commit 61033e6

Browse files
committed
WPF/OffScreen cleanup
- More code cleanup, use null-propagating operator - WPF Add protected virtual UpdateDragCursor
1 parent c1b89c6 commit 61033e6

File tree

2 files changed

+30
-96
lines changed

2 files changed

+30
-96
lines changed

CefSharp.OffScreen/ChromiumWebBrowser.cs

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ public void RegisterAsyncJsObject(string name, object objectToBind, BindingOptio
630630

631631
public IJavascriptObjectRepository JavascriptObjectRepository
632632
{
633-
get { return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; }
633+
get { return managedCefBrowserAdapter?.JavascriptObjectRepository; }
634634
}
635635

636636
/// <summary>
@@ -772,9 +772,9 @@ protected virtual void OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buf
772772
/// <param name="cursor">If type is Custom then customCursorInfo will be populated with the custom cursor information</param>
773773
/// <param name="type">cursor type</param>
774774
/// <param name="customCursorInfo">custom cursor Information</param>
775-
void IRenderWebBrowser.OnCursorChange(IntPtr handle, CursorType type, CursorInfo customCursorInfo)
775+
void IRenderWebBrowser.OnCursorChange(IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
776776
{
777-
OnCursorChange(handle, type, customCursorInfo);
777+
OnCursorChange(cursor, type, customCursorInfo);
778778
}
779779

780780
/// <summary>
@@ -783,7 +783,7 @@ void IRenderWebBrowser.OnCursorChange(IntPtr handle, CursorType type, CursorInfo
783783
/// <param name="cursor">If type is Custom then customCursorInfo will be populated with the custom cursor information</param>
784784
/// <param name="type">cursor type</param>
785785
/// <param name="customCursorInfo">custom cursor Information</param>
786-
protected virtual void OnCursorChange(IntPtr handle, CursorType type, CursorInfo customCursorInfo)
786+
protected virtual void OnCursorChange(IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
787787
{
788788
}
789789

@@ -874,11 +874,7 @@ protected virtual void OnImeCompositionRangeChanged(Range selectedRange, Rect[]
874874
/// <param name="args">The <see cref="ConsoleMessageEventArgs"/> instance containing the event data.</param>
875875
void IWebBrowserInternal.OnConsoleMessage(ConsoleMessageEventArgs args)
876876
{
877-
var handler = ConsoleMessage;
878-
if (handler != null)
879-
{
880-
handler(this, args);
881-
}
877+
ConsoleMessage?.Invoke(this, args);
882878
}
883879

884880
/// <summary>
@@ -887,11 +883,7 @@ void IWebBrowserInternal.OnConsoleMessage(ConsoleMessageEventArgs args)
887883
/// <param name="args">The <see cref="FrameLoadStartEventArgs"/> instance containing the event data.</param>
888884
void IWebBrowserInternal.OnFrameLoadStart(FrameLoadStartEventArgs args)
889885
{
890-
var handler = FrameLoadStart;
891-
if (handler != null)
892-
{
893-
handler(this, args);
894-
}
886+
FrameLoadStart?.Invoke(this, args);
895887
}
896888

897889
/// <summary>
@@ -900,11 +892,7 @@ void IWebBrowserInternal.OnFrameLoadStart(FrameLoadStartEventArgs args)
900892
/// <param name="args">The <see cref="FrameLoadEndEventArgs"/> instance containing the event data.</param>
901893
void IWebBrowserInternal.OnFrameLoadEnd(FrameLoadEndEventArgs args)
902894
{
903-
var handler = FrameLoadEnd;
904-
if (handler != null)
905-
{
906-
handler(this, args);
907-
}
895+
FrameLoadEnd?.Invoke(this, args);
908896
}
909897

910898
/// <summary>
@@ -917,11 +905,7 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
917905

918906
IsBrowserInitialized = true;
919907

920-
var handler = BrowserInitialized;
921-
if (handler != null)
922-
{
923-
handler(this, EventArgs.Empty);
924-
}
908+
BrowserInitialized?.Invoke(this, EventArgs.Empty);
925909
}
926910

927911
/// <summary>
@@ -930,11 +914,7 @@ void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
930914
/// <param name="args">The <see cref="LoadErrorEventArgs"/> instance containing the event data.</param>
931915
void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
932916
{
933-
var handler = LoadError;
934-
if (handler != null)
935-
{
936-
handler(this, args);
937-
}
917+
LoadError?.Invoke(this, args);
938918
}
939919

940920
/// <summary>
@@ -958,11 +938,7 @@ IBrowserAdapter IWebBrowserInternal.BrowserAdapter
958938
/// <param name="args">The <see cref="StatusMessageEventArgs"/> instance containing the event data.</param>
959939
void IWebBrowserInternal.OnStatusMessage(StatusMessageEventArgs args)
960940
{
961-
var handler = StatusMessage;
962-
if (handler != null)
963-
{
964-
handler(this, args);
965-
}
941+
StatusMessage?.Invoke(this, args);
966942
}
967943

968944
/// <summary>
@@ -973,11 +949,7 @@ void IWebBrowserInternal.SetAddress(AddressChangedEventArgs args)
973949
{
974950
Address = args.Address;
975951

976-
var handler = AddressChanged;
977-
if (handler != null)
978-
{
979-
handler(this, args);
980-
}
952+
AddressChanged?.Invoke(this, args);
981953
}
982954

983955
/// <summary>
@@ -990,11 +962,7 @@ void IWebBrowserInternal.SetLoadingStateChange(LoadingStateChangedEventArgs args
990962
CanGoForward = args.CanGoForward;
991963
IsLoading = args.IsLoading;
992964

993-
var handler = LoadingStateChanged;
994-
if (handler != null)
995-
{
996-
handler(this, args);
997-
}
965+
LoadingStateChanged?.Invoke(this, args);
998966
}
999967

1000968
/// <summary>
@@ -1003,11 +971,7 @@ void IWebBrowserInternal.SetLoadingStateChange(LoadingStateChangedEventArgs args
1003971
/// <param name="args">The <see cref="TitleChangedEventArgs"/> instance containing the event data.</param>
1004972
void IWebBrowserInternal.SetTitle(TitleChangedEventArgs args)
1005973
{
1006-
var handler = TitleChanged;
1007-
if (handler != null)
1008-
{
1009-
handler(this, args);
1010-
}
974+
TitleChanged?.Invoke(this, args);
1011975
}
1012976

1013977
/// <summary>

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,11 @@ bool IRenderWebBrowser.StartDragging(IDragData dragData, DragOperationsMask allo
735735
}
736736

737737
void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation)
738+
{
739+
UpdateDragCursor(operation);
740+
}
741+
742+
protected virtual void UpdateDragCursor(DragOperationsMask operation)
738743
{
739744
//TODO: Someone should implement this
740745
}
@@ -877,11 +882,7 @@ void IWebBrowserInternal.SetLoadingStateChange(LoadingStateChangedEventArgs args
877882
((DelegateCommand)ReloadCommand).RaiseCanExecuteChanged();
878883
});
879884

880-
var handler = LoadingStateChanged;
881-
if (handler != null)
882-
{
883-
handler(this, args);
884-
}
885+
LoadingStateChanged?.Invoke(this, args);
885886
}
886887

887888
/// <summary>
@@ -908,11 +909,7 @@ void IWebBrowserInternal.SetTooltipText(string tooltipText)
908909
/// <param name="args">The <see cref="FrameLoadStartEventArgs"/> instance containing the event data.</param>
909910
void IWebBrowserInternal.OnFrameLoadStart(FrameLoadStartEventArgs args)
910911
{
911-
var handler = FrameLoadStart;
912-
if (handler != null)
913-
{
914-
handler(this, args);
915-
}
912+
FrameLoadStart?.Invoke(this, args);
916913
}
917914

918915
/// <summary>
@@ -921,12 +918,7 @@ void IWebBrowserInternal.OnFrameLoadStart(FrameLoadStartEventArgs args)
921918
/// <param name="args">The <see cref="FrameLoadEndEventArgs"/> instance containing the event data.</param>
922919
void IWebBrowserInternal.OnFrameLoadEnd(FrameLoadEndEventArgs args)
923920
{
924-
var handler = FrameLoadEnd;
925-
926-
if (handler != null)
927-
{
928-
handler(this, args);
929-
}
921+
FrameLoadEnd?.Invoke(this, args);
930922
}
931923

932924
/// <summary>
@@ -935,11 +927,7 @@ void IWebBrowserInternal.OnFrameLoadEnd(FrameLoadEndEventArgs args)
935927
/// <param name="args">The <see cref="ConsoleMessageEventArgs"/> instance containing the event data.</param>
936928
void IWebBrowserInternal.OnConsoleMessage(ConsoleMessageEventArgs args)
937929
{
938-
var handler = ConsoleMessage;
939-
if (handler != null)
940-
{
941-
handler(this, args);
942-
}
930+
ConsoleMessage?.Invoke(this, args);
943931
}
944932

945933
/// <summary>
@@ -948,11 +936,7 @@ void IWebBrowserInternal.OnConsoleMessage(ConsoleMessageEventArgs args)
948936
/// <param name="args">The <see cref="StatusMessageEventArgs"/> instance containing the event data.</param>
949937
void IWebBrowserInternal.OnStatusMessage(StatusMessageEventArgs args)
950938
{
951-
var handler = StatusMessage;
952-
if (handler != null)
953-
{
954-
handler(this, args);
955-
}
939+
StatusMessage?.Invoke(this, args);
956940
}
957941

958942
/// <summary>
@@ -961,11 +945,7 @@ void IWebBrowserInternal.OnStatusMessage(StatusMessageEventArgs args)
961945
/// <param name="args">The <see cref="LoadErrorEventArgs"/> instance containing the event data.</param>
962946
void IWebBrowserInternal.OnLoadError(LoadErrorEventArgs args)
963947
{
964-
var handler = LoadError;
965-
if (handler != null)
966-
{
967-
handler(this, args);
968-
}
948+
LoadError?.Invoke(this, args);
969949
}
970950

971951
void IWebBrowserInternal.SetCanExecuteJavascriptOnMainFrame(bool canExecute)
@@ -1163,12 +1143,7 @@ private static void OnIsBrowserInitializedChanged(DependencyObject d, Dependency
11631143

11641144
owner.OnIsBrowserInitializedChanged(oldValue, newValue);
11651145

1166-
var handlers = owner.IsBrowserInitializedChanged;
1167-
1168-
if (handlers != null)
1169-
{
1170-
handlers(owner, e);
1171-
}
1146+
owner.IsBrowserInitializedChanged?.Invoke(owner, e);
11721147
}
11731148

11741149
/// <summary>
@@ -1236,12 +1211,7 @@ private static void OnTitleChanged(DependencyObject d, DependencyPropertyChanged
12361211
{
12371212
var owner = (ChromiumWebBrowser)d;
12381213

1239-
var handlers = owner.TitleChanged;
1240-
1241-
if (handlers != null)
1242-
{
1243-
handlers(owner, e);
1244-
}
1214+
owner.TitleChanged?.Invoke(owner, e);
12451215
}
12461216

12471217
#endregion Title dependency property
@@ -1898,9 +1868,9 @@ private Popup CreatePopup()
18981868
AllowsTransparency = true
18991869
};
19001870

1901-
BindingOperations.SetBinding(newPopup, FrameworkElement.LayoutTransformProperty, new Binding
1871+
BindingOperations.SetBinding(newPopup, LayoutTransformProperty, new Binding
19021872
{
1903-
Path = new PropertyPath(FrameworkElement.LayoutTransformProperty),
1873+
Path = new PropertyPath(LayoutTransformProperty),
19041874
Source = this,
19051875
});
19061876

@@ -2302,7 +2272,7 @@ public void UseLegacyKeyboardHandler()
23022272
/// <param name="name">The name of the object. (e.g. "foo", if you want the object to be accessible as window.foo).</param>
23032273
/// <param name="objectToBind">The object to be made accessible to Javascript.</param>
23042274
/// <param name="options">binding options - camelCaseJavascriptNames default to true </param>
2305-
/// <exception cref="System.Exception">Browser is already initialized. RegisterJsObject must be +
2275+
/// <exception cref="Exception">Browser is already initialized. RegisterJsObject must be +
23062276
/// called before the underlying CEF browser is created.</exception>
23072277
public void RegisterJsObject(string name, object objectToBind, BindingOptions options = null)
23082278
{
@@ -2373,7 +2343,7 @@ public void RegisterAsyncJsObject(string name, object objectToBind, BindingOptio
23732343

23742344
public IJavascriptObjectRepository JavascriptObjectRepository
23752345
{
2376-
get { return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; }
2346+
get { return managedCefBrowserAdapter?.JavascriptObjectRepository; }
23772347
}
23782348

23792349
/// <summary>

0 commit comments

Comments
 (0)