WIP: web parser + ui

This commit is contained in:
Ray 2021-09-17 16:37:01 +01:00
parent c00d42a7d8
commit 682af23f55
11 changed files with 540 additions and 147 deletions

View File

@ -128,6 +128,16 @@ namespace BookmarkManager {
} }
} }
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
internal static System.Drawing.Bitmap loading_block {
get {
object obj = ResourceManager.GetObject("loading_block", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to https://www.hiimray.co.uk/software-bookmark-manager. /// Looks up a localized string similar to https://www.hiimray.co.uk/software-bookmark-manager.
/// </summary> /// </summary>

View File

@ -139,6 +139,9 @@
<data name="hexagon" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="hexagon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\hexagon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>Resources\hexagon.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>
<data name="loading_block" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\loading-block.gif;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="url_help" xml:space="preserve"> <data name="url_help" xml:space="preserve">
<value>https://www.hiimray.co.uk/software-bookmark-manager</value> <value>https://www.hiimray.co.uk/software-bookmark-manager</value>
</data> </data>

View File

@ -5,6 +5,11 @@ using System;
using RyzStudio.Windows.Forms; using RyzStudio.Windows.Forms;
using bzit.bomg.Models; using bzit.bomg.Models;
using System.Drawing; using System.Drawing;
using BookmarkManager;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Net;
using System.IO;
namespace FizzyLauncher namespace FizzyLauncher
{ {
@ -18,21 +23,23 @@ namespace FizzyLauncher
private Label label2; private Label label2;
private Label label3; private Label label3;
private TMemoBox memoBox1; private TMemoBox memoBox1;
private TMemoBox memoBox2; private TMemoBox memoBox2;
private Label label4; private Label label4;
private TButtonTextBox textBox2; private TButtonTextBox textBox2;
private PictureBox pictureBox1; private PictureBox pictureBox1;
private PictureBox pictureBox2;
protected TreeNode treeNode = null;
private ToolTip toolTip1; private ToolTip toolTip1;
private System.ComponentModel.IContainer components; private System.ComponentModel.IContainer components;
protected bool isBusy = false;
protected string faviconAddress = null; protected string faviconAddress = null;
protected WebParser webParser = null;
protected WebClient webClient = null;
public BookmarkForm(BookmarkItemViewModel model) : base()
public BookmarkForm(BookmarkItem model) : base()
{ {
InitializeComponent(); InitializeComponent();
@ -61,7 +68,9 @@ namespace FizzyLauncher
this.textBox2 = new RyzStudio.Windows.ThemedForms.TButtonTextBox(); this.textBox2 = new RyzStudio.Windows.ThemedForms.TButtonTextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.pictureBox2 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// label1 // label1
@ -240,11 +249,25 @@ namespace FizzyLauncher
this.pictureBox1.TabIndex = 201; this.pictureBox1.TabIndex = 201;
this.pictureBox1.TabStop = false; this.pictureBox1.TabStop = false;
// //
// pictureBox2
//
this.pictureBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox2.BackColor = System.Drawing.Color.Transparent;
this.pictureBox2.ErrorImage = null;
this.pictureBox2.InitialImage = null;
this.pictureBox2.Location = new System.Drawing.Point(196, 469);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(32, 32);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBox2.TabIndex = 202;
this.pictureBox2.TabStop = false;
//
// BookmarkForm // BookmarkForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(384, 521); this.ClientSize = new System.Drawing.Size(384, 521);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox2);
this.Controls.Add(this.memoBox2); this.Controls.Add(this.memoBox2);
@ -261,6 +284,7 @@ namespace FizzyLauncher
this.Name = "BookmarkForm"; this.Name = "BookmarkForm";
this.Text = "Edit Bookmark"; this.Text = "Edit Bookmark";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -270,13 +294,23 @@ namespace FizzyLauncher
{ {
base.OnShown(e); base.OnShown(e);
this.DialogResult = System.Windows.Forms.DialogResult.None; this.DialogResult = DialogResult.None;
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (IsBusy)
{
e.Cancel = true;
}
} }
public BookmarkItemViewModel Model public BookmarkItem Model
{ {
get => new BookmarkItemViewModel() get => new BookmarkItem()
{ {
SiteName = textBox1.Text?.Trim() ?? string.Empty, SiteName = textBox1.Text?.Trim() ?? string.Empty,
SiteAddress = textBox2.Text?.Trim() ?? string.Empty, SiteAddress = textBox2.Text?.Trim() ?? string.Empty,
@ -288,6 +322,23 @@ namespace FizzyLauncher
public Image Favicon { get => pictureBox1.Image; } public Image Favicon { get => pictureBox1.Image; }
protected bool IsBusy
{
get => isBusy;
set
{
isBusy = value;
ThreadControl.SetValue(pictureBox2, (isBusy ? AppResource.loading_block : null));
ThreadControl.SetEnable(textBox1, !isBusy);
ThreadControl.SetEnable(textBox2, !isBusy);
ThreadControl.SetEnable(memoBox1 , !isBusy);
ThreadControl.SetEnable(memoBox2, !isBusy);
ThreadControl.SetEnable(button1, !isBusy);
}
}
private void button1_MouseClick(object sender, MouseEventArgs e) private void button1_MouseClick(object sender, MouseEventArgs e)
{ {
@ -295,9 +346,71 @@ namespace FizzyLauncher
this.Close(); this.Close();
} }
private void textBox2_OnButtonClick(object sender, EventArgs e) private async void textBox2_OnButtonClick(object sender, EventArgs e)
{ {
await Task.Run(() =>
{
if (IsBusy) return;
IsBusy = true;
if (webParser == null) webParser = new WebParser();
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
IsBusy = false;
return;
}
BookmarkItem rs = webParser.RetrieveDetails(textBox2.Text);
if (rs == null)
{
IsBusy = false;
return;
}
if (!string.IsNullOrWhiteSpace(rs.SiteName)) ThreadControl.SetText(textBox1, rs.SiteName);
if (!string.IsNullOrWhiteSpace(rs.SiteDescription)) ThreadControl.SetText(memoBox1, rs.SiteDescription);
if (string.IsNullOrWhiteSpace(rs.FaviconAddress))
{
ThreadControl.Clear(pictureBox1);
}
else
{
ThreadControl.SetValue(pictureBox1, RetrieveImage(rs.FaviconAddress));
}
IsBusy = false;
});
}
private Image RetrieveImage(string url)
{
if (string.IsNullOrWhiteSpace(url))
{
return null;
}
if (webClient == null) webClient = new WebClient();
webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
try
{
byte[] byteData = webClient.DownloadData(url);
if (!RyzStudio.IO.FileType.IsImage(byteData))
{
throw new Exception("Not a supported image");
}
Image img = Image.FromStream(new MemoryStream(byteData));
return new Bitmap(img, 16, 16);
}
catch (Exception)
{
return null;
}
} }
} }

View File

@ -49,7 +49,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Remove="Models\BookmarkItemModel.cs" />
<Compile Remove="Models\TileGroupModel.cs" /> <Compile Remove="Models\TileGroupModel.cs" />
<Compile Remove="Models\TileModel.cs" /> <Compile Remove="Models\TileModel.cs" />
<Compile Remove="NewForm.cs" /> <Compile Remove="NewForm.cs" />

View File

@ -37,7 +37,7 @@ namespace BookmarkManager
return Result.Create(false, "Could not read file, unexpected format"); return Result.Create(false, "Could not read file, unexpected format");
} }
List<BookmarkItemViewModel> rs = JsonConvert.DeserializeObject<List<BookmarkItemViewModel>>(sourceCode); List<BookmarkItem> rs = JsonConvert.DeserializeObject<List<BookmarkItem>>(sourceCode);
if (rs == null) if (rs == null)
{ {
return Result.Create(false, "Could not read file, incorrect format"); return Result.Create(false, "Could not read file, incorrect format");
@ -69,7 +69,7 @@ namespace BookmarkManager
public override Result Save(BookmarkTreeView treeview, string filename, string password) public override Result Save(BookmarkTreeView treeview, string filename, string password)
{ {
List<BookmarkItemViewModel> rs = treeview.GetBookmarkList(); List<BookmarkItem> rs = treeview.GetBookmarkList();
bool rv = SharpZipLib.CreateSingle(filename, password, "bookmarks.json", JsonConvert.SerializeObject(rs)); bool rv = SharpZipLib.CreateSingle(filename, password, "bookmarks.json", JsonConvert.SerializeObject(rs));

249
Classes/WebParser.cs Normal file
View File

@ -0,0 +1,249 @@
using bzit.bomg.Models;
using HtmlAgilityPack;
using RyzStudio.Net;
using System;
using System.Net;
namespace BookmarkManager
{
public class WebParser
{
protected HttpWeb webClient = null;
public BookmarkItem RetrieveDetails(string url)
{
string sourceCode = retrieveSourceCode(url);
if (string.IsNullOrWhiteSpace(sourceCode))
{
return null;
}
BookmarkItem rs = new BookmarkItem();
HtmlDocument document = new HtmlDocument();
document.LoadHtml(sourceCode);
rs.SiteName = parseSiteTitle(document);
rs.SiteAddress = url;
rs.SiteDescription = parseSiteDescription(document);
rs.FaviconAddress = parseSiteIcon(document);
// resolve relative URL
if (!string.IsNullOrWhiteSpace(rs.FaviconAddress))
{
Uri iconAddressURI;
bool rv = Uri.TryCreate(new Uri(url), rs.FaviconAddress, out iconAddressURI);
if (rv)
{
rs.FaviconAddress = iconAddressURI.ToString();
}
}
return rs;
}
protected string retrieveSourceCode(string url)
{
if (webClient == null) webClient = new HttpWeb();
string sourceCode;
try
{
int statusCode = webClient.GetResponse(out sourceCode, url);
if ((statusCode == 200) || (statusCode == 301) || (statusCode == 302))
{
return sourceCode;
}
}
catch (Exception)
{
return null;
}
return null;
}
protected string parseSiteDescription(HtmlDocument doc)
{
string rs = null;
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@name='description']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@property='og:description']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@name='twitter:description']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@property='og:description']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@itemprop='description']", "content", string.Empty);
}
return rs;
}
protected string parseSiteIcon(HtmlDocument doc)
{
string rs = null;
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//link[translate(@rel, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'shortcut icon']", "href", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//link[translate(@rel, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'icon']", "href", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//link[translate(@rel, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'apple-touch-icon']", "href", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//link[translate(@rel, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'apple-touch-icon-precomposed']", "href", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[translate(@property, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'og:image']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[translate(@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'twitter:image']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[translate(@property, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'og:image']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[translate(@itemprop, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'image']", "content", string.Empty);
}
//if (string.IsNullOrWhiteSpace(rs))
//{
// rs = "/favicon.ico";
//}
return rs;
}
protected string parseSiteTitle(HtmlDocument doc)
{
string rs = null;
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue(doc, "//title", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@property='og:title']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@name='twitter:title']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@property='og:site_name']", "content", string.Empty);
}
if (string.IsNullOrWhiteSpace(rs))
{
rs = parseTagValue_Attr(doc, "//meta[@itemprop='name']", "content", string.Empty);
}
return rs?.Trim() ?? string.Empty;
}
protected string parseTagValue(HtmlDocument doc, string xpath, string defaultValue = "")
{
HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(xpath);
if (hnc == null)
{
return defaultValue;
}
if (hnc.Count <= 0)
{
return defaultValue;
}
foreach (HtmlNode hn in hnc)
{
if (string.IsNullOrWhiteSpace(hn.InnerHtml))
{
continue;
}
string rs = WebUtility.HtmlDecode(hn.InnerHtml)?.Replace("\r", "")?.Replace("\n", " ")?.Trim();
if (string.IsNullOrWhiteSpace(rs))
{
continue;
}
return rs;
}
return defaultValue;
}
protected string parseTagValue_Attr(HtmlDocument doc, string xpath, string attr, string defaultValue = "")
{
HtmlNodeCollection hnc = doc.DocumentNode.SelectNodes(xpath);
if (hnc == null)
{
return defaultValue;
}
if (hnc.Count <= 0)
{
return defaultValue;
}
foreach (HtmlNode hn in hnc)
{
if (hn.Attributes[attr] == null)
{
continue;
}
if (string.IsNullOrWhiteSpace(hn.Attributes[attr].Value))
{
continue;
}
return System.Web.HttpUtility.HtmlDecode(hn.Attributes[attr].Value?.Trim());
}
return defaultValue;
}
}
}

