EmbedSample.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using Avalonia.Controls;
  7. using Avalonia.Platform;
  8. using Avalonia.Threading;
  9. using MonoMac.AppKit;
  10. using MonoMac.Foundation;
  11. using MonoMac.WebKit;
  12. using Encoding = SharpDX.Text.Encoding;
  13. namespace NativeEmbedSample
  14. {
  15. public class EmbedSample : NativeControlHost
  16. {
  17. public bool IsSecond { get; set; }
  18. private Process _mplayer;
  19. IPlatformHandle CreateLinux(IPlatformHandle parent)
  20. {
  21. if (IsSecond)
  22. {
  23. var chooser = GtkHelper.CreateGtkFileChooser(parent.Handle);
  24. if (chooser != null)
  25. return chooser;
  26. }
  27. var control = base.CreateNativeControlCore(parent);
  28. var nodes = Path.GetFullPath(Path.Combine(typeof(EmbedSample).Assembly.GetModules()[0].FullyQualifiedName,
  29. "..",
  30. "nodes.mp4"));
  31. _mplayer = Process.Start(new ProcessStartInfo("mplayer",
  32. $"-vo x11 -zoom -loop 0 -wid {control.Handle.ToInt64()} \"{nodes}\"")
  33. {
  34. UseShellExecute = false,
  35. });
  36. return control;
  37. }
  38. void DestroyLinux(IPlatformHandle handle)
  39. {
  40. _mplayer?.Kill();
  41. _mplayer = null;
  42. base.DestroyNativeControlCore(handle);
  43. }
  44. private const string RichText =
  45. @"{\rtf1\ansi\ansicpg1251\deff0\nouicompat\deflang1049{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
  46. {\colortbl ;\red255\green0\blue0;\red0\green77\blue187;\red0\green176\blue80;\red155\green0\blue211;\red247\green150\blue70;\red75\green172\blue198;}
  47. {\*\generator Riched20 6.3.9600}\viewkind4\uc1
  48. \pard\sa200\sl276\slmult1\f0\fs22\lang9 <PREFIX>I \i am\i0 a \cf1\b Rich Text \cf0\b0\fs24 control\cf2\fs28 !\cf3\fs32 !\cf4\fs36 !\cf1\fs40 !\cf5\fs44 !\cf6\fs48 !\cf0\fs44\par
  49. }";
  50. IPlatformHandle CreateWin32(IPlatformHandle parent)
  51. {
  52. WinApi.LoadLibrary("Msftedit.dll");
  53. var handle = WinApi.CreateWindowEx(0, "RICHEDIT50W",
  54. @"Rich Edit",
  55. 0x800000 | 0x10000000 | 0x40000000 | 0x800000 | 0x10000 | 0x0004, 0, 0, 1, 1, parent.Handle,
  56. IntPtr.Zero, WinApi.GetModuleHandle(null), IntPtr.Zero);
  57. var st = new WinApi.SETTEXTEX { Codepage = 65001, Flags = 0x00000008 };
  58. var text = RichText.Replace("<PREFIX>", IsSecond ? "\\qr " : "");
  59. var bytes = Encoding.UTF8.GetBytes(text);
  60. WinApi.SendMessage(handle, 0x0400 + 97, ref st, bytes);
  61. return new PlatformHandle(handle, "HWND");
  62. }
  63. void DestroyWin32(IPlatformHandle handle)
  64. {
  65. WinApi.DestroyWindow(handle.Handle);
  66. }
  67. IPlatformHandle CreateOSX(IPlatformHandle parent)
  68. {
  69. // Note: We are using MonoMac for example purposes
  70. // It shouldn't be used in production apps
  71. MacHelper.EnsureInitialized();
  72. var webView = new WebView();
  73. Dispatcher.UIThread.Post(() =>
  74. {
  75. webView.MainFrame.LoadRequest(new NSUrlRequest(new NSUrl(
  76. IsSecond ? "https://bing.com": "https://google.com/")));
  77. });
  78. return new MacOSViewHandle(webView);
  79. }
  80. void DestroyOSX(IPlatformHandle handle)
  81. {
  82. ((MacOSViewHandle)handle).Dispose();
  83. }
  84. protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle parent)
  85. {
  86. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  87. return CreateLinux(parent);
  88. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  89. return CreateWin32(parent);
  90. if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  91. return CreateOSX(parent);
  92. return base.CreateNativeControlCore(parent);
  93. }
  94. protected override void DestroyNativeControlCore(IPlatformHandle control)
  95. {
  96. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  97. DestroyLinux(control);
  98. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  99. DestroyWin32(control);
  100. else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  101. DestroyOSX(control);
  102. else
  103. base.DestroyNativeControlCore(control);
  104. }
  105. }
  106. }