Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/plans/186-cross-platform-filemaker-clipboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# FileMaker clipboard mapping (#186)

Local implementation on `fuzzz/186-cross-platform-filemaker-clipboard`.
No publishing or PR without permission.

## Fix

Keep one explicit bidirectional mapping in `ClipboardService`, using the supplied
FileMaker calculation and the seven downloaded clipboard captures:

| Existing logical type | macOS clipboard key |
| --- | --- |
| `Mac-XMSC` (scripts) | `dyn.ah62d4rv4gk8zuxnxkq` |
| `Mac-XMSS` (script steps) | `dyn.ah62d4rv4gk8zuxnxnq` |
| `Mac-XMTB` (tables) | `dyn.ah62d4rv4gk8zuxnykk` |
| `Mac-XMFD` (fields) | `dyn.ah62d4rv4gk8zuxngku` |
| `Mac-XML2` (layout objects) | `dyn.ah62d4rv4gk8zuxnqgk` |
| `Mac-XMFN` (custom functions) | `dyn.ah62d4rv4gk8zuxngm2` |
| `Mac-XMVL` (value lists; from ZIP) | `dyn.ah62d4rv4gk8zuxn0mu` |

Read actual Avalonia item descriptors: map incoming keys to logical IDs and accept
macOS XML strings directly. Keep the existing prefixed-byte conversion for Windows.
Write current XML as a string under the mapped key on macOS; otherwise keep the
existing logical identifier and Windows wire bytes. Unknown macOS output types fail
through existing error handling. Normal copy and both script-copy commands use this
service; naming, grouping, editors, persistence, plain-text copy, and diagnostics
remain unchanged.

No native interop, UTI resolver, custom decoder/validation policy, warning/result
framework, Problems-panel changes, or additional clipboard infrastructure.

## Checks

All seven captured XML strings are fixtures under
`tests/SharpFM.Tests/Fixtures/FileMakerClipboard/`. Test mappings both ways, Windows
non-ASCII byte lengths, runtime output selection without a prior read, and the three
copy commands' wrapper choices. Full solution: 2,390 tests pass locally.

Still manually validate FileMaker paste/re-copy on Windows and macOS before claiming
platform support. The ZIPs establish inbound strings, not successful outbound paste.
72 changes: 67 additions & 5 deletions src/SharpFM/Services/ClipboardService.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,83 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Input.Platform;
using SharpFM.Model;

namespace SharpFM.Services;

