Skip to content

Commit

Permalink
部分 if 条件翻转以缩小代码嵌套层级
Browse files Browse the repository at this point in the history
一般是在 if 控制范围很大的情况下进行调整,如果控制范围小则不调整。
从第三方代码的 Demo 中复制的代码块不修改。
  • Loading branch information
calcitem authored and wmjordan committed Jan 15, 2022
1 parent a6ef580 commit a25ebdf
Show file tree
Hide file tree
Showing 24 changed files with 401 additions and 367 deletions.
53 changes: 28 additions & 25 deletions App/Common/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,37 @@ static char ToLowerAscii(char c) {
}

internal static bool CheckOverwrite(string targetFile) {
if (File.Exists(targetFile)) {
switch (__OverwriteMode) {
case OverwriteType.Prompt:
var r = Common.FormHelper.YesNoCancelBox(String.Join("\n", new string[] {
"是否覆盖目标文件?", targetFile, "\n按住 Shift 键重复此对话框的选择,本次操作不再弹出覆盖文件提示。"
}));
if (r == System.Windows.Forms.DialogResult.No) {
if (FormHelper.IsShiftKeyDown) {
__OverwriteMode = OverwriteType.Skip;
}
goto case OverwriteType.Skip;
}
else if (r == System.Windows.Forms.DialogResult.Cancel) {
throw new OperationCanceledException();
}
if (!File.Exists(targetFile)) {
return true;
}

switch (__OverwriteMode) {
case OverwriteType.Prompt:
var r = Common.FormHelper.YesNoCancelBox(String.Join("\n", new string[] {
"是否覆盖目标文件?", targetFile, "\n按住 Shift 键重复此对话框的选择,本次操作不再弹出覆盖文件提示。"
}));
if (r == System.Windows.Forms.DialogResult.No) {
if (FormHelper.IsShiftKeyDown) {
__OverwriteMode = OverwriteType.Overwrite;
__OverwriteMode = OverwriteType.Skip;
}
break;
case OverwriteType.Overwrite:
return true;
case OverwriteType.Skip:
Tracker.TraceMessage(Tracker.Category.ImportantMessage, "取消覆盖文件:" + targetFile);
return false;
default:
goto case OverwriteType.Prompt;
}
goto case OverwriteType.Skip;
}
else if (r == System.Windows.Forms.DialogResult.Cancel) {
throw new OperationCanceledException();
}
if (FormHelper.IsShiftKeyDown) {
__OverwriteMode = OverwriteType.Overwrite;
}
break;
case OverwriteType.Overwrite:
return true;
case OverwriteType.Skip:
Tracker.TraceMessage(Tracker.Category.ImportantMessage, "取消覆盖文件:" + targetFile);
return false;
default:
goto case OverwriteType.Prompt;
}

return true;
}

Expand Down
18 changes: 10 additions & 8 deletions App/Common/FilePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,16 @@ public FilePath AppendPathSeparator() {
/// <summary>删除路径尾部的 <see cref="Path.DirectorySeparatorChar"/> 或 <see cref="Path.AltDirectorySeparatorChar"/> 字符。</summary>
/// <returns>删除了尾部“\”字符的路径。</returns>
public FilePath TrimPathSeparator() {
if (_value != null) {
string p = _value;
int i;
for (i = p.Length - 1; i >= 0; i--) {
char c = p[i];
if (!Char.IsWhiteSpace(c) && IsDirectorySeparator(c) == false) {
return _value.Substring(0, i + 1);
}
if (_value == null) {
return Empty;
}

string p = _value;
int i;
for (i = p.Length - 1; i >= 0; i--) {
char c = p[i];
if (!Char.IsWhiteSpace(c) && IsDirectorySeparator(c) == false) {
return _value.Substring(0, i + 1);
}
}
return Empty;
Expand Down
18 changes: 10 additions & 8 deletions App/Functions/AutoBookmark/FontNameConditionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ public AutoBookmarkCondition Filter {
#endregion

private void ControlChanged(object sender, EventArgs e) {
if (_lock == false) {
if (sender == _FontNameBox) {
_filter.FontName = _FontNameBox.Text;
}
else if (sender == _FullMatchBox) {
_filter.MatchFullName = _FullMatchBox.Checked;
}
EditAdjustmentForm.UpdateFilter(this);
if (_lock) {
return;
}

if (sender == _FontNameBox) {
_filter.FontName = _FontNameBox.Text;
}
else if (sender == _FullMatchBox) {
_filter.MatchFullName = _FullMatchBox.Checked;
}
EditAdjustmentForm.UpdateFilter(this);
}
}
}
30 changes: 16 additions & 14 deletions App/Functions/AutoBookmark/TextConditionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ public AutoBookmarkCondition Filter {
#endregion

private void ControlChanged(object sender, EventArgs e) {
if (_lock == false) {
if (sender == _PatternBox) {
_filter.Pattern.Text = _PatternBox.Text;
}
else if (sender == _FullMatchBox) {
_filter.Pattern.FullMatch = _FullMatchBox.Checked;
}
else if (sender == _MatchCaseBox) {
_filter.Pattern.MatchCase = _MatchCaseBox.Checked;
}
else if (sender == _UseRegexBox) {
_filter.Pattern.UseRegularExpression = _UseRegexBox.Checked;
}
EditAdjustmentForm.UpdateFilter(this);
if (_lock) {
return;
}

if (sender == _PatternBox) {
_filter.Pattern.Text = _PatternBox.Text;
}
else if (sender == _FullMatchBox) {
_filter.Pattern.FullMatch = _FullMatchBox.Checked;
}
else if (sender == _MatchCaseBox) {
_filter.Pattern.MatchCase = _MatchCaseBox.Checked;
}
else if (sender == _UseRegexBox) {
_filter.Pattern.UseRegularExpression = _UseRegexBox.Checked;
}
EditAdjustmentForm.UpdateFilter(this);
}
}
}
13 changes: 7 additions & 6 deletions App/Functions/AutoBookmarkControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,15 @@ private void _LevelAdjustmentBox_ItemActivate(object sender, EventArgs e) {

private void _AddFilterMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) {
var c = EditAdjustmentForm.CreateCondition(e.ClickedItem.Name);
if (c != null) {
using (var dialog = new EditAdjustmentForm(new AutoBookmarkOptions.LevelAdjustmentOption { Condition = c })) {
if (dialog.ShowDialog() == DialogResult.OK && dialog.Filter.Condition != null) {
_LevelAdjustmentBox.AddObject(dialog.Filter);
}
if (c == null) {
return;
}

using (var dialog = new EditAdjustmentForm(new AutoBookmarkOptions.LevelAdjustmentOption { Condition = c })) {
if (dialog.ShowDialog() == DialogResult.OK && dialog.Filter.Condition != null) {
_LevelAdjustmentBox.AddObject(dialog.Filter);
}
}
}

}
}
20 changes: 11 additions & 9 deletions App/Functions/CustomButton/GlassButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,20 @@ protected override void OnMouseUp(MouseEventArgs e) {
/// <param name="mevent">A <see cref="T:System.Windows.Forms.MouseEventArgs" /> that contains the event data.</param>
protected override void OnMouseMove(MouseEventArgs mevent) {
base.OnMouseMove(mevent);
if (mevent.Button != MouseButtons.None) {
if (!ClientRectangle.Contains(mevent.X, mevent.Y)) {
if (isHovered) {
isHovered = false;
Invalidate();
}
}
else if (!isHovered) {
isHovered = true;
if (mevent.Button == MouseButtons.None) {
return;
}

if (!ClientRectangle.Contains(mevent.X, mevent.Y)) {
if (isHovered) {
isHovered = false;
Invalidate();
}
}
else if (!isHovered) {
isHovered = true;
Invalidate();
}
}

/// <summary>
Expand Down
93 changes: 49 additions & 44 deletions App/Functions/DocumentInspectorControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,41 @@ private void DocumentInspectorControl_OnLoad(object sender, EventArgs e) {
if (d.ImageKey != null) {
return d.ImageKey;
}
if (d.Type != PdfObjectType.Normal) {
switch (d.Type) {
case PdfObjectType.Trailer:
return __OpNameIcons["Document"];
case PdfObjectType.Root:
break;
case PdfObjectType.Pages:
return __OpNameIcons["Pages"];
case PdfObjectType.Page:
return __OpNameIcons["Page"];
case PdfObjectType.Image:
return __OpNameIcons["Image"];
case PdfObjectType.Outline:
return __OpNameIcons["Outline"];
case PdfObjectType.PageCommands:
return __OpNameIcons["PageCommands"];
case PdfObjectType.PageCommand:
if (d.ImageKey == null) {
var n = d.ExtensiveObject as string;
if ((n != null && __OpNameIcons.TryGetValue(n, out int ic))
|| (d.Name.StartsWith(Constants.ContentPrefix + ":") && __OpNameIcons.TryGetValue(d.Name, out ic))
) {
d.ImageKey = ic;
}
else {
d.ImageKey = __OpNameIcons["Null"];
}

if (d.Type == PdfObjectType.Normal) {
return GetImageKey(d);
}

switch (d.Type) {
case PdfObjectType.Trailer:
return __OpNameIcons["Document"];
case PdfObjectType.Root:
break;
case PdfObjectType.Pages:
return __OpNameIcons["Pages"];
case PdfObjectType.Page:
return __OpNameIcons["Page"];
case PdfObjectType.Image:
return __OpNameIcons["Image"];
case PdfObjectType.Outline:
return __OpNameIcons["Outline"];
case PdfObjectType.PageCommands:
return __OpNameIcons["PageCommands"];
case PdfObjectType.PageCommand:
if (d.ImageKey == null) {
var n = d.ExtensiveObject as string;
if ((n != null && __OpNameIcons.TryGetValue(n, out int ic))
|| (d.Name.StartsWith(Constants.ContentPrefix + ":") && __OpNameIcons.TryGetValue(d.Name, out ic))
) {
d.ImageKey = ic;
}
return d.ImageKey;
case PdfObjectType.Hidden:
return __OpNameIcons["Hidden"];
}
else {
d.ImageKey = __OpNameIcons["Null"];
}
}
return d.ImageKey;
case PdfObjectType.Hidden:
return __OpNameIcons["Hidden"];
}
return GetImageKey(d);
}
Expand Down Expand Up @@ -501,19 +504,21 @@ private void LoadDocument(string path) {

private void ShowDescription(string name, string description, string type) {
_DescriptionBox.Text = String.Empty;
if (String.IsNullOrEmpty(name) == false) {
_DescriptionBox.SetSelectionFontSize(13);
_DescriptionBox.SetSelectionBold(true);
_DescriptionBox.AppendText(name);
_DescriptionBox.SetSelectionFontSize(9);
if (type != null) {
_DescriptionBox.AppendText(Environment.NewLine);
_DescriptionBox.AppendText("类型:" + type);
}
if (description != null) {
_DescriptionBox.AppendText(Environment.NewLine);
_DescriptionBox.AppendText(description);
}
if (String.IsNullOrEmpty(name)) {
return;
}

_DescriptionBox.SetSelectionFontSize(13);
_DescriptionBox.SetSelectionBold(true);
_DescriptionBox.AppendText(name);
_DescriptionBox.SetSelectionFontSize(9);
if (type != null) {
_DescriptionBox.AppendText(Environment.NewLine);
_DescriptionBox.AppendText("类型:" + type);
}
if (description != null) {
_DescriptionBox.AppendText(Environment.NewLine);
_DescriptionBox.AppendText(description);
}
}

Expand Down
23 changes: 12 additions & 11 deletions App/Functions/FormState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Drawing;
using System.Drawing;
using System.Windows.Forms;

namespace PDFPatcher.Functions
Expand All @@ -16,16 +15,18 @@ public sealed class FormState
bool IsMaximized;

public void Maximize(Form targetForm) {
if (!IsMaximized) {
IsMaximized = true;
if (targetForm.WindowState == FormWindowState.Maximized) {
targetForm.WindowState = FormWindowState.Normal;
}
Save(targetForm);
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.TopMost = true;
targetForm.Bounds = Screen.FromControl(targetForm).Bounds;
if (IsMaximized) {
return;
}

IsMaximized = true;
if (targetForm.WindowState == FormWindowState.Maximized) {
targetForm.WindowState = FormWindowState.Normal;
}
Save(targetForm);
targetForm.FormBorderStyle = FormBorderStyle.None;
targetForm.TopMost = true;
targetForm.Bounds = Screen.FromControl(targetForm).Bounds;
}

void Save(Form targetForm) {
Expand Down
24 changes: 12 additions & 12 deletions App/Functions/FunctionControl.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using PDFPatcher.Common;

Expand All @@ -26,16 +24,18 @@ public void ExecuteCommand(ToolStripItem item) {
}

public virtual void ExecuteCommand(string commandName, params string[] parameters) {
if (Commands.OpenFile == commandName) {
// 将第一个文本框设置为文件路径
if (parameters.Length > 0 && String.IsNullOrEmpty(parameters[0]) == false
&& FileHelper.HasExtension(parameters[0], Constants.FileExtensions.Pdf)
) {
foreach (Control c in Controls) {
if (c is SourceFileControl i) {
i.Text = parameters[0];
return;
}
if (Commands.OpenFile != commandName) {
return;
}

// 将第一个文本框设置为文件路径
if (parameters.Length > 0 && String.IsNullOrEmpty(parameters[0]) == false
&& FileHelper.HasExtension(parameters[0], Constants.FileExtensions.Pdf)
) {
foreach (Control c in Controls) {
if (c is SourceFileControl i) {
i.Text = parameters[0];
return;
}
}
}
Expand Down
Loading

0 comments on commit a25ebdf

Please sign in to comment.