public static ClipboardStateResponse GetResponse(Guid stateId) { ClipboardState state = AppState.Current.ClipboardStates.FirstOrDefault(x => x.Id == stateId); ClipboardItem item = state?.Items.FirstOrDefault( x => ClipboardParser.CanSerialize(ClipboardParser.GetAbstractFormat(x.Format))); if (state != null && item != null) { return(new ClipboardStateResponse { StateGuid = stateId, Buffer = item.GetDataBuffer(), Format = item.Format, DateTime = state.DateTime, IsError = false, }); } else { return(new ClipboardStateResponse { StateGuid = stateId, Buffer = null, Format = (DataFormat)0, DateTime = state.DateTime, IsError = true, }); } }
public static ClipboardInfoResponse GetResponse(List <Guid> knownStateGuids) { return(new ClipboardInfoResponse { States = (knownStateGuids != null ? AppState.Current.ClipboardStates.Where(x => !knownStateGuids.Contains(x.Id)) : AppState.Current.ClipboardStates) .Select(x => { ClipboardItem item = ClipboardParser.GetPreferredItem(x.Items, serializable: true); if (item != null) { return new ClipboardStateInfo { Id = x.Id, Size = item.Size, Format = ClipboardParser.GetAbstractFormat(item.Format), DateTime = x.DateTime, }; } else { return null; } }) .Where(x => x != null) .ToList(), }); }
private ClipboardViewModel CreateClipboardViewModel(ClipboardState state) { ClipboardItem item = ClipboardParser.GetPreferredItem(state.Items, serializable: false); if (item == null) { _logger.LogInfo(LogEvents.UnknClipbdFmt, state.Items.Select(x => new { x.Format, x.FormatName })); return(new UnknownViewModel( state, DateTime.Now, onPaste: OnPasteStateClicked, onDelete: OnDeleteStateClicked)); } switch (ClipboardParser.GetAbstractFormat(item.Format)) { case AbstractDataFormat.Image: return(new ImageViewModel( state, DateTime.Now, onPaste: OnPasteStateClicked, onDelete: OnDeleteStateClicked) { Image = ClipboardParser.ParseImage(item), }); case AbstractDataFormat.Text: var text = ClipboardParser.ParseText(item); string source = text.Trim(); if (source.Length < 50 && ColorParser.TryParse(source, out Color color)) { Color textColor = (color.R + color.G + color.B) * (float)color.A / 255 / 3 >= 127 ? System.Windows.Media.Colors.Black : System.Windows.Media.Colors.White; return(new ColorViewModel( state, DateTime.Now, onPaste: OnPasteStateClicked, onDelete: OnDeleteStateClicked) { BackgroundColor = new SolidColorBrush(color), TextColor = new SolidColorBrush(textColor), Text = text.Trim(), }); } else { string textContent = PostProcessText(ClipboardParser.ParseText(item)); return(new TextViewModel( state, DateTime.Now, onPaste: OnPasteStateClicked, onDelete: OnDeleteStateClicked) { Text = textContent, }); } case AbstractDataFormat.FileDrop: string[] items = ClipboardParser.ParseFileDrop(item) .Where(x => File.Exists(x) || Directory.Exists(x)) .ToArray(); return(new FileDropViewModel( state, DateTime.Now, onPaste: OnPasteStateClicked, onDelete: OnDeleteStateClicked) { Items = items, }); default: _logger.LogInfo(LogEvents.UnknClipbdFmt, state.Items.Select(x => new { x.Format, x.FormatName })); return(new UnknownViewModel( state, DateTime.Now, onPaste: OnPasteStateClicked, onDelete: OnDeleteStateClicked)); } }