[ExcludeFromCodeCoverage]
public class ClipboardService : IClipboardService
{
private readonly Window _window;
private static readonly Dictionary<string, string> MacFormats = new()
{
["Mac-XMSC"] = "dyn.ah62d4rv4gk8zuxnxkq",
["Mac-XMSS"] = "dyn.ah62d4rv4gk8zuxnxnq",
["Mac-XMTB"] = "dyn.ah62d4rv4gk8zuxnykk",
["Mac-XMFD"] = "dyn.ah62d4rv4gk8zuxngku",
["Mac-XML2"] = "dyn.ah62d4rv4gk8zuxnqgk",
["Mac-XMFN"] = "dyn.ah62d4rv4gk8zuxngm2",
["Mac-XMVL"] = "dyn.ah62d4rv4gk8zuxn0mu"
};

private readonly Window? _window;
private readonly Func<Task<IAsyncDataTransfer?>> _read;
private readonly Func<IAsyncDataTransfer, Task> _write;
private readonly bool _isMacOS;

public ClipboardService(Window window)
: this(() => GetClipboard(window).TryGetDataAsync(), data => GetClipboard(window).SetDataAsync(data), OperatingSystem.IsMacOS())
{
_window = window;
}

internal ClipboardService(Func<Task<IAsyncDataTransfer?>> read, Func<IAsyncDataTransfer, Task> write, bool isMacOS)
{
_read = read;
_write = write;
_isMacOS = isMacOS;
}

public async Task<IReadOnlyList<Clip>> ReadFileMakerAsync()
{
var clips = new List<Clip>();
using var transfer = await _read();
if (transfer is null) return clips;
foreach (var item in transfer.Items)
{
foreach (var format in item.Formats)
{
var logical = MacFormats.FirstOrDefault(pair => pair.Value == format.Identifier).Key ?? format.Identifier;
if (!logical.StartsWith("Mac-", StringComparison.OrdinalIgnoreCase)) continue;
// Use the exposed descriptor: macOS captures contain XML strings, not wire bytes.
var clip = await item.TryGetRawAsync(format) switch
{
string xml => Clip.FromXml("new-clip", logical, xml),
byte[] bytes => Clip.FromWireBytes("new-clip", logical, bytes),
_ => null
};
if (clip is not null) clips.Add(clip);
}
}
return clips;
}

public async Task WriteFileMakerAsync(Clip clip)
{
var transfer = new DataTransfer();
if (_isMacOS)
{
if (!MacFormats.TryGetValue(clip.FormatId, out var format))
throw new NotSupportedException("No macOS clipboard mapping exists for this clip type.");
transfer.Add(DataTransferItem.Create(DataFormat.CreateStringPlatformFormat(format), clip.Xml));
}
else
transfer.Add(DataTransferItem.Create(DataFormat.CreateBytesPlatformFormat(clip.FormatId), clip.WireBytes));
await _write(transfer);
}

public async Task SetTextAsync(string text)
{
var clipboard = GetClipboard();
Expand Down Expand Up @@ -49,6 +109,8 @@ public async Task<string[]> GetFormatsAsync()
return await transfer.TryGetValueAsync(dataFormat);
}

private IClipboard GetClipboard() =>
_window.Clipboard ?? throw new InvalidOperationException("Clipboard is not available.");
private IClipboard GetClipboard() => GetClipboard(_window);

private static IClipboard GetClipboard(Window? window) =>
window?.Clipboard ?? throw new InvalidOperationException("Clipboard is not available.");
}
4 changes: 4 additions & 0 deletions src/SharpFM/Services/IClipboardService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using SharpFM.Model;

namespace SharpFM.Services;

