[GH-ISSUE #1] Color inheritance from parent primitives #1

Closed
opened 2026-03-04 01:01:01 +03:00 by kerem · 7 comments
Owner

Originally created by @dpotapov on GitHub (Jan 8, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/1

Hi,

First of all, thank you very much for this library! I'm going to use it to replace dialog in my scripts...

Just would like to propose an enhancement: ability to inherit background and foreground colors from parent primitives. Right now I have to explicitly call SetBackgroundColor/SetTextColor on every new element, which is a bit annoying.

Thanks!

Originally created by @dpotapov on GitHub (Jan 8, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/1 Hi, First of all, thank you very much for this library! I'm going to use it to replace [dialog](http://invisible-island.net/dialog/dialog.html#screenshot) in my scripts... Just would like to propose an enhancement: ability to inherit background and foreground colors from parent primitives. Right now I have to explicitly call SetBackgroundColor/SetTextColor on every new element, which is a bit annoying. Thanks!
kerem closed this issue 2026-03-04 01:01:01 +03:00
Author
Owner

@rivo commented on GitHub (Jan 9, 2018):

I'm not 100% sure what kind of solution you are looking for. Do you mean you would like to set package-wide default colors for background and text?

<!-- gh-comment-id:356219247 --> @rivo commented on GitHub (Jan 9, 2018): I'm not 100% sure what kind of solution you are looking for. Do you mean you would like to set package-wide default colors for background and text?
Author
Owner

@Spriithy commented on GitHub (Jan 9, 2018):

I think he means that if one sets Backgroung color of an element, then all its subsequent children will -by default- have their background color set to black. Same would go for foreground colors.

See his proposal as color settings inheritance.

<!-- gh-comment-id:356225335 --> @Spriithy commented on GitHub (Jan 9, 2018): I think he means that if one sets Backgroung color of an element, then all its subsequent children will -by default- have their background color set to black. Same would go for foreground colors. See his proposal as color settings inheritance.
Author
Owner

@rivo commented on GitHub (Jan 9, 2018):

Maybe I'm confused by what you mean by "children" then. For example, TextView inherits from Box and if I use SetBackgroundColor() (which is a function of Box), it will also be used for the TextView. Do you mean composed primitives such as Flex? I.e. make SetBackgroundColor() called on Flex apply to all its contained primitives?

Can you provide an example?

<!-- gh-comment-id:356241949 --> @rivo commented on GitHub (Jan 9, 2018): Maybe I'm confused by what you mean by "children" then. For example, `TextView` inherits from `Box` and if I use `SetBackgroundColor()` (which is a function of `Box`), it will also be used for the `TextView`. Do you mean composed primitives such as `Flex`? I.e. make `SetBackgroundColor()` called on `Flex` apply to all its contained primitives? Can you provide an example?
Author
Owner

@dpotapov commented on GitHub (Jan 9, 2018):

Let me illustrate what I'm trying to achieve...

Currently if I draw primitives on a blue screen, I have black background by default:
screenshot

I would like to achieve something like this without adding SetBackgroundColor() for each primitive:
screenshot2

Code example:

app := tview.NewApplication()
pages := tview.NewPages()

list := tview.NewList().ShowSecondaryText(false).
	AddItem("one", "", '1', nil).
	AddItem("two", "", '2', nil).
	AddItem("three", "", '3', nil)

table := tview.NewTable().
	SetCellSimple(0, 0, "Item 1").
	SetCellSimple(1, 0, "Item 2")
table.SetBorder(true)

// I want to illustrate here some complex layout with nested primitives
pages.AddPage("1",
	tview.NewFlex().
		AddItem(list, 0, 1, true).
		AddItem(tview.NewFlex().
			SetDirection(tview.FlexRow).
			AddItem(tview.NewBox(), 0, 1, false).
			AddItem(table, 0, 1, true), 0, 1, true),
	true, true)

// NewBackTitle draws blue background with dialog-like title
root := NewBackTitle(pages).SetTitle("Test application").SetTitleColor(tcell.ColorAqua)

if err := app.SetRoot(root, true).SetFocus(root).Run(); err != nil {
	panic(err)
}

Naive approach would be to have a function like app.SetBackgroundColor(tcell.ColorBlue) to set the background for entire application. However I don't want to stick to some particular implementation. Color inheritance could be a solution, but there might be different approaches (e.g. https://github.com/marcusolsson/tui-go/issues/74)

<!-- gh-comment-id:356313126 --> @dpotapov commented on GitHub (Jan 9, 2018): Let me illustrate what I'm trying to achieve... Currently if I draw primitives on a blue screen, I have black background by default: ![screenshot](https://i.imgur.com/TZlDWGu.png) I would like to achieve something like this without adding `SetBackgroundColor()` for each primitive: ![screenshot2](https://i.imgur.com/tn591aF.png) Code example: ```go app := tview.NewApplication() pages := tview.NewPages() list := tview.NewList().ShowSecondaryText(false). AddItem("one", "", '1', nil). AddItem("two", "", '2', nil). AddItem("three", "", '3', nil) table := tview.NewTable(). SetCellSimple(0, 0, "Item 1"). SetCellSimple(1, 0, "Item 2") table.SetBorder(true) // I want to illustrate here some complex layout with nested primitives pages.AddPage("1", tview.NewFlex(). AddItem(list, 0, 1, true). AddItem(tview.NewFlex(). SetDirection(tview.FlexRow). AddItem(tview.NewBox(), 0, 1, false). AddItem(table, 0, 1, true), 0, 1, true), true, true) // NewBackTitle draws blue background with dialog-like title root := NewBackTitle(pages).SetTitle("Test application").SetTitleColor(tcell.ColorAqua) if err := app.SetRoot(root, true).SetFocus(root).Run(); err != nil { panic(err) } ``` Naive approach would be to have a function like `app.SetBackgroundColor(tcell.ColorBlue)` to set the background for entire application. However I don't want to stick to some particular implementation. Color inheritance could be a solution, but there might be different approaches (e.g. https://github.com/marcusolsson/tui-go/issues/74)
Author
Owner

@rivo commented on GitHub (Jan 9, 2018):

Thanks for the explanation. I think I understand now what you mean. I'm gravitating towards something simpler than what they're discussing at tui-go. At the moment, I think it could be either the color inheritance solution for any primitives that contain other primitives (Flex, Pages, Frame, Form). Or a package-global style, something like this:

tview.Style.BackgroundColor = tcell.ColorBlue
tview.Style.PrimaryTextColor = tcell.ColorWhite

In your example, are you choosing the blue background because your entire application will have a blue background? Or is this just for that one page and other pages may be styled differently?

<!-- gh-comment-id:356330354 --> @rivo commented on GitHub (Jan 9, 2018): Thanks for the explanation. I think I understand now what you mean. I'm gravitating towards something simpler than what they're discussing at `tui-go`. At the moment, I think it could be either the color inheritance solution for any primitives that contain other primitives (Flex, Pages, Frame, Form). Or a package-global style, something like this: ```go tview.Style.BackgroundColor = tcell.ColorBlue tview.Style.PrimaryTextColor = tcell.ColorWhite ``` In your example, are you choosing the blue background because your entire application will have a blue background? Or is this just for that one page and other pages may be styled differently?
Author
Owner

@dpotapov commented on GitHub (Jan 9, 2018):

+1 for simplicity.

In my case the entire application will have a blue background.

<!-- gh-comment-id:356360313 --> @dpotapov commented on GitHub (Jan 9, 2018): +1 for simplicity. In my case the entire application will have a blue background.
Author
Owner

@rivo commented on GitHub (Jan 10, 2018):

For now, I've added a package-global variable Styles which can be adapted to your preferred look and feel.

I might introduce inherited styles for composed primitives when the need comes up. But for now, this change should help you.

<!-- gh-comment-id:356538633 --> @rivo commented on GitHub (Jan 10, 2018): For now, I've added a package-global variable `Styles` which can be adapted to your preferred look and feel. I might introduce inherited styles for composed primitives when the need comes up. But for now, this change should help you.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#1
No description provided.