|
| 1 | +import Foundation |
1 | 2 | import nativeapi |
2 | 3 |
|
| 4 | +// MARK: - Helper Functions |
| 5 | + |
| 6 | +/// Convert display orientation to readable string |
| 7 | +func orientationToString(_ orientation: nativeapi.DisplayOrientation) -> String { |
| 8 | + switch orientation { |
| 9 | + case .portrait: |
| 10 | + return "Portrait (0 deg)" |
| 11 | + case .landscape: |
| 12 | + return "Landscape (90 deg)" |
| 13 | + case .portraitFlipped: |
| 14 | + return "Portrait Flipped (180 deg)" |
| 15 | + case .landscapeFlipped: |
| 16 | + return "Landscape Flipped (270 deg)" |
| 17 | + @unknown default: |
| 18 | + return "Unknown" |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// Truncate string if it's too long |
| 23 | +func truncateString(_ str: String, maxLength: Int) -> String { |
| 24 | + if str.count <= maxLength { |
| 25 | + return str |
| 26 | + } |
| 27 | + let truncated = String(str.prefix(maxLength - 3)) |
| 28 | + return truncated + "..." |
| 29 | +} |
| 30 | + |
| 31 | +/// Format a table row with proper alignment |
| 32 | +func formatTableRow(_ content: String, totalWidth: Int = 70) -> String { |
| 33 | + let truncated = truncateString(content, maxLength: totalWidth - 4) |
| 34 | + let padding = totalWidth - 4 - truncated.count |
| 35 | + return "│ \(truncated)\(String(repeating: " ", count: padding)) │" |
| 36 | +} |
| 37 | + |
| 38 | +/// Create table border using ASCII characters |
| 39 | +func createTableBorder(totalWidth: Int = 70) -> String { |
| 40 | + return "+" + String(repeating: "-", count: totalWidth - 2) + "+" |
| 41 | +} |
| 42 | + |
| 43 | +/// Print display information in a formatted table |
| 44 | +func printDisplayInfo(_ display: nativeapi.Display) { |
| 45 | + let tableWidth = 70 |
| 46 | + |
| 47 | + print(createTableBorder(totalWidth: tableWidth)) |
| 48 | + print(formatTableRow("Display: \(display.name)", totalWidth: tableWidth)) |
| 49 | + print(createTableBorder(totalWidth: tableWidth)) |
| 50 | + print(formatTableRow("ID: \(display.id)", totalWidth: tableWidth)) |
| 51 | + |
| 52 | + // Position |
| 53 | + let positionStr = "Position: (\(Int(display.position.x)), \(Int(display.position.y)))" |
| 54 | + print(formatTableRow(positionStr, totalWidth: tableWidth)) |
| 55 | + |
| 56 | + // Size |
| 57 | + let sizeStr = "Size: \(Int(display.size.width)) x \(Int(display.size.height))" |
| 58 | + print(formatTableRow(sizeStr, totalWidth: tableWidth)) |
| 59 | + |
| 60 | + // Work Area |
| 61 | + let workAreaStr = |
| 62 | + "Work Area: (\(Int(display.workArea.x)), \(Int(display.workArea.y))) \(Int(display.workArea.width)) x \(Int(display.workArea.height))" |
| 63 | + print(formatTableRow(workAreaStr, totalWidth: tableWidth)) |
| 64 | + |
| 65 | + // Scale Factor |
| 66 | + let scaleStr = String(format: "Scale Factor: %.2f", display.scaleFactor) |
| 67 | + print(formatTableRow(scaleStr, totalWidth: tableWidth)) |
| 68 | + |
| 69 | + // Primary status |
| 70 | + let primaryStr = "Primary: \(display.isPrimary ? "Yes" : "No")" |
| 71 | + print(formatTableRow(primaryStr, totalWidth: tableWidth)) |
| 72 | + |
| 73 | + // Orientation |
| 74 | + let orientationStr = "Orientation: \(orientationToString(display.orientation))" |
| 75 | + print(formatTableRow(orientationStr, totalWidth: tableWidth)) |
| 76 | + |
| 77 | + // Refresh Rate |
| 78 | + let refreshStr = "Refresh Rate: \(display.refreshRate) Hz" |
| 79 | + print(formatTableRow(refreshStr, totalWidth: tableWidth)) |
| 80 | + |
| 81 | + // Bit Depth (if available) |
| 82 | + if display.bitDepth > 0 { |
| 83 | + let bitDepthStr = "Bit Depth: \(display.bitDepth) bits" |
| 84 | + print(formatTableRow(bitDepthStr, totalWidth: tableWidth)) |
| 85 | + } |
| 86 | + |
| 87 | + // Manufacturer (if available) |
| 88 | + if !display.manufacturer.isEmpty { |
| 89 | + let manufacturerStr = "Manufacturer: \(display.manufacturer)" |
| 90 | + print(formatTableRow(manufacturerStr, totalWidth: tableWidth)) |
| 91 | + } |
| 92 | + |
| 93 | + // Model (if available) |
| 94 | + if !display.model.isEmpty { |
| 95 | + let modelStr = "Model: \(display.model)" |
| 96 | + print(formatTableRow(modelStr, totalWidth: tableWidth)) |
| 97 | + } |
| 98 | + |
| 99 | + print(createTableBorder(totalWidth: tableWidth)) |
| 100 | +} |
| 101 | + |
| 102 | +// MARK: - Main Program |
| 103 | + |
| 104 | +print("=== Native API Display Manager Demo ===") |
| 105 | +print() |
| 106 | + |
3 | 107 | var displayManager = nativeapi.DisplayManager() |
4 | | -let allDisplays = displayManager.GetAll() |
| 108 | + |
| 109 | +// Get all displays |
| 110 | +let displays = displayManager.GetAll() |
| 111 | + |
| 112 | +if displays.isEmpty { |
| 113 | + print("❌ No displays found!") |
| 114 | + exit(1) |
| 115 | +} |
| 116 | + |
| 117 | +print("📺 Found \(displays.count) display(s):") |
| 118 | +print() |
| 119 | + |
| 120 | +// Display primary display first |
5 | 121 | let primaryDisplay = displayManager.GetPrimary() |
| 122 | +print("🌟 PRIMARY DISPLAY:") |
| 123 | +printDisplayInfo(primaryDisplay) |
| 124 | +print() |
| 125 | + |
| 126 | +// Display secondary displays |
| 127 | +let secondaryDisplays = displays.filter { !$0.isPrimary } |
| 128 | +if !secondaryDisplays.isEmpty { |
| 129 | + print("📱 SECONDARY DISPLAYS:") |
| 130 | + for display in secondaryDisplays { |
| 131 | + printDisplayInfo(display) |
| 132 | + print() |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// Display cursor position |
6 | 137 | let cursorPosition = displayManager.GetCursorPosition() |
| 138 | +print("🖱️ Current Cursor Position: (\(cursorPosition.x), \(cursorPosition.y))") |
| 139 | +print() |
| 140 | + |
| 141 | +// Display summary statistics |
| 142 | +let totalWidth = displays.reduce(0) { $0 + $1.size.width } |
| 143 | +let totalHeight = displays.map { $0.size.height }.max() ?? 0 |
| 144 | +let scaleFactors = displays.map { $0.scaleFactor } |
| 145 | +let minScaleFactor = scaleFactors.min() ?? 0 |
| 146 | +let maxScaleFactor = scaleFactors.max() ?? 0 |
| 147 | + |
| 148 | +let summaryWidth = 60 |
| 149 | +print("📊 SUMMARY:") |
| 150 | +print(createTableBorder(totalWidth: summaryWidth)) |
| 151 | + |
| 152 | +let totalDisplaysStr = "Total Displays: \(displays.count)" |
| 153 | +print(formatTableRow(totalDisplaysStr, totalWidth: summaryWidth)) |
| 154 | + |
| 155 | +let combinedWidthStr = "Combined Width: \(Int(totalWidth))" |
| 156 | +print(formatTableRow(combinedWidthStr, totalWidth: summaryWidth)) |
| 157 | + |
| 158 | +let maxHeightStr = "Max Height: \(Int(totalHeight))" |
| 159 | +print(formatTableRow(maxHeightStr, totalWidth: summaryWidth)) |
| 160 | + |
| 161 | +let scaleRangeStr = String(format: "Scale Range: %.1fx - %.1fx", minScaleFactor, maxScaleFactor) |
| 162 | +print(formatTableRow(scaleRangeStr, totalWidth: summaryWidth)) |
7 | 163 |
|
8 | | -print(cursorPosition) |
9 | | -print(primaryDisplay) |
10 | | -print(allDisplays) |
| 164 | +print(createTableBorder(totalWidth: summaryWidth)) |
11 | 165 |
|
12 | | -print(primaryDisplay.width) |
13 | | -print(primaryDisplay.height) |
| 166 | +print() |
| 167 | +print("✅ Display information retrieved successfully!") |
0 commit comments