Skip to content

Commit 89666df

Browse files
calebedenCopilot
andauthored
fix(chat): repeated image paste and right-click menu failures (#1113)
* fix(chat): preserve image paste after rerender Co-authored-by: Copilot App <[email protected]> * fix(chat): restore reliable composer context menu Co-authored-by: Copilot App <[email protected]> --------- Co-authored-by: Copilot App <[email protected]>
1 parent c9608c8 commit 89666df

4 files changed

Lines changed: 380 additions & 33 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: 175 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,19 @@ public override Element Render()
599599
var voiceCancellation = UseRef<CancellationTokenSource?>(null);
600600
var voiceOperation = UseRef(0);
601601
var voiceStopOperation = UseRef(0);
602-
var pasteHooked = UseRef(false);
602+
var onAttachmentPasted = UseRef<Action<ChatAttachment>>(props.OnAttachmentPasted);
603+
onAttachmentPasted.Current = props.OnAttachmentPasted;
604+
var pasteHandler = UseRef<TextControlPasteEventHandler>(async (_, args) =>
605+
{
606+
if (GetBitmapClipboardContent() is not { } clipboardContent)
607+
return;
608+
609+
// Paste is a synchronous routed event. Suppress the default text paste
610+
// before awaiting bitmap extraction so a multi-format clipboard cannot
611+
// insert text alongside the image attachment.
612+
args.Handled = true;
613+
await PasteImageFromClipboardAsync(clipboardContent, onAttachmentPasted.Current);
614+
});
603615
var inputText = UseRef(text);
604616
var inputControl = UseRef<TextBox?>(null);
605617
var slashPopup = UseRef<Microsoft.UI.Xaml.Controls.Primitives.Popup?>(null);
@@ -1110,38 +1122,20 @@ void CommitSlashText(string value, ReactorSlashMenuState nextState)
11101122
control.Resources["TextControlBorderBrush"] = transparent;
11111123
control.Resources["TextControlBorderBrushFocused"] = transparent;
11121124
control.Resources["TextControlBorderBrushPointerOver"] = transparent;
1113-
if (!pasteHooked.Current)
1114-
{
1115-
pasteHooked.Current = true;
1116-
control.Paste += async (_, args) =>
1117-
{
1118-
var clipboardContent = global::Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
1119-
if (clipboardContent is null
1120-
|| !clipboardContent.Contains(
1121-
global::Windows.ApplicationModel.DataTransfer.StandardDataFormats.Bitmap))
1122-
{
1123-
return;
1124-
}
1125-
1126-
// Paste is a synchronous routed event. Suppress the default text paste
1127-
// before awaiting bitmap extraction so a multi-format clipboard cannot
1128-
// insert text alongside the image attachment.
1129-
args.Handled = true;
1130-
try
1131-
{
1132-
var attachment = await TryReadImageFromClipboardAsync(clipboardContent);
1133-
if (attachment is null)
1134-
return;
1135-
1136-
props.OnAttachmentPasted(attachment);
1137-
}
1138-
catch (Exception ex)
1139-
{
1140-
OpenClawTray.Services.Logger.Debug(
1141-
$"Reactor chat composer: clipboard image paste failed: {ex.Message}");
1142-
}
1143-
};
1144-
}
1125+
})
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;
11451139
});
11461140
UseEffect((Func<Action>)(() =>
11471141
{
@@ -1746,6 +1740,154 @@ private static async Task ReceiveVoiceAsync(
17461740
};
17471741
}
17481742

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+
17491891
private static string PlaceholderFor(string connectionState) => connectionState switch
17501892
{
17511893
"connected" => Localized("Chat_Composer_Placeholder_Connected", "Message Assistant (Enter to send)"),

0 commit comments

Comments
 (0)