japhb / Terminal-Widgets

Simple Raku widgets for full-screen TUIs
Artistic License 2.0
4 stars 3 forks source link

Request for an example that illustrates switching between Forms #5

Closed Xliff closed 1 year ago

Xliff commented 1 year ago

Consider the following piece of code:

use v6.c;

use Terminal::Widgets::Simple;
use Terminal::Widgets::Utils::Color;
my $app;

my @ittems = 'Item ' »~» <a b c d e f g>;

class Form2UI is TopLevel {
    has Form $.form .= new;

    method initial-layout($builder, $width, $height) {
        my %style;

        # NOTE: To see the effect of styling, try these:
        # %style = padding-width => [0, 2],
        #          border-width  => 1,
        #          margin-width  => 1;

        my $return = do with $builder {
            .plain-text(
              text  => 'Please select an item',
              h     => 3
             ),
            .menu(
              id => 'menu',
                h  => 20,
              y  => 2,

              :$.form,
              :%style,
              label         => "Select another Item",
              items         => @items,
            ),
            .node(
                .button(
                  :$.form,
                  :%style,

                  label          => 'Select',
                  process-input  => { $.by-id<menu>[ $.by-id<menu>.selected ] }),
                .button(  :$.form, :%style, label => 'Quit',
                                   process-input  => { $.terminal.quit }),
            )
        }

        $return;
    }
}

class FormUI is TopLevel {
    has Form $.form .= new;

    method initial-layout($builder, $width, $height) {
        my %style;

        # NOTE: To see the effect of styling, try these:
        # %style = padding-width => [0, 2],
        #          border-width  => 1,
        #          margin-width  => 1;

        my $return = do with $builder {
            .plain-text(
              text  => 'Select an Item',
              h     => 3
             ),
            .menu(
              id => 'menu',
                h  => 20,
              y  => 2,

              :$.form,
              :%style,
              label => "Select an Item",
              items => @items,
            ),
            .node(
                .button(  :$.form, :%style, label => 'Select Item',
                                   process-input  => { self.goto-page-2 }),
                .button(  :$.form, :%style, label => 'Quit',
                                   process-input  => { $.terminal.quit }),
            )
        }

        $return;
    }

    method goto-page-2 {
      my $vf = Form2UI.new(
        title     => 'Page 2',
        x         => 0,
        y         => 0,
        w         => $.terminal.w,
        h         => $.terminal.h,
        terminal  => $.terminal
      );

      $.terminal.set-toplevel($vf);
    }
}

sub MAIN {
  App.new.boot-to-screen(
    'form',
    FormUI,

    title => 'Page 1',
  );
}

Now this is intended for you to select an item on page 1, and then select another item from page 2. The switch between pages does not work. Can you fabricate an example that corrects the intent of the included code?