選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

274 行
8.8 KiB

  1. using CefSharp.DevTools.Network;
  2. using DamnOverSharp.Renderers.WPF;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Timers;
  8. using System.Windows.Controls;
  9. using System.Windows.Forms;
  10. using System.Windows.Forms.VisualStyles;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Threading;
  14. using static System.Net.Mime.MediaTypeNames;
  15. namespace DamnOverSharp.WPF.UiLibrary
  16. {
  17. /// <summary>
  18. /// Interaction logic for VirtualTextBox.xaml
  19. /// </summary>
  20. public partial class VirtualTextBox : VirtualControlBase
  21. {
  22. private DispatcherTimer CaretBlink = new DispatcherTimer();
  23. private bool IsCaretBlinkInvisible = false;
  24. public VirtualTextBox()
  25. {
  26. InitializeComponent();
  27. CaretBlink.Interval = TimeSpan.FromMilliseconds(600);
  28. CaretBlink.Tick += CaretBlink_Elapsed;
  29. }
  30. private void CaretBlink_Elapsed(object sender, EventArgs args)
  31. {
  32. IsCaretBlinkInvisible = !IsCaretBlinkInvisible;
  33. caret.Visibility = IsCaretBlinkInvisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
  34. UpdateVisual();
  35. }
  36. public override bool OnVirtualMouseDown() => true;
  37. public override bool OnVirtualMouseUp() => true;
  38. public override void OnGotVirtualFocus()
  39. {
  40. CaretIndex = CaretIndex;
  41. caret.Visibility = System.Windows.Visibility.Visible;
  42. CaretBlink.Interval = TimeSpan.FromMilliseconds(600);
  43. CaretBlink.Start();
  44. border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF569DE5"));
  45. base.OnGotVirtualFocus();
  46. }
  47. public override void OnLostVirtualFocus()
  48. {
  49. caret.Visibility = System.Windows.Visibility.Hidden;
  50. CaretBlink.Stop();
  51. border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFABADB3"));
  52. base.OnLostVirtualFocus();
  53. }
  54. public override void OnVirtualMouseEnter()
  55. {
  56. if (!VirtualFocused)
  57. {
  58. border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF7EB4EA"));
  59. }
  60. base.OnVirtualMouseEnter();
  61. }
  62. public override void OnVirtualMouseLeave()
  63. {
  64. if (VirtualFocused)
  65. {
  66. border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF569DE5"));
  67. }
  68. else
  69. {
  70. border.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFABADB3"));
  71. }
  72. base.OnVirtualMouseLeave();
  73. }
  74. public override void OnVirtualKeyDown(Keys keyCode)
  75. {
  76. switch (keyCode)
  77. {
  78. case Keys.Back:
  79. Remove(1);
  80. break;
  81. case Keys.Left:
  82. CaretIndex = Math.Max(0, CaretIndex - 1);
  83. UpdateVisual();
  84. break;
  85. case Keys.Right:
  86. CaretIndex = Math.Min(Text.Length, CaretIndex + 1);
  87. UpdateVisual();
  88. break;
  89. case Keys.Space:
  90. Insert(" ");
  91. break;
  92. case Keys.Tab:
  93. Insert(" ");
  94. break;
  95. case Keys.Oemcomma:
  96. Insert(",");
  97. break;
  98. case Keys.OemPeriod:
  99. Insert(".");
  100. break;
  101. case Keys.Oemplus:
  102. Insert("+");
  103. break;
  104. case Keys.OemMinus:
  105. Insert("-");
  106. break;
  107. case Keys.Oemtilde:
  108. Insert("`");
  109. break;
  110. case Keys.D0:
  111. Insert("0");
  112. break;
  113. case Keys.D1:
  114. Insert("1");
  115. break;
  116. case Keys.D2:
  117. Insert("2");
  118. break;
  119. case Keys.D3:
  120. Insert("3");
  121. break;
  122. case Keys.D4:
  123. Insert("4");
  124. break;
  125. case Keys.D5:
  126. Insert("5");
  127. break;
  128. case Keys.D6:
  129. Insert("6");
  130. break;
  131. case Keys.D7:
  132. Insert("7");
  133. break;
  134. case Keys.D8:
  135. Insert("8");
  136. break;
  137. case Keys.D9:
  138. Insert("9");
  139. break;
  140. case Keys.OemBackslash:
  141. Insert(@"\");
  142. break;
  143. case Keys.OemPipe:
  144. Insert("|");
  145. break;
  146. case Keys.LControlKey:
  147. case Keys.RControlKey:
  148. case Keys.LShiftKey:
  149. case Keys.RShiftKey:
  150. case Keys.LWin:
  151. case Keys.RWin:
  152. case Keys.Alt:
  153. case Keys.PrintScreen:
  154. case Keys.OemCloseBrackets:
  155. case Keys.OemOpenBrackets:
  156. break;
  157. default:
  158. if(keyCode.ToString().Length > 1)
  159. {
  160. Debug.WriteLine("Key not implemented: " + keyCode.ToString());
  161. return;
  162. }
  163. if (WpfInteractionManager.IsShiftDown)
  164. {
  165. Insert(char.ToUpper(char.Parse(keyCode.ToString())).ToString());
  166. }
  167. else
  168. {
  169. Insert(char.ToLower(char.Parse(keyCode.ToString())).ToString());
  170. }
  171. break;
  172. }
  173. }
  174. public void Insert(string text)
  175. {
  176. _Text = Text.Insert(CaretIndex, text);
  177. characters.Children.Insert(CaretIndex, new TextBlock() { Text = text, Foreground = Brushes.Black });
  178. CaretIndex += text.Length;
  179. UpdateVisual();
  180. }
  181. public void Remove(int count)
  182. {
  183. if (CaretIndex == 0)
  184. {
  185. return;
  186. }
  187. count = Math.Min(Text.Length, count);
  188. _Text = Text.Remove(CaretIndex - count, count);
  189. characters.Children.RemoveRange(CaretIndex - count, count);
  190. CaretIndex -= count;
  191. UpdateVisual();
  192. }
  193. private int _CaretIndex = 0;
  194. public int CaretIndex
  195. {
  196. get => _CaretIndex;
  197. set
  198. {
  199. if (VirtualFocused)
  200. {
  201. CaretBlink.Stop();
  202. CaretBlink.Interval = TimeSpan.FromMilliseconds(600);
  203. CaretBlink.Start();
  204. caret.Visibility = System.Windows.Visibility.Visible;
  205. }
  206. _CaretIndex = value;
  207. characters.UpdateLayout();
  208. content.UpdateLayout();
  209. caret.Margin = new System.Windows.Thickness(Math.Max(0, Math.Min(characters.ActualWidth - 1, characters.Children.OfType<TextBlock>().ToList().GetRange(0, value).Sum(x => x.ActualWidth))), 0, 0, 0);
  210. if(characters.ActualWidth <= contentArea.ActualWidth)
  211. {
  212. content.Margin = new System.Windows.Thickness(0, 0, 0, 0);
  213. return;
  214. }
  215. else if (caret.Margin.Left > Math.Abs(content.Margin.Left) + contentArea.ActualWidth - 5)
  216. {
  217. content.Margin = new System.Windows.Thickness(Math.Min(0, -caret.Margin.Left + contentArea.ActualWidth - 5), 0, 0, 0);
  218. }
  219. else if (caret.Margin.Left < Math.Abs(content.Margin.Left) + 5)
  220. {
  221. content.Margin = new System.Windows.Thickness(Math.Min(0, -caret.Margin.Left + 5), 0, 0, 0);
  222. }
  223. content.Margin = new System.Windows.Thickness(Math.Max(content.Margin.Left, contentArea.ActualWidth - characters.ActualWidth), 0, 0, 0);
  224. }
  225. }
  226. private string _Text = "";
  227. public string Text
  228. {
  229. get => _Text;
  230. set
  231. {
  232. _Text = value;
  233. characters.Children.Clear();
  234. foreach (char c in _Text)
  235. {
  236. characters.Children.Add(new TextBlock() { Text = c.ToString(), Foreground = Brushes.Black });
  237. }
  238. CaretIndex = value.Length;
  239. UpdateVisual();
  240. }
  241. }
  242. }
  243. }