@@ -140,6 +140,27 @@ public partial class MainWindow : Window
140140 public static RoutedCommand ChildFrameEventsCommand = new RoutedCommand ( ) ;
141141 public static RoutedCommand RemoveChildFrameEventsCommand = new RoutedCommand ( ) ;
142142
143+ public static RoutedCommand StartCommand = new RoutedCommand ( ) ;
144+
145+ public static RoutedCommand FindNextCommand = new RoutedCommand ( ) ;
146+
147+ public static RoutedCommand FindPreviousCommand = new RoutedCommand ( ) ;
148+
149+ public static RoutedCommand StopFindCommand = new RoutedCommand ( ) ;
150+ public static RoutedCommand FindTermCommand = new RoutedCommand ( ) ;
151+
152+ public static RoutedCommand GetMatchCountCommand = new RoutedCommand ( ) ;
153+
154+ public static RoutedCommand GetActiveMatchIndexCommand = new RoutedCommand ( ) ;
155+
156+ public static RoutedCommand ToggleCaseSensitiveCommand = new RoutedCommand ( ) ;
157+
158+ public static RoutedCommand ToggleShouldHighlightAllMatchesCommand = new RoutedCommand ( ) ;
159+
160+ public static RoutedCommand ToggleShouldMatchWordCommand = new RoutedCommand ( ) ;
161+
162+ public static RoutedCommand ToggleSuppressDefaultFindDialogCommand = new RoutedCommand ( ) ;
163+
143164#endregion commands
144165
145166 bool _isNavigating = false ;
@@ -3931,5 +3952,241 @@ void HandleChildFrameNavigationCompleted(object sender, CoreWebView2NavigationCo
39313952 MessageBox . Show ( this , "Id: " + Frame . FrameId + " NavigationCompleted" , "Child frame Navigation Completed" , MessageBoxButton . OK ) ;
39323953 }
39333954#endif
3955+ #if USE_WEBVIEW2_EXPERIMENTAL
3956+
3957+ private CoreWebView2FindOptions _findOptions ;
3958+ private bool _findEventHandlersSet = false ;
3959+ private string _lastSearchTerm = string . Empty ;
3960+ #endif
3961+ void StartExecuted ( object target , ExecutedRoutedEventArgs e )
3962+ {
3963+ #if USE_WEBVIEW2_EXPERIMENTAL
3964+
3965+ if ( _iWebView2 ? . CoreWebView2 == null || _iWebView2 . CoreWebView2 . Find == null )
3966+ {
3967+ MessageBox . Show ( "Find API is unavailable." ) ;
3968+ return ;
3969+ }
3970+
3971+ var dialog = new TextInputDialog (
3972+ title : "Find on Page Term" ,
3973+ description : "Enter find term:" ,
3974+ defaultInput : "WebView2" ) ;
3975+
3976+ if ( dialog . ShowDialog ( ) == true )
3977+ {
3978+ _iWebView2 . CoreWebView2 . Find . Stop ( ) ;
3979+
3980+
3981+ if ( _findOptions == null )
3982+ {
3983+ _findOptions = CreateDefaultFindOptions ( ) ;
3984+ }
3985+
3986+ _findOptions . FindTerm = dialog . Input . Text ;
3987+
3988+ SetupFindEventHandlers ( ) ;
3989+
3990+ _ = _iWebView2 . CoreWebView2 . Find . StartAsync ( _findOptions ) ;
3991+ }
3992+ #endif
3993+ }
3994+
3995+ void FindNextExecuted ( object target , ExecutedRoutedEventArgs e )
3996+ {
3997+ #if USE_WEBVIEW2_EXPERIMENTAL
3998+
3999+ if ( _iWebView2 ? . CoreWebView2 ? . Find == null ) return ;
4000+ _iWebView2 . CoreWebView2 . Find . FindNext ( ) ;
4001+ #endif
4002+ }
4003+
4004+ void FindPreviousExecuted ( object target , ExecutedRoutedEventArgs e )
4005+ {
4006+ #if USE_WEBVIEW2_EXPERIMENTAL
4007+ if ( _iWebView2 ? . CoreWebView2 ? . Find == null ) return ;
4008+ _iWebView2 . CoreWebView2 . Find . FindPrevious ( ) ;
4009+ #endif
4010+ }
4011+
4012+ void StopFindExecuted ( object target , ExecutedRoutedEventArgs e )
4013+ {
4014+ #if USE_WEBVIEW2_EXPERIMENTAL
4015+ _iWebView2 . CoreWebView2 . Find . Stop ( ) ;
4016+ #endif
4017+ }
4018+ #if USE_WEBVIEW2_EXPERIMENTAL
4019+ // Creating find options via environment
4020+ private CoreWebView2FindOptions CreateDefaultFindOptions ( )
4021+ {
4022+ var webviewEnv = _iWebView2 . CoreWebView2 . Environment ;
4023+ if ( webviewEnv == null ) return null ;
4024+
4025+ var findOptions = webviewEnv . CreateFindOptions ( ) ;
4026+ findOptions . FindTerm = "WebView2" ;
4027+ findOptions . IsCaseSensitive = false ;
4028+ findOptions . ShouldHighlightAllMatches = true ;
4029+ findOptions . ShouldMatchWord = false ;
4030+ findOptions . SuppressDefaultFindDialog = false ;
4031+ return findOptions ;
4032+ }
4033+ #endif
4034+ void ChangeFindTermExecuted ( object target , ExecutedRoutedEventArgs e )
4035+ {
4036+ #if USE_WEBVIEW2_EXPERIMENTAL
4037+
4038+ // Make sure the WebView2 and its Find interface are available
4039+ if ( _iWebView2 ? . CoreWebView2 == null || _iWebView2 . CoreWebView2 . Find == null )
4040+ {
4041+ MessageBox . Show ( "Find API is unavailable or not yet initialized." ) ;
4042+ return ;
4043+ }
4044+
4045+ // If we haven’t created _findOptions yet, do it now
4046+ if ( _findOptions == null )
4047+ {
4048+ _findOptions = CreateDefaultFindOptions ( ) ;
4049+ }
4050+
4051+ // Prompt the user for a new find term
4052+ var dialog = new TextInputDialog (
4053+ title : "Change Find on Page Term" ,
4054+ description : "Enter new find term:" ,
4055+ defaultInput : _findOptions . FindTerm // show the current term if you like
4056+ ) ;
4057+
4058+ // If user clicks OK, update the find term
4059+ if ( dialog . ShowDialog ( ) == true )
4060+ {
4061+ _findOptions . FindTerm = dialog . Input . Text ;
4062+ _lastSearchTerm = _findOptions . FindTerm ; // track if desired
4063+
4064+ // We do *not* start the find here; that’s done in StartExecuted.
4065+ MessageBox . Show ( $ "Find term changed to: { _findOptions . FindTerm } ", "Find on Page" ) ;
4066+ }
4067+ #endif
4068+ }
4069+
4070+ void GetMatchCountExecuted ( object target , ExecutedRoutedEventArgs e )
4071+ {
4072+ #if USE_WEBVIEW2_EXPERIMENTAL
4073+
4074+ if ( _iWebView2 ? . CoreWebView2 ? . Find == null ) return ;
4075+
4076+ int matchCount = _iWebView2 . CoreWebView2 . Find . MatchCount ;
4077+ MessageBox . Show ( $ "Match Count: { matchCount } ", "Find Operation" , MessageBoxButton . OK ) ;
4078+ #endif
4079+
4080+ }
4081+
4082+ void GetActiveMatchIndexExecuted ( object target , ExecutedRoutedEventArgs e )
4083+ {
4084+ #if USE_WEBVIEW2_EXPERIMENTAL
4085+
4086+ if ( _iWebView2 ? . CoreWebView2 ? . Find == null ) return ;
4087+
4088+ int activeIndex = _iWebView2 . CoreWebView2 . Find . ActiveMatchIndex ;
4089+ MessageBox . Show ( $ "Active Match Index: { activeIndex } ", "Find Operation" , MessageBoxButton . OK ) ;
4090+ #endif
4091+
4092+ }
4093+
4094+ void ToggleCaseSensitiveExecuted ( object target , ExecutedRoutedEventArgs e )
4095+ {
4096+ #if USE_WEBVIEW2_EXPERIMENTAL
4097+
4098+ ToggleFindOptionAndRestart (
4099+ ( ) => _findOptions . IsCaseSensitive ,
4100+ val => _findOptions . IsCaseSensitive = val ) ;
4101+ #endif
4102+
4103+ }
4104+
4105+ void ToggleShouldHighlightAllMatchesExecuted ( object target , ExecutedRoutedEventArgs e )
4106+ {
4107+ #if USE_WEBVIEW2_EXPERIMENTAL
4108+
4109+ ToggleFindOptionAndRestart (
4110+ ( ) => _findOptions . ShouldHighlightAllMatches ,
4111+ val => _findOptions . ShouldHighlightAllMatches = val ) ;
4112+ #endif
4113+
4114+ }
4115+
4116+ void ToggleShouldMatchWordExecuted ( object target , ExecutedRoutedEventArgs e )
4117+ {
4118+ #if USE_WEBVIEW2_EXPERIMENTAL
4119+
4120+ ToggleFindOptionAndRestart (
4121+ ( ) => _findOptions . ShouldMatchWord ,
4122+ val => _findOptions . ShouldMatchWord = val ) ;
4123+ #endif
4124+
4125+ }
4126+
4127+ void ToggleSuppressDefaultFindDialogExecuted ( object target , ExecutedRoutedEventArgs e )
4128+ {
4129+ #if USE_WEBVIEW2_EXPERIMENTAL
4130+
4131+ ToggleFindOptionAndRestart (
4132+ ( ) => _findOptions . SuppressDefaultFindDialog ,
4133+ val => _findOptions . SuppressDefaultFindDialog = val ) ;
4134+ #endif
4135+
4136+ }
4137+ #if USE_WEBVIEW2_EXPERIMENTAL
4138+
4139+ private async void ToggleFindOptionAndRestart ( Func < bool > optionGetter , Action < bool > optionSetter )
4140+ {
4141+ if ( _iWebView2 ? . CoreWebView2 ? . Find == null )
4142+ return ;
4143+
4144+ _iWebView2 . CoreWebView2 . Find . Stop ( ) ;
4145+
4146+
4147+ if ( _findOptions == null )
4148+ {
4149+ _findOptions = CreateDefaultFindOptions ( ) ;
4150+ }
4151+
4152+ bool currentVal = optionGetter ( ) ;
4153+ optionSetter ( ! currentVal ) ;
4154+
4155+ await _iWebView2 . CoreWebView2 . Find . StartAsync ( _findOptions ) ;
4156+ }
4157+
4158+ private void SetupFindEventHandlers ( )
4159+ {
4160+ if ( _findEventHandlersSet ) return ; // Only attach once
4161+ var findObject = _iWebView2 . CoreWebView2 . Find ;
4162+ findObject . MatchCountChanged += FindObject_MatchCountChanged ;
4163+ findObject . ActiveMatchIndexChanged += FindObject_ActiveMatchIndexChanged ;
4164+ _findEventHandlersSet = true ;
4165+ }
4166+
4167+ private void RemoveFindEventHandlers ( )
4168+ {
4169+ if ( ! _findEventHandlersSet ) return ;
4170+ var findObject = _iWebView2 . CoreWebView2 . Find ;
4171+ findObject . MatchCountChanged -= FindObject_MatchCountChanged ;
4172+ findObject . ActiveMatchIndexChanged -= FindObject_ActiveMatchIndexChanged ;
4173+ _findEventHandlersSet = false ;
4174+ }
4175+
4176+ private void FindObject_MatchCountChanged ( object sender , object e )
4177+ {
4178+ var findObject = _iWebView2 . CoreWebView2 . Find ;
4179+ int matchCount = findObject . MatchCount ;
4180+ Debug . WriteLine ( $ "[FindOnPage] MatchCountChanged -> { matchCount } ") ;
4181+ }
4182+
4183+ private void FindObject_ActiveMatchIndexChanged ( object sender , object e )
4184+ {
4185+ var findObject = _iWebView2 . CoreWebView2 . Find ;
4186+ int activeIndex = findObject . ActiveMatchIndex ;
4187+ Debug . WriteLine ( $ "[FindOnPage] ActiveMatchIndexChanged -> { activeIndex } ") ;
4188+ }
4189+ #endif
4190+
39344191 }
39354192}
0 commit comments