Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,61 @@ private void SampleCodePresenter_Loaded(object sender, RoutedEventArgs e)
{
if (CodePresenter?.ContextMenu != null)
{
// Adjust context menu to only show 'Copy' if there is a selection
// Local function to build centered tooltip similar to library TextContextMenu style.
ToolTip BuildCenteredMouseToolTip(string text)
{
var tt = new ToolTip
{
Content = text,
Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse
};
tt.Opened += (s2, e2) =>
{
if (s2 is ToolTip t)
{
t.HorizontalOffset = 0;
t.VerticalOffset = 0;
void ApplyOffsets()
{
const double gap = 32;
t.HorizontalOffset = -t.ActualWidth / 2.0;
t.VerticalOffset = -(t.ActualHeight + gap);
}

if (t.ActualWidth <= 0 || t.ActualHeight <= 0)
{
t.Dispatcher.BeginInvoke((Action)(() =>
{
ApplyOffsets();
}));
}
else
{
ApplyOffsets();
}
}
};
return tt;
}

// Apply tooltip/header customization.
foreach (var item in CodePresenter.ContextMenu.Items.OfType<MenuItem>())
{
if (item.Command == ApplicationCommands.Copy)
{
item.Header = "Copy";
item.ToolTip = BuildCenteredMouseToolTip("Copy the selected content to the clipboard");
ToolTipService.SetInitialShowDelay(item, 700);
}
else if (item.Command == ApplicationCommands.SelectAll)
{
item.Header = "Select All";
item.ToolTip = BuildCenteredMouseToolTip("Select all content");
ToolTipService.SetInitialShowDelay(item, 700);
}
}

// Adjust context menu to only show 'Copy' if there is a selection
CodePresenter.ContextMenu.Opened += (s, args) =>
{
var hasSelection = CodePresenter?.SelectionLength > 0;
Expand Down Expand Up @@ -254,4 +308,4 @@ private void FixAvalonEditScrolling()
}
}
}
}
}
57 changes: 56 additions & 1 deletion source/iNKORE.UI.WPF.Modern/Controls/TextContextMenu.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using iNKORE.UI.WPF.Modern.Common;
using iNKORE.UI.WPF.Modern.Common;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using iNKORE.UI.WPF.Modern.Controls.Helpers;
using System.Linq;
Expand Down Expand Up @@ -249,29 +249,84 @@ private void UpdateItems(Control target)
{
if (menuItem.Command is RoutedUICommand command)
{
// Local helper to build a tooltip that centers horizontally above the mouse cursor
ToolTip BuildCenteredMouseToolTip(string text)
{
var tt = new ToolTip
{
Content = text,
Placement = PlacementMode.Mouse
};
tt.Opened += (s, e) =>
{
if (s is ToolTip t)
{
t.HorizontalOffset = 0;
t.VerticalOffset = 0;
void ApplyOffsets()
{
const double gap = 32;
t.HorizontalOffset = -t.ActualWidth / 2.0;
t.VerticalOffset = -(t.ActualHeight + gap);
}

if (t.ActualWidth <= 0 || t.ActualHeight <= 0)
{
t.Dispatcher.BeginInvoke((System.Action)(() =>
{
ApplyOffsets();
}));
}
else
{
ApplyOffsets();
}
}
};
return tt;
}

if (command == ApplicationCommands.Cut)
{
menuItem.Header = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandLabelCut);
var cutDescription = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandDescriptionCut);
menuItem.ToolTip = BuildCenteredMouseToolTip(cutDescription);
ToolTipService.SetInitialShowDelay(menuItem, 700);
}
else if (command == ApplicationCommands.Copy)
{
menuItem.Header = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandLabelCopy);
var copyDescription = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandDescriptionCopy);
menuItem.ToolTip = BuildCenteredMouseToolTip(copyDescription);
ToolTipService.SetInitialShowDelay(menuItem, 700);
}
else if (command == ApplicationCommands.Paste)
{
menuItem.Header = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandLabelPaste);
var pasteDescription = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandDescriptionPaste);
menuItem.ToolTip = BuildCenteredMouseToolTip(pasteDescription);
ToolTipService.SetInitialShowDelay(menuItem, 700);
}
else if (command == ApplicationCommands.Undo)
{
menuItem.Header = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandLabelUndo);
var undoDescription = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandDescriptionUndo);
menuItem.ToolTip = BuildCenteredMouseToolTip(undoDescription);
ToolTipService.SetInitialShowDelay(menuItem, 700);
}
else if (command == ApplicationCommands.Redo)
{
menuItem.Header = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandLabelRedo);
var redoDescription = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandDescriptionRedo);
menuItem.ToolTip = BuildCenteredMouseToolTip(redoDescription);
ToolTipService.SetInitialShowDelay(menuItem, 700);
}
else if (command == ApplicationCommands.SelectAll)
{
menuItem.Header = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandLabelSelectAll);
var selectAllDescription = ResourceAccessor.GetLocalizedStringResource(SR_TextCommandDescriptionSelectAll);
menuItem.ToolTip = BuildCenteredMouseToolTip(selectAllDescription);
ToolTipService.SetInitialShowDelay(menuItem, 700);
}

menuItem.CommandTarget = target;
Expand Down
Loading