Skip to content

Commit 661827d

Browse files
calebedenCopilot
andcommitted
fix(chat): restore reliable composer context menu
Co-authored-by: Copilot App <[email protected]>
1 parent e925d30 commit 661827d

4 files changed

Lines changed: 332 additions & 24 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace OpenClawTray.Chat;
2+
3+
internal readonly record struct ChatComposerContextMenuState(
4+
bool ShowUndo,
5+
bool ShowRedo,
6+
bool ShowCut,
7+
bool ShowCopy,
8+
bool ShowPaste,
9+
bool ShowSelectAll,
10+
bool ShowEditSeparator,
11+
bool ShowSelectAllSeparator)
12+
{
13+
public static ChatComposerContextMenuState Project(
14+
bool canUndo,
15+
bool canRedo,
16+
bool hasSelection,
17+
bool canPaste,
18+
bool hasText)
19+
{
20+
var showEditSeparator = (canUndo || canRedo) && (hasSelection || canPaste);
21+
var showSelectAllSeparator =
22+
hasText && (canUndo || canRedo || hasSelection || canPaste);
23+
24+
return new ChatComposerContextMenuState(
25+
ShowUndo: canUndo,
26+
ShowRedo: canRedo,
27+
ShowCut: hasSelection,
28+
ShowCopy: hasSelection,
29+
ShowPaste: canPaste,
30+
ShowSelectAll: hasText,
31+
ShowEditSeparator: showEditSeparator,
32+
ShowSelectAllSeparator: showSelectAllSeparator);
33+
}
34+
}

‎src/OpenClaw.Tray.WinUI/Chat/OpenClawReactorChatRoot.cs‎

