WindowImpl.cs 3.5 KB

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