Utils.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using CefSharp;
  6. namespace prBrowser
  7. {
  8. internal static class Utils
  9. {
  10. public static bool IsFocussed(TextBox tb)
  11. {
  12. return tb.Focused;
  13. }
  14. public static void AddHotKey(Form form, Action function, Keys key, bool ctrl = false, bool shift = false, bool alt = false)
  15. {
  16. form.KeyPreview = true;
  17. form.KeyDown += delegate(object sender, KeyEventArgs e)
  18. {
  19. if (e.IsHotkey(key, ctrl, shift, alt))
  20. {
  21. function();
  22. }
  23. };
  24. }
  25. public static bool IsFullySelected(TextBox tb)
  26. {
  27. return tb.SelectionLength == tb.TextLength;
  28. }
  29. public static bool HasSelection(TextBox tb)
  30. {
  31. return tb.SelectionLength > 0;
  32. }
  33. public static bool CheckIfFilePath(this string path)
  34. {
  35. if (path[1] == ':' && path[2] == '\\' && char.IsLetter(path[0]))
  36. {
  37. return true;
  38. }
  39. return false;
  40. }
  41. public static bool CheckIfFilePath2(this string path)
  42. {
  43. if (path[1] == ':' && path[2] == '/' && char.IsLetter(path[0]))
  44. {
  45. return true;
  46. }
  47. return false;
  48. }
  49. public static string GetAfter(this string text, string find, int startAt = 0, bool returnAll = false, bool forward = true)
  50. {
  51. if (text == null)
  52. {
  53. return returnAll ? text : "";
  54. }
  55. int idx = (forward ? text.IndexOf(find, startAt, StringComparison.Ordinal) : text.LastIndexOf(find, text.Length - startAt, StringComparison.Ordinal));
  56. if (idx == -1)
  57. {
  58. return returnAll ? text : "";
  59. }
  60. idx += find.Length;
  61. return text.Substring(idx);
  62. }
  63. public static string GetAfterLast(this string text, string find, bool returnAll = false)
  64. {
  65. int idx = text.LastIndexOf(find, StringComparison.Ordinal);
  66. if (idx == -1)
  67. {
  68. return returnAll ? text : "";
  69. }
  70. idx += find.Length;
  71. return text.Substring(idx);
  72. }
  73. public static bool SupportedInFilePath(this char c)
  74. {
  75. return c != '?' && c != '\'' && c != '"' && c != '/' && c != '\\' && c != ';' && c != ':' && c != '&' && c != '*' && c != '<' && c != '>' && c != '|' && c != '{' && c != '}' && c != '[' && c != ']' && c != '(' && c != ')';
  76. }
  77. public static string ChangePathSlash(this string filePath, string slash)
  78. {
  79. if (slash == "\\" && filePath.Contains('/'))
  80. {
  81. return filePath.Replace("/", "\\");
  82. }
  83. if (slash == "/" && filePath.Contains('\\'))
  84. {
  85. return filePath.Replace("\\", "/");
  86. }
  87. return filePath;
  88. }
  89. public static string FileURLToPath(this string url)
  90. {
  91. return url.RemovePrefix("file:///").ChangePathSlash("\\").DecodeURLForFilepath();
  92. }
  93. public static bool FileNotExists(this string path)
  94. {
  95. return !File.Exists(path);
  96. }
  97. public static string ConvertToString(this object o)
  98. {
  99. if (o is string)
  100. {
  101. return o as string;
  102. }
  103. return null;
  104. }
  105. public static bool CheckIfValid(this string text, bool trimAndCheck = false)
  106. {
  107. return text != null && text.Length > 0;
  108. }
  109. public static void InvokeOnParent(this Control control, MethodInvoker method)
  110. {
  111. Control parent = ((control.Parent == null) ? control : control.Parent);
  112. if (parent.IsHandleCreated)
  113. {
  114. parent.Invoke(method);
  115. }
  116. }
  117. public static bool IsHotkey(this KeyEventArgs eventData, Keys key, bool ctrl = false, bool shift = false, bool alt = false)
  118. {
  119. return eventData.KeyCode == key && eventData.Control == ctrl && eventData.Shift == shift && eventData.Alt == alt;
  120. }
  121. public static CefState ToCefState(this bool value)
  122. {
  123. return value ? CefState.Enabled : CefState.Disabled;
  124. }
  125. public static bool IsBitmaskOn(this int num, int bitmask)
  126. {
  127. return (num & bitmask) != 0;
  128. }
  129. public static bool BeginsWith(this string str, string beginsWith, bool caseSensitive = true)
  130. {
  131. if (beginsWith.Length > str.Length)
  132. {
  133. return false;
  134. }
  135. if (beginsWith.Length == str.Length)
  136. {
  137. return string.Equals(beginsWith, str, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
  138. }
  139. return str.LastIndexOf(beginsWith, beginsWith.Length - 1, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase) == 0;
  140. }
  141. public static string RemovePrefix(this string str, string prefix, bool caseSensitive = true)
  142. {
  143. if (str.Length >= prefix.Length && str.BeginsWith(prefix, caseSensitive))
  144. {
  145. return str.Substring(prefix.Length);
  146. }
  147. return str;
  148. }
  149. }
  150. }