adamdruppe / arsd

This is a collection of modules that I've released over the years. Most of them stand alone, or have just one or two dependencies in here, so you don't have to download this whole repo.
http://arsd-official.dpldocs.info/arsd.html
530 stars 125 forks source link

Add list option tag and getSelection #330

Closed andre2007 closed 2 years ago

andre2007 commented 2 years ago

Idea is, you can store arbitrary data (e.g. a class pointer) with the item. Therefore while reacting on ChangeEvent, you have all needed data.

import arsd.minigui;

class Foo
{
    int i;
}

void main()
{
    auto window = new MainWindow();
    auto list = new ListWidget(window);

    Foo foo1 = new Foo();
    foo1.i = 3;

    list.addOption("Sample", cast(ptrdiff_t) cast(void*) foo1);

    Foo foo2 = cast(Foo) cast(void*) list.options[0].tag;
    window.loop();
}
adamdruppe commented 2 years ago

OK, yeah, I like it. That's useful. My own concern is typing it as ptrdiff_t but storing pointers in it is technically prohibited by the GC rules; the gc might not see it and prematurely free the reference. Could of course just say that's the user's problem... but I might change that.

Gotta run i'll think about this more and probably merge it later tonight or tomorrow, just keep in mind if I change it before tagging the release you might need to adjust your code too..

andre2007 commented 2 years ago

You are right, the sample coding has this bug. The idea is, in some cases it is sufficient to store just a number which might point to a index of another array. In more sophisticated cases you want to store a object refence. Ptrdiff_t can handle both cases.

Another option would be to have multiple tag fields:

Ptrdiff_t tag
Object tagObject
string tagString
adamdruppe commented 2 years ago

Yeah, could be a fancy union or something too. I think the void* is a reasonable type for the low level then maybe a template interface on top could do better.

adamdruppe commented 2 years ago

I just tagged a thing but haven't had time to work back on this yet, I'll get there eventually though. I have a couple projects with a Thursday deadline so hopefully my time will open up again by then. I probably do have a plan here though.

adamdruppe commented 2 years ago

Well, I'm finally out of the deadline crunch, the stuff that isn't done we're just living without. So got a bit of time here.

Going with just this little change to your PR for now: https://github.com/adamdruppe/arsd/commit/e36512f1d487c9c7fbc6a97842f922aff186f79c

and we'll see about a nicer api in a bit, like adding a runtime type check or something. Just didn't want to keep you waiting another eternity.

andre2007 commented 2 years ago

Thank you Adam.