comparison foosdk/sdk/foobar2000/helpers-mac/fb2k-platform.mm @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "fb2k-platform.h"
2 #include "NSFont+pp.h"
3
4 namespace fb2k {
5 NSString * strToPlatform( const char * s, size_t len ) {
6 pfc::string8 temp( s, len );
7 return strToPlatform( temp );
8 }
9 NSString * strToPlatform( const char * arg, NSString * returnIfError ) {
10 if ( arg ) @try {
11 NSString * ret = [NSString stringWithUTF8String: arg];
12 if ( ret ) return ret;
13 } @catch(NSException *) {}
14 return returnIfError;
15 }
16 NSString * strToPlatform( const char * s ) {
17 return [NSString stringWithUTF8String: s];
18 }
19 NSString * strToPlatform( stringRef s ) {
20 if ( s.is_empty( ) ) return nil;
21 return strToPlatform( s->c_str() );
22 }
23 stringRef strFromPlatform( NSString * s ) {
24 return makeString( s.UTF8String );
25 }
26
27 stringRef urlFromPlatform( id obj ) {
28 if ( [obj isKindOfClass: [NSURL class] ] ) {
29 NSURL * URL = obj;
30 obj = URL.absoluteString;
31 if ([obj hasPrefix:@"file://"])
32 obj = URL.path;
33 }
34 if ( [obj respondsToSelector: @selector(UTF8String)]) {
35 pfc::string8 temp;
36 filesystem::g_get_canonical_path( [obj UTF8String], temp );
37 return makeString( temp );
38 }
39 return nullptr;
40 }
41
42 NSURL * urlToPlatform(const char * arg) {
43 pfc::string8 native;
44 if (filesystem::g_get_native_path(arg, native)) {
45 return [NSURL fileURLWithPath: [NSString stringWithUTF8String: native ] ];
46 }
47 return [NSURL URLWithString: [NSString stringWithUTF8String: arg]];
48 }
49
50 void openURL( const char * URL ) {
51 @autoreleasepool {
52 [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: [NSString stringWithUTF8String: URL]]];
53 }
54 }
55 void openWebBrowser( const char * URL) {
56 openURL(URL);
57 }
58
59 NSImage * imageToPlatform( fb2k::objRef obj ) {
60 fb2k::image::ptr img;
61 if ( img &= obj ) {
62 return (__bridge NSImage *) img->getNative();
63 }
64 return nil;
65 }
66 BOOL testFontParams(NSDictionary<NSString*, NSString*> * arg) {
67 return arg[@"font-name"] || arg[@"font-size"];
68 }
69 NSFont * fontFromParams(NSDictionary<NSString*, NSString*> * arg, NSFont * base) {
70 NSString * fontName = arg[@"font-name"];
71 NSString * fontSize = arg[@"font-size"];
72 NSFont * font = nil;
73 if ( fontName && fontSize ) {
74 font = [NSFont fontWithName: fontName size: fontSize.floatValue];
75 } else if ( fontName ) {
76 font = [NSFont fontWithName: fontName size: base ? base.pointSize : NSFont.systemFontSize];
77 } else if ( fontSize ) {
78 if ( base ) {
79 font = [ base fontWithSize: fontSize.floatValue ];
80 } else {
81 font = [NSFont monospacedDigitSystemFontOfSize: fontSize.floatValue weight: NSFontWeightRegular];
82 }
83 }
84 if ( font ) return font;
85 if ( base ) return base;
86 // Reuse shared font object
87 return [NSFont pp_monospacedDigitFont];
88 }
89 void tableViewPrepareForFont( NSTableView * tableView, NSFont * font ) {
90
91 // Must use NSTableViewRowSizeStyleCustom
92 // Other options either discard our font or break down with multiple cell templates used (autolayout)
93 // Height is fixed anyway, better precalculate it
94 assert( tableView.rowSizeStyle == NSTableViewRowSizeStyleCustom );
95
96 tableView.rowHeight = tableViewRowHeightForFont(font);
97 }
98 CGFloat tableViewRowHeightForFont( NSFont * f ) {
99 return (CGFloat) round( f.pointSize * 1.5 );
100 }
101 }
102
103 namespace pfc {
104 string8 strFromPlatform(NSString* str) {
105 string8 ret;
106 if ( str ) ret = str.UTF8String;
107 return ret;
108 }
109 NSString * strToPlatform( const char * str) {
110 return fb2k::strToPlatform( str );
111 }
112 NSString * strToPlatform(string8 const& str) {
113 return fb2k::strToPlatform( str.c_str() );
114 }
115 string8 strFromPlatform(CFStringRef str) {
116 return strFromPlatform( (__bridge NSString*) str );
117 }
118 }