<commit_before>#include <assert.h>
#include "TreeAction.h"

namespace ViWm {
namespace Actions
{
    void    reSplit( LayoutTree::SplitSide goodSide
                   , DesktopLayout &l
                   , WindowMakerState &state )
    {
        LayoutTree  *root = l[ state.currentScreen ].layoutRoot;

        if ( ! root ) return;

        LayoutTree  *selected = root->getSelected();

        // it's an end node, so must add a new Node
        if ( selected->getParent() == 0 )
        {
            LayoutNode  *n = new LayoutNode();

            n->addNode( selected );
            n->addNode( 0 );

            l[ state.currentScreen ].layoutRoot = n;
            l[ state.currentScreen ].initialSplit = goodSide;

            return;
        }

        LayoutNode  *parent = selected->getParent();

        // already well splited, so add a new node.
        if (parent->getLastDirection() == goodSide)
            parent->insertBefore( selected, 0 );

        // not the good split side, so we must
        // subdivide the parent node.
        else
        {
            LayoutNode  *newNode = new LayoutNode();
            parent->replace( selected, newNode );

            // order is important
            newNode->addNode( selected );
            newNode->addNode( 0 );
        }
    }

    Action::ReturnInfo HorizontalSplit::operator()( DesktopLayout &l, WindowMakerState &state )
    {
        reSplit( LayoutTree::SplitHorizontal, l, state );
        return NeedRelayout;
    }

    HorizontalSplit::HorizontalSplit( StringId display, StringId descr )
        : Action( display, descr )
    {}

    Action::ReturnInfo VerticalSplit::operator()( DesktopLayout &l, WindowMakerState &state )
    {
        reSplit( LayoutTree::SplitVertical, l, state );
        return NeedRelayout;
    }

    VerticalSplit::VerticalSplit( StringId display, StringId descr )
        : Action( display, descr )
    {}

    NodeRotate::NodeRotate( StringId display, StringId descr
                          , int directionAmount )
        : Action( display, descr )
        , amount( directionAmount )
    {}

    Action::ReturnInfo NodeRotate::operator()( DesktopLayout &l, WindowMakerState &state )
    {
        LayoutTree  *root = l[ state.currentScreen ].layoutRoot;

        if ( ! root ) return Nothing;

        LayoutTree  *selected = root->getSelected();

        // it's an end node, so must add a new Node
        if ( selected->getParent() == 0 )
            return Nothing;

        selected->getParent()->rotate( 1 );

        return NeedRelayout;
    }

    Action::ReturnInfo Relayout::operator ()( DesktopLayout& l, WindowMakerState &s )
    {
        /* yes, that's it */
        return NeedRelayout;
    }

    Relayout::Relayout( StringId display, StringId descr )
        : Action( display, descr )
    {}


    VerticalMove::VerticalMove(StringId display, StringId descr, int amount)
        : Action( display, descr ), _moveAmount( amount )
    {
        assert( amount != 0 );
    }

    LayoutNode* findWellOriented( LayoutTree::SplitSide direction, LayoutTree *src )
    {
        LayoutNode* parent = src->getParent();
        if ( parent == 0 ) return 0;

        if ( parent->getLastDirection() == direction )
            return parent;
        else return findWellOriented( direction, parent );
    }

    Action::ReturnInfo VerticalMove::operator ()( DesktopLayout& l, WindowMakerState &s )
    {
        LayoutTree* root = l[ s.currentScreen ].layoutRoot;
        if ( !root ) return Nothing;

        LayoutLeaf* selected = root->getSelected();
        if ( !selected ) return Nothing;

        const Rect& selectSize =
            selected->getParent() != 0
            ? selected->getParent()->getSelectedSize()
            : l[ s.currentScreen ].getSize();

        LayoutNode* goodParent =
            findWellOriented( LayoutTree::SplitVertical, selected );

        LayoutLeaf* nextSelection;

        if ( !goodParent || !goodParent->moveSelection( _moveAmount ) )
            return Nothing;

        if ( _moveAmount < 0 )
            goodParent->pickNode( selectSize.x + selectSize.width / 2 
                                , selectSize.y - LayoutNode::SplitWidth - 1 );
        else
            goodParent->pickNode( selectSize.x + selectSize.width / 2
                                , selectSize.y + selectSize.height + 1 );

        // our next window, still in the same screen.
        nextSelection = goodParent->getSelected();

        if ( s.current )
            s.current->SetTransparency( s.getConf().idleTransparency() );

        s.current = &nextSelection->getWindow();
        s.current->GiveFocus();

        return Nothing;
    }

