Metal 使用举例

Metal 使用举例

原生的 Metal

在 Rails 里 metal 也属于 middleware,我们可以这么用:

# config/routes.rb
get 'hello' => 'hello#index'
# ...
# app/controllers/hello_controller.rb
class HelloController < ActionController::Metal
  def index
    self.response_body = "Hello World!"
  end
end

然后浏览器访问:http://localhost:3000/hello 可以获取刚才的内容。

日志会由原来的:

Started GET "/hello" for 127.0.0.1 at 2014-04-27 09:04:49 +0800
Processing by HelloController#index as HTML
Completed 200 OK in 1ms (ActiveRecord: 0.0ms)

变为:

    Started GET "/hello" for 127.0.0.1 at 2014-04-27 08:57:07 +0800

加入 Rendering 模块

默认 ActionController::Metal 是没有提供渲染视图、模板和其它需要明确调用到 response_body=, content_type=, status= 的方法。如果你需要这些,可以引入它们:

class HelloController < ActionController::Metal
  include AbstractController::Rendering
  include ActionView::Layouts

  append_view_path "#{Rails.root}/app/views"

  def index
    render "hello/index"
  end
end

加入 Redirection 模块

想使用重定向相关代码,你也需要引入它们:

class HelloController < ActionController::Metal
  include ActionController::Redirecting
  include Rails.application.routes.url_helpers

  def index
    redirect_to root_url
  end
end

加入其它模块

参考 ActionController::Base 引入其它模块,以达到目的。

class ApiController < ActionController::Metal # 使用 Metal, 而不是 Base
  include ActionController::Helpers
  include ActionController::Redirecting
  include ActionController::Rendering
  include ActionController::Renderers::All
  include ActionController::ConditionalGet

  # 需要响应 .json .xml 等不同格式的话,使用它。
  include ActionController::MimeResponds
  include ActionController::RequestForgeryProtection
  # 如果你需要 SSL
  include ActionController::ForceSSL
  include AbstractController::Callbacks
  # 想要知道 Controller 处理过程中,性能这方面的数据。
  include ActionController::Instrumentation
  # 需要转换 params 类型的话,可以使用它。
  include ActionController::ParamsWrapper
  include Devise::Controllers::Helpers

  # 在项目里使用路由相关的 helper 方法。
  include Rails.application.routes.url_helpers

  # 视图文件放在哪?
  append_view_path "#{Rails.root}/app/views"

  # 需要转换 params 类型的话,可以使用它。
  # { "person": { "name": "Kelby", "email": "leekelby@gmail.com" }}
  wrap_parameters format: [:json]

  # 根据客户端决定是否需要。(另,如果客户端不是浏览器的话,它会自动跳过)
  protect_from_forgery
end

上述代码,仅供参考。关于各模块及其作用,详情可以参考对应章节。

参考

Developing api with rails metal

用技术做兼职,找技术兼职,就上:
http://shixian.com/

