Skip to content
Merged
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
62 changes: 62 additions & 0 deletions crates/ocs_plugin_api/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ pub enum WidgetChildren {
pub struct WidgetRow {
pub label: String,
pub children: WidgetChildren,
/// When set, the row is a BUTTON: activating it carries this action
/// name to the plugin through `SurfaceEvent::NodeActivated` (the
/// host's activation gesture; the plugin binds the semantics).
/// Buttons are leaf rows — an `action` together with children is
/// refused by validation. Bounded by the text budget.
#[serde(default)]
pub action: Option<String>,
}

/// A keyed page of rows (finding F4): installs under `parent`
Expand Down Expand Up @@ -98,6 +105,11 @@ pub enum WidgetError {
DuplicateRow,
/// A row's label exceeds the text budget.
LabelTooLong,
/// A button row's action name exceeds the text budget.
ActionTooLong,
/// A button row (an `action` is set) also declared children — buttons
/// are leaf rows.
ActionWithChildren,
/// The resulting surface would hold rows unreachable from the roots.
OrphanRows,
}
Expand All @@ -116,6 +128,10 @@ impl core::fmt::Display for WidgetError {
}
WidgetError::DuplicateRow => write!(f, "page row id collides with an existing row"),
WidgetError::LabelTooLong => write!(f, "row label exceeds the text budget"),
WidgetError::ActionTooLong => write!(f, "button action name exceeds the text budget"),
WidgetError::ActionWithChildren => {
write!(f, "button rows cannot also declare children")
}
WidgetError::OrphanRows => write!(f, "page would orphan rows from the roots"),
}
}
Expand Down Expand Up @@ -151,6 +167,14 @@ impl WidgetSurface {
if row.label.len() > crate::scene::MAX_TEXT_BYTES {
return Err(WidgetError::LabelTooLong);
}
if let Some(action) = &row.action {
if action.len() > crate::scene::MAX_TEXT_BYTES {
return Err(WidgetError::ActionTooLong);
}
if !matches!(row.children, WidgetChildren::Leaf) {
return Err(WidgetError::ActionWithChildren);
}
}
}
let mut seen = HashSet::new();
let mut stack: Vec<StableId> = self.roots.iter().copied().collect();
Expand Down Expand Up @@ -296,9 +320,47 @@ mod tests {
WidgetRow {
label: label.to_owned(),
children,
action: None,
}
}

#[test]
fn button_rows_are_leaf_only_and_bounded() {
let mut surface = WidgetSurface::new(WidgetRevision(1));
surface.rows.insert(
StableId(1),
WidgetRow {
label: "Run".to_owned(),
children: WidgetChildren::Leaf,
action: Some("run".to_owned()),
},
);
surface.roots = vec![StableId(1)];
assert_eq!(surface.validate(), Ok(()));

// A button WITH children is refused.
surface.rows.insert(
StableId(1),
WidgetRow {
label: "Run".to_owned(),
children: WidgetChildren::Unloaded,
action: Some("run".to_owned()),
},
);
assert_eq!(surface.validate(), Err(WidgetError::ActionWithChildren));

// An over-budget action name is refused.
surface.rows.insert(
StableId(1),
WidgetRow {
label: "Run".to_owned(),
children: WidgetChildren::Leaf,
action: Some("x".repeat(crate::scene::MAX_TEXT_BYTES + 1)),
},
);
assert_eq!(surface.validate(), Err(WidgetError::ActionTooLong));
}

fn surface_with_parent() -> WidgetSurface {
let mut surface = WidgetSurface::new(WidgetRevision(3));
surface
Expand Down
11 changes: 11 additions & 0 deletions src/ui/window/plugin_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,24 @@ fn render_level<'a>(
text(" ").size(12).into()
};
let label = row.label.clone();
let is_action = row.action.is_some();
let activate_btn = button(row![text(label).size(13)].spacing(4))
.on_press(Message::PanelWidgetNodeActivated {
owner: owner.to_owned(),
panel: panel.to_owned(),
node: row_id,
})
.style(move |_, status| {
// Semantic BUTTON rows (an `action` is set) render with a
// visible border so they read as controls, not tree rows.
let mut style = iced::widget::button::Style::default();
if is_action {
style.border = iced::Border {
color: iced::Color::from_rgba8(150, 150, 190, 1.0),
width: 1.0,
radius: 3.0.into(),
};
}
if selected && status != iced::widget::button::Status::Disabled {
style.background = Some(iced::Background::Color(iced::Color::from_rgba8(
120, 120, 160, 0.25,
Expand Down Expand Up @@ -177,6 +187,7 @@ mod tests {
WidgetRow {
label: label.to_owned(),
children,
action: None,
}
}

Expand Down
Loading