jisho NewTab 加入红 x 按钮,点击则关闭这个 tab
Browse files- src/WpfEditor/EchoDictWindow.xaml.cs +58 -2
- src/WpfEditor/readme.txt +12 -0
src/WpfEditor/EchoDictWindow.xaml.cs
CHANGED
|
@@ -162,12 +162,68 @@ namespace Echodict
|
|
| 162 |
browser.Address = url;
|
| 163 |
|
| 164 |
var tabItem = new TabItem();
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
tabItem.Content = browser;
|
| 167 |
|
| 168 |
browser.TitleChanged += (s, e) =>
|
| 169 |
{
|
| 170 |
-
Dispatcher.Invoke(() =>
|
| 171 |
};
|
| 172 |
|
| 173 |
MainTabControl.Items.Add(tabItem);
|
|
|
|
| 162 |
browser.Address = url;
|
| 163 |
|
| 164 |
var tabItem = new TabItem();
|
| 165 |
+
|
| 166 |
+
// Custom header with close button
|
| 167 |
+
var headerPanel = new DockPanel
|
| 168 |
+
{
|
| 169 |
+
Background = Brushes.Transparent // Ensure hit testing works on the whole panel
|
| 170 |
+
};
|
| 171 |
+
|
| 172 |
+
var textBlock = new TextBlock
|
| 173 |
+
{
|
| 174 |
+
Text = "Loading...",
|
| 175 |
+
VerticalAlignment = VerticalAlignment.Center
|
| 176 |
+
};
|
| 177 |
+
|
| 178 |
+
var closeButton = new Button
|
| 179 |
+
{
|
| 180 |
+
Content = "×",
|
| 181 |
+
Width = 16,
|
| 182 |
+
Height = 16,
|
| 183 |
+
FontSize = 12,
|
| 184 |
+
Padding = new Thickness(0, -2, 0, 0),
|
| 185 |
+
BorderThickness = new Thickness(0),
|
| 186 |
+
Background = Brushes.Transparent,
|
| 187 |
+
Foreground = Brushes.Red,
|
| 188 |
+
VerticalAlignment = VerticalAlignment.Center,
|
| 189 |
+
Margin = new Thickness(5, 0, 0, 0),
|
| 190 |
+
Visibility = Visibility.Hidden, // Initially hidden
|
| 191 |
+
ToolTip = "Close Tab",
|
| 192 |
+
FontWeight = FontWeights.Bold
|
| 193 |
+
};
|
| 194 |
+
|
| 195 |
+
// Close tab logic
|
| 196 |
+
closeButton.Click += (s, e) =>
|
| 197 |
+
{
|
| 198 |
+
try
|
| 199 |
+
{
|
| 200 |
+
if (tabItem.Content is ChromiumWebBrowser b)
|
| 201 |
+
{
|
| 202 |
+
b.Dispose();
|
| 203 |
+
}
|
| 204 |
+
MainTabControl.Items.Remove(tabItem);
|
| 205 |
+
e.Handled = true; // Prevent tab selection
|
| 206 |
+
}
|
| 207 |
+
catch (Exception ex)
|
| 208 |
+
{
|
| 209 |
+
System.Diagnostics.Debug.WriteLine($"Error closing tab: {ex.Message}");
|
| 210 |
+
}
|
| 211 |
+
};
|
| 212 |
+
|
| 213 |
+
// Toggle visibility on hover
|
| 214 |
+
headerPanel.MouseEnter += (s, e) => closeButton.Visibility = Visibility.Visible;
|
| 215 |
+
headerPanel.MouseLeave += (s, e) => closeButton.Visibility = Visibility.Hidden;
|
| 216 |
+
|
| 217 |
+
DockPanel.SetDock(closeButton, Dock.Right);
|
| 218 |
+
headerPanel.Children.Add(closeButton);
|
| 219 |
+
headerPanel.Children.Add(textBlock);
|
| 220 |
+
|
| 221 |
+
tabItem.Header = headerPanel;
|
| 222 |
tabItem.Content = browser;
|
| 223 |
|
| 224 |
browser.TitleChanged += (s, e) =>
|
| 225 |
{
|
| 226 |
+
Dispatcher.Invoke(() => textBlock.Text = e.NewValue?.ToString() ?? "New Tab");
|
| 227 |
};
|
| 228 |
|
| 229 |
MainTabControl.Items.Add(tabItem);
|
src/WpfEditor/readme.txt
CHANGED
|
@@ -119,6 +119,18 @@ Here are the changes:
|
|
| 119 |
|
| 120 |
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
|
| 124 |
|
|
|
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
+
`/e:/huggingface/imradv3/src/WpfEditor/EchodictWindow.xaml.cs#L158-175` 这个新开的 NewTab 要能被关闭,比如说鼠标移动到最上面的 tab 标题上,显示一个小的红色x按钮,点击则关闭并销毁这个 tab
|
| 123 |
+
|
| 124 |
+
I have updated EchodictWindow.xaml.cs to add a close button to the tabs.
|
| 125 |
+
|
| 126 |
+
1. Modified AddNewTab :
|
| 127 |
+
- Instead of setting a simple string as the header, I now create a DockPanel containing a TextBlock for the title and a Button for the close action.
|
| 128 |
+
- The close button is a small, transparent button with a red "×".
|
| 129 |
+
- Hover Behavior : The close button's Visibility is set to Hidden by default. I added MouseEnter and MouseLeave event handlers to the DockPanel to show/hide the button when the user hovers over the tab header.
|
| 130 |
+
- Close Logic : Clicking the button disposes of the browser instance and removes the tab from the TabControl .
|
| 131 |
+
This provides the requested functionality: moving the mouse over the tab title reveals a small red 'x' button that closes the tab when clicked.
|
| 132 |
+
|
| 133 |
+
|
| 134 |
|
| 135 |
|
| 136 |
|