Util.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace ZenFulcrum.EmbeddedBrowser {
  5. public static class Util {
  6. /**
  7. * Sometimes creating a culture in a different thread causes Mono to crash
  8. * with mono_class_vtable_full.
  9. *
  10. * This variant of StartsWith won't try to use a culture.
  11. */
  12. public static bool SafeStartsWith(this string check, string starter) {
  13. if (check == null || starter == null) return false;
  14. if (check.Length < starter.Length) return false;
  15. for (int i = 0; i < starter.Length; ++i) {
  16. if (check[i] != starter[i]) return false;
  17. }
  18. return true;
  19. }
  20. /// <summary>
  21. /// Converts a UTF8-encoded null-terminated string to a CLR string.
  22. /// </summary>
  23. /// <param name="strIn"></param>
  24. /// <returns></returns>
  25. public static string PtrToStringUTF8(IntPtr strIn) {
  26. if (strIn == IntPtr.Zero) return null;
  27. int strLen = 0;
  28. while (Marshal.ReadByte(strIn, strLen) != 0) ++strLen;
  29. var buffer = new byte[strLen];
  30. Marshal.Copy(strIn, buffer, 0, strLen);
  31. return Encoding.UTF8.GetString(buffer);
  32. }
  33. }
  34. public class JSException : Exception {
  35. public JSException(string what) : base(what) {}
  36. }
  37. public enum KeyAction {
  38. Press, Release, PressAndRelease
  39. }
  40. public class BrowserFocusState {
  41. public bool hasKeyboardFocus;
  42. public bool hasMouseFocus;
  43. public string focusedTagName;
  44. public bool focusedNodeEditable;
  45. }
  46. public class BrowserNavState {
  47. public bool canGoForward, canGoBack, loading;
  48. public string url = "";
  49. }
  50. }