Skip to content

Commit e61a21c

Browse files
Merge pull request #95 from blackholeearth/circular_downloadProgress_atbutton
Circular download progress at dl button, ctrl click to open NewTab,
2 parents ee9fffa + 2796f78 commit e61a21c

File tree

10 files changed

+310
-78
lines changed

10 files changed

+310
-78
lines changed

src/Config/BrowserConfig.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CefSharp;
22
using CefSharp.WinForms;
3+
using System.Net;
34

45
namespace SharpBrowser.Config {
56
internal static class BrowserConfig {
@@ -101,19 +102,29 @@ internal static class BrowserConfig {
101102
public static bool TextAreaResize = true;
102103

103104

105+
//3way to set proxy, needs enum to reduce complication.
106+
public static ProxyMode currentProxyMode = ProxyMode.AutoDetect;
107+
//public static bool useSystemProxy = true;
104108

105109
/// <summary>
106110
/// If true then the following proxy is used for all browsing and downloads.
107111
/// </summary>
108-
public static bool Proxy = false;
112+
//public static bool customProxy = false;
109113
public static string ProxyIP = "123.123.123.123";
110114
public static int ProxyPort = 123;
111115
public static string ProxyUsername = "username";
112116
public static string ProxyPassword = "pass";
113117
public static string ProxyBypassList = "";
114118

115-
116-
119+
public enum ProxyMode
120+
{
121+
/// <summary>
122+
/// aka use system proxy
123+
/// </summary>
124+
AutoDetect,
125+
CustomProxy,
126+
NoProxy,
127+
}
117128

118129
/// <summary>
119130
/// Load the above config into the CEF `BrowserSettings` object.
@@ -157,13 +168,21 @@ public static void GetCefSettings(CefSettings settings) {
157168
settings.CefCommandLineArgs.Add("enable-media-stream", "1");
158169
}
159170

160-
// enable proxy if wanted
161-
if (Proxy) {
162-
CefSharpSettings.Proxy = new ProxyOptions(ProxyIP,
163-
ProxyPort.ToString(), ProxyUsername,
164-
ProxyPassword, ProxyBypassList);
171+
if (currentProxyMode == ProxyMode.AutoDetect)
172+
{
173+
//settings.CefCommandLineArgs.Add("proxy-auto-detect"); // or maybe do nothing.
174+
}
175+
else if (currentProxyMode == ProxyMode.CustomProxy)
176+
{
177+
// enable proxy if wanted
178+
CefSharpSettings.Proxy = new ProxyOptions(
179+
ProxyIP,
180+
ProxyPort.ToString(),
181+
ProxyUsername,ProxyPassword,
182+
ProxyBypassList);
165183
}
166-
else {
184+
else if (currentProxyMode == ProxyMode.NoProxy)
185+
{
167186

168187
// disable proxy if not wanted
169188
settings.CefCommandLineArgs.Add("no-proxy-server");

src/Controls/BorderedTextBox.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ public class BorderedTextBox : Panel
6060
private TextBox textBox;
6161
private bool focusedAlways = false;
6262
private Color normalBorderColor = Color.LightGray;
63-
//private Color focusedBorderColor = Color.FromArgb(86,156,214);
63+
//private Color focusedBorderColor = Color.FromArgb(86, 156, 214);
6464
//private Color focusedBorderColor = Color.FromArgb(0,00,225);
65-
private Color focusedBorderColor = Color.FromArgb(11, 87, 208);
65+
//private Color focusedBorderColor = Color.FromArgb(11, 87, 208);
66+
private Color focusedBorderColor = Color.FromArgb(153, 187, 239); //edge light blue
6667
public int borderThickness = 2;
6768

69+
int roundingRadius=19; //5 firefox, //19 full-round //15
70+
6871
public TextBox TextBox
6972
{
7073
get { return textBox; }
@@ -143,15 +146,16 @@ protected override void OnPaint(PaintEventArgs e)
143146
new Rectangle(0 + borderThickness, 0 + borderThickness,
144147
this.ClientSize.Width - borderThickness * 2,
145148
this.ClientSize.Height - borderThickness * 2)
146-
, 15);
149+
, roundingRadius);
150+
147151
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
148152

149153
//e.Graphics.DrawRectangle(borderPen,
150154
e.Graphics.DrawRoundRectangle(borderPen,
151155
new Rectangle(0+borderThickness, 0 + borderThickness,
152156
this.ClientSize.Width - borderThickness*2,
153157
this.ClientSize.Height - borderThickness*2)
154-
,15);
158+
, roundingRadius);
155159
}
156160
base.OnPaint(e);
157161
}

src/Controls/BrowserTabStrip/BrowserTabStrip.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class BrowserTabStrip : BaseStyledPanel, ISupportInitialize, IDisposabl
2222
private BrowserTabPage selectedItem;
2323
private ContextMenuStrip menu;
2424
private TabCloseButton closeButton;
25-
private TabNewButton newTabButton;
25+
private NewTabButton newTabButton;
2626
private BrowserTabStripItemCollection items;
2727

2828
private StringFormat DrawStringFormat;
@@ -121,7 +121,7 @@ public BrowserTabStrip() {
121121
menu.ItemClicked += OnMenuItemClicked;
122122
menu.VisibleChanged += OnMenuVisibleChanged;
123123
closeButton = new TabCloseButton(base.ToolStripRenderer);
124-
newTabButton = new TabNewButton(base.ToolStripRenderer);
124+
newTabButton = new NewTabButton(base.ToolStripRenderer);
125125
DrawStringFormat = new StringFormat();
126126
EndInit();
127127
UpdateLayout();
@@ -357,6 +357,7 @@ protected override void OnPaint(PaintEventArgs e) {
357357
}
358358

359359
//--------------------------------------------------------
360+
// DRAW BOTTOM LINE to tabButtons, Except Active Tab.
360361

361362
if (Items.DrawnCount == 0 || Items.VisibleCount == 0) {
362363
e.Graphics.DrawLine(Pens.Red, new Point(0, BrowserTabStyle.TabHeight), new Point(base.ClientRectangle.Width, BrowserTabStyle.TabHeight));
@@ -491,11 +492,12 @@ private void OnDrawTabButton(Graphics g, BrowserTabPage tab) {
491492
}
492493

493494
// draw tab text title
494-
// FIX: fix janky text rendering and bad kerning by using TextRenderer instead of DrawString
495-
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
496-
if (tab == SelectedTab) {
495+
if (tab == SelectedTab)
496+
{
497497
textRect.Width -= 25;
498498
}
499+
// FIX: fix janky text rendering and bad kerning by using TextRenderer instead of DrawString
500+
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
499501
TextRenderer.DrawText(g,tab.Title,font,Rectangle.Round(textRect),ForeColorSel,
500502
TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis
501503
);

src/Controls/BrowserTabStrip/Buttons/TabNewButton.cs renamed to src/Controls/BrowserTabStrip/Buttons/NewTabButton.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Windows.Forms;
44

55
namespace SharpBrowser.Controls.BrowserTabStrip.Buttons {
6-
internal class TabNewButton(ToolStripProfessionalRenderer renderer) : TabButtonBase(renderer) {
6+
internal class NewTabButton(ToolStripProfessionalRenderer renderer) : TabButtonBase(renderer) {
77

88
public override void Draw(Graphics g) {
99
if (IsVisible) {
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using SharpBrowser.Managers;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Drawing;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Windows.Forms;
9+
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
10+
11+
namespace SharpBrowser.Controls
12+
{
13+
14+
/// <summary>
15+
/// Draws Download Progress as Circle to Given Button..
16+
/// </summary>
17+
public class CircularDownloadProgress
18+
{
19+
20+
public CircularDownloadProgress(Button btnDL)
21+
{
22+
23+
init_downloads_indicator(btnDL);
24+
}
25+
//------ draw Circular downloading Progress
26+
27+
Button _btnDL;
28+
public void init_downloads_indicator(Button btnDL)
29+
{
30+
_btnDL = btnDL;
31+
btnDL.Paint += btnDL_Paint;
32+
var tmr_downloader = new System.Windows.Forms.Timer();
33+
tmr_downloader.Interval = 500;
34+
tmr_downloader.Tick += Tmr_downloader_Tick;
35+
tmr_downloader.Start();
36+
}
37+
38+
int testdl_pct = 0;
39+
private void Tmr_downloader_Tick(object sender, EventArgs e)
40+
{
41+
42+
bool isDownloading=false;
43+
bool needs_lastRefresh=false;
44+
try
45+
{
46+
isDownloading = DownloadManager.DownloadsInProgress();
47+
if (!isDownloading)
48+
{
49+
var curdlitemKV_val = DownloadManager.Downloads
50+
.Where(x => DateTime.Now.Subtract( x.Value.EndTime ?? DateTime.MinValue).TotalSeconds <3 )
51+
.Select(x=>x.Value)
52+
.FirstOrDefault();
53+
//var curdlitem = curdlitemKV.Value;
54+
if(curdlitemKV_val != null)
55+
needs_lastRefresh = true;
56+
}
57+
}
58+
catch (Exception ex) { }
59+
60+
if (isDownloading || needs_lastRefresh)
61+
_btnDL.Refresh();
62+
63+
//BtnDownloads.Invalidate();
64+
65+
66+
//testdl_pct = testdl_pct + 10;
67+
//if (testdl_pct > 100)
68+
// testdl_pct = 0;
69+
}
70+
71+
private void btnDL_Paint(object sender, PaintEventArgs e)
72+
{
73+
try
74+
{
75+
var isDownloading = DownloadManager.DownloadsInProgress();
76+
float pct_ofRecentDownloadingItem = 0;
77+
if (isDownloading)
78+
{
79+
var curdlitemKV = DownloadManager.Downloads.Where(x => x.Value.IsInProgress).FirstOrDefault();
80+
var curdlitem = curdlitemKV.Value;
81+
pct_ofRecentDownloadingItem = (int)(curdlitem.ReceivedBytes * 100.0f / curdlitem.TotalBytes);
82+
}
83+
84+
//var isDownloading = true;
85+
if (isDownloading)
86+
{
87+
//var pct1 = 100; // 100 %;
88+
//var pct2 = 50; // 50 %;
89+
//var pct3 =25; // 20 %;
90+
var pct = testdl_pct; //test val; // <<<<---- input download percentage HERE;;
91+
pct = (int)pct_ofRecentDownloadingItem;
92+
93+
var pctAs360val = pct / 100.0f * 360;
94+
var arc_StartOffset = 90;
95+
96+
//Color activeColorORG = Color.FromArgb(11, 87, 208);
97+
//Color activeLightColor = Color.FromArgb(76, 194, 255);
98+
Color activeColor = Color.FromArgb(27, 117, 208);
99+
int gray = 200;
100+
var myGray = Color.FromArgb(gray, gray, gray);
101+
102+
//var loc = BtnDownloads.Location;
103+
//var sz = BtnDownloads.Size;
104+
var loc = new Point(0, 0);
105+
var sz = _btnDL.ClientRectangle;
106+
var pad = 0;
107+
//var btng = BtnDownloads.CreateGraphics();
108+
109+
var thickness = 4;
110+
111+
var btng = e.Graphics;
112+
//var btng = BtnDownloads.CreateGraphics();
113+
btng.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
114+
115+
116+
var szHW_min = Math.Min(sz.Width, sz.Height);
117+
var szHW_max_minus_min = Math.Abs(sz.Width - sz.Height) / 2; //aka center it. vertical.
118+
Rectangle rect = new Rectangle(
119+
loc.X + pad + thickness / 2,
120+
loc.Y + pad + thickness / 2,
121+
szHW_min - 1 * pad - thickness / 2 * 2 - 1,
122+
szHW_min - 1 * pad - thickness / 2 * 2 - 1
123+
);
124+
125+
rect.Offset(szHW_max_minus_min, 0);
126+
//sz.Width - 1 * pad - thickness / 2 * 2 - 1,
127+
//sz.Height - 1 * pad - thickness / 2 * 2 - 1
128+
//);
129+
//btng.FillRectangle(new Pen(new SolidBrush(Color.Black), 10).Brush, rect);
130+
btng.DrawArc(new Pen(new SolidBrush(myGray), thickness), rect, 0 + arc_StartOffset, 360);
131+
btng.DrawArc(new Pen(new SolidBrush(activeColor), thickness), rect, 0 + arc_StartOffset, pctAs360val);
132+
133+
}
134+
}
135+
catch (Exception ex)
136+
{
137+
Console.WriteLine(ex + "ERROR at btnDL_Paint:");
138+
}
139+
}
140+
141+
142+
143+
}
144+
}

src/Controls/DrawingExt.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ public static Color ChangeColorBrightness(this Color color, float correctionFact
288288
return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
289289
}
290290

291+
/// <summary>
292+
/// Creates color with corrected brightness.
293+
/// </summary>
294+
/// <param name="color">Color to correct.</param>
295+
/// <param name="correctionFactor">The brightness correction factor. Must be between -1 and 1.
296+
/// Negative values produce darker colors.</param>
297+
/// <returns>
298+
/// Corrected <see cref="Color"/> structure.
299+
/// </returns>
291300
public static Color ChangeColorBrightness(this Color color, double correctionFactor)
292301
=> ChangeColorBrightness(color, (float)correctionFactor);
293302

src/Handlers/RequestHandler.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace SharpBrowser.Handlers {
88
internal class RequestHandler : IRequestHandler {
99
MainForm myForm;
1010

11+
public static bool settings__onCtrlClick_focusNewTab = true;
12+
1113
public RequestHandler(MainForm form) {
1214
myForm = form;
1315
}
@@ -107,6 +109,8 @@ public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFr
107109
public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback) {
108110
return true;
109111
}
112+
113+
110114
//
111115
// Summary:
112116
// Called on the UI thread before OnBeforeBrowse in certain limited cases where
@@ -132,8 +136,36 @@ public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, Cef
132136
// Returns:
133137
// Return true to cancel the navigation or false to allow the navigation to
134138
// proceed in the source browser's top-level frame.
135-
public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) {
139+
public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame,
140+
string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) {
141+
//return false;
142+
143+
// check if the user intended to open the link in a new tab
144+
// (Ctrl+Click often results in NewBackgroundTab or NewForegroundTab)
145+
if (targetDisposition == WindowOpenDisposition.NewForegroundTab ||
146+
targetDisposition == WindowOpenDisposition.NewBackgroundTab)
147+
{
148+
// Check if it was a user gesture (actual click)
149+
if (userGesture)
150+
{
151+
myForm.Invoke((MethodInvoker)(() => {
152+
myForm.AddNewBrowserTab(targetUrl,
153+
//focusNewTab: (targetDisposition == WindowOpenDisposition.NewForegroundTab),
154+
focusNewTab: settings__onCtrlClick_focusNewTab,
155+
browser.MainFrame.Url
156+
);
157+
}));
158+
159+
160+
//handled.. (dont load it in the current tab)
161+
return true;
162+
}
163+
}
164+
165+
// For all other cases, return false to let the default navigation proceed.
136166
return false;
167+
168+
137169
}
138170
//
139171
// Summary:

src/MainForm.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)