    HorizontalMove::HorizontalMove(StringId display, StringId descr, int amount)
        : Action( display, descr ), _moveAmount( amount )
    {
        assert( amount != 0 );
    }

    Action::ReturnInfo HorizontalMove::operator ()( DesktopLayout& l
                                                  , WindowMakerState &s )
    {
        LayoutTree* root = l[ s.currentScreen ].layoutRoot;
        if ( !root ) return Nothing;

        LayoutLeaf* selected = root->getSelected();
        if ( !selected ) return Nothing;

        const Rect& selectSize =
            selected->getParent() != 0
            ? selected->getParent()->getSelectedSize()
            : l[ s.currentScreen ].getSize();

        LayoutNode* goodParent =
            findWellOriented( LayoutTree::SplitVertical, selected );

        LayoutLeaf* nextSelection;

        if ( goodParent && goodParent->moveSelection( _moveAmount ) )
        {
            if ( _moveAmount < 0 )
                goodParent->pickNode( selectSize.x - LayoutNode::SplitWidth - 1
                                    , selectSize.y + selectSize.height / 2 );
            else
                goodParent->pickNode( selectSize.x + selectSize.width + 1
                                    , selectSize.y + selectSize.height / 2 );

            // our next window, still in the same screen.
            nextSelection = goodParent->getSelected();
        }
        // we didn't find a split with the good orientation
        // so, we want to give focus to another screen, if
        // any.
        else if ( _moveAmount < 0 ) // we are moving to the left =)
        {<commit_msg>Fixes for tree movement<commit_after>#include <assert.h>
#include "TreeAction.h"

namespace ViWm {
namespace Actions
{
    void    reSplit( LayoutTree::SplitSide goodSide
                   , DesktopLayout &l
                   , WindowMakerState &state )
    {
        LayoutTree  *root = l[ state.currentScreen ].layoutRoot;

        if ( ! root ) return;

        LayoutTree  *selected = root->getSelected();

        // it's an end node, so must add a new Node
        if ( selected->getParent() == 0 )
        {
            LayoutNode  *n = new LayoutNode();

            n->addNode( selected );
            n->addNode( 0 );

            l[ state.currentScreen ].layoutRoot = n;
            l[ state.currentScreen ].initialSplit = goodSide;

            return;
        }

        LayoutNode  *parent = selected->getParent();

        // already well splited, so add a new node.
        if (parent->getLastDirection() == goodSide)
            parent->insertBefore( selected, 0 );

        // not the good split side, so we must
        // subdivide the parent node.
        else
        {
            LayoutNode  *newNode = new LayoutNode();
            parent->replace( selected, newNode );

            // order is important
            newNode->addNode( selected );
            newNode->addNode( 0 );
        }
    }

    Action::ReturnInfo HorizontalSplit::operator()( DesktopLayout &l, WindowMakerState &state )
    {
        reSplit( LayoutTree::SplitHorizontal, l, state );
        return NeedRelayout;
    }

    HorizontalSplit::HorizontalSplit( StringId display, StringId descr )
        : Action( display, descr )
    {}

    Action::ReturnInfo VerticalSplit::operator()( DesktopLayout &l, WindowMakerState &state )
    {
        reSplit( LayoutTree::SplitVertical, l, state );
        return NeedRelayout;
    }

    VerticalSplit::VerticalSplit( StringId display, StringId descr )
        : Action( display, descr )
    {}

    NodeRotate::NodeRotate( StringId display, StringId descr
                          , int directionAmount )
        : Action( display, descr )
        , amount( directionAmount )
    {}

    Action::ReturnInfo NodeRotate::operator()( DesktopLayout &l, WindowMakerState &state )
    {
        LayoutTree  *root = l[ state.currentScreen ].layoutRoot;

        if ( ! root ) return Nothing;

        LayoutTree  *selected = root->getSelected();

        // it's an end node, so must add a new Node
        if ( selected->getParent() == 0 )
            return Nothing;

        selected->getParent()->rotate( 1 );

        return NeedRelayout;
    }

    Action::ReturnInfo Relayout::operator ()( DesktopLayout& l, WindowMakerState &s )
    {
        /* yes, that's it */
        return NeedRelayout;
    }

    Relayout::Relayout( StringId display, StringId descr )
        : Action( display, descr )
    {}


    VerticalMove::VerticalMove(StringId display, StringId descr, int amount)
        : Action( display, descr ), _moveAmount( amount )
    {
        assert( amount != 0 );
    }

    LayoutNode* findWellOriented( LayoutTree::SplitSide direction, LayoutTree *src )
    {
        LayoutNode* parent = src->getParent();
        if ( parent == 0 ) return 0;

        if ( parent->getLastDirection() == direction )
            return parent;
        else return findWellOriented( direction, parent );
    }

    Action::ReturnInfo VerticalMove::operator ()( DesktopLayout& l, WindowMakerState &s )
    {
        LayoutTree* root = l[ s.currentScreen ].layoutRoot;
        if ( !root ) return Nothing;

        LayoutLeaf* selected = root->getSelected();
        if ( !selected ) return Nothing;

        const Rect& selectSize =
            selected->getParent() != 0
            ? selected->getParent()->getSelectedSize()
            : l[ s.currentScreen ].getSize();

        LayoutNode* goodParent =
            findWellOriented( LayoutTree::SplitHorizontal, selected );

        LayoutLeaf* nextSelection;

        if ( !goodParent || !goodParent->moveSelection( _moveAmount ) )
            return Nothing;

        if ( _moveAmount < 0 )
            goodParent->pickNode( selectSize.x + selectSize.width / 2 
                                , selectSize.y - LayoutNode::SplitWidth - 1 );
        else
            goodParent->pickNode( selectSize.x + selectSize.width / 2
                                , selectSize.y + selectSize.height + LayoutNode::SplitWidth + 1 );

        // our next window, still in the same screen.
        nextSelection = goodParent->getSelected();

        if ( s.current )
            s.current->SetTransparency( s.getConf().idleTransparency() );

        s.current = &nextSelection->getWindow();
        s.current->GiveFocus();

        return Nothing;
    }

    HorizontalMove::HorizontalMove(StringId display, StringId descr, int amount)
        : Action( display, descr ), _moveAmount( amount )
    {
        assert( amount != 0 );
    }

    Action::ReturnInfo HorizontalMove::operator ()( DesktopLayout& l
                                                  , WindowMakerState &s )
    {
        LayoutTree* root = l[ s.currentScreen ].layoutRoot;
        if ( !root ) return Nothing;

        LayoutLeaf* selected = root->getSelected();
        if ( !selected ) return Nothing;

        const Rect& selectSize =
            selected->getParent() != 0
            ? selected->getParent()->getSelectedSize()
            : l[ s.currentScreen ].getSize();

        LayoutNode* goodParent =
            findWellOriented( LayoutTree::SplitVertical, selected );

        LayoutLeaf* nextSelection;

        if ( goodParent && goodParent->moveSelection( _moveAmount ) )
        {
            if ( _moveAmount < 0 )
                goodParent->pickNode( selectSize.x - LayoutNode::SplitWidth - 1
                                    , selectSize.y + selectSize.height / 2 );
            else
                goodParent->pickNode( selectSize.x + selectSize.width + LayoutNode::SplitWidth + 1
                                    , selectSize.y + selectSize.height / 2 );

            // our next window, still in the same screen.
            nextSelection = goodParent->getSelected();
        }
        // we didn't find a split with the good orientation
        // so, we want to give focus to another screen, if
        // any.
        else if ( _moveAmount < 0 ) // we are moving to the left =)
        {
{'fraction_non_alphanumeric': 0.10581010864430798, 'fraction_numerical': 0.0036214769327664936, 'mean_word_length': 1.9175470831419386, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '729115', 'n_tokens_mistral': 3469, 'n_tokens_neox': 3270, 'n_words': 889}