|
| 1 | +#import <UIKit/UIKit.h> |
| 2 | +#import <Foundation/Foundation.h> |
| 3 | +#include <string> |
| 4 | +#include "../../display.h" |
| 5 | + |
| 6 | +namespace nativeapi { |
| 7 | + |
| 8 | +// Private implementation class |
| 9 | +class Display::Impl { |
| 10 | + public: |
| 11 | + Impl(UIScreen* screen) : ui_screen_(screen) {} |
| 12 | + UIScreen* ui_screen_; |
| 13 | +}; |
| 14 | + |
| 15 | +Display::Display() : pimpl_(std::make_unique<Impl>([UIScreen mainScreen])) {} |
| 16 | + |
| 17 | +Display::Display(void* display) : pimpl_(std::make_unique<Impl>((__bridge UIScreen*)display)) {} |
| 18 | + |
| 19 | +Display::Display(const Display& other) : pimpl_(std::make_unique<Impl>(other.pimpl_->ui_screen_)) {} |
| 20 | + |
| 21 | +Display& Display::operator=(const Display& other) { |
| 22 | + if (this != &other) { |
| 23 | + pimpl_->ui_screen_ = other.pimpl_->ui_screen_; |
| 24 | + } |
| 25 | + return *this; |
| 26 | +} |
| 27 | + |
| 28 | +Display::Display(Display&& other) noexcept : pimpl_(std::move(other.pimpl_)) {} |
| 29 | + |
| 30 | +Display& Display::operator=(Display&& other) noexcept { |
| 31 | + if (this != &other) { |
| 32 | + pimpl_ = std::move(other.pimpl_); |
| 33 | + } |
| 34 | + return *this; |
| 35 | +} |
| 36 | + |
| 37 | +Display::~Display() {} |
| 38 | + |
| 39 | +std::string Display::GetId() const { |
| 40 | + if (!pimpl_->ui_screen_) { |
| 41 | + return ""; |
| 42 | + } |
| 43 | + |
| 44 | + // Use the screen's bounds as a unique identifier |
| 45 | + CGRect bounds = pimpl_->ui_screen_.bounds; |
| 46 | + return std::to_string((int)bounds.origin.x) + "_" + std::to_string((int)bounds.origin.y); |
| 47 | +} |
| 48 | + |
| 49 | +std::string Display::GetName() const { |
| 50 | + if (!pimpl_->ui_screen_) { |
| 51 | + return "Unknown"; |
| 52 | + } |
| 53 | + |
| 54 | + // iOS doesn't provide a friendly name for screens |
| 55 | + // Use scale factor and size to differentiate |
| 56 | + CGFloat scale = pimpl_->ui_screen_.scale; |
| 57 | + CGRect bounds = pimpl_->ui_screen_.bounds; |
| 58 | + |
| 59 | + return std::string("Screen ") + std::to_string((int)bounds.size.width) + "x" + |
| 60 | + std::to_string((int)bounds.size.height) + "@" + std::to_string((int)scale) + "x"; |
| 61 | +} |
| 62 | + |
| 63 | +Point Display::GetPosition() const { |
| 64 | + if (!pimpl_->ui_screen_) { |
| 65 | + return Point{0, 0}; |
| 66 | + } |
| 67 | + |
| 68 | + CGRect bounds = pimpl_->ui_screen_.bounds; |
| 69 | + return Point{static_cast<double>(bounds.origin.x), static_cast<double>(bounds.origin.y)}; |
| 70 | +} |
| 71 | + |
| 72 | +Size Display::GetSize() const { |
| 73 | + if (!pimpl_->ui_screen_) { |
| 74 | + return Size{0, 0}; |
| 75 | + } |
| 76 | + |
| 77 | + CGRect bounds = pimpl_->ui_screen_.bounds; |
| 78 | + return Size{static_cast<double>(bounds.size.width), static_cast<double>(bounds.size.height)}; |
| 79 | +} |
| 80 | + |
| 81 | +Rectangle Display::GetWorkArea() const { |
| 82 | + if (!pimpl_->ui_screen_) { |
| 83 | + return Rectangle{0, 0, 0, 0}; |
| 84 | + } |
| 85 | + |
| 86 | + CGRect bounds = pimpl_->ui_screen_.bounds; |
| 87 | + return Rectangle{static_cast<double>(bounds.origin.x), static_cast<double>(bounds.origin.y), |
| 88 | + static_cast<double>(bounds.size.width), static_cast<double>(bounds.size.height)}; |
| 89 | +} |
| 90 | + |
| 91 | +double Display::GetScaleFactor() const { |
| 92 | + return pimpl_->ui_screen_ ? static_cast<double>(pimpl_->ui_screen_.scale) : 1.0; |
| 93 | +} |
| 94 | + |
| 95 | +bool Display::IsPrimary() const { |
| 96 | + return pimpl_->ui_screen_ == [UIScreen mainScreen]; |
| 97 | +} |
| 98 | + |
| 99 | +DisplayOrientation Display::GetOrientation() const { |
| 100 | + if (!pimpl_->ui_screen_) { |
| 101 | + return DisplayOrientation::kPortrait; |
| 102 | + } |
| 103 | + |
| 104 | + // Check orientation based on screen bounds |
| 105 | + CGRect bounds = pimpl_->ui_screen_.bounds; |
| 106 | + if (bounds.size.width > bounds.size.height) { |
| 107 | + return DisplayOrientation::kLandscape; |
| 108 | + } else { |
| 109 | + return DisplayOrientation::kPortrait; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +int Display::GetRefreshRate() const { |
| 114 | + if (!pimpl_->ui_screen_) { |
| 115 | + return 60; |
| 116 | + } |
| 117 | + |
| 118 | + // iOS doesn't expose refresh rate directly, return standard 60Hz |
| 119 | + return 60; |
| 120 | +} |
| 121 | + |
| 122 | +int Display::GetBitDepth() const { |
| 123 | + // iOS devices typically use 32-bit color depth |
| 124 | + return 32; |
| 125 | +} |
| 126 | + |
| 127 | +void* Display::GetNativeObjectInternal() const { |
| 128 | + return (__bridge void*)pimpl_->ui_screen_; |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace nativeapi |
| 132 | + |
0 commit comments