Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion apps/gpui_components/lib/gpui/components/schema_declarations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ defmodule GPUI.Components.Schema.Declarations do
@components [
%{
__struct__: GPUI.Schema.Component,
attrs: [id: :required_string],
attrs: [
id: :required_string,
accepts: {:default, {:enum, ["files", "values"]}, "files"}
],
children: true,
events: [
drag_enter: :"phx-drag-enter",
Expand All @@ -21,6 +24,20 @@ defmodule GPUI.Components.Schema.Declarations do
stateful: true,
tag: :ui_drop_target
},
%{
__struct__: GPUI.Schema.Component,
attrs: [id: :required_string, value: :required_string],
children: true,
events: [],
extension: nil,
kind: :drag_source_component,
public_hidden_attrs: [],
public_required_attrs: [],
public_slots: [],
required_events: [],
stateful: false,
tag: :ui_drag_source
},
%{
__struct__: GPUI.Schema.Component,
attrs: [
Expand Down
28 changes: 25 additions & 3 deletions apps/gpui_components/lib/gpui/ui.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule GPUI.UI do
virtual_collection_options: :ui_virtual_collection,
virtual_item_options: :ui_virtual_item,
drop_target_options: :ui_drop_target,
drag_source_options: :ui_drag_source,
rich_text_options: :ui_rich_text,
data_table_options: :ui_data_table,
table_column_options: :ui_table_column,
Expand Down Expand Up @@ -516,10 +517,13 @@ defmodule GPUI.UI do
end

@doc """
Builds a neutral operating-system external-path drop target.
Builds a neutral drop target.

Paths always refer to the display machine. The renderer bounds and validates
paths before emitting typed drag events and never reads dropped files.
With `accepts: "files"` (the default) it takes operating-system external
paths, which always refer to the display machine; the renderer bounds and
validates them before emitting typed drag events and never reads dropped
files. With `accepts: "values"` it takes in-app drags started on a
`drag_source/1`, whose value arrives as the payload's `text`.

#{ComponentsSchema.component_options_doc(:ui_drop_target)}
"""
Expand All @@ -536,6 +540,24 @@ defmodule GPUI.UI do
component(:ui_drop_target, assigns)
end

@doc """
Builds an in-app drag source around its children.

Pressing on the children and moving starts a drag carrying `value`, which
drop targets with `accepts: "values"` receive as the payload's `text`. A
press that doesn't move still clicks the children. Values must be valid
UTF-8 and fit the transfer payload's 1 MiB text limit.

#{ComponentsSchema.component_options_doc(:ui_drag_source)}
"""
@spec drag_source(drag_source_options()) :: Element.t()
def drag_source(assigns) when is_map(assigns) do
assigns = ComponentsSchema.apply_defaults(assigns, :ui_drag_source)
ComponentsSchema.validate_component_assigns!(assigns, :ui_drag_source)
GPUI.Transfer.Payload.new(text: assigns.value)
component(:ui_drag_source, assigns)
end

@doc """
Builds immutable, selectable, natively shaped rich text.

Expand Down
43 changes: 43 additions & 0 deletions apps/gpui_components/native/src/generated/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum GeneratedComponentKind {
FrostComponent,
PaintComponent,
DropTargetComponent,
DragSourceComponent,
SplitComponent,
ButtonComponent,
ProgressComponent,
Expand Down Expand Up @@ -2294,6 +2295,7 @@ pub(crate) fn decode_generated_paint_component<'a>(
pub(crate) struct DropTargetComponentNode {
pub(crate) style: StyleAttrs,
pub(crate) id: String,
pub(crate) accepts: Option<String>,
pub(crate) children: Vec<ElementNode>,
pub(crate) drag_enter: Option<String>,
pub(crate) drag_move: Option<String>,
Expand All @@ -2309,6 +2311,14 @@ pub(crate) fn decode_generated_drop_target_component<'a>(
Ok(DropTargetComponentNode {
style: decode_style(term)?,
id: component_required_string_attr(term, atoms::id())?,
accepts: match component_enum_attr(
term,
atoms::accepts(),
&vec!["files", "values"],
)? {
Some(value) => Some(value),
None => Some("files".to_string()),
},
children: decode_children(term)?,
drag_enter: component_string_attr(term, atoms::phx_drag_enter())?,
drag_move: component_string_attr(term, atoms::phx_drag_move())?,
Expand All @@ -2319,6 +2329,28 @@ pub(crate) fn decode_generated_drop_target_component<'a>(
#[derive(Clone, Debug)]
#[cfg(feature = "real-gpui")]
#[allow(dead_code)]
pub(crate) struct DragSourceComponentNode {
pub(crate) style: StyleAttrs,
pub(crate) id: String,
pub(crate) value: String,
pub(crate) children: Vec<ElementNode>,
}
#[cfg(feature = "real-gpui")]
#[allow(clippy::redundant_field_names)]
#[allow(clippy::useless_vec)]
pub(crate) fn decode_generated_drag_source_component<'a>(
term: Term<'a>,
) -> NifResult<DragSourceComponentNode> {
Ok(DragSourceComponentNode {
style: decode_style(term)?,
id: component_required_string_attr(term, atoms::id())?,
value: component_required_string_attr(term, atoms::value())?,
children: decode_children(term)?,
})
}
#[derive(Clone, Debug)]
#[cfg(feature = "real-gpui")]
#[allow(dead_code)]
pub(crate) struct SplitComponentNode {
pub(crate) style: StyleAttrs,
pub(crate) id: String,
Expand Down Expand Up @@ -3840,6 +3872,7 @@ pub(crate) enum ElementNode {
FrostComponent(FrostComponentNode),
PaintComponent(PaintComponentNode),
DropTargetComponent(DropTargetComponentNode),
DragSourceComponent(DragSourceComponentNode),
SplitComponent(SplitComponentNode),
ButtonComponent(ButtonComponentNode),
ProgressComponent(ProgressComponentNode),
Expand Down Expand Up @@ -3905,6 +3938,7 @@ pub enum GeneratedElementTag {
UiFrost,
UiPaint,
UiDropTarget,
UiDragSource,
UiSplit,
UiButton,
UiProgress,
Expand Down Expand Up @@ -3969,6 +4003,7 @@ pub fn decode_generated_element_tag(tag: &str) -> GeneratedElementTag {
"ui_frost" => GeneratedElementTag::UiFrost,
"ui_paint" => GeneratedElementTag::UiPaint,
"ui_drop_target" => GeneratedElementTag::UiDropTarget,
"ui_drag_source" => GeneratedElementTag::UiDragSource,
"ui_split" => GeneratedElementTag::UiSplit,
"ui_button" => GeneratedElementTag::UiButton,
"ui_progress" => GeneratedElementTag::UiProgress,
Expand Down Expand Up @@ -4034,6 +4069,7 @@ pub fn generated_component_kind(tag: GeneratedElementTag) -> GeneratedComponentK
GeneratedElementTag::UiFrost => GeneratedComponentKind::FrostComponent,
GeneratedElementTag::UiPaint => GeneratedComponentKind::PaintComponent,
GeneratedElementTag::UiDropTarget => GeneratedComponentKind::DropTargetComponent,
GeneratedElementTag::UiDragSource => GeneratedComponentKind::DragSourceComponent,
GeneratedElementTag::UiSplit => GeneratedComponentKind::SplitComponent,
GeneratedElementTag::UiButton => GeneratedComponentKind::ButtonComponent,
GeneratedElementTag::UiProgress => GeneratedComponentKind::ProgressComponent,
Expand Down Expand Up @@ -4147,6 +4183,10 @@ pub(crate) fn decode_generated_element_node<'a>(
decode_generated_drop_target_component(term)
.map(|node| ElementNode::DropTargetComponent(node))
}
GeneratedComponentKind::DragSourceComponent => {
decode_generated_drag_source_component(term)
.map(|node| ElementNode::DragSourceComponent(node))
}
GeneratedComponentKind::SplitComponent => {
decode_generated_split_component(term)
.map(|node| ElementNode::SplitComponent(node))
Expand Down Expand Up @@ -4345,6 +4385,9 @@ pub(crate) fn render_generated_component_node(
ElementNode::DropTargetComponent(node) => {
element::component::drop_target::render(node, context)
}
ElementNode::DragSourceComponent(node) => {
element::component::drag_source::render(node, context)
}
ElementNode::SplitComponent(node) => {
element::component::split::render(node, context)
}
Expand Down
27 changes: 27 additions & 0 deletions apps/gpui_components/test/test/gpui/ui/data_views_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ defmodule GPUI.UI.DataViewsTest do
assert attrs[:"phx-drop"] == "dropped"
end

test "builds in-app drag sources and value-accepting drop targets" do
child = %Element{type: :text, children: ["Worker"]}

assert %Element{type: :ui_drag_source, attrs: source, children: [^child]} =
UI.drag_source(%{id: "worker", value: "sprite:worker", children: [child]})

assert source[:value] == "sprite:worker"

assert %Element{type: :ui_drop_target, attrs: target} =
UI.drop_target(%{id: "group", accepts: "values", "phx-drop": "add_worker"})

assert target[:accepts] == "values"
assert target[:"phx-drop"] == "add_worker"
assert UI.drop_target(%{id: "files"}).attrs[:accepts] == "files"
end

test "drag source values obey the transfer payload UTF-8 and size limits" do
value = String.duplicate("x", 1_048_576)
assert UI.drag_source(%{id: "limit", value: value}).attrs[:value] == value

for invalid <- [value <> "x", <<255>>] do
assert_raise ArgumentError, fn ->
UI.drag_source(%{id: "invalid", value: invalid})
end
end
end

test "adds bounded clipboard reads to an ordinary button" do
assert %Element{type: :ui_button, attrs: attrs} =
UI.button(%{
Expand Down
2 changes: 2 additions & 0 deletions apps/gpui_native/native/src/element/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub(crate) mod data_table;
#[cfg(feature = "components")]
pub(crate) mod display;
#[cfg(feature = "components")]
pub(crate) mod drag_source;
#[cfg(feature = "components")]
pub(crate) mod drop_target;
pub(crate) mod edge_fade;
#[cfg(feature = "components")]
Expand Down
57 changes: 57 additions & 0 deletions apps/gpui_native/native/src/element/component/drag_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::{gpui, DragSourceComponentNode, ElementRenderContext};

/// The value an in-app drag carries to drop targets that accept values.
#[derive(Clone, Debug)]
pub(crate) struct DraggedValue {
pub(crate) value: String,
}

/// Nothing follows the pointer: the application draws its own feedback
/// from the drop target's move events.
#[cfg(feature = "components")]
struct DragPreview;

#[cfg(feature = "components")]
impl gpui::Render for DragPreview {
fn render(
&mut self,
_window: &mut gpui::Window,
_cx: &mut gpui::Context<Self>,
) -> impl gpui::IntoElement {
gpui::div()
}
}

#[cfg(feature = "components")]
pub(crate) fn render(
node: DragSourceComponentNode,
context: &mut ElementRenderContext<'_, '_>,
) -> gpui::AnyElement {
use crate::element::apply_generated_render_styles;
use gpui::{
AppContext, InteractiveElement, IntoElement, ParentElement, StatefulInteractiveElement,
};

let children = node
.children
.into_iter()
.map(|child| child.render(context))
.collect::<Vec<_>>();
let value = DraggedValue { value: node.value };

apply_generated_render_styles(gpui::div(), node.style)
.id(node.id)
.children(children)
.on_drag(value, |_value, _offset, _window, cx| {
cx.new(|_| DragPreview)
})
.into_any_element()
}

#[cfg(not(feature = "components"))]
pub(crate) fn render(
node: DragSourceComponentNode,
context: &mut ElementRenderContext<'_, '_>,
) -> gpui::AnyElement {
super::render_component_fallback(node.style, None, node.children, context)
}
Loading