Skip to content

Commit 7bb6ff6

Browse files
committed
WIP 5
1 parent 0925b6b commit 7bb6ff6

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

docs/guide/multiplatform.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,111 @@
11
# 多平台支持
2+
3+
Miuix 是一个支持多种平台的 Compose Multiplatform UI 框架,允许您使用相同的代码库在不同平台上构建应用程序。
4+
5+
## 支持的平台
6+
7+
目前,Miuix 支持以下平台:
8+
9+
- **Android**:适用于 Android 移动设备
10+
- **iOS**:适用于 iPhone 和 iPad 设备
11+
- **Desktop(JVM)**:适用于基于 JVM 的桌面应用
12+
- **WasmJs**:适用于 WebAssembly 环境
13+
- **MacOS**:适用于 macOS 原生应用
14+
- **Js**:适用于 JavaScript 环境
15+
16+
## 平台检测与适配
17+
18+
您可以使用 `platform()` 函数检测当前运行的平台,并据此调整 UI 或功能:
19+
20+
```kotlin
21+
when (platform()) {
22+
Platform.Android -> { // Android 特定代码 }
23+
Platform.IOS -> { // iOS 特定代码 }
24+
Platform.Desktop -> { // JVM 特定代码 }
25+
Platform.WasmJs -> { // WebAssembly 特定代码 }
26+
Platform.MacOS -> { // macOS 特定代码 }
27+
Platform.Js -> { // JavaScript 特定代码 }
28+
}
29+
```
30+
31+
## 窗口尺寸管理
32+
33+
Miuix 提供了跨平台的窗口尺寸获取功能:
34+
35+
```kotlin
36+
@Composable
37+
fun MyResponsiveContent() {
38+
val windowSize = getWindowSize()
39+
val width = windowSize.width
40+
val height = windowSize.height
41+
// 根据窗口尺寸调整 UI 布局
42+
Surface(
43+
modifier = Modifier.fillMaxSize(),
44+
) {
45+
if (width > 600) {
46+
Text("宽屏布局")
47+
} else {
48+
Text("窄屏布局")
49+
}
50+
Text("\n高度:$height")
51+
}
52+
}
53+
```
54+
55+
## 设备特性获取
56+
57+
### 设备圆角
58+
59+
安卓设备的屏幕圆角不同且其他平台不存在屏幕圆角获取,您可以使用 `getRoundedCorner()` 函数获取设备的圆角大小(不存在时使用预设值):
60+
61+
```kotlin
62+
@Composable
63+
fun AdaptiveRoundedComponent() {
64+
val cornerRadius = getRoundedCorner()
65+
Surface(
66+
shape = RoundedCornerShape(cornerRadius),
67+
// 其他属性
68+
) {
69+
// 内容
70+
}
71+
}
72+
```
73+
74+
## 平台特定行为处理
75+
76+
### 返回操作处理
77+
78+
Compose MultiPlatform 没有提供完整的跨平台 BackHandler 接口,因此 Miuix 提供了统一的 `BackHandler` 接口:
79+
80+
```kotlin
81+
@Composable
82+
fun Screen() {
83+
// 处理返回操作
84+
BackHandler(enabled = true) {
85+
// 返回操作的处理逻辑
86+
// 例如:导航到前一个屏幕或关闭对话框
87+
}
88+
}
89+
```
90+
91+
## 最佳实践
92+
93+
1. **预先适配检查**:在开发过程中定期在各目标平台上测试您的应用
94+
2. **响应式设计**:可以使用 `getWindowSize()` 创建适应不同屏幕尺寸的布局
95+
3. **条件渲染**:根据 `platform()` 的结果有条件地渲染组件或功能
96+
4. **平台特定扩展**:为特定平台创建扩展函数,使代码更整洁
97+
98+
```kotlin
99+
@Composable
100+
fun PlatformSpecificFeature() {
101+
when (platform()) {
102+
Platform.Android, Platform.IOS -> MobileView()
103+
Platform.Desktop, Platform.MacOS -> DesktopView()
104+
Platform.WasmJs, Platform.Js -> WebView()
105+
}
106+
}
107+
```
108+
109+
## 平台特定依赖
110+
111+
为了优化应用体积,您可以只引入特定平台所需的依赖。请参考[快速开始](/guide/getting-started)部分了解如何添加特定平台的依赖。

0 commit comments

Comments
 (0)