GitHub
3 年前
当前提交
81f7ced6
共有 6 个文件被更改,包括 149 次插入 和 52 次删除
-
3com.unity.uiwidgets/Editor/UIWidgetsEditorPanel.cs
-
67com.unity.uiwidgets/Runtime/services/keyboard_key.cs
-
2com.unity.uiwidgets/Runtime/widgets/actions.cs
-
31com.unity.uiwidgets/Runtime/widgets/shortcuts.cs
-
95com.unity.uiwidgets/Runtime/widgets/raw_keyoard_listener.cs
-
3com.unity.uiwidgets/Runtime/widgets/raw_keyoard_listener.cs.meta
|
|||
using Unity.UIWidgets.foundation; |
|||
using Unity.UIWidgets.service; |
|||
|
|||
namespace Unity.UIWidgets.widgets { |
|||
public class RawKeyboardListener : StatefulWidget { |
|||
public RawKeyboardListener( |
|||
Key key = null, |
|||
FocusNode focusNode = null, |
|||
bool autofocus = false, |
|||
ValueChanged<RawKeyEvent> onKey = null, |
|||
Widget child = null |
|||
) : base(key: key) { |
|||
D.assert(focusNode != null); |
|||
D.assert(child != null); |
|||
this.focusNode = focusNode; |
|||
this.autofocus = autofocus; |
|||
this.onKey = onKey; |
|||
this.child = child; |
|||
} |
|||
|
|||
public readonly FocusNode focusNode; |
|||
|
|||
public readonly bool autofocus; |
|||
|
|||
public readonly ValueChanged<RawKeyEvent> onKey; |
|||
|
|||
public readonly Widget child; |
|||
|
|||
public override State createState() => new _RawKeyboardListenerState(); |
|||
|
|||
public override void debugFillProperties(DiagnosticPropertiesBuilder properties) { |
|||
base.debugFillProperties(properties); |
|||
properties.add(new DiagnosticsProperty<FocusNode>("focusNode", focusNode)); |
|||
} |
|||
} |
|||
|
|||
public class _RawKeyboardListenerState : State<RawKeyboardListener> { |
|||
public override void initState() { |
|||
base.initState(); |
|||
widget.focusNode.addListener(_handleFocusChanged); |
|||
} |
|||
|
|||
public override void didUpdateWidget(StatefulWidget oldWidget) { |
|||
var _oldWidgt = oldWidget as RawKeyboardListener; |
|||
base.didUpdateWidget(oldWidget); |
|||
if (widget.focusNode != _oldWidgt.focusNode) { |
|||
_oldWidgt.focusNode.removeListener(_handleFocusChanged); |
|||
widget.focusNode.addListener(_handleFocusChanged); |
|||
} |
|||
} |
|||
|
|||
|
|||
public override void dispose() { |
|||
widget.focusNode.removeListener(_handleFocusChanged); |
|||
_detachKeyboardIfAttached(); |
|||
base.dispose(); |
|||
} |
|||
|
|||
void _handleFocusChanged() { |
|||
if (widget.focusNode.hasFocus) |
|||
_attachKeyboardIfDetached(); |
|||
else |
|||
_detachKeyboardIfAttached(); |
|||
} |
|||
|
|||
bool _listening = false; |
|||
|
|||
void _attachKeyboardIfDetached() { |
|||
if (_listening) |
|||
return; |
|||
RawKeyboard.instance.addListener(_handleRawKeyEvent); |
|||
_listening = true; |
|||
} |
|||
|
|||
void _detachKeyboardIfAttached() { |
|||
if (!_listening) |
|||
return; |
|||
RawKeyboard.instance.removeListener(_handleRawKeyEvent); |
|||
_listening = false; |
|||
} |
|||
|
|||
void _handleRawKeyEvent(RawKeyEvent evt) { |
|||
if (widget.onKey != null) |
|||
widget.onKey(evt); |
|||
} |
|||
|
|||
public override Widget build(BuildContext context) { |
|||
return new Focus( |
|||
focusNode: widget.focusNode, |
|||
autofocus: widget.autofocus, |
|||
child: widget.child |
|||
); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: ceed7889e4244dc09b9b2d9cb5151900 |
|||
timeCreated: 1629712713 |
撰写
预览
正在加载...
取消
保存
Reference in new issue