View File

@ -701,7 +701,7 @@ namespace FizzyLauncher
} }
// add placeholder // add placeholder
TreeNode node = treeView1.AddItem(treeView1.SelectedNode, new BookmarkItemViewModel() TreeNode node = treeView1.AddItem(treeView1.SelectedNode, new BookmarkItem()
{ {
SiteName = "New Bookmark" SiteName = "New Bookmark"
}); });
@ -946,7 +946,7 @@ namespace FizzyLauncher
return; return;
} }
BookmarkItemViewModel viewModel = (BookmarkItemViewModel)node.Tag; BookmarkItem viewModel = (BookmarkItem)node.Tag;
if (viewModel == null) if (viewModel == null)
{ {
return; return;

View File

@ -3,7 +3,7 @@ using System.Text;
namespace bzit.bomg.Models namespace bzit.bomg.Models
{ {
public class BookmarkItemViewModel public class BookmarkItem
{ {
public string SiteName { get; set; } public string SiteName { get; set; }

BIN
Resources/loading-block.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -334,7 +334,6 @@ namespace RyzStudio.Windows.Forms
} }
} }
public static List<T> FindChildControl<T>(Control control) where T : Control public static List<T> FindChildControl<T>(Control control) where T : Control
{ {
List<T> rs = new List<T>(); List<T> rs = new List<T>();
@ -355,6 +354,24 @@ namespace RyzStudio.Windows.Forms
return rs; return rs;
} }
public static string GetSelectedValue(ListBox sender)
{
string rv = string.Empty;
if (sender.InvokeRequired)
{
sender.Invoke(new MethodInvoker(() => {
rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString();
}));
}
else
{
rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString();
}
return rv;
}
public static string GetText(Control control, bool doTrim = true) public static string GetText(Control control, bool doTrim = true)
{ {
string rv = string.Empty; string rv = string.Empty;
@ -373,6 +390,45 @@ namespace RyzStudio.Windows.Forms
return rv; return rv;
} }
public static int GetValue(NumericUpDown sender)
{
int rv = 0;
if (sender.InvokeRequired)
{
sender.Invoke(new MethodInvoker(() => {
rv = (int)sender.Value;
}));
}
else
{
rv = (int)sender.Value;
}
return rv;
}
public static bool IsChild(TreeNode dragNode, TreeNode dropNode)
{
TreeNode tn = dropNode;
while (true)
{
if (tn.Parent == null)
{
break;
}
if (tn.Equals(dragNode))
{
return true;
}
tn = tn.Parent;
}
return false;
}
public static void SetChecked(ToolStripMenuItem control, bool value) public static void SetChecked(ToolStripMenuItem control, bool value)
{ {
if (control.GetCurrentParent().InvokeRequired) if (control.GetCurrentParent().InvokeRequired)
@ -388,6 +444,50 @@ namespace RyzStudio.Windows.Forms
} }
} }
public static void SetClientHeight(Control control, int value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
control.ClientSize = new Size(control.ClientSize.Width, value);
}));
}
else
{
control.ClientSize = new Size(control.ClientSize.Width, value);
}
}
public static void SetClientSize(Control control, int width, int height) => SetClientSize(control, new Size(width, height));
public static void SetClientSize(Control control, Size value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
control.ClientSize = value;
}));
}
else
{
control.ClientSize = value;
}
}
public static void SetClientWidth(Control control, int value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
control.ClientSize = new Size(value, control.ClientSize.Height);
}));
}
else
{
control.ClientSize = new Size(value, control.ClientSize.Height);
}
}
public static void SetEnable(Control control, bool value) public static void SetEnable(Control control, bool value)
{ {
if (control.InvokeRequired) if (control.InvokeRequired)
@ -439,42 +539,6 @@ namespace RyzStudio.Windows.Forms
} }
} }
public static int GetValue(NumericUpDown sender)
{
int rv = 0;
if (sender.InvokeRequired)
{
sender.Invoke(new MethodInvoker(() => {
rv = (int)sender.Value;
}));
}
else
{
rv = (int)sender.Value;
}
return rv;
}
public static string GetSelectedValue(ListBox sender)
{
string rv = string.Empty;
if (sender.InvokeRequired)
{
sender.Invoke(new MethodInvoker(() => {
rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString();
}));
}
else
{
rv = (sender.SelectedItem == null) ? string.Empty : sender.SelectedItem.ToString();
}
return rv;
}
public static void SetHeight(Control control, int value) public static void SetHeight(Control control, int value)
{ {
if (control.InvokeRequired) if (control.InvokeRequired)
@ -503,36 +567,6 @@ namespace RyzStudio.Windows.Forms
} }
} }
public static void SetClientSize(Control control, int width, int height) => SetClientSize(control, new Size(width, height));
public static void SetClientSize(Control control, Size value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
control.ClientSize = value;
}));
}
else
{
control.ClientSize = value;
}
}
public static void SetClientHeight(Control control, int value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
control.ClientSize = new Size(control.ClientSize.Width, value);
}));
}
else
{
control.ClientSize = new Size(control.ClientSize.Width, value);
}
}
public static void SetSize(Control control, int width, int height) => SetSize(control, new Size(width, height)); public static void SetSize(Control control, int width, int height) => SetSize(control, new Size(width, height));
public static void SetSize(Control control, Size value) public static void SetSize(Control control, Size value)
@ -671,19 +705,5 @@ namespace RyzStudio.Windows.Forms
} }
} }
public static void SetClientWidth(Control control, int value)
{
if (control.InvokeRequired)
{
control.Invoke(new MethodInvoker(() => {
control.ClientSize = new Size(value, control.ClientSize.Height);
}));
}
else
{
control.ClientSize = new Size(value, control.ClientSize.Height);
}
}
} }
} }

