Skip to content

[rmkit][ui] Widget ownership model #72

Open
@mrichards42

Description

@mrichards42

What's the "correct" way to keep references to widgets? It looks like Scene ends up taking ownership when you call add, which makes sense, since you need the widgets to live at least as long as the scene. Assuming a single main scene which lasts the entire lifetime of the program, it seems like it doesn't matter all that much, so storing raw pointers is perfectly reasonable, e.g.

class App {
public:
  ui::Text * textWidget;
  App() {
    auto scene = ui::make_scene();
    textWidget = new ui::Text(...); // nobody owns this yet
    scene->add(myText);             // now scene owns this
    ui::MainLoop::set_scene(scene); // scene owned by MainLoop, so textWidget won't be destroyed
  }
};

I'm getting a little tripped up with widgets that use overlays, like Dialog. I think Dialog and Scene have a circular reference? I added some logging and never saw a Dialog get destroyed, unless I deleted it myself, which led to unpredictable behavior (since Scene still thought it owned the dialog).

// Like other widgets, this starts out unowned
auto dlg = new Dialog(...);

// Showing a dialog does roughly this:
dlg->scene = ui::make_scene(); // scene owned by dlg
dlg->scene->add(dlg);          // dlg owned by scene
ui::MainLoop::show_overlay(dlg->scene); // scene owned by MainLoop

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions