Developer's Blog

Snow Leopard に対応しつつ、Lion の API を叩く

こんにちは、Sleipnir 3 for Mac 開発担当の宮本です。

OS X Lion では、スワイプ(スクロール)によるページ切り替えやフルスクリーンなど、魅力的な新機能が追加されました。 Sleipnir 3 for Mac では、これらの新機能をいち早く取り入れています。

ただ、これらの機能は10.7以降の API が必要なので、Snow Leopard では使えません。かといって切り捨てられないので、Lion だけ新しい API を使うようにしたいですね。 また、Lion では新しい API を使ってもビルドできますが、Snow Leopard ではできません。 Snow Leopard でも開発をしているので、これは問題です。

そこで、今回は Lion と Snow Leopard どちらでも動作して、なおかつどちらの OS でも開発できる方法を紹介します。

 

Lion で定義されている API が Snow Leopard では定義されていません。 普通にこれらの API を呼び出すコードがあれば警告やエラーがでてしまうので、同じものを Snow Leopard で定義してしまいます。やることはこれだけです。 警告とエラー防ぐためのものであって、Snow Leopard で呼び出されないようにコードは書くので実装は必要ありません。

#if !defined(MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7

@interface NSWindow (LionAPIs)
- (void)toggleFullScreen:(id)sender;
@end

enum {
  NSWindowCollectionBehaviorFullScreenPrimary = 1 << 7,
  NSWindowCollectionBehaviorFullScreenAuxiliary = 1 << 8
};

enum {
   NSFullScreenWindowMask = 1 << 14
};

enum {
  NSWindowFullScreenButton = 7
};

enum {     
    NSEventPhaseNone        = 0,
    NSEventPhaseBegan       = 0x1 << 0,
    NSEventPhaseStationary  = 0x1 << 1,
    NSEventPhaseChanged     = 0x1 << 2,
    NSEventPhaseEnded       = 0x1 << 3,
    NSEventPhaseCancelled   = 0x1 << 4,
}; typedef NSUInteger NSEventPhase;

enum {     
    NSEventSwipeTrackingLockDirection = 0x1 << 0,     
    NSEventSwipeTrackingClampGestureAmount = 0x1 << 1   
}; typedef NSUInteger NSEventSwipeTrackingOptions;

@interface NSEvent (LionAPIs)
+ (BOOL)isSwipeTrackingFromScrollEventsEnabled;
- (NSEventPhase)phase;
- (CGFloat)scrollingDeltaX;
- (CGFloat)scrollingDeltaY;
- (void)trackSwipeEventWithOptions:(NSEventSwipeTrackingOptions)options dampenAmountThresholdMin:(CGFloat)minDampenThreshold max:(CGFloat)maxDampenThreshold usingHandler:(void (^)(CGFloat gestureAmount, NSEventPhase phase, BOOL isComplete, BOOL *stop))trackingHandler;
@end

#endif  // MAC_OS_X_VERSION_10_7

あとは、こんなものを書いて、Lion と Snow Leopard で処理を分けるだけです。

#define LION if (osVersion() == 10.7)
#define ELSE else
LION {
    [win toggleFullScreen:nil];
}

今回は Mac の話でしたが、iOS でも同じです。下位互換を保ちつつ、新機能を取り入れたい時に使ってみてください。



Copyright © 2019 Fenrir Inc. All rights reserved.