Tweak CefSharp to allow multiple instances, remove progressbar

This commit is contained in:
Swee 2024-09-05 21:20:30 -07:00
parent 4a3eedd8c6
commit 32ca2eef85
3 changed files with 174 additions and 162 deletions

268
Form1.Designer.cs generated
View file

@ -33,27 +33,26 @@
fileToolStripMenuItem = new ToolStripMenuItem();
openDevToolsToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem = new ToolStripMenuItem();
helpToolStripMenuItem = new ToolStripMenuItem();
aboutToolStripMenuItem = new ToolStripMenuItem();
renderToolStripMenuItem = new ToolStripMenuItem();
fontFamilyToolStripMenuItem = new ToolStripMenuItem();
serifToolStripMenuItem = new ToolStripMenuItem();
sansSerifToolStripMenuItem = new ToolStripMenuItem();
forceMonospaceToolStripMenuItem = new ToolStripMenuItem();
colorThemeToolStripMenuItem = new ToolStripMenuItem();
systemToolStripMenuItem = new ToolStripMenuItem();
lightToolStripMenuItem = new ToolStripMenuItem();
darkToolStripMenuItem = new ToolStripMenuItem();
helpToolStripMenuItem = new ToolStripMenuItem();
aboutToolStripMenuItem = new ToolStripMenuItem();
Opener = new OpenFileDialog();
Saver = new SaveFileDialog();
groupBox4 = new GroupBox();
chromiumWebBrowser1 = new CefSharp.WinForms.ChromiumWebBrowser();
progressBar1 = new ProgressBar();
button4 = new Button();
button3 = new Button();
groupBox3 = new GroupBox();
button2 = new Button();
textBox2 = new TextBox();
colorThemeToolStripMenuItem = new ToolStripMenuItem();
systemToolStripMenuItem = new ToolStripMenuItem();
lightToolStripMenuItem = new ToolStripMenuItem();
darkToolStripMenuItem = new ToolStripMenuItem();
menuStrip1.SuspendLayout();
groupBox4.SuspendLayout();
groupBox3.SuspendLayout();
@ -61,7 +60,7 @@
//
// menuStrip1
//
menuStrip1.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem, helpToolStripMenuItem, renderToolStripMenuItem });
menuStrip1.Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem, renderToolStripMenuItem, helpToolStripMenuItem });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.RenderMode = ToolStripRenderMode.System;
@ -91,33 +90,19 @@
exitToolStripMenuItem.Text = "Exit";
exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
//
// helpToolStripMenuItem
//
helpToolStripMenuItem.Alignment = ToolStripItemAlignment.Right;
helpToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { aboutToolStripMenuItem });
helpToolStripMenuItem.Name = "helpToolStripMenuItem";
helpToolStripMenuItem.Size = new Size(44, 20);
helpToolStripMenuItem.Text = "Help";
//
// aboutToolStripMenuItem
//
aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
aboutToolStripMenuItem.Size = new Size(107, 22);
aboutToolStripMenuItem.Text = "About";
aboutToolStripMenuItem.Click += aboutToolStripMenuItem_Click;
//
// renderToolStripMenuItem
//
renderToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { fontFamilyToolStripMenuItem, colorThemeToolStripMenuItem });
renderToolStripMenuItem.Name = "renderToolStripMenuItem";
renderToolStripMenuItem.Size = new Size(56, 20);
renderToolStripMenuItem.Text = "Render";
renderToolStripMenuItem.Click += renderToolStripMenuItem_Click;
//
// fontFamilyToolStripMenuItem
//
fontFamilyToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { serifToolStripMenuItem, sansSerifToolStripMenuItem, forceMonospaceToolStripMenuItem });
fontFamilyToolStripMenuItem.Name = "fontFamilyToolStripMenuItem";
fontFamilyToolStripMenuItem.Size = new Size(180, 22);
fontFamilyToolStripMenuItem.Size = new Size(140, 22);
fontFamilyToolStripMenuItem.Text = "Font Family";
//
// serifToolStripMenuItem
@ -143,121 +128,11 @@
forceMonospaceToolStripMenuItem.Text = "(Force) Monospace";
forceMonospaceToolStripMenuItem.Click += forceMonospaceToolStripMenuItem_Click;
//
// Opener
//
Opener.DefaultExt = "md";
Opener.Filter = "Markdown files|*.md|All files|*.*";
Opener.SupportMultiDottedExtensions = true;
//
// Saver
//
Saver.CheckPathExists = false;
Saver.DefaultExt = "html";
Saver.Filter = "HTML websites|*.html|All files|*.*";
//
// groupBox4
//
groupBox4.Controls.Add(chromiumWebBrowser1);
groupBox4.Controls.Add(progressBar1);
groupBox4.Controls.Add(button4);
groupBox4.Dock = DockStyle.Fill;
groupBox4.FlatStyle = FlatStyle.System;
groupBox4.Location = new Point(0, 91);
groupBox4.Name = "groupBox4";
groupBox4.Size = new Size(800, 378);
groupBox4.TabIndex = 3;
groupBox4.TabStop = false;
groupBox4.Text = "Preview";
groupBox4.UseCompatibleTextRendering = true;
groupBox4.Enter += groupBox4_Enter;
//
// chromiumWebBrowser1
//
chromiumWebBrowser1.ActivateBrowserOnCreation = false;
chromiumWebBrowser1.Dock = DockStyle.Fill;
chromiumWebBrowser1.Location = new Point(3, 47);
chromiumWebBrowser1.Name = "chromiumWebBrowser1";
chromiumWebBrowser1.Size = new Size(794, 328);
chromiumWebBrowser1.TabIndex = 3;
chromiumWebBrowser1.LoadingStateChanged += chromiumWebBrowser1_LoadingStateChanged;
//
// progressBar1
//
progressBar1.Dock = DockStyle.Top;
progressBar1.Location = new Point(3, 42);
progressBar1.Name = "progressBar1";
progressBar1.Size = new Size(794, 5);
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.TabIndex = 2;
//
// button4
//
button4.Dock = DockStyle.Top;
button4.Enabled = false;
button4.FlatStyle = FlatStyle.Flat;
button4.Location = new Point(3, 19);
button4.Name = "button4";
button4.Size = new Size(794, 23);
button4.TabIndex = 1;
button4.Text = "Render preview";
button4.UseVisualStyleBackColor = true;
button4.Click += button4_Click;
//
// button3
//
button3.BackColor = Color.Transparent;
button3.Dock = DockStyle.Bottom;
button3.Enabled = false;
button3.FlatStyle = FlatStyle.Flat;
button3.Location = new Point(0, 469);
button3.Name = "button3";
button3.Size = new Size(800, 23);
button3.TabIndex = 4;
button3.Text = "Convert";
button3.UseVisualStyleBackColor = false;
button3.Click += button3_Click;
//
// groupBox3
//
groupBox3.Controls.Add(button2);
groupBox3.Controls.Add(textBox2);
groupBox3.Dock = DockStyle.Top;
groupBox3.FlatStyle = FlatStyle.System;
groupBox3.Location = new Point(0, 24);
groupBox3.Name = "groupBox3";
groupBox3.Size = new Size(800, 67);
groupBox3.TabIndex = 2;
groupBox3.TabStop = false;
groupBox3.Text = "Input file";
//
// button2
//
button2.Dock = DockStyle.Fill;
button2.FlatStyle = FlatStyle.Flat;
button2.Location = new Point(3, 35);
button2.Name = "button2";
button2.Size = new Size(794, 29);
button2.TabIndex = 3;
button2.Text = "Select file";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
//
// textBox2
//
textBox2.BorderStyle = BorderStyle.None;
textBox2.Dock = DockStyle.Top;
textBox2.Location = new Point(3, 19);
textBox2.Name = "textBox2";
textBox2.PlaceholderText = "Type path or select file below. ";
textBox2.Size = new Size(794, 16);
textBox2.TabIndex = 2;
textBox2.TextChanged += textBox2_TextChanged;
//
// colorThemeToolStripMenuItem
//
colorThemeToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { systemToolStripMenuItem, lightToolStripMenuItem, darkToolStripMenuItem });
colorThemeToolStripMenuItem.Name = "colorThemeToolStripMenuItem";
colorThemeToolStripMenuItem.Size = new Size(180, 22);
colorThemeToolStripMenuItem.Size = new Size(140, 22);
colorThemeToolStripMenuItem.Text = "Color theme";
//
// systemToolStripMenuItem
@ -283,6 +158,120 @@
darkToolStripMenuItem.Size = new Size(161, 22);
darkToolStripMenuItem.Text = "Dark";
//
// helpToolStripMenuItem
//
helpToolStripMenuItem.Alignment = ToolStripItemAlignment.Right;
helpToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { aboutToolStripMenuItem });
helpToolStripMenuItem.Name = "helpToolStripMenuItem";
helpToolStripMenuItem.Size = new Size(44, 20);
helpToolStripMenuItem.Text = "Help";
//
// aboutToolStripMenuItem
//
aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
aboutToolStripMenuItem.Size = new Size(107, 22);
aboutToolStripMenuItem.Text = "About";
aboutToolStripMenuItem.Click += aboutToolStripMenuItem_Click;
//
// Opener
//
Opener.DefaultExt = "md";
Opener.Filter = "Markdown files|*.md|All files|*.*";
Opener.SupportMultiDottedExtensions = true;
//
// Saver
//
Saver.CheckPathExists = false;
Saver.DefaultExt = "html";
Saver.Filter = "HTML websites|*.html|All files|*.*";
//
// groupBox4
//
groupBox4.Controls.Add(chromiumWebBrowser1);
groupBox4.Controls.Add(button4);
groupBox4.Dock = DockStyle.Fill;
groupBox4.FlatStyle = FlatStyle.System;
groupBox4.Location = new Point(0, 91);
groupBox4.Name = "groupBox4";
groupBox4.Size = new Size(800, 378);
groupBox4.TabIndex = 3;
groupBox4.TabStop = false;
groupBox4.Text = "Preview";
groupBox4.UseCompatibleTextRendering = true;
groupBox4.Enter += groupBox4_Enter;
//
// chromiumWebBrowser1
//
chromiumWebBrowser1.ActivateBrowserOnCreation = false;
chromiumWebBrowser1.Dock = DockStyle.Fill;
chromiumWebBrowser1.Location = new Point(3, 42);
chromiumWebBrowser1.Name = "chromiumWebBrowser1";
chromiumWebBrowser1.Size = new Size(794, 333);
chromiumWebBrowser1.TabIndex = 3;
chromiumWebBrowser1.AddressChanged += chromiumWebBrowser1_AddressChanged;
//
// button4
//
button4.Dock = DockStyle.Top;
button4.Enabled = false;
button4.FlatStyle = FlatStyle.System;
button4.Location = new Point(3, 19);
button4.Name = "button4";
button4.Size = new Size(794, 23);
button4.TabIndex = 1;
button4.Text = "Render preview";
button4.UseVisualStyleBackColor = true;
button4.Click += button4_Click;
//
// button3
//
button3.Dock = DockStyle.Bottom;
button3.Enabled = false;
button3.FlatStyle = FlatStyle.System;
button3.Location = new Point(0, 469);
button3.Name = "button3";
button3.Size = new Size(800, 23);
button3.TabIndex = 4;
button3.Text = "Convert";
button3.UseVisualStyleBackColor = false;
button3.Click += button3_Click;
//
// groupBox3
//
groupBox3.Controls.Add(button2);
groupBox3.Controls.Add(textBox2);
groupBox3.Dock = DockStyle.Top;
groupBox3.FlatStyle = FlatStyle.System;
groupBox3.Location = new Point(0, 24);
groupBox3.Name = "groupBox3";
groupBox3.Size = new Size(800, 67);
groupBox3.TabIndex = 2;
groupBox3.TabStop = false;
groupBox3.Text = "Input file";
//
// button2
//
button2.Dock = DockStyle.Fill;
button2.FlatStyle = FlatStyle.System;
button2.Location = new Point(3, 35);
button2.Name = "button2";
button2.Size = new Size(794, 29);
button2.TabIndex = 3;
button2.Text = "Select file";
button2.UseVisualStyleBackColor = true;
button2.Click += button2_Click;
//
// textBox2
//
textBox2.BorderStyle = BorderStyle.None;
textBox2.Dock = DockStyle.Top;
textBox2.Location = new Point(3, 19);
textBox2.Name = "textBox2";
textBox2.PlaceholderText = "Type path or select file below. ";
textBox2.Size = new Size(794, 16);
textBox2.TabIndex = 2;
textBox2.TextChanged += textBox2_TextChanged;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -319,11 +308,6 @@
private Button button3;
private Button button4;
private ToolStripMenuItem renderToolStripMenuItem;
private ToolStripMenuItem fontFamilyToolStripMenuItem;
private ToolStripMenuItem serifToolStripMenuItem;
private ToolStripMenuItem sansSerifToolStripMenuItem;
private ToolStripMenuItem forceMonospaceToolStripMenuItem;
private ProgressBar progressBar1;
private ToolStripMenuItem openDevToolsToolStripMenuItem;
private ToolStripMenuItem exitToolStripMenuItem;
private GroupBox groupBox3;
@ -334,5 +318,9 @@
private ToolStripMenuItem systemToolStripMenuItem;
private ToolStripMenuItem lightToolStripMenuItem;
private ToolStripMenuItem darkToolStripMenuItem;
private ToolStripMenuItem fontFamilyToolStripMenuItem;
private ToolStripMenuItem serifToolStripMenuItem;
private ToolStripMenuItem sansSerifToolStripMenuItem;
private ToolStripMenuItem forceMonospaceToolStripMenuItem;
}
}

