Skip to content

Commit

Permalink
Common: Add helper for creating an NSString from a string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
TellowKrinkle authored and stenzek committed Jan 26, 2024
1 parent 36e8194 commit d651334
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions common/CocoaTools.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

// MARK: - Metal Layers

static NSString*_Nonnull NSStringFromStringView(std::string_view sv)
{
return [[NSString alloc] initWithBytes:sv.data() length:sv.size() encoding:NSUTF8StringEncoding];
}

bool CocoaTools::CreateMetalLayer(WindowInfo* wi)
{
if (![NSThread isMainThread])
Expand Down Expand Up @@ -156,7 +161,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

std::optional<std::string> CocoaTools::MoveToTrash(std::string_view file)
{
NSURL* url = [NSURL fileURLWithPath:[[NSString alloc] initWithBytes:file.data() length:file.size() encoding:NSUTF8StringEncoding]];
NSURL* url = [NSURL fileURLWithPath:NSStringFromStringView(file)];
NSURL* new_url;
if (![[NSFileManager defaultManager] trashItemAtURL:url resultingItemURL:&new_url error:nil])
return std::nullopt;
Expand All @@ -170,7 +175,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
[task setExecutableURL:[NSURL fileURLWithPath:@"/bin/sh"]];
[task setEnvironment:@{
@"WAITPID": [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]],
@"LAUNCHAPP": [[NSString alloc] initWithBytes:file.data() length:file.size() encoding:NSUTF8StringEncoding],
@"LAUNCHAPP": NSStringFromStringView(file),
}];
[task setArguments:@[@"-c", @"while /bin/ps -p $WAITPID > /dev/null; do /bin/sleep 0.1; done; exec /usr/bin/open \"$LAUNCHAPP\";"]];
return [task launchAndReturnError:nil];
Expand All @@ -181,7 +186,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

bool CocoaTools::ShowInFinder(std::string_view file)
{
NSString* str = [[NSString alloc] initWithBytes:file.data() length:file.size() encoding:NSUTF8StringEncoding];
return [[NSWorkspace sharedWorkspace] selectFile:str
return [[NSWorkspace sharedWorkspace] selectFile:NSStringFromStringView(file)
inFileViewerRootedAtPath:nil];
}

0 comments on commit d651334

Please sign in to comment.