123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using CefSharp;
- namespace prBrowser
- {
- internal static class Utils
- {
- public static bool IsFocussed(TextBox tb)
- {
- return tb.Focused;
- }
- public static void AddHotKey(Form form, Action function, Keys key, bool ctrl = false, bool shift = false, bool alt = false)
- {
- form.KeyPreview = true;
- form.KeyDown += delegate(object sender, KeyEventArgs e)
- {
- if (e.IsHotkey(key, ctrl, shift, alt))
- {
- function();
- }
- };
- }
- public static bool IsFullySelected(TextBox tb)
- {
- return tb.SelectionLength == tb.TextLength;
- }
- public static bool HasSelection(TextBox tb)
- {
- return tb.SelectionLength > 0;
- }
- public static bool CheckIfFilePath(this string path)
- {
- if (path[1] == ':' && path[2] == '\\' && char.IsLetter(path[0]))
- {
- return true;
- }
- return false;
- }
- public static bool CheckIfFilePath2(this string path)
- {
- if (path[1] == ':' && path[2] == '/' && char.IsLetter(path[0]))
- {
- return true;
- }
- return false;
- }
- public static string GetAfter(this string text, string find, int startAt = 0, bool returnAll = false, bool forward = true)
- {
- if (text == null)
- {
- return returnAll ? text : "";
- }
- int idx = (forward ? text.IndexOf(find, startAt, StringComparison.Ordinal) : text.LastIndexOf(find, text.Length - startAt, StringComparison.Ordinal));
- if (idx == -1)
- {
- return returnAll ? text : "";
- }
- idx += find.Length;
- return text.Substring(idx);
- }
- public static string GetAfterLast(this string text, string find, bool returnAll = false)
- {
- int idx = text.LastIndexOf(find, StringComparison.Ordinal);
- if (idx == -1)
- {
- return returnAll ? text : "";
- }
- idx += find.Length;
- return text.Substring(idx);
- }
- public static bool SupportedInFilePath(this char c)
- {
- return c != '?' && c != '\'' && c != '"' && c != '/' && c != '\\' && c != ';' && c != ':' && c != '&' && c != '*' && c != '<' && c != '>' && c != '|' && c != '{' && c != '}' && c != '[' && c != ']' && c != '(' && c != ')';
- }
- public static string ChangePathSlash(this string filePath, string slash)
- {
- if (slash == "\\" && filePath.Contains('/'))
- {
- return filePath.Replace("/", "\\");
- }
- if (slash == "/" && filePath.Contains('\\'))
- {
- return filePath.Replace("\\", "/");
- }
- return filePath;
- }
- public static string FileURLToPath(this string url)
- {
- return url.RemovePrefix("file:///").ChangePathSlash("\\").DecodeURLForFilepath();
- }
- public static bool FileNotExists(this string path)
- {
- return !File.Exists(path);
- }
- public static string ConvertToString(this object o)
- {
- if (o is string)
- {
- return o as string;
- }
- return null;
- }
- public static bool CheckIfValid(this string text, bool trimAndCheck = false)
- {
- return text != null && text.Length > 0;
- }
- public static void InvokeOnParent(this Control control, MethodInvoker method)
- {
- Control parent = ((control.Parent == null) ? control : control.Parent);
- if (parent.IsHandleCreated)
- {
- parent.Invoke(method);
- }
- }
- public static bool IsHotkey(this KeyEventArgs eventData, Keys key, bool ctrl = false, bool shift = false, bool alt = false)
- {
- return eventData.KeyCode == key && eventData.Control == ctrl && eventData.Shift == shift && eventData.Alt == alt;
- }
- public static CefState ToCefState(this bool value)
- {
- return value ? CefState.Enabled : CefState.Disabled;
- }
- public static bool IsBitmaskOn(this int num, int bitmask)
- {
- return (num & bitmask) != 0;
- }
- public static bool BeginsWith(this string str, string beginsWith, bool caseSensitive = true)
- {
- if (beginsWith.Length > str.Length)
- {
- return false;
- }
- if (beginsWith.Length == str.Length)
- {
- return string.Equals(beginsWith, str, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
- }
- return str.LastIndexOf(beginsWith, beginsWith.Length - 1, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase) == 0;
- }
- public static string RemovePrefix(this string str, string prefix, bool caseSensitive = true)
- {
- if (str.Length >= prefix.Length && str.BeginsWith(prefix, caseSensitive))
- {
- return str.Substring(prefix.Length);
- }
- return str;
- }
- }
- }
|