-
Notifications
You must be signed in to change notification settings - Fork 102
Clipboard helpers were added #15 #16
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
base: master
Are you sure you want to change the base?
Clipboard helpers were added #15 #16
Conversation
@prasannavl any thoughts? |
@Trapov, thank you for the PR. Sorry, was a busy week. Had to put this off until the weekend. Will have a look by the weekend. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestions
new EventLoop().Run(_wind); | ||
else | ||
{ | ||
throw new Exception("Can't subscribe to the clipboard"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably should throw bounded to context exceptions, like SubscribeException
?
private void ReleaseUnmanagedResources() | ||
{ | ||
//TODO: implement cancelation token | ||
_cancellationTokenSource.Cancel(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably somehow interact with the EventLoop().Run() method, just to stop it.
if (User32Methods.OpenClipboard(new IntPtr())) | ||
{ | ||
var handle = User32Methods.GetClipboardData((uint) ClipboardFormat.CF_BITMAP); | ||
data = GdiplusHelpers.CreateBitmap(handle) ?? throw new Exception("Can't read the bitmap"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably should throw bounded to context exceptions, like ClipboardReadException
?
WinApi/Gdiplus/Helpers.cs
Outdated
{ | ||
public static class GdiplusHelpers | ||
{ | ||
public static byte[] CreateBitmap(IntPtr hGlobal) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this method generates an image NOT in memory, maybe return the image itself(or filename, a handle, ect) and not bytes?
@prasannavl well, i know it's been a while ago since i opened the PR but i still want it to be merged. |
Hi @Trapov, I'm sorry I haven't been able to catch up on this. I've been struggling with some hand surgeries for the last few months, and have been completely off programming. Just slowly easing back in. Unfortunately, I'm still catching up with a lot of the backlog and it may take a while for me to look into the design. That being said, could you please make a separate PR for the PS: For future reference, I also suggest making separate PRs for direct Win32 items (Methods, Constants, etc), helpers, and library features. That helps in merging smaller isolated items without delays. :) |
@prasannavl sorry to hear that, get better. Ok, will do like you said. Also the feature is not nearly done, there are still some things that aren't tested and have bad design, i want to hear your opinion on those things, such us: cancellation token, getting an image from the cliboard and etc. |
@prasannavl ok, now this PR is solely dedicated to pinvoke/helper-methods and constants. I can make another one with the Win32Clipboard control, but it will depend from this branch, since it will use the clipboard functions, constants and helpers; not really sure if that's a great idea. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Trapov - While I do not want to discourage you -- as suggested, could you please isolate the Constants+Methods
(which are direct Win32 derivations) from Helpers
into separate PRs?
Contants + Methods, I can merge instantly. This way, helpers can be reverted later if needed. Bunching up the PRs make it difficult to do that.
WinApi/User32/Helpers.cs
Outdated
|
||
var result = User32Methods.SetClipboardData((uint)ClipboardFormat.CF_TEXT, allocatedMemory); | ||
|
||
if (result == IntPtr.Zero) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throwing exceptions on a native API that indicates an error result is a no no. Instead, a better design would be to make it return a bool if IntPtr is zero, so the application can act in whichever way it deems necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I'm not entirely sure about the symmetry of the APIs. PasteTextToClipboard
doesn't actually paste text, just data -- it's a byte array. But copying returns a string (that could be null). Too many caveats. It'll be confusing to use this API.
So, I think a better design would be PasteToClipboard
. And the GetFromClipboard
that's just the data again. And then have helper method GetUnicodeTextFromClipboard
, that always returns a valid string -- String.Empty
instead of null.
WinApi/User32/Helpers.cs
Outdated
{ | ||
var bytesLength = data.Length; | ||
var allocatedMemory = Marshal.AllocHGlobal(bytesLength); | ||
try |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try-finally is useless here. Marshal.Copy
is not going to throw. Infact AllocHGlobal
is the one that can throw that's outside the scope - though it's not needed to handle it. Just simple statements will do, since if OutOfMemory
is thrown, the application will have to react to it anyway.
WinApi/User32/Helpers.cs
Outdated
|
||
public static unsafe string GetUnicodeTextFromClipboard() | ||
{ | ||
var outString = default(string); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default of string is null. I'd try to be explicit, so there are less chances of confusion of a consumer thinking it'll always return a string, and it's more readable. Just don't initialise it instead, and move it into the if
statement. Just return a null in the end.
WinApi/User32/Helpers.cs
Outdated
var data = (char*)User32Methods.GetClipboardData((uint) ClipboardFormat.CF_UNICODETEXT); | ||
outString = new string(data); | ||
} | ||
User32Methods.CloseClipboard(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has to be moved above into the if statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@prasannavl, Oh, right, and in the TrySetClipboardData
method as well. Damn, didn't see it at first. Will update it shortly, sorry to keep you waiting.
WinApi/User32/Helpers.cs
Outdated
return outString; | ||
} | ||
|
||
public static unsafe string GetTextFromClipboard() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comments as the above method.
Thank you for the response.
@prasannavl I would do that, but since Will fix everything else. |
Yeah, Clipboard is not a simple API. It's handles a lot of conversions and complexities. In that case, what do you think about just focusing on text instead in the helpers. Just take a string input for |
Yeah, sounds good. |
Additionally, SetClipboardData could have an overload with the exact format constants. |
@prasannavl what do you think about those apis? |
This is great, actually 👍. I like it. Just thinking out loud here: Would it be better to have And also has the minor side advantage of getting nice and relevant autocomplete with or without fuzzy matching, when you type |
@prasannavl, yea that makes sense. Will do. |
@prasannavl done. You probably want to squash all my commits, or rebase, because they don't make much sense in the context of your repo history, imho. |
@prasannavl, any other problems with the pull request? |
@Trapov If you're still active, you should |
Few functions connected to the windows clipboard were added and a wrapper around it.
will update my pull request in the future, please don't merge yet. created it for the sake of knowing what im doing
Wasn't able to solve my problem with a cancelation token in Clipboard tho, maybe make EventLoop().Run() async? I know it should be wraped in the the using statement, but still.
Issue -> #15
22.06.18 Update:
The Win32Clipboard class was moved into a different PR