View File

@ -39,14 +39,14 @@ namespace RyzStudio.Windows.Forms
return System.Web.HttpUtility.UrlEncode(value); return System.Web.HttpUtility.UrlEncode(value);
} }
public static BookmarkItemViewModel GetNodeModel(TreeNode node) public static BookmarkItem GetNodeModel(TreeNode node)
{ {
if (node == null) if (node == null)
{ {
return null; return null;
} }
return (BookmarkItemViewModel)node.Tag; return (BookmarkItem)node.Tag;
} }
public static string GetNodePath(TreeNode node) public static string GetNodePath(TreeNode node)
@ -90,7 +90,7 @@ namespace RyzStudio.Windows.Forms
} }
else else
{ {
if (node.Tag is BookmarkItemViewModel) if (node.Tag is BookmarkItem)
{ {
return NodeType.Page; return NodeType.Page;
} }
@ -259,27 +259,27 @@ namespace RyzStudio.Windows.Forms
return; return;
} }
TreeNode en = this.GetNodeAt(this.PointToClient(new Point(e.X, e.Y))); TreeNode node = this.GetNodeAt(this.PointToClient(new Point(e.X, e.Y)));
if (en == null) if (node == null)
{ {
return; return;
} }
if (IsNodeChild(draggingNode, en)) if (ThreadControl.IsChild(draggingNode, node))
{ {
return; return;
} }
TreeNode dn = draggingNode; TreeNode dn = draggingNode;
if (en.Tag == null) if (node.Tag == null)
{ {
dn.Parent.Nodes.Remove(dn); dn.Parent.Nodes.Remove(dn);
en.Nodes.Insert(0, dn); node.Nodes.Insert(0, dn);
} }
else else
{ {
en.Parent.Nodes.Remove(dn); node.Parent.Nodes.Remove(dn);
en.Parent.Nodes.Insert(en.Index + 1, dn); node.Parent.Nodes.Insert(node.Index + 1, dn);
} }
this.HasChanged = true; this.HasChanged = true;
@ -428,7 +428,7 @@ namespace RyzStudio.Windows.Forms
break; break;
case NodeType.Page: case NodeType.Page:
BookmarkItemViewModel viewModel = this.GetNodeModel(); BookmarkItem viewModel = this.GetNodeModel();
if (viewModel != null) if (viewModel != null)
{ {
try try
@ -612,33 +612,33 @@ namespace RyzStudio.Windows.Forms
// this.HasChanged = false; // this.HasChanged = false;
//} //}
public void AddItem(BookmarkItemViewModel viewModel) public void AddItem(BookmarkItem item)
{ {
int iconIndex = addIcon(viewModel); int iconIndex = addIcon(item);
TreeNode tn = new TreeNode(viewModel.SiteName, iconIndex, iconIndex); TreeNode tn = new TreeNode(item.SiteName, iconIndex, iconIndex);
tn.Tag = viewModel; tn.Tag = item;
tn.ToolTipText = viewModel.ToString(); tn.ToolTipText = item.ToString();
TreeNode tn2 = AddFolderPath(viewModel.TreeviewPath); TreeNode tn2 = AddFolderPath(item.TreeviewPath);
ThreadControl.Add(tn2, tn); ThreadControl.Add(tn2, tn);
this.HasChanged = true; this.HasChanged = true;
} }
public TreeNode AddItem(TreeNode treeNode, BookmarkItemViewModel viewModel) public TreeNode AddItem(TreeNode treeNode, BookmarkItem item)
{ {
if (treeNode == null) if (treeNode == null)
{ {
return null; return null;
} }
int iconIndex = addIcon(viewModel); int iconIndex = addIcon(item);
TreeNode tn = new TreeNode(viewModel.SiteName, iconIndex, iconIndex); TreeNode tn = new TreeNode(item.SiteName, iconIndex, iconIndex);
tn.Tag = viewModel; tn.Tag = item;
tn.ToolTipText = viewModel.ToString(); tn.ToolTipText = item.ToString();
treeNode.Nodes.Add(tn); treeNode.Nodes.Add(tn);
@ -734,16 +734,15 @@ namespace RyzStudio.Windows.Forms
} }
} }
public NodeType GetNodeType() => GetNodeType(this.SelectedNode); public NodeType GetNodeType() => GetNodeType(this.SelectedNode);
public string GetNodePath() => GetNodePath(this.SelectedNode); public string GetNodePath() => GetNodePath(this.SelectedNode);
public BookmarkItemViewModel GetNodeModel() => GetNodeModel(this.SelectedNode); public BookmarkItem GetNodeModel() => GetNodeModel(this.SelectedNode);
public List<BookmarkItemViewModel> GetBookmarkList() public List<BookmarkItem> GetBookmarkList()
{ {
List<BookmarkItemViewModel> rs = new List<BookmarkItemViewModel>(); List<BookmarkItem> rs = new List<BookmarkItem>();
if (this.Nodes.Count <= 0) if (this.Nodes.Count <= 0)
{ {
@ -856,26 +855,26 @@ namespace RyzStudio.Windows.Forms
public new void Sort() => Sort(this.SelectedNode); public new void Sort() => Sort(this.SelectedNode);
public void UpdateItem(TreeNode treeNode, BookmarkItemViewModel model, Image image) public void UpdateItem(TreeNode treeNode, BookmarkItem item, Image image)
{ {
if (treeNode == null) if (treeNode == null)
{ {
return; return;
} }
int iconIndex = addIcon(model); int iconIndex = addIcon(item);
treeNode.Text = model.SiteName; treeNode.Text = item.SiteName;
treeNode.ImageIndex = iconIndex; treeNode.ImageIndex = iconIndex;
treeNode.SelectedImageIndex = iconIndex; treeNode.SelectedImageIndex = iconIndex;
treeNode.Tag = model; treeNode.Tag = item;
treeNode.ToolTipText = model.ToString(); treeNode.ToolTipText = item.ToString();
this.HasChanged = true; this.HasChanged = true;
} }
protected int addIcon(BookmarkItemViewModel viewModel) protected int addIcon(BookmarkItem viewModel)
{ {
return (int)IconSet.Default; return (int)IconSet.Default;
} }
@ -973,28 +972,28 @@ namespace RyzStudio.Windows.Forms
return tn; return tn;
} }
protected bool IsNodeChild(TreeNode dragNode, TreeNode dropNode) //protected bool IsNodeChild(TreeNode dragNode, TreeNode dropNode)
{ //{
TreeNode tn = dropNode; // TreeNode tn = dropNode;
while (true) // while (true)
{ // {
if (tn.Parent == null) // if (tn.Parent == null)
{ // {
break; // break;
} // }
if (tn.Equals(dragNode)) // if (tn.Equals(dragNode))
{ // {
return true; // return true;
} // }
tn = tn.Parent; // tn = tn.Parent;
} // }
return false; // return false;
} //}
protected void TraverseBookmarkList(List<BookmarkItemViewModel> rs, TreeNode node) protected void TraverseBookmarkList(List<BookmarkItem> rs, TreeNode node)
{ {
foreach (TreeNode tn in node.Nodes) foreach (TreeNode tn in node.Nodes)
{ {
@ -1005,7 +1004,7 @@ namespace RyzStudio.Windows.Forms
} }
else if (nodeType == NodeType.Page) else if (nodeType == NodeType.Page)
{ {
BookmarkItemViewModel nodeTag = GetNodeModel(tn); BookmarkItem nodeTag = GetNodeModel(tn);
nodeTag.TreeviewPath = GetNodePath(tn); nodeTag.TreeviewPath = GetNodePath(tn);
if (nodeTag != null) if (nodeTag != null)