WindowImplBase.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using Avalonia.Input;
  6. using Avalonia.Input.Raw;
  7. using Avalonia.Platform;
  8. using Gdk;
  9. using Action = System.Action;
  10. using WindowEdge = Avalonia.Controls.WindowEdge;
  11. using GLib;
  12. namespace Avalonia.Gtk
  13. {
  14. using Gtk = global::Gtk;
  15. public abstract class WindowImplBase : IWindowImpl
  16. {
  17. private IInputRoot _inputRoot;
  18. protected Gtk.Widget _window;
  19. private FramebufferManager _framebuffer;
  20. private Gtk.IMContext _imContext;
  21. private uint _lastKeyEventTimestamp;
  22. private static readonly Gdk.Cursor DefaultCursor = new Gdk.Cursor(CursorType.LeftPtr);
  23. protected WindowImplBase(Gtk.Widget window)
  24. {
  25. _window = window;
  26. _framebuffer = new FramebufferManager(this);
  27. Init();
  28. }
  29. void Init()
  30. {
  31. Handle = _window as IPlatformHandle;
  32. _window.Events = EventMask.AllEventsMask;
  33. _imContext = new Gtk.IMMulticontext();
  34. _imContext.Commit += ImContext_Commit;
  35. _window.Realized += OnRealized;
  36. _window.Realize();
  37. _window.ButtonPressEvent += OnButtonPressEvent;
  38. _window.ButtonReleaseEvent += OnButtonReleaseEvent;
  39. _window.ScrollEvent += OnScrollEvent;
  40. _window.Destroyed += OnDestroyed;
  41. _window.KeyPressEvent += OnKeyPressEvent;
  42. _window.KeyReleaseEvent += OnKeyReleaseEvent;
  43. _window.ExposeEvent += OnExposeEvent;
  44. _window.MotionNotifyEvent += OnMotionNotifyEvent;
  45. }
  46. public IPlatformHandle Handle { get; private set; }
  47. public Gtk.Widget Widget => _window;
  48. public Gdk.Drawable CurrentDrawable { get; private set; }
  49. void OnRealized (object sender, EventArgs eventArgs)
  50. {
  51. _imContext.ClientWindow = _window.GdkWindow;
  52. }
  53. public abstract Size ClientSize { get; set; }
  54. public Size MaxClientSize
  55. {
  56. get
  57. {
  58. // TODO: This should take into account things such as taskbar and window border
  59. // thickness etc.
  60. return new Size(_window.Screen.Width, _window.Screen.Height);
  61. }
  62. }
  63. public Avalonia.Controls.WindowState WindowState
  64. {
  65. get
  66. {
  67. switch (_window.GdkWindow.State)
  68. {
  69. case Gdk.WindowState.Iconified:
  70. return Controls.WindowState.Minimized;
  71. case Gdk.WindowState.Maximized:
  72. return Controls.WindowState.Maximized;
  73. default:
  74. return Controls.WindowState.Normal;
  75. }
  76. }
  77. set
  78. {
  79. switch (value)
  80. {
  81. case Controls.WindowState.Minimized:
  82. _window.GdkWindow.Iconify();
  83. break;
  84. case Controls.WindowState.Maximized:
  85. _window.GdkWindow.Maximize();
  86. break;
  87. case Controls.WindowState.Normal:
  88. _window.GdkWindow.Deiconify();
  89. _window.GdkWindow.Unmaximize();
  90. break;
  91. }
  92. }
  93. }
  94. public double Scaling => 1;
  95. public Action Activated { get; set; }
  96. public Action Closed { get; set; }
  97. public Action Deactivated { get; set; }
  98. public Action<RawInputEventArgs> Input { get; set; }
  99. public Action<Rect> Paint { get; set; }
  100. public Action<Size> Resized { get; set; }
  101. public Action<Point> PositionChanged { get; set; }
  102. public Action<double> ScalingChanged { get; set; }
  103. public IEnumerable<object> Surfaces => new object[]
  104. {
  105. Handle,
  106. new Func<Gdk.Drawable>(() => CurrentDrawable),
  107. _framebuffer
  108. };
  109. public IPopupImpl CreatePopup()
  110. {
  111. return new PopupImpl();
  112. }
  113. public void Invalidate(Rect rect)
  114. {
  115. if (_window?.GdkWindow != null)
  116. _window.GdkWindow.InvalidateRect(
  117. new Rectangle((int) rect.X, (int) rect.Y, (int) rect.Width, (int) rect.Height), true);
  118. }
  119. public Point PointToClient(Point point)
  120. {
  121. int x, y;
  122. _window.GdkWindow.GetDeskrelativeOrigin(out x, out y);
  123. return new Point(point.X - x, point.Y - y);
  124. }
  125. public Point PointToScreen(Point point)
  126. {
  127. int x, y;
  128. _window.GdkWindow.GetDeskrelativeOrigin(out x, out y);
  129. return new Point(point.X + x, point.Y + y);
  130. }
  131. public void SetInputRoot(IInputRoot inputRoot)
  132. {
  133. _inputRoot = inputRoot;
  134. }
  135. public abstract void SetTitle(string title);
  136. public abstract IDisposable ShowDialog();
  137. public abstract void SetSystemDecorations(bool enabled);
  138. public abstract void SetIcon(IWindowIconImpl icon);
  139. public void SetCursor(IPlatformHandle cursor)
  140. {
  141. _window.GdkWindow.Cursor = cursor != null ? new Gdk.Cursor(cursor.Handle) : DefaultCursor;
  142. }
  143. public void Show() => _window.Show();
  144. public void Hide() => _window.Hide();
  145. public abstract void BeginMoveDrag();
  146. public abstract void BeginResizeDrag(WindowEdge edge);
  147. public abstract Point Position { get; set; }
  148. void ITopLevelImpl.Activate()
  149. {
  150. _window.Activate();
  151. }
  152. private static InputModifiers GetModifierKeys(ModifierType state)
  153. {
  154. var rv = InputModifiers.None;
  155. if (state.HasFlag(ModifierType.ControlMask))
  156. rv |= InputModifiers.Control;
  157. if (state.HasFlag(ModifierType.ShiftMask))
  158. rv |= InputModifiers.Shift;
  159. if (state.HasFlag(ModifierType.Mod1Mask))
  160. rv |= InputModifiers.Control;
  161. if(state.HasFlag(ModifierType.Button1Mask))
  162. rv |= InputModifiers.LeftMouseButton;
  163. if (state.HasFlag(ModifierType.Button2Mask))
  164. rv |= InputModifiers.RightMouseButton;
  165. if (state.HasFlag(ModifierType.Button3Mask))
  166. rv |= InputModifiers.MiddleMouseButton;
  167. return rv;
  168. }
  169. void OnButtonPressEvent(object o, Gtk.ButtonPressEventArgs args)
  170. {
  171. var evnt = args.Event;
  172. var e = new RawMouseEventArgs(
  173. GtkMouseDevice.Instance,
  174. evnt.Time,
  175. _inputRoot,
  176. evnt.Button == 1
  177. ? RawMouseEventType.LeftButtonDown
  178. : evnt.Button == 3 ? RawMouseEventType.RightButtonDown : RawMouseEventType.MiddleButtonDown,
  179. new Point(evnt.X, evnt.Y), GetModifierKeys(evnt.State));
  180. Input(e);
  181. }
  182. void OnScrollEvent(object o, Gtk.ScrollEventArgs args)
  183. {
  184. var evnt = args.Event;
  185. double step = 1;
  186. var delta = new Vector();
  187. if (evnt.Direction == ScrollDirection.Down)
  188. delta = new Vector(0, -step);
  189. else if (evnt.Direction == ScrollDirection.Up)
  190. delta = new Vector(0, step);
  191. else if (evnt.Direction == ScrollDirection.Right)
  192. delta = new Vector(-step, 0);
  193. if (evnt.Direction == ScrollDirection.Left)
  194. delta = new Vector(step, 0);
  195. var e = new RawMouseWheelEventArgs(GtkMouseDevice.Instance, evnt.Time, _inputRoot, new Point(evnt.X, evnt.Y), delta, GetModifierKeys(evnt.State));
  196. Input(e);
  197. }
  198. protected void OnButtonReleaseEvent(object o, Gtk.ButtonReleaseEventArgs args)
  199. {
  200. var evnt = args.Event;
  201. var e = new RawMouseEventArgs(
  202. GtkMouseDevice.Instance,
  203. evnt.Time,
  204. _inputRoot,
  205. evnt.Button == 1
  206. ? RawMouseEventType.LeftButtonUp
  207. : evnt.Button == 3 ? RawMouseEventType.RightButtonUp : RawMouseEventType.MiddleButtonUp,
  208. new Point(evnt.X, evnt.Y), GetModifierKeys(evnt.State));
  209. Input(e);
  210. }
  211. void OnDestroyed(object sender, EventArgs eventArgs)
  212. {
  213. Closed();
  214. }
  215. private void ProcessKeyEvent(EventKey evnt)
  216. {
  217. _lastKeyEventTimestamp = evnt.Time;
  218. if (_imContext.FilterKeypress(evnt))
  219. return;
  220. var e = new RawKeyEventArgs(
  221. GtkKeyboardDevice.Instance,
  222. evnt.Time,
  223. evnt.Type == EventType.KeyPress ? RawKeyEventType.KeyDown : RawKeyEventType.KeyUp,
  224. Common.KeyTransform.ConvertKey(evnt.Key), GetModifierKeys(evnt.State));
  225. Input(e);
  226. }
  227. [ConnectBefore]
  228. void OnKeyPressEvent(object o, Gtk.KeyPressEventArgs args)
  229. {
  230. args.RetVal = true;
  231. ProcessKeyEvent(args.Event);
  232. }
  233. void OnKeyReleaseEvent(object o, Gtk.KeyReleaseEventArgs args)
  234. {
  235. args.RetVal = true;
  236. ProcessKeyEvent(args.Event);
  237. }
  238. private void ImContext_Commit(object o, Gtk.CommitArgs args)
  239. {
  240. Input(new RawTextInputEventArgs(GtkKeyboardDevice.Instance, _lastKeyEventTimestamp, args.Str));
  241. }
  242. void OnExposeEvent(object o, Gtk.ExposeEventArgs args)
  243. {
  244. CurrentDrawable = args.Event.Window;
  245. Paint(args.Event.Area.ToAvalonia());
  246. CurrentDrawable = null;
  247. args.RetVal = true;
  248. }
  249. void OnMotionNotifyEvent(object o, Gtk.MotionNotifyEventArgs args)
  250. {
  251. var evnt = args.Event;
  252. var position = new Point(evnt.X, evnt.Y);
  253. GtkMouseDevice.Instance.SetClientPosition(position);
  254. var e = new RawMouseEventArgs(
  255. GtkMouseDevice.Instance,
  256. evnt.Time,
  257. _inputRoot,
  258. RawMouseEventType.Move,
  259. position, GetModifierKeys(evnt.State));
  260. Input(e);
  261. args.RetVal = true;
  262. }
  263. public void Dispose()
  264. {
  265. _framebuffer.Dispose();
  266. _window.Hide();
  267. _window.Dispose();
  268. _window = null;
  269. }
  270. }
  271. }