WindowImplBase.cs 11 KB

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