View file

@ -1,7 +1,9 @@
using CefSharp;
using CefSharp.WinForms;
using Markdig;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Security.Policy;
namespace MintHTML
{
@ -167,6 +169,8 @@ namespace MintHTML
</svg>
";
string css;
bool extraInstance = false;
int extraNum = 0;
// Custom functions
private void convert()
{
@ -176,26 +180,31 @@ namespace MintHTML
public Form1()
{
if (File.Exists(appdata + "/SweeZero/MintHTML/RootCache/lockfile"))
if (File.Exists(appdata + "/SweeZero/MintHTML/Caches/Instance/lockfile"))
{
if (MessageBox.Show("There is another instance of MintHTML running. If there isn't, press Yes to try opening anyways.", "Cef Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
extraInstance = true;
extraNum = 1;
while (File.Exists(appdata + "/SweeZero/MintHTML/Caches/Instance" + extraNum.ToString() + "/lockfile"))
{
try
{
File.Delete(appdata + "/SweeZero/MintHTML/RootCache/lockfile");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Cef Error (fatal)", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
extraNum++;
}
var settings = new CefSettings()
{
RootCachePath = appdata + "/SweeZero/MintHTML/RootCache",
RootCachePath = appdata + "/SweeZero/MintHTML/Caches/Instance" + extraNum.ToString(),
WindowlessRenderingEnabled = true,
};
InitializeComponent();
Cef.Initialize(settings);
}
else
{
var settings = new CefSettings()
{
RootCachePath = appdata + "/SweeZero/MintHTML/Caches/Instance",
WindowlessRenderingEnabled = true,
};
Cef.Initialize(settings);
}
InitializeComponent();
css = @"<style>
html{
font-family: sans-serif
@ -231,7 +240,7 @@ font-family: sans-serif
DialogResult result = Saver.ShowDialog();
if (result == DialogResult.OK)
{
if (MessageBox.Show("Do you want to keep the current CSS settings?", "Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
if (MessageBox.Show("Do you want to convert the rendering settings to CSS?", "Converter", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
convert();
File.WriteAllText(Saver.FileName, css + htmlfile);
@ -251,12 +260,6 @@ font-family: sans-serif
Cef.Shutdown();
}
private void chromiumWebBrowser1_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
this.Invoke(new Action(() => progressBar1.Visible = chromiumWebBrowser1.IsLoading));
this.Invoke(new Action(() => openDevToolsToolStripMenuItem.Enabled = !chromiumWebBrowser1.IsLoading));
}
private void openDevToolsToolStripMenuItem_Click_1(object sender, EventArgs e)
{
chromiumWebBrowser1.ShowDevTools();
@ -332,5 +335,22 @@ font-family: monospace
{
}
private void renderToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void chromiumWebBrowser1_AddressChanged(object sender, AddressChangedEventArgs e)
{
if (!(chromiumWebBrowser1.Address.Substring(0, 5) == "data:"))
{
string address = chromiumWebBrowser1.Address;
chromiumWebBrowser1.Back();
if (MessageBox.Show("Open link?\n" + address, "Cef question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Process.Start(new ProcessStartInfo(address) { UseShellExecute = true });
}
}
}
}
}

View file

@ -1,3 +1,7 @@
using CefSharp;
using CefSharp.DevTools.Page;
using System.Net;
using System.Security.Cryptography.X509Certificates;
namespace MintHTML
{
internal static class Program
@ -14,4 +18,4 @@ namespace MintHTML
Application.Run(new Form1());
}
}
}
}