IWithFind.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2024 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.Threading.Tasks;
  16. namespace Vuplex.WebView {
  17. /// <summary>
  18. /// An interface implemented by a webview if it supports finding text in the page.
  19. /// </summary>
  20. /// <example>
  21. /// <code>
  22. /// await webViewPrefab.WaitUntilInitialized;
  23. /// // Search for the word "and" in the page.
  24. /// var webViewWithFind = webViewPrefab.WebView as IWithFind;
  25. /// if (webViewWithFind == null) {
  26. /// Debug.Log("This 3D WebView plugin doesn't yet support IWithFind: " + webViewPrefab.WebView.PluginType);
  27. /// return;
  28. /// }
  29. /// var result = await webViewWithFind.Find("and", true);
  30. /// Debug.Log($"Number of matches: {result.MatchCount}. Index of current match: {result.CurrentMatchIndex}");
  31. /// if (result.MatchCount > 1) {
  32. /// // Later, scroll to the next instance of "and" in the page.
  33. /// await webViewWithFind.Find("and", true);
  34. /// // Later, go back to the first match.
  35. /// await webViewWithFind.Find("and", false);
  36. /// }
  37. /// // Later, clear all matches.
  38. /// webViewWithFind.ClearFindMatches();
  39. /// </code>
  40. /// </example>
  41. public interface IWithFind {
  42. /// <summary>
  43. /// Clears the visual indicator of matches triggered by a previous call to Find().
  44. /// </summary>
  45. void ClearFindMatches();
  46. /// <summary>
  47. /// Finds the given text in the page. If the page contains a match for the given text,
  48. /// then the browser scrolls to and highlights that match. If the returned FindResult
  49. /// indicates that there are matches, then the application can call Find() again
  50. /// with the same text to scroll to the next or previous match, as determined by
  51. /// the `forward` parameter. Highlighted matches can be cleared by calling
  52. /// ClearFindMatches().
  53. /// </summary>
  54. Task<FindResult> Find(string text, bool forward);
  55. }
  56. /// <summary>
  57. /// The result of a call to IWithFind.Find().
  58. /// </summary>
  59. public struct FindResult {
  60. /// <summary>
  61. /// The index of the current highlighted match.
  62. /// </summary>
  63. public int CurrentMatchIndex;
  64. /// <summary>
  65. /// The total number of matches for the given text.
  66. /// </summary>
  67. public int MatchCount;
  68. public override string ToString() => $"MatchCount: {MatchCount}, CurrentMatchIndex: {CurrentMatchIndex}";
  69. }
  70. }