Code Monkey home page Code Monkey logo

Comments (6)

sgraf812 avatar sgraf812 commented on July 30, 2024

Transparency is allowed by the pad function of vty it seems, but I don't see where it has a counterpart in bricks API.

from brick.

jtdaugherty avatar jtdaugherty commented on July 30, 2024

If I understand this right, I am hearing two requests:

  • Is there a way to do padding with transparency?
  • Is there a way to do placement in a container without knowing its size?

The answer to the first one is to take a look at translateBy, and the answer to the second is no: if you don't know how big the parent container is, you can't know how much your translation offset should be. But, it isn't too hard to do this generically. Here's a generic way to make a layer from a widget and place it at the bottom left. (I haven't tested this but it should be just about right.) The idea is to render the child first, then use its size and the parent container size (from the rendering context) to translate the child. Translation gives you the transparency you want, unlike padding. For inspiration you can look at how the "layer" variants of the centering functions are implemented (e.g. hCenterLayer).

bottomLeftCorner :: Widget a -> Widget a
bottomLeftCorner child =
  Widget Greedy Greedy $ do
    ctx <- getContext
    childResult <- render child
    let childHeight = result^.imageL.to imageHeight
        offsetX = 0
        offsetY = ctx^.availHeightL - childHeight
        offset = Location (offsetX, offsetY)
    return $ addResultOffset offset $ childResult & imageL .~ translateBy offset

from brick.

sgraf812 avatar sgraf812 commented on July 30, 2024

Thanks for the tips! I think I got what I want. I packaged it up in some margin* combinators. They seem to work, judging from fiddling around with them, but no guarantees:

marginLeft :: Padding -> Widget a -> Widget a
marginLeft padding p =
  let (pad, sz) = case padding of
        Max -> (id, Greedy)
        Pad i -> (const i, hSize p)
  in Widget sz (vSize p) $ do
      (availWidth, lim) <- calculateLim padding availWidthL
      result <- render $ hLimit lim p
      let offsetX = pad (availWidth - result^.imageL.to imageWidth)
          offset = Location (offsetX, 0)
      return $ addResultOffset offset $ result & imageL %~ translateX offsetX


marginRight :: Padding -> Widget a -> Widget a
marginRight padding p =
  let (pad, sz) = case padding of
        Max -> (id, Greedy)
        Pad i -> (const i, hSize p)
  in Widget sz (vSize p) $ do
      (availWidth, lim) <- calculateLim padding availWidthL
      result <- render $ hLimit lim p
      let childWidth = result^.imageL.to imageWidth
          width = childWidth + pad (availWidth - childWidth)
      return $ result & imageL %~ resizeWidth width


marginTop :: Padding -> Widget a -> Widget a
marginTop padding p =
  let (pad, sz) = case padding of
        Max -> (id, Greedy)
        Pad i -> (const i, vSize p)
  in Widget (hSize p) sz $ do
      (availHeight, lim) <- calculateLim padding availHeightL
      result <- render $ vLimit lim p
      let offsetY = pad (availHeight - result^.imageL.to imageHeight)
          offset = Location (0, offsetY)
      return $ addResultOffset offset $ result & imageL %~ translateY offsetY


marginBottom :: Padding -> Widget a -> Widget a
marginBottom padding p =
  let (pad, sz) = case padding of
        Max -> (id, Greedy)
        Pad i -> (const i, vSize p)
  in Widget (hSize p) sz $ do
      (availHeight, lim) <- calculateLim padding availHeightL
      result <- render $ vLimit lim p
      let childHeight = result^.imageL.to imageHeight
          height = childHeight + pad (availHeight - childHeight)
      return $ result & imageL %~ resizeHeight height


calculateLim :: Padding -> Lens' Context Int -> RenderM n (Int, Int)
calculateLim padding widthOrHeight = do
    c <- getContext
    let lim = case padding of
          Max -> c^.widthOrHeight
          Pad i -> c^.widthOrHeight - i
    return (c^.widthOrHeight, lim)

They're pretty similar to padLeft et al., feel free to use them if you want to.

from brick.

jtdaugherty avatar jtdaugherty commented on July 30, 2024

Looking at the implementation I'm not sure what these functions are supposed to do. Can you give a description of just the first one?

from brick.

sgraf812 avatar sgraf812 commented on July 30, 2024

It's more or less like the pad*, but the padding is 'transparent'. Think bottomLeftCorner = marginTop Max . marginRight Max.

So marginLeft is like padLeft, but instead of fill ' ' the padding characters, it translates over them. The resemblence should be more clear if you inline calculateLim.

marginTop is analogous, but marginBottom and marginRight have to resize the Image instead of translating it.

Is that clear enough? If not, I could make a screenshot of what I mean.

from brick.

jtdaugherty avatar jtdaugherty commented on July 30, 2024

That makes sense. Thanks!

from brick.

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.