public interface IClipboardService
{
Task<IReadOnlyList<Clip>> ReadFileMakerAsync();
Task WriteFileMakerAsync(Clip clip);
Task SetTextAsync(string text);
Task SetDataAsync(string format, byte[] data);
Task<string[]> GetFormatsAsync();
Expand Down
39 changes: 15 additions & 24 deletions src/SharpFM/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public async Task PasteFileMakerClipData()
{
try
{
var formats = await _clipboard.GetFormatsAsync();
var clips = await _clipboard.ReadFileMakerAsync();
int count = 0;
ClipViewModel? lastAdded = null;

Expand All @@ -500,20 +500,14 @@ public async Task PasteFileMakerClipData()
// that's a selected clip's folder or an explicitly tapped empty one.
var pasteRoot = TargetFolderPath;

var recognized = new List<string>();
var unrecognized = new List<string>();
foreach (var format in formats.Distinct())
{
if (string.IsNullOrEmpty(format)) continue;
if (!format.StartsWith("Mac-", StringComparison.CurrentCultureIgnoreCase)) continue;
(ClipTypeRegistry.IsRegistered(format) ? recognized : unrecognized).Add(format);
}
var recognized = clips.Where(c => ClipTypeRegistry.IsRegistered(c.FormatId));
var unrecognized = clips.Where(c => !ClipTypeRegistry.IsRegistered(c.FormatId));

var batch = new PasteBatchState();

foreach (var format in recognized)
foreach (var clip in recognized)
{
var (added, last) = await PasteOneFormat(format, pasteRoot, batch);
var (added, last) = await PasteOneClip(clip, pasteRoot, batch);
lastAdded = last ?? lastAdded;
count += added;
if (batch.Cancelled) break;
Expand All @@ -525,16 +519,16 @@ public async Task PasteFileMakerClipData()
string? unknownFormatPasted = null;
if (count == 0 && !batch.Cancelled)
{
foreach (var format in unrecognized)
foreach (var clip in unrecognized)
{
var (added, last) = await PasteOneFormat(format, pasteRoot, batch);
var (added, last) = await PasteOneClip(clip, pasteRoot, batch);
if (added == 0) continue;
lastAdded = last ?? lastAdded;
count += added;
unknownFormatPasted = format;
unknownFormatPasted = clip.FormatId;
_logger.LogInformation(
"Pasted unknown format {Format}; will round-trip as raw XML.",
format);
clip.FormatId);
break;
}
}
Expand Down Expand Up @@ -564,15 +558,12 @@ public async Task PasteFileMakerClipData()
}
}

private async Task<(int added, ClipViewModel? last)> PasteOneFormat(
string format,
private async Task<(int added, ClipViewModel? last)> PasteOneClip(
Clip rawClip,
IReadOnlyList<string> pasteRoot,
PasteBatchState batch)
{
object? clipData = await _clipboard.GetDataAsync(format);
if (clipData is not byte[] dataObj) return (0, null);

var rawClip = Clip.FromWireBytes("new-clip", format, dataObj);
var format = rawClip.FormatId;
int added = 0;
ClipViewModel? last = null;

Expand Down Expand Up @@ -668,7 +659,7 @@ public async Task CopySelectedToClip()
try
{
data.HandleEditorContentChanged();
await _clipboard.SetDataAsync(data.ClipType, data.Clip.WireBytes);
await _clipboard.WriteFileMakerAsync(data.Clip);
ShowStatus("Copied to FileMaker clipboard");
}
catch (Exception e)
Expand Down Expand Up @@ -708,7 +699,7 @@ public async Task CopyAsScript()

var xmlWithWrapper = script.ToXml();
var clip = Clip.FromXml(data.Clip.Name, "Mac-XMSC", xmlWithWrapper);
await _clipboard.SetDataAsync("Mac-XMSC", clip.WireBytes);
await _clipboard.WriteFileMakerAsync(clip);
ShowStatus("Copied as Script to FileMaker clipboard");
}
catch (Exception e)
Expand Down Expand Up @@ -745,7 +736,7 @@ public async Task CopyAsScriptSteps()

var xmlNoWrapper = script.ToXml();
var clip = Clip.FromXml(data.Clip.Name, "Mac-XMSS", xmlNoWrapper);
await _clipboard.SetDataAsync("Mac-XMSS", clip.WireBytes);
await _clipboard.WriteFileMakerAsync(clip);
ShowStatus("Copied as Script Steps to FileMaker clipboard");
}
catch (Exception e)
Expand Down
2 changes: 2 additions & 0 deletions tests/SharpFM.Plugin.Tests/PluginHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace SharpFM.Plugin.Tests;

public class MockClipboardService : IClipboardService
{
public Task<IReadOnlyList<Clip>> ReadFileMakerAsync() => Task.FromResult<IReadOnlyList<Clip>>([]);
public Task WriteFileMakerAsync(Clip clip) => Task.CompletedTask;
public Task SetTextAsync(string text) => Task.CompletedTask;
public Task SetDataAsync(string format, byte[] data) => Task.CompletedTask;
public Task<string[]> GetFormatsAsync() => Task.FromResult(Array.Empty<string>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# These are exact UTF-8 exports; preserve embedded CR and LF on every platform.
*.xml -text
14 changes: 14 additions & 0 deletions tests/SharpFM.Tests/Fixtures/FileMakerClipboard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# FileMaker clipboard fixtures

Source: [#186 attachment](https://github.com/fuzzzerd/SharpFM/issues/186#issuecomment-6861883978),
`SharpFM_clipboard_snapshots_macOS_27.2.1_M4_aarch64.zip`.

The seven XML files are verbatim `payloads/0000.txt` exports from the corresponding
nested captures. They are Avalonia strings exported as UTF-8, not native wire bytes.
The filenames identify the types; optional descriptions were one capture behind.
The value-list capture adds `dyn.ah62d4rv4gk8zuxn0mu` -> `Mac-XMVL` to the supplied
FileMaker mapping. Other mappings are listed in the local plan and service tests.

Preserve original bytes/line endings (`.gitattributes`). Accompanying image/PDF
exports are excluded. Custom functions and value lists retain opaque XML handling.
These fixtures do not establish FileMaker output acceptance or editor fidelity.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<fmxmlsnippet type="FMObjectList"><CustomFunction id="1" functionArity="0" visible="True" parameters="" name="Test Function"><Calculation><![CDATA[Get ( FileName )]]></Calculation></CustomFunction></fmxmlsnippet>

Large diffs are not rendered by default.

Loading
Loading