<think>我们之前已经分析了工程目录结构和各文件的作用,现在需要进一步举例说明模块间的调用关系。以下将详细描述几个典型场景的调用流程:###1.键盘初始化流程- **起点**:`ServiceExtAbility.ets`(服务扩展Ability,作为键盘服务的入口)-调用`VirtualKeyboardManager.ets`的初始化方法。- **VirtualKeyboardManager.ets**:-创建`KeyboardContext.ets`实例(全局状态管理)。-调用`WindowManager.ets`创建键盘窗口。-通过`ConfigManager.ets`加载持久化配置(如主题、振动设置)。 -**ConfigManager.ets**:-使用`PersistenceUtils.ets`读取本地存储的用户设置。-将配置数据设置到`KeyboardContext.ets`中。 -**键盘UI渲染**:- `WindowManager.ets`加载`KeyboardPage.ets`页面。-`KeyboardPage.ets`根据`KeyboardContext.ets`中的键盘类型(固定/悬浮)选择渲染`FixedKeyboard.ets`或`FloatingCharacterKeyboard.ets`。-键盘组件内部使用`Key.ets`绘制按键,按键样式由`KeyStyle.ets`和`ThemeStyle.ets`动态计算。 ###2.用户点击按键事件处理- **按键触发**:`Key.ets`组件捕获用户点击事件。-**事件处理**:-调用`SoundUtil.ets`播放音效(根据`Constants.SoundEffect`配置)。-调用`VibrationManager.ets`触发振动(强度由`VibrationLevel`决定)。- **输入逻辑**:-功能键(如回车):通过`FunctionKeyContext.ets`处理特殊行为。-字符键:将字符提交给系统输入法引擎(通过`VirtualKeyboardStub.ets`与系统服务交互)。- **状态更新**:-若按键触发了键盘布局切换(如中/英文),则通知`KeyboardContext.ets`更新状态,触发UI重绘。 ###3.主题切换流程- **用户操作**:在工具栏点击`ThemeColorButton.ets`。- **事件响应**:-`ThemeColorButton.ets`调用`ConfigManager.ets`保存新主题到本地(使用`PersistenceUtils.ets`)。-**全局状态更新**:-`ConfigManager.ets`更新`KeyboardContext.ets`的主题状态。- **UI刷新**:-`KeyboardContext.ets`的状态变化触发`KeyboardPage.ets`重新渲染。-所有按键组件(`Key.ets`)通过`ThemeStyle.ets`获取新主题的颜色和图标资源。 ###4.屏幕折叠事件响应-**事件监听**:`DisplayEventManager.ets`注册系统折叠状态变化事件。 -**事件触发**:-当屏幕折叠时,`DisplayEventManager.ets`收到事件,调用`MotionSpaceChangeListener.ets`。-**处理逻辑**:- `MotionSpaceChangeListener.ets`判断当前状态满足条件(如首次悬停),则启动`UserGuide.ets`。-`UserGuide.ets`通过`DialogWindowManager.ets`弹出引导窗口(`UserGuideDialogPage.ets`)。 ###5.触控板手势处理- **手势捕获**:`TouchpadItem.ets`监听触摸事件(滑动、点击)。- **事件映射**:-将手势转化为鼠标事件(如左键点击),通过`PhysicalKeyboardManager.ets`模拟输入。 -**状态同步**:-若触控板区域需要更新(如灵敏度调整),则调用`VirtualKeyboardManager.ets`同步配置。###总结调用关系核心围绕**状态管理**(`KeyboardContext.ets`)、**事件传递**(各Manager)、**UI渲染**(ArkUI组件)三层展开,通过工具类和实体模块实现解耦。§§相关问题§§1. `ServiceExtAbility.ets`如何与系统服务交互?2.按键音效和振动如何实现跨模块调用?3.屏幕折叠事件如何从系统传递到`DisplayEventManager.ets`?4.触控板手势事件如何映射为鼠标操作?5.主题切换时如何避免UI卡顿?</think>### 更多调用关系示例分析 #### 1. **键盘显示流程** 1. **系统触发**: - 应用获得焦点 → `VirtualKeyboardStub.ets` 接收 `IME_START` 事件 2. **状态管理**: - `VirtualKeyboardStub.ets` → `VirtualKeyboardManager.ets` 更新 `KeyboardStatus.isShow=true` 3. **窗口控制**: - `VirtualKeyboardManager.ets` → `WindowManager.ets` 调用窗口创建方法 4. **UI渲染**: - `WindowManager.ets` → 加载 `KeyboardPage.ets` - `KeyboardPage.ets` → 根据 `KeyboardType` 选择渲染 `FixedKeyboard.ets` 或 `FloatingCharacterKeyboard.ets` 5. **资源加载**: - `Key.ets` 组件 → 通过 `ResourcesUtil.ets` 加载 `resources/media` 的图标资源 #### 2. **主题切换流程** 1. **用户操作**: - 点击 `ThemeColorButton.ets` 组件 2. **状态更新**: - `ThemeColorButton.ets` → `ConfigManager.ets` 保存新主题到持久化存储 3. **全局通知**: - `ConfigManager.ets` → `KeyboardContext.ets` 更新主题状态 4. **样式重载**: - `KeyboardContext.ets` → 触发 `ThemeStyle.ets` 重新计算样式 5. **UI刷新**: - `Key.ets` 组件接收新样式 → 通过 `KeyStyle.ets` 应用新颜色/圆角 #### 3. **折叠屏手势引导** 1. **事件捕获**: - 屏幕折叠 → `DisplayEventManager.ets` 检测到 `FoldType.FOLDED` 2. **逻辑判断**: - `DisplayEventManager.ets` → `OobeUtil.ets` 检查是否首次触发 3. **引导触发**: - `OobeUtil.ets` → `UserGuide.ets` 生成引导数据 4. **弹窗显示**: - `UserGuide.ets` → `DialogWindowManager.ets` 打开 `UserGuideDialogPage.ets` 5. **UI渲染**: - `UserGuideDialogPage.ets` → 使用 `GuideDialog.ets` 组件展示手势说明 #### 4. **按键反馈处理** 1. **按键按下**: - `Key.ets` 捕获点击事件 → 创建 `CustomDragEvent.ets` 对象 2. **振动反馈**: - `Key.ets` → `VibrationController.ets` 根据 `Constants.VIBRATE_STRONG` 触发振动 3. **音效播放**: - 并行调用 `SoundUtil.ets` → 加载 `SoundEffect.ELegANT_METAL` 音频 4. **输入处理**: - 字符键 → 通过 `VirtualKeyboardStub.ets` 提交给系统输入法 - 功能键 → 调用 `FunctionKeyContext.ets` 执行特殊操作 #### 5. **分辨率适配流程** 1. **屏幕旋转**: - `DisplayAdaptation.ets` 检测分辨率变化 2. **重计算参数**: - 调用 `DimensionUtil.ets` 动态调整 `Constants.WINDOW_WIDTH` 等参数 3. **布局更新**: - `KeyboardContext.ets` 触发重新布局事件 4. **组件响应**: - `FixedKeyboard.ets` 通过 `KeySpacingType.WIDE` 调整按键间距 - `Toolbar.ets` 根据 `Constants.TOOLBAR_HEIGHT` 重绘工具栏 ### 核心模块协作图 ```mermaid graph LR A[系统事件] --> B(VirtualKeyboardStub) B --> C{VirtualKeyboardManager} C --> D[WindowManager] C --> E[KeyboardContext] D --> F[KeyboardPage] E --> F F --> G[FixedKeyboard/FloatingKeyboard] G --> H[Key.ets] H --> I[VibrationController] H --> J[SoundUtil] E --> K[ConfigManager] K --> L[PersistenceUtils] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值