Git Product home page Git Product logo

layout's People

Contributors

cancel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

layout's Issues

Help:how to understand this codes for LAY_HCENTER

in layout.h
360-1642103179134111
in oui.h
switch(flags & UI_HFILL)
{
case UI_HCENTER:
{
pkid->margins[dim] += (space-pkid->size[dim])/2 - pkid->margins[wdim];
}
break;
..............
}

but I calculated pkid->margins[dim] by OUI.H code, but I found it was not in the middle.

remove item

how do i remove an item without having to rebuild the entire context list?

Potential issue when child expands container using margin

While using layout library in my project I've come across somewhat unexpected layouting behaviour.

Minimal reproducible example follows:

  1. Create root item of arbitrary size, without any additional configuration it acts as layout container
  2. In that root insert row container with unfixed size
  3. In that row container insert item with fixed size, and margin from the bottom

Equivalent code:

    lay_id root = lay_item(ctx);
    lay_set_size_xy(ctx, root, 1, 100);

    lay_id row = lay_item(ctx);
    lay_set_contain(ctx, row, LAY_ROW);
    lay_insert(ctx, root, row);

    lay_id child = lay_item(ctx);
    lay_set_size_xy(ctx, child, 1, 50);
    lay_set_margins_ltrb(ctx, child, 0, 0, 0, 10);
    lay_insert(ctx, row, child);

In a case like that I would expect the following

  1. row's height becomes 60, since item's height is 50 + item's bottom margin is 10
  2. item is placed at the begginng of the row (row pos = [0, 20], item pos = [0,20])

It can be validated by a test:

    lay_run_context(ctx);
    lay_vec4 root_rect = lay_get_rect(ctx, root);
    lay_vec4 row_rect = lay_get_rect(ctx, row);
    lay_vec4 child_rect = lay_get_rect(ctx, child);

    LTEST_VEC4EQ(root_rect, 0, 0, 1, 100);
    LTEST_VEC4EQ(row_rect, 0, 20, 1, 60);
    LTEST_VEC4EQ(child_rect, 0, 20, 1, 50);

It is consistent with like other layout engines implement margins (like yoga layout or flexboxes in basically all modern browsers).

However, in current version of the library:

  1. row's height indeed calculated as 60
  2. but item is placed above the row, at position [0,15]

From my understanding, the culprit is the lay_arrange_overlay_squeezed_range function, specifically line 1104:

// in LAY_HCENTER case
rect[dim] += (space - rect[2 + dim]) / 2 - margins[wdim];

The following change seems to be fixing the problem, and doesn't break any of the tests

--- a/layout.h
+++ b/layout.h
@@ -1101,7 +1101,7 @@ void lay_arrange_overlay_squeezed_range(
         switch (b_flags & LAY_HFILL) {
             case LAY_HCENTER:
                 rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size);
-                rect[dim] += (space - rect[2 + dim]) / 2 - margins[wdim];
+                rect[dim] += (space - rect[2 + dim] - margins[wdim]) / 2;
                 break;
             case LAY_RIGHT:
                 rect[2 + dim] = lay_scalar_min(rect[2 + dim], min_size);

Before submitting an actual pull request I would like to ask:

  • Whether or not there is any actual reason this function implemented the way it is, or nobody just ever tried such use case
  • If you can spot any other side effects of the suggested chang which are not covered by tests, but may turn out to be a breaking change

Code not working as described

While reading through your code, I found something that may be an error.

layout/layout.h

Lines 783 to 784 in b0c0282

// width = start margin + calculated width + end margin
lay_scalar child_size = rect[dim] + rect[2 + dim] + pchild->margins[wdim];

From your comment, here should be pchild->margins[dim] + rect[2 + dim] + pchild->margins[wdim]?

Also, about the layout algorithm itself, margins are not allowed to overlap with other margins. Did I understand it correctly?

Preset/fixed item ID numbers

I would think what might sometimes be good to have is the mode for preset (fixed) item ID numbers, you will specify how many they are and then you will call the function to allocate that many, instead of calling lay_item for each one.

This could be done to check first if ctx->count is zero; if not, then it is an error, but if it is zero then it will set its capacity and count to the specified number, allocate them, and then initialize each item in a loop, similarly than lay_item already does.

(My own "smallxrm" library, which is an implementation of the X resource manager, has a xrm_init_quarks function with a similar use. You will allocate many items with fixed preset ID numbers from the beginning, which can be compile-time constants, rather than having to pass strings always or allocating them and assigning them to variables. (You can still add additional quarks afterward which do not have fixed numbers.))

Naming is dumb

Using names like lay_context_reset instead of lay_reset_context made sense when matching the style of the original project I wrote this for, but in hindsight it's pretty annoying. The functions will be renamed to lay_verb_noun where appropriate in an upcoming commit, and a regex to update existing code will be supplied.

LAY_RIGHT position doesn't add left margins

When using LAY_BOTTOM or LAY_RIGHT anchors for an item's behavior, it doens't subtract the "left" margin, leaving you with an invalid offset: (yellow rectangles are the margins)

image

Changing the code to the following fixes the problem:

case LAY_RIGHT:
    child_rect[dim] += space - child_rect[2 + dim] - child_margins[wdim] - child_margins[dim];
    break;

image

This might possibly be an issue in lay_arrange_overlay_squeezed_range as well, but I have not tested it.

I can do a pull request if you want.

help: how to deal with layout for lay_top or lay_down ?

this ui layout is very good,but I do not understand how tp deal with layout for lay_top or lay_down?
I had read codes, do not found
case lay_top:
some code;
break;
case lay_down:
some code;
break;
and so on
I don't know if you can understand what I mean?

LAY_RESTRICT in g++

Layout defines it like this if __GNUC__ is defined:

#define LAY_RESTRICT restrict

However, this is not valid on g++. Changing this to __restrict makes it compile on g++ 6.3.0.

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.