123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System;
- using System.IO;
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Markup.Xaml;
- using JsonRpcLite.InProcess;
- using JsonRpcLite.Rpc;
- using Xilium.CefGlue;
- using Xilium.CefGlue.Avalonia;
- using Xilium.CefGlue.Common;
- using Xilium.CefGlue.Common.Handlers;
- namespace Flyinsono.Demo
- {
- public sealed class JsonRpcHandler<T> : IHostHandler
- {
- private readonly JsonRpcInProcessEngine _engine = new();
- private readonly JsonRpcServer _server = new();
- private readonly JsonRpcClient _client = new();
- public string Host { get; }
- public JsonRpcHandler(string host, T service)
- {
- Host = host;
- _server.UseEngine(_engine);
- _client.UseEngine(_engine);
- _server.RegisterService(service);
- _server.Start();
- }
- public HandleResult Handle(string path, byte[] postData = null)
- {
- try
- {
- var serviceName = path;
- if (serviceName.StartsWith("/"))
- {
- serviceName = serviceName.Substring(1);
- }
- var resultData = _client.ProcessAsync(serviceName, postData).Result;
- var resultStream = new MemoryStream(resultData);
- return new HandleResult(resultStream, "application/json");
- }
- catch
- {
- // ignored
- }
- return null;
- }
- }
- public class MainWindow : Window
- {
- public AvaloniaCefBrowser _browser;
- public MainWindow()
- {
- InitializeComponent();
- #if DEBUG
- this.AttachDevTools();
- #endif
- }
- private void InitializeComponent()
- {
- AvaloniaXamlLoader.Load(this);
- var browserContainer = this.FindControl<Border>("BrowserBorder");
- var flyinsonoHandler = new FileSystemHostHandler("app.flyinsono.com",Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "flyinsono.com"));
- var handler = new HostRequestHandler();
- handler.RegisterHostHandler(flyinsonoHandler);
- _browser = new AvaloniaCefBrowser
- {
- Address = "http://app.flyinsono.com/index.html",
- RequestHandler = handler,
- };
- _browser.Settings.WebSecurity = CefState.Disabled;
- _browser.ContextMenuHandler = new TextContextMenuHandler(_browser);
- browserContainer.Child = _browser;
- }
- }
- public class TextContextMenuHandler : ContextMenuHandler
- {
- private readonly BaseCefBrowser _owner;
- public TextContextMenuHandler(BaseCefBrowser owner)
- {
- _owner = owner;
- }
- protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model)
- {
- if (!((state.ContextMenuType & CefContextMenuTypeFlags.Editable) != 0 || (state.ContextMenuType & CefContextMenuTypeFlags.Selection) != 0))
- {
- model.Clear();
- }
- base.OnBeforeContextMenu(browser, frame, state, model);
- }
- protected override bool RunContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams parameters, CefMenuModel model, CefRunContextMenuCallback callback)
- {
- //This is for enable the style.
- _owner.HandleOpenContextMenu(parameters, model, callback);
- return true;
- }
- }
- }
|