This repository has been archived on 2025-03-23. You can view files and clone it, but cannot push or open issues or pull requests.
xerobrowser/WebBrowser/DisplayHandler.cs
2023-02-20 03:39:55 -07:00

107 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using CefSharp;
using CefSharp.Enums;
using CefSharp.Structs;
using WebBrowser;
using Size = CefSharp.Structs.Size;
namespace DiamondCreeperBrowser
{
public class DisplayHandler : IDisplayHandler
{
public frmBrowser frmref;
public DisplayHandler(frmBrowser frm)
{
frmref = frm;
}
public static Icon BytesToIcon(byte[] bytes)
{
using (MemoryStream ms = new MemoryStream(bytes))
{
return new Icon(ms);
}
}
public void OnAddressChanged(IWebBrowser chromiumWebBrowser, AddressChangedEventArgs addressChangedArgs)
{
}
public bool OnAutoResize(IWebBrowser chromiumWebBrowser, IBrowser browser, Size newSize)
{
return true;
}
public bool OnConsoleMessage(IWebBrowser chromiumWebBrowser, ConsoleMessageEventArgs consoleMessageArgs)
{
return true;
}
public bool OnCursorChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IntPtr cursor, CursorType type, CursorInfo customCursorInfo)
{
return true;
}
public void OnFaviconUrlChange(IWebBrowser chromiumWebBrowser, IBrowser browser, IList<string> urls)
{
try
{
if (urls.Count > 0)
{
WebClient wc = new WebClient();
byte[] buffer = wc.DownloadData(urls[0]);
frmref.Icon = BytesToIcon(buffer);
frmref.Update();
frmref.ParentTabs.UpdateThumbnailPreviewIcon(frmref.ParentTabs.Tabs.Single(t => t.Content == frmref));
frmref.ParentTabs.RedrawTabs();
}
} catch (Exception){
try
{
WebClient wc = new WebClient();
Uri furi = new Uri(chromiumWebBrowser.Address);
byte[] buffer = wc.DownloadData(furi.GetLeftPart(UriPartial.Authority) +"/favicon.ico");
frmref.Icon = BytesToIcon(buffer);
frmref.Update();
frmref.ParentTabs.UpdateThumbnailPreviewIcon(frmref.ParentTabs.Tabs.Single(t => t.Content == frmref));
frmref.ParentTabs.RedrawTabs();
}
catch (Exception){
ComponentResourceManager resources = new ComponentResourceManager(typeof(frmBrowser));
frmref.Icon = ((Icon)(resources.GetObject("$this.Icon")));
frmref.Update();
frmref.ParentTabs.UpdateThumbnailPreviewIcon(frmref.ParentTabs.Tabs.Single(t => t.Content == frmref));
frmref.ParentTabs.RedrawTabs();
};
};
}
public void OnFullscreenModeChange(IWebBrowser chromiumWebBrowser, IBrowser browser, bool fullscreen)
{
}
public void OnLoadingProgressChange(IWebBrowser chromiumWebBrowser, IBrowser browser, double progress)
{
}
public void OnStatusMessage(IWebBrowser chromiumWebBrowser, StatusMessageEventArgs statusMessageArgs)
{
}
public void OnTitleChanged(IWebBrowser chromiumWebBrowser, TitleChangedEventArgs titleChangedArgs)
{
}
public bool OnTooltipChanged(IWebBrowser chromiumWebBrowser, ref string text)
{
return true;
}
}
}