Lines changed: 164 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -603,31 +603,14 @@ public override Element Render()
603603
onAttachmentPasted.Current = props.OnAttachmentPasted;
604604
var pasteHandler = UseRef<TextControlPasteEventHandler>(async (_, args) =>
605605
{
606-
var clipboardContent = global::Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
607-
if (clipboardContent is null
608-
|| !clipboardContent.Contains(
609-
global::Windows.ApplicationModel.DataTransfer.StandardDataFormats.Bitmap))
610-
{
606+
if (GetBitmapClipboardContent() is not { } clipboardContent)
611607
return;
612-
}
613608

614609
// Paste is a synchronous routed event. Suppress the default text paste
615610
// before awaiting bitmap extraction so a multi-format clipboard cannot
616611
// insert text alongside the image attachment.
617612
args.Handled = true;
618-
try
619-
{
620-
var attachment = await TryReadImageFromClipboardAsync(clipboardContent);
621-
if (attachment is null)
622-
return;
623-
624-
onAttachmentPasted.Current(attachment);
625-
}
626-
catch (Exception ex)
627-
{
628-
OpenClawTray.Services.Logger.Debug(
629-
$"Reactor chat composer: clipboard image paste failed: {ex.Message}");
630-
}
613+
await PasteImageFromClipboardAsync(clipboardContent, onAttachmentPasted.Current);
631614
});
632615
var inputText = UseRef(text);
633616
var inputControl = UseRef<TextBox?>(null);
@@ -1140,8 +1123,20 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
11401123
control.Resources["TextControlBorderBrushFocused"] = transparent;
11411124
control.Resources["TextControlBorderBrushPointerOver"] = transparent;
11421125
})
1143-
.OnMount(control => ((TextBox)control).Paste += pasteHandler.Current)
1144-
.OnUnmount(control => ((TextBox)control).Paste -= pasteHandler.Current);
1126+
.OnMount(control =>
1127+
{
1128+
var textBox = (TextBox)control;
1129+
textBox.Paste += pasteHandler.Current;
1130+
textBox.ContextFlyout = CreateComposerContextFlyout(
1131+
textBox,
1132+
() => onAttachmentPasted.Current);
1133+
})
1134+
.OnUnmount(control =>
1135+
{
1136+
var textBox = (TextBox)control;
1137+
textBox.Paste -= pasteHandler.Current;
1138+
textBox.ContextFlyout = null;
1139+
});
11451140
UseEffect((Func<Action>)(() =>
11461141
{
11471142
if (inputControl.Current is { } anchor)
@@ -1745,6 +1740,154 @@ private static async Task ReceiveVoiceAsync(
17451740
};
17461741
}
17471742

1743+
private static global::Windows.ApplicationModel.DataTransfer.DataPackageView? GetBitmapClipboardContent()
1744+
{
1745+
try
1746+
{
1747+
var content = global::Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
1748+
return content is not null
1749+
&& content.Contains(
1750+
global::Windows.ApplicationModel.DataTransfer.StandardDataFormats.Bitmap)
1751+
? content
1752+
: null;
1753+
}
1754+
catch (System.Runtime.InteropServices.COMException ex)
1755+
{
1756+
OpenClawTray.Services.Logger.Debug(
1757+
$"Reactor chat composer: clipboard access failed: {ex.Message}");
1758+
return null;
1759+
}
1760+
}
1761+
1762+
private static MenuFlyout CreateComposerContextFlyout(
1763+
TextBox textBox,
1764+
Func<Action<ChatAttachment>> getOnAttachmentPasted)
1765+
{
1766+
var undoItem = CreateStandardMenuItem(
1767+
Microsoft.UI.Xaml.Input.StandardUICommandKind.Undo,
1768+
textBox.Undo);
1769+
var redoItem = CreateStandardMenuItem(
1770+
Microsoft.UI.Xaml.Input.StandardUICommandKind.Redo,
1771+
textBox.Redo);
1772+
var cutItem = CreateStandardMenuItem(
1773+
Microsoft.UI.Xaml.Input.StandardUICommandKind.Cut,
1774+
textBox.CutSelectionToClipboard);
1775+
var copyItem = CreateStandardMenuItem(
1776+
Microsoft.UI.Xaml.Input.StandardUICommandKind.Copy,
1777+
textBox.CopySelectionToClipboard);
1778+
var pasteItem = CreateStandardMenuItem(
1779+
Microsoft.UI.Xaml.Input.StandardUICommandKind.Paste,
1780+
() =>
1781+
{
1782+
if (GetBitmapClipboardContent() is { } clipboardContent)
1783+
_ = PasteImageFromClipboardAsync(clipboardContent, getOnAttachmentPasted());
1784+
else
1785+
PasteTextFromClipboard(textBox);
1786+
});
1787+
var selectAllItem = CreateStandardMenuItem(
1788+
Microsoft.UI.Xaml.Input.StandardUICommandKind.SelectAll,
1789+
textBox.SelectAll);
1790+
Microsoft.UI.Xaml.Automation.AutomationProperties.SetAutomationId(
1791+
pasteItem,
1792+
"ChatComposerPasteMenuItem");
1793+
1794+
var editSeparator = new MenuFlyoutSeparator();
1795+
var selectAllSeparator = new MenuFlyoutSeparator();
1796+
var menu = new MenuFlyout();
1797+
menu.Items.Add(undoItem);
1798+
menu.Items.Add(redoItem);
1799+
menu.Items.Add(editSeparator);
1800+
menu.Items.Add(cutItem);
1801+
menu.Items.Add(copyItem);
1802+
menu.Items.Add(pasteItem);
1803+
menu.Items.Add(selectAllSeparator);
1804+
menu.Items.Add(selectAllItem);
1805+
menu.Opening += (_, _) =>
1806+
{
1807+
var state = ChatComposerContextMenuState.Project(
1808+
textBox.CanUndo,
1809+
textBox.CanRedo,
1810+
textBox.SelectionLength > 0,
1811+
ClipboardContainsPasteContent(),
1812+
!string.IsNullOrEmpty(textBox.Text));
1813+
undoItem.Visibility = ToVisibility(state.ShowUndo);
1814+
redoItem.Visibility = ToVisibility(state.ShowRedo);
1815+
cutItem.Visibility = ToVisibility(state.ShowCut);
1816+
copyItem.Visibility = ToVisibility(state.ShowCopy);
1817+
pasteItem.Visibility = ToVisibility(state.ShowPaste);
1818+
selectAllItem.Visibility = ToVisibility(state.ShowSelectAll);
1819+
editSeparator.Visibility = ToVisibility(state.ShowEditSeparator);
1820+
selectAllSeparator.Visibility = ToVisibility(state.ShowSelectAllSeparator);
1821+
};
1822+
return menu;
1823+
}
1824+
1825+
private static Visibility ToVisibility(bool visible) =>
1826+
visible ? Visibility.Visible : Visibility.Collapsed;
1827+
1828+
private static MenuFlyoutItem CreateStandardMenuItem(
1829+
Microsoft.UI.Xaml.Input.StandardUICommandKind kind,
1830+
Action execute)
1831+
{
1832+
var command = new Microsoft.UI.Xaml.Input.StandardUICommand(kind);
1833+
command.CanExecuteRequested += (_, args) => args.CanExecute = true;
1834+
command.ExecuteRequested += (_, _) => execute();
1835+
return new MenuFlyoutItem
1836+
{
1837+
Command = command,
1838+
Visibility = Visibility.Collapsed,
1839+
};
1840+
}
1841+
1842+
private static bool ClipboardContainsPasteContent()
1843+
{
1844+
try
1845+
{
1846+
var content = global::Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
1847+
return content is not null
1848+
&& (content.Contains(
1849+
global::Windows.ApplicationModel.DataTransfer.StandardDataFormats.Bitmap)
1850+
|| content.Contains(
1851+
global::Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text));
1852+
}
1853+
catch (System.Runtime.InteropServices.COMException ex)
1854+
{
1855+
OpenClawTray.Services.Logger.Debug(
1856+
$"Reactor chat composer: clipboard access failed: {ex.Message}");
1857+
return false;
1858+
}
1859+
}
1860+
1861+
private static void PasteTextFromClipboard(TextBox textBox)
1862+
{
1863+
try
1864+
{
1865+
textBox.PasteFromClipboard();
1866+
}
1867+
catch (System.Runtime.InteropServices.COMException ex)
1868+
{
1869+
OpenClawTray.Services.Logger.Debug(
1870+
$"Reactor chat composer: clipboard text paste failed: {ex.Message}");
1871+
}
1872+
}
1873+
1874+
private static async Task PasteImageFromClipboardAsync(
1875+
global::Windows.ApplicationModel.DataTransfer.DataPackageView clipboardContent,
1876+
Action<ChatAttachment> onAttachmentPasted)
1877+
{
1878+
try
1879+
{
1880+
var attachment = await TryReadImageFromClipboardAsync(clipboardContent);
1881+
if (attachment is not null)
1882+
onAttachmentPasted(attachment);
1883+
}
1884+
catch (Exception ex)
1885+
{
1886+
OpenClawTray.Services.Logger.Debug(
1887+
$"Reactor chat composer: clipboard image paste failed: {ex.Message}");
1888+
}
1889+
}
1890+
17481891
private static string PlaceholderFor(string connectionState) => connectionState switch
17491892
{
17501893
"connected" => Localized("Chat_Composer_Placeholder_Connected", "Message Assistant (Enter to send)"),

0 commit comments

Comments
 (0)