Backend-agnostic retained UI-tree abstraction for Kagura (UINode, layout constraints, input/render adapters; extracted from kagura_engine)
let drag = @ui.UIDragController::new(threshold=8.0)
// 毎フレーム呼ぶ。sources / targets は描画にも使う LayoutResult の配列。
let events = drag.update(
input,
source_at=fn(x, y) { @ui.hit_test(sources, x, y) },
target_at=fn(source, x, y) {
match @ui.hit_test(targets, x, y) {
Some(target) => if accepts(source, target) { Some(target) } else { None }
None => None
}
},
)
for event in events {
match event {
@ui.UIEvent::Drag({ phase: @ui.UIDragPhase::Drop, source, target: Some(target), .. }) =>
apply_drop(source, target)
_ => ()
}
}| イベント | 意味 |
|---|---|
| Click(source) | 移動量がしきい値に達する前に離した |
| Drag(Start) | しきい値に達した。既定は論理座標で8 |
| Drag(Move) | ドラッグ開始または座標の変更 |
| Drag(Enter / Leave) | 受け入れ可能な対象への出入り。対象変更はLeave → Enter |
| Drag(Drop) | 離した位置の対象が受け入れ可能 |
| Drag(Cancel(reason)) | 明示的な取消、入力取消、無効化、対象外へのドロップ |
| Drag(End(success)) | 開始済みのドラッグの終了。Dropの後だけtrue |
just ui-dnd-test # JS / nativeの共通UIテストと警告チェックlet modals = @ui.UIModalController::new()
let _ = modals.open(dialog_id, opener=Some(button_id), focusable=[close_id, next_id])
// モーダルがある間は accepts(dialog_id) のスコープだけへ入力を送る。
modals.cycle(1) // Tab / 下方向。-1 は Shift+Tab / 上方向。
let closed = modals.close(@ui.UIModalCloseReason::Cancel)
// closed.restore_focus を呼び出し側の表示・入力フォーカスに反映する。pub trait LayoutEngine {
fn compute_layout(Self, root : UINodeId, constraint : LayoutConstraint) -> Array[LayoutResult] raise
}pub trait UIInputAdapter {
fn from_input_snapshot(Self, snapshot : InputSnapshot) -> Array[UIEvent]
}pub trait UIRenderAdapter {
fn build_draw_commands(Self, frame : FrameBudget, layouts : Array[LayoutResult], target : ImageHandle) -> Array[DrawTrianglesCommand] raise
}pub struct LayoutConstraint {
min_width : Double
min_height : Double
max_width : Double
max_height : Double
} derive(Debug)impl Show for LayoutConstraintimpl Show for LayoutRectimpl Show for LayoutResultimpl LayoutEngine for SimpleLayoutEnginefn compute_layout(self : SimpleLayoutEngine, root : UINodeId, constraint : LayoutConstraint) -> Array[LayoutResult] raisefn SimpleLayoutEngine::compute_layout(self : SimpleLayoutEngine, root : UINodeId, constraint : LayoutConstraint) -> Array[LayoutResult] raiseimpl UIInputAdapter for SimpleUIInputAdapterfn SimpleUIInputAdapter::from_input_snapshot(self : SimpleUIInputAdapter, snapshot : InputSnapshot) -> Array[UIEvent]impl UIRenderAdapter for SimpleUIRenderAdapterfn build_draw_commands(self : SimpleUIRenderAdapter, _frame : FrameBudget, layouts : Array[LayoutResult], target : ImageHandle) -> Array[DrawTrianglesCommand] raisefn SimpleUIRenderAdapter::build_draw_commands(self : SimpleUIRenderAdapter, _frame : FrameBudget, layouts : Array[LayoutResult], target : ImageHandle) -> Array[DrawTrianglesCommand] raiseimpl Show for UIAlignmentimpl Show for UIDirectionimpl Show for UIDragCancelReasonpub struct UIDragController {
input : UIPointerInputAdapter
threshold : Double
gesture : UIDragGesture?
}fn UIDragController::cancel(self : UIDragController, reason? : UIDragCancelReason) -> Array[UIEvent]fn UIDragController::handle_pointer(self : UIDragController, frame : UIPointerFrame, source_at~ : (Double, Double) -> UINodeId?, target_at~ : (UINodeId, Double, Double) -> UINodeId?, enabled? : Bool) -> Array[UIEvent]fn UIDragController::update(self : UIDragController, input : InputSnapshot, source_at~ : (Double, Double) -> UINodeId?, target_at~ : (UINodeId, Double, Double) -> UINodeId?, enabled? : Bool) -> Array[UIEvent]pub struct UIDragEvent {
phase : UIDragPhase
source : UINodeId
target : UINodeId?
pointer : UIPointerId
start_x : Double
start_y : Double
x : Double
y : Double
} derive(Debug)pub(all) enum UIDragPhase {
Start
Move
Enter
Leave
Drop
Cancel(UIDragCancelReason)
End(Bool)
} derive(Eq, Debug)impl Show for UIDragPhasepub struct UIDragSnapshot {
source : UINodeId
target : UINodeId?
pointer : UIPointerId
start_x : Double
start_y : Double
x : Double
y : Double
dragging : Bool
}pub enum UIEvent {
PointerDown(Double, Double)
PointerUp(Double, Double)
PointerMove(Double, Double)
Scroll(Double, Double)
KeyDown(Int)
KeyUp(Int)
PointerCancel
Click(UINodeId)
Drag(UIDragEvent)
} derive(Debug)impl Show for UIFlexWrapimpl Show for UIFocusEventfn UIFocusManager::handle_pointer_down(self : UIFocusManager, layouts : Array[LayoutResult], x : Double, y : Double) -> Array[UIFocusEvent]fn UIInteractionState::new(available? : Bool, selected? : Bool, drop_target? : Bool, disabled_reason? : String?) -> UIInteractionStatepub struct UIModalController {
// private fields
}fn UIModalController::close(self : UIModalController, reason : UIModalCloseReason) -> UIModalClosed?fn UIModalController::open(self : UIModalController, id : UINodeId, opener? : UINodeId?, focusable? : Array[UINodeId]) -> Boolpub struct UIModalLayer {
// private fields
}fn UIModalLayer::handle_pointer(self : UIModalLayer, frame : UIPointerFrame, source_at~ : (Double, Double) -> UINodeId?, target_at~ : (UINodeId, Double, Double) -> UINodeId?) -> Array[UIEvent]fn UIModalLayer::interaction(self : UIModalLayer, id : UINodeId, state : UIInteractionState) -> UIInteractionStatefn UIModalLayer::open(self : UIModalLayer, id : UINodeId, opener? : UINodeId?, focusable? : Array[UINodeId], backdrop? : UINodeId?) -> Boolfn UIModalLayer::update(self : UIModalLayer, input : InputSnapshot, source_at~ : (Double, Double) -> UINodeId?, target_at~ : (UINodeId, Double, Double) -> UINodeId?) -> Array[UIEvent]pub(all) enum UINavigationEvent {
Move(UINavigationDirection)
Confirm
Cancel
Auxiliary(Int)
} derive(Eq, Debug)pub struct UINavigationInputAdapter {
// private fields
}fn UINavigationInputAdapter::update(self : UINavigationInputAdapter, snapshot : InputSnapshot, elapsed_ms? : Double) -> Array[UINavigationEvent]pub(all) struct UINodeStyle {
direction : UIDirection
width : UISizing
height : UISizing
padding_left : Double
padding_right : Double
padding_top : Double
padding_bottom : Double
gap : Double
align : UIAlignment
justify_content : UIAlignment
flex_wrap : UIFlexWrap
} derive(Debug)impl Show for UINodeStylepub struct UIPointerFrame {
pointer : UIPointerId
x : Double
y : Double
pressed : Bool
released : Bool
down : Bool
cancelled : Bool
}fn UIPointerFrame::new(pointer : UIPointerId, x : Double, y : Double, pressed? : Bool, released? : Bool, down? : Bool, cancelled? : Bool) -> UIPointerFrameimpl Show for UIPointerIdpub struct UIPointerInputAdapter {
keys : KeyInputState
mouse : MouseButtonInputState
touches : TouchInputState
owned_touch : Int?
frame : UIPointerFrame
}impl UIInputAdapter for UIPointerInputAdapterfn UIPointerInputAdapter::from_input_snapshot(self : UIPointerInputAdapter, input : InputSnapshot) -> Array[UIEvent]pub(all) struct UISafeArea {
top : Double
right : Double
bottom : Double
left : Double
} derive(Debug)impl Show for UISafeAreapub(all) struct UISnapshot {
screen_width : Double
screen_height : Double
dpr : Double
safe_area : UISafeArea
frame : Int
state : String
nodes : Array[UISnapshotNode]
focus_order : Array[String]
} derive(Debug)impl Show for UISnapshotfn UISnapshot::hit_test(self : UISnapshot, x : Double, y : Double, drop_only? : Bool) -> UISnapshotNode?pub(all) struct UISnapshotNode {
path : String
id : String
role : String
classes : String
left : Double
top : Double
width : Double
height : Double
z : Int
visible : Bool
clip : UISnapshotRect?
text : String?
text_measured : UITextMetrics?
focusable : Bool
focus_index : Int
focused : Bool
hit : UISnapshotRect?
interaction : UIInteractionState?
} derive(Debug)impl Show for UISnapshotNodepub(all) struct UISnapshotRect {
left : Double
top : Double
width : Double
height : Double
} derive(Debug)impl Show for UISnapshotRectimpl Show for UITextMetricsfn hit_test_interactive(layouts : Array[LayoutResult], x : Double, y : Double, state_for : (UINodeId) -> UIInteractionState, drop_only? : Bool) -> UINodeId?fn ui_events_from_input_edge(key_state : KeyInputState, mouse_state : MouseButtonInputState, cursor_x : Double, cursor_y : Double) -> Array[UIEvent]Install
Download zipBackend-agnostic retained UI-tree abstraction for Kagura (UINode, layout constraints, input/render adapters; extracted from kagura_engine)