WindowImpl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Reactive.Disposables;
  3. using Avalonia.Platform;
  4. using Gdk;
  5. namespace Avalonia.Gtk
  6. {
  7. using Gtk = global::Gtk;
  8. public class WindowImpl : TopLevelImpl, IWindowImpl
  9. {
  10. private Gtk.Window _window;
  11. private Gtk.Window Window => _window ?? (_window = (Gtk.Window)Widget);
  12. public WindowImpl(Gtk.WindowType type) : base(new PlatformHandleAwareWindow(type))
  13. {
  14. Init();
  15. }
  16. public WindowImpl()
  17. : base(new PlatformHandleAwareWindow(Gtk.WindowType.Toplevel) {DefaultSize = new Gdk.Size(900, 480)})
  18. {
  19. Init();
  20. }
  21. void Init()
  22. {
  23. Window.FocusActivated += OnFocusActivated;
  24. Window.ConfigureEvent += OnConfigureEvent;
  25. _lastClientSize = ClientSize;
  26. _lastPosition = Position;
  27. }
  28. private Size _lastClientSize;
  29. private Point _lastPosition;
  30. void OnConfigureEvent(object o, Gtk.ConfigureEventArgs args)
  31. {
  32. var evnt = args.Event;
  33. args.RetVal = true;
  34. var newSize = new Size(evnt.Width, evnt.Height);
  35. if (newSize != _lastClientSize)
  36. {
  37. Resized(newSize);
  38. _lastClientSize = newSize;
  39. }
  40. var newPosition = new Point(evnt.X, evnt.Y);
  41. if (newPosition != _lastPosition)
  42. {
  43. PositionChanged(newPosition);
  44. _lastPosition = newPosition;
  45. }
  46. }
  47. public override Size ClientSize
  48. {
  49. get
  50. {
  51. int width;
  52. int height;
  53. Window.GetSize(out width, out height);
  54. return new Size(width, height);
  55. }
  56. }
  57. public void Resize(Size value)
  58. {
  59. Window.Resize((int)value.Width, (int)value.Height);
  60. }
  61. public void SetTitle(string title)
  62. {
  63. Window.Title = title;
  64. }
  65. void IWindowBaseImpl.Activate()
  66. {
  67. _window.Activate();
  68. }
  69. void OnFocusActivated(object sender, EventArgs eventArgs)
  70. {
  71. Activated();
  72. }
  73. public void BeginMoveDrag()
  74. {
  75. int x, y;
  76. ModifierType mod;
  77. Window.Screen.RootWindow.GetPointer(out x, out y, out mod);
  78. Window.BeginMoveDrag(1, x, y, 0);
  79. }
  80. public void BeginResizeDrag(Controls.WindowEdge edge)
  81. {
  82. int x, y;
  83. ModifierType mod;
  84. Window.Screen.RootWindow.GetPointer(out x, out y, out mod);
  85. Window.BeginResizeDrag((Gdk.WindowEdge)(int)edge, 1, x, y, 0);
  86. }
  87. public Point Position
  88. {
  89. get
  90. {
  91. int x, y;
  92. Window.GetPosition(out x, out y);
  93. return new Point(x, y);
  94. }
  95. set { Window.Move((int)value.X, (int)value.Y); }
  96. }
  97. public IDisposable ShowDialog()
  98. {
  99. Window.Modal = true;
  100. Window.Show();
  101. return Disposable.Empty;
  102. }
  103. public void SetSystemDecorations(bool enabled) => Window.Decorated = enabled;
  104. public void SetIcon(IWindowIconImpl icon)
  105. {
  106. Window.Icon = ((IconImpl)icon).Pixbuf;
  107. }
  108. public void ShowTaskbarIcon(bool value) => Window.SkipTaskbarHint = !value;
  109. }
  110. }