WinXItem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using BulePointLilac.Controls;
  2. using BulePointLilac.Methods;
  3. using ContextMenuManager.Controls.Interfaces;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. namespace ContextMenuManager.Controls
  8. {
  9. sealed class WinXItem : MyListItem, IChkVisibleItem, IBtnShowMenuItem,
  10. ITsiTextItem, ITsiWebSearchItem, ITsiFilePathItem, ITsiDeleteItem, IFoldSubItem
  11. {
  12. private static readonly IWshRuntimeLibrary.WshShell WshShell = new IWshRuntimeLibrary.WshShell();
  13. public WinXItem(string filePath, IFoldGroupItem group)
  14. {
  15. InitializeComponents();
  16. this.FoldGroupItem = group;
  17. this.FilePath = filePath;
  18. }
  19. private string filePath;
  20. public string FilePath
  21. {
  22. get => filePath;
  23. set
  24. {
  25. filePath = value;
  26. this.Shortcut = WshShell.CreateShortcut(value);
  27. this.Text = this.ItemText;
  28. this.Image = this.ItemIcon.ToBitmap();
  29. ChkVisible.Checked = this.ItemVisible;
  30. }
  31. }
  32. public string ItemText
  33. {
  34. get
  35. {
  36. string name = Shortcut.Description.Trim();
  37. if(name == string.Empty) name = WinXList.GetMenuName(FilePath);
  38. if(name == string.Empty) name = Path.GetFileNameWithoutExtension(FilePath);
  39. return name;
  40. }
  41. set
  42. {
  43. Shortcut.Description = value;
  44. Shortcut.Save();
  45. ExplorerRestarter.NeedRestart = true;
  46. }
  47. }
  48. public bool ItemVisible
  49. {
  50. get => (File.GetAttributes(FilePath) & FileAttributes.Hidden) != FileAttributes.Hidden;
  51. set
  52. {
  53. FileAttributes attributes = File.GetAttributes(FilePath);
  54. if(value) attributes &= ~FileAttributes.Hidden;
  55. else attributes |= FileAttributes.Hidden;
  56. File.SetAttributes(FilePath, attributes);
  57. ExplorerRestarter.NeedRestart = true;
  58. }
  59. }
  60. private string IconLocation
  61. {
  62. get
  63. {
  64. if(Shortcut.IconLocation.StartsWith(","))
  65. Shortcut.IconLocation = $"{Shortcut.TargetPath}{Shortcut.IconLocation}";
  66. return Shortcut.IconLocation;
  67. }
  68. }
  69. private IWshRuntimeLibrary.IWshShortcut Shortcut;
  70. private Icon ItemIcon => ResourceIcon.GetIcon(IconLocation) ?? Icon.ExtractAssociatedIcon(Shortcut.TargetPath);
  71. public string SearchText => $"{AppString.SideBar.WinX} {Text}";
  72. public string ItemFilePath
  73. {
  74. get
  75. {
  76. if(File.Exists(Shortcut.TargetPath)) return Shortcut.TargetPath;
  77. else return FilePath;
  78. }
  79. }
  80. public IFoldGroupItem FoldGroupItem { get; set; }
  81. public VisibleCheckBox ChkVisible { get; set; }
  82. public MenuButton BtnShowMenu { get; set; }
  83. public ChangeTextMenuItem TsiChangeText { get; set; }
  84. public WebSearchMenuItem TsiSearch { get; set; }
  85. public FilePropertiesMenuItem TsiFileProperties { get; set; }
  86. public FileLocationMenuItem TsiFileLocation { get; set; }
  87. public DeleteMeMenuItem TsiDeleteMe { get; set; }
  88. readonly ToolStripMenuItem TsiDetails = new ToolStripMenuItem(AppString.Menu.Details);
  89. private void InitializeComponents()
  90. {
  91. BtnShowMenu = new MenuButton(this);
  92. ChkVisible = new VisibleCheckBox(this);
  93. TsiChangeText = new ChangeTextMenuItem(this);
  94. TsiSearch = new WebSearchMenuItem(this);
  95. TsiFileLocation = new FileLocationMenuItem(this);
  96. TsiFileProperties = new FilePropertiesMenuItem(this);
  97. TsiDeleteMe = new DeleteMeMenuItem(this);
  98. ContextMenuStrip.Items.AddRange(new ToolStripItem[] { TsiChangeText,
  99. new ToolStripSeparator(), TsiDetails, new ToolStripSeparator(), TsiDeleteMe });
  100. TsiDetails.DropDownItems.AddRange(new ToolStripItem[] { TsiSearch,
  101. new ToolStripSeparator(), TsiFileProperties, TsiFileLocation });
  102. }
  103. public void DeleteMe()
  104. {
  105. File.Delete(this.FilePath);
  106. this.Dispose();
  107. }
  108. }
  109. }