Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue where the clipboard content isn't preserved after shutting down the app. #24

Merged
merged 2 commits into from
Oct 10, 2021
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
12 changes: 12 additions & 0 deletions src/dev/impl/DevToys/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity

try
{
// Bug #22: Here we flush the Clipboard to make sure content in clipboard to remain available
// after the application shuts down.
Windows.ApplicationModel.DataTransfer.Clipboard.Flush();
}
catch (Exception)
{
// ignore
}

deferral.Complete();
}

Expand Down
3 changes: 2 additions & 1 deletion src/dev/impl/DevToys/UI/Controls/CodeEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ private void CopyButton_Click(object sender, RoutedEventArgs e)
};
data.SetText(Text ?? string.Empty);

Clipboard.SetContent(data);
Clipboard.SetContentWithOptions(data, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true });
Clipboard.Flush();
}

private async void OpenFileButton_Click(object sender, RoutedEventArgs e)
Expand Down
8 changes: 6 additions & 2 deletions src/dev/impl/DevToys/UI/Controls/CustomTextBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@
TextWrapping="{x:Bind AcceptsReturn, Mode=OneWay, Converter={StaticResource BooleanToTextWrappingConverter}}"
IsReadOnly="{x:Bind IsReadOnly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AcceptsReturn="{x:Bind AcceptsReturn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{x:Bind Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Text="{x:Bind Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CopyingToClipboard="TextBox_CopyingToClipboard"
CuttingToClipboard="TextBox_CuttingToClipboard"/>
<RichEditBox
x:Name="RichEditBox"
x:Load="{x:Bind IsRichTextEdit}"
Expand All @@ -111,7 +113,9 @@
IsSpellCheckEnabled="False"
TextWrapping="{x:Bind AcceptsReturn, Mode=OneWay, Converter={StaticResource BooleanToTextWrappingConverter}}"
IsReadOnly="{x:Bind IsReadOnly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AcceptsReturn="{x:Bind AcceptsReturn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
AcceptsReturn="{x:Bind AcceptsReturn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
CopyingToClipboard="RichEditBox_CopyingToClipboard"
CuttingToClipboard="RichEditBox_CuttingToClipboard">
<RichEditBox.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem
Expand Down
46 changes: 45 additions & 1 deletion src/dev/impl/DevToys/UI/Controls/CustomTextBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private bool CanExecuteCutCommand()
private void ExecuteCutCommand()
{
RichEditBox.TextDocument.Selection.Cut();
Clipboard.Flush();
}

#endregion
Expand All @@ -132,6 +133,7 @@ private bool CanExecuteCopyCommand()
private void ExecuteCopyCommand()
{
RichEditBox.TextDocument.Selection.Copy();
Clipboard.Flush();
}

#endregion
Expand Down Expand Up @@ -377,6 +379,23 @@ private RichEditBox GetRichEditBox()
return (RichEditBox)(RichEditBox ?? FindName(nameof(RichEditBox)));
}

private void CopyTextBoxSelectionToClipboard()
{
DataPackage dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
dataPackage.SetText(TextBox.SelectedText);
Clipboard.SetContentWithOptions(dataPackage, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true });
Clipboard.Flush(); // This method allows the content to remain available after the application shuts down.
}

private void CopyRichEditBoxSelectionToClipboard()
{
RichEditBox.Document.Selection.GetText(TextGetOptions.UseCrlf, out string text);
DataPackage dataPackage = new DataPackage { RequestedOperation = DataPackageOperation.Copy };
dataPackage.SetText(text);
Clipboard.SetContentWithOptions(dataPackage, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true });
Clipboard.Flush(); // This method allows the content to remain available after the application shuts down.
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
UpdateUI();
Expand Down Expand Up @@ -431,7 +450,8 @@ private void CopyButton_Click(object sender, RoutedEventArgs e)
};
data.SetText(Text);

Clipboard.SetContent(data);
Clipboard.SetContentWithOptions(data, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true });
Clipboard.Flush(); // This method allows the content to remain available after the application shuts down.
}

private async void OpenFileButton_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -484,6 +504,30 @@ private void RichEditBox_TextChanging(RichEditBox sender, RichEditBoxTextChangin
}
}

private void TextBox_CopyingToClipboard(TextBox sender, TextControlCopyingToClipboardEventArgs args)
{
CopyTextBoxSelectionToClipboard();
args.Handled = true;
}

private void TextBox_CuttingToClipboard(TextBox sender, TextControlCuttingToClipboardEventArgs args)
{
CopyTextBoxSelectionToClipboard();
args.Handled = true;
}

private void RichEditBox_CopyingToClipboard(RichEditBox sender, TextControlCopyingToClipboardEventArgs args)
{
CopyRichEditBoxSelectionToClipboard();
args.Handled = true;
}

private void RichEditBox_CuttingToClipboard(RichEditBox sender, TextControlCuttingToClipboardEventArgs args)
{
CopyRichEditBoxSelectionToClipboard();
args.Handled = true;
}

private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.NewSize.Width < CommandsToolBar.ActualWidth + 100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ private void ExecuteCopyVersionCommand()
};
data.SetText(Version);

Clipboard.SetContent(data);
Clipboard.SetContentWithOptions(data, new ClipboardContentOptions() { IsAllowedInHistory = true, IsRoamable = true });
Clipboard.Flush();
}

#endregion
Expand Down