Git Product home page Git Product logo

Comments (7)

tig avatar tig commented on August 20, 2024 1

Also, I goofed and misnamed the branch here. That just adds to the confusion.

from terminal.gui.

tig avatar tig commented on August 20, 2024 1

If no Accept event hanlder is set, TextView works properly:

    [Theory]
    [InlineData (true, 0)]
    [InlineData (false, 1)]
    public void Accept_No_Handler_Enables_Default_Button_Accept (bool multiline, int expectedButtonAccept)
    {
        var superView = new Window ();
        var tv = new TextView ()
        {
            Multiline = multiline
        };
        var button = new Button ()
        {
            IsDefault = true,
        };

        superView.Add (tv, button);

        var buttonAccept = 0;
        button.Accept += ButtonAccept;

        tv.SetFocus ();
        Assert.True (tv.HasFocus);

        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (expectedButtonAccept, buttonAccept);

        button.SetFocus ();
        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (expectedButtonAccept + 1, buttonAccept);

        return;

        void ButtonAccept (object sender, CancelEventArgs e)
        {
            buttonAccept++;
        }
    }

This new test fails. I'll fix.

    [Theory]
    [InlineData (false, false, 0)]
    [InlineData (false, true, 1)]
    public void Accept_Handler_Cancel_Prevents_Default_Button_Accept (bool multiline, bool cancelAccept, int expectedButtonAccepts)
    {
        var superView = new Window ();
        var tv = new TextView ()
        {
            Multiline = multiline
        };
        var button = new Button ()
        {
            IsDefault = true,
        };

        superView.Add (tv, button);

        var buttonAccept = 0;
        button.Accept += ButtonAccept;

        var textViewAccept = 0;
        tv.Accept += TextViewAccept;

        tv.SetFocus ();
        Assert.True (tv.HasFocus);

        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (1, textViewAccept);
        Assert.Equal (expectedButtonAccepts, buttonAccept);

        button.SetFocus ();
        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (1, textViewAccept);
        Assert.Equal (expectedButtonAccepts + 1, buttonAccept);

        return;

        void TextViewAccept (object sender, CancelEventArgs e)
        {
            textViewAccept++;
            e.Cancel = cancelAccept;
        }

        void ButtonAccept (object sender, CancelEventArgs e)
        {
            buttonAccept++;
        }
    }

from terminal.gui.

tig avatar tig commented on August 20, 2024

This defines the correct behavior, I think...

    [Theory]
    [InlineData (false, 0)]
    [InlineData (true, 1)]
    public void Accept_Handler_Cancel_Prevents_Default_Button_Accept (bool cancelAccept, int expectedButtonAccepts)
    {
        var superView = new Window ();
        var tf = new TextField ();
        var button = new Button ()
        {
            IsDefault = true,
        };

        superView.Add (tf, button);

        var buttonAccept = 0;
        button.Accept += ButtonAccept;

        var textFieldAccept = 0;
        tf.Accept += TextFieldAccept;

        tf.SetFocus ();
        Assert.True (tf.HasFocus);

        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (1, textFieldAccept);
        Assert.Equal (expectedButtonAccepts, buttonAccept);

        button.SetFocus ();
        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (1, textFieldAccept);
        Assert.Equal (expectedButtonAccepts + 1, buttonAccept);

        return;

        void TextFieldAccept (object sender, CancelEventArgs e)
        {
            textFieldAccept++;
            e.Cancel = cancelAccept;
        }

        void ButtonAccept (object sender, CancelEventArgs e)
        {
            buttonAccept++;
        }
    }

    [Fact]
    public void Accept_No_Handler_Enables_Default_Button_Accept ()
    {
        var superView = new Window ();
        var tf = new TextField ();
        var button = new Button ()
        {
            IsDefault = true,
        };

        superView.Add (tf, button);

        var buttonAccept = 0;
        button.Accept += ButtonAccept;

        tf.SetFocus ();
        Assert.True (tf.HasFocus);

        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (1, buttonAccept);

        button.SetFocus ();
        superView.NewKeyDownEvent (Key.Enter);
        Assert.Equal (2, buttonAccept);

        return;

        void ButtonAccept (object sender, CancelEventArgs e)
        {
            buttonAccept++;
        }
    }

The bug is View.OnAccept should return null if there's no event handler and TextField should do this:

        AddCommand (Command.Accept, () => OnAccept () == false);

from terminal.gui.

BDisp avatar BDisp commented on August 20, 2024

@tig sorry for my confusion. TextView contains the Multiline property and not the TextField. If you use a TextView with Multiline as true and with focus, then the Enter key must be manipulated by the TextView and not by the button, right?

from terminal.gui.

tig avatar tig commented on August 20, 2024

@tig sorry for my confusion. TextView contains the Multiline property and not the TextField. If you use a TextView with Multiline as true and with focus, then the Enter key must be manipulated by the TextView and not by the button, right?

I think so.

But if textView.MultiLine == false, shouldn't it behave just like a TextField in this regard?

from terminal.gui.

BDisp avatar BDisp commented on August 20, 2024

I think so.

But if textView.MultiLine == false, shouldn't it behave just like a TextField in this regard?

Yes, in this case return false for the default button to process it.

from terminal.gui.

BDisp avatar BDisp commented on August 20, 2024

Also, I goofed and misnamed the branch here. That just adds to the confusion.

It was also my fault for confusing TextField with TextView, sorry. What matters is that it is corrected in the code and does not include the name of the branch or title. Furthermore, TextField was also improperly handling Accept.

from terminal.gui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.