Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components are widgets #316

Merged
merged 18 commits into from
May 10, 2022
Merged

Components are widgets #316

merged 18 commits into from
May 10, 2022

Conversation

dhardy
Copy link
Collaborator

@dhardy dhardy commented May 9, 2022

Alternative to #315: components are widgets. This resolves ambiguities over whether something is a widget or a component. It is sometimes a little inefficient (storing a WidgetId for things like a label which don't need one), but this is not significant.

  • Replace Layout and Mark components with real widgets (removes kas::component module)
  • Allow widgets introduced by layout syntax to be enumerated by WidgetChildren and configured
  • Allow embedding of widgets within layout syntax (currently these are boxed; given something like RFC 2524 they could be placed inline, but again this inefficiency is not important)
  • Remove support for glob (*) from layout syntax; this is less important (since widgets may be embedded) and potentially confusing given that layout syntax also declares child widgets

Example: the stopwatch can now be declared as follows, with the buttons inlined into the layout:

        #[widget{
            layout = row: [
                self.display,
                TextButton::new_msg("&reset", MsgReset),
                TextButton::new_msg("&start / &stop", MsgStart),
            ];
        }]
        struct {
            #[widget] display: impl HasString = Frame::new(Label::new("0.000".to_string())),
            saved: Duration = Duration::default(),
            start: Option<Instant> = None,
        }

Now, child widgets must appear as #[widget] fields in one of two cases:

  • The field is directly read/written to by event handlers
  • Construction of the field depends on constructor parameters or moved values

Inherits some other commits from #315: removing Widget: 'static bound; preferring dyn Widget over W: Widget in event-management code; some clean-up. Closes #315.

The Label widget now allows replacing the text class.

Minor change to layout of examples/data-list*

Comment on lines +42 to +50
impl Layout for Self {
fn size_rules(&mut self, mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
mgr.mark(self.style, axis)
}

fn draw(&mut self, mut draw: DrawMgr) {
draw.mark(self.core.rect, self.style);
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inherits default implementations:

  • set_rect just assigns to self.core.rect (fine)
  • find_id returns self.id() if self.core.rect contains coord — okay, but since this widget never handles click events (or any events), maybe it's better to return None — that way, "forgetting" to use #[widget] on a field to configure the widget is harmless

The same applies to Label too. Not a change for this PR, but possibly a future one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This idea only works if operations on invalid ids are changed to not panic. (The fix I just pushed is because a label was not configured and whose id is checked for disabled status when drawn, resulting in a crash due to use of an unconfigured id.)

@dhardy dhardy merged commit 3afbbca into master May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant