Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/common/cocoa_tools.h
7448 views
1
// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <[email protected]>
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#ifndef __APPLE__
5
#error This file should only be included when compiling for MacOS.
6
#endif
7
8
#include "types.h"
9
10
#include <optional>
11
#include <span>
12
#include <string>
13
#include <string_view>
14
#include <utility>
15
16
class Error;
17
18
#ifdef __OBJC__
19
#import <AppKit/AppKit.h>
20
#import <Cocoa/Cocoa.h>
21
22
namespace CocoaTools {
23
24
NSString* StringViewToNSString(std::string_view str);
25
void NSErrorToErrorObject(Error* errptr, std::string_view message, NSError* error);
26
27
/// Converts NSError to a human-readable string.
28
std::string NSErrorToString(NSError* error);
29
30
} // namespace CocoaTools
31
32
#endif
33
34
namespace CocoaTools {
35
36
// Converts to Mach timebase.
37
u64 ConvertMachTimeBaseToNanoseconds(u64 ns);
38
u64 ConvertNanosecondsToMachTimeBase(u64 ns);
39
40
/// Moves a file from one location to another, using NSFileManager.
41
bool MoveFile(const char* source, const char* destination, Error* error);
42
43
/// Returns the bundle path.
44
std::optional<std::string> GetBundlePath();
45
46
/// Get the bundle path to the actual application without any translocation fun
47
std::optional<std::string> GetNonTranslocatedBundlePath();
48
49
/// Launch the given application once this one quits
50
bool DelayedLaunch(std::string_view file, std::span<const std::string_view> args = {});
51
52
/// Returns the size of a NSView in pixels.
53
std::optional<std::pair<int, int>> GetViewSizeInPixels(const void* view);
54
55
/// Returns the "real" scaling factor for a given view, on its current display.
56
std::optional<double> GetViewRealScalingFactor(const void* view);
57
58
/// Returns the refresh rate of the display the window is placed on.
59
std::optional<float> GetViewRefreshRate(const void* view, Error* error);
60
61
/// Creates metal layer on specified window surface.
62
void* CreateMetalLayer(void* view, Error* error);
63
64
/// Destroys metal layer on specified window surface.
65
void DestroyMetalLayer(void* view, void* layer);
66
67
} // namespace CocoaTools
68
69