Skip to content

Commit

Permalink
Merge pull request #163 from jorgerojas26/sidebar-layout
Browse files Browse the repository at this point in the history
feat: add offset layout to the sidebar
  • Loading branch information
jorgerojas26 authored Feb 15, 2025
2 parents 00e8b4e + ff062f3 commit 0d3a141
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ URL = 'postgres://postgres:urlencodedpassword@localhost:5432/foo'
[application]
DefaultPageSize = 300
DisableSidebar = false
SidebarOverlay = false
```

The `[aplication]` section is used to define some app settings. Not all settings are available yet, this is a work in progress.
Expand Down
1 change: 1 addition & 0 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func defaultConfig() *Config {
return &Config{
AppConfig: &models.AppConfig{
DefaultPageSize: 300,
SidebarOverlay: false,
},
}
}
Expand Down
113 changes: 81 additions & 32 deletions components/results_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ type ResultsTableState struct {

type ResultsTable struct {
*tview.Table
state *ResultsTableState
Page *tview.Pages
Wrapper *tview.Flex
Menu *ResultsTableMenu
Filter *ResultsTableFilter
Error *tview.Modal
Loading *tview.Modal
Pagination *Pagination
Editor *SQLEditor
EditorPages *tview.Pages
ResultsInfo *tview.TextView
Tree *Tree
Sidebar *Sidebar
DBDriver drivers.Driver
state *ResultsTableState
Page *tview.Pages
Wrapper *tview.Flex
Menu *ResultsTableMenu
Filter *ResultsTableFilter
Error *tview.Modal
Loading *tview.Modal
Pagination *Pagination
Editor *SQLEditor
EditorPages *tview.Pages
ResultsInfo *tview.TextView
Tree *Tree
Sidebar *Sidebar
SidebarContainer *tview.Flex
DBDriver drivers.Driver
}

func NewResultsTable(listOfDBChanges *[]models.DBDMLChange, tree *Tree, dbdriver drivers.Driver) *ResultsTable {
Expand Down Expand Up @@ -104,18 +105,22 @@ func NewResultsTable(listOfDBChanges *[]models.DBDMLChange, tree *Tree, dbdriver
Tree: tree,
DBDriver: dbdriver,
Sidebar: sidebar,
// SidebarContainer is only used when AppConfig.SidebarOverlay is false.
SidebarContainer: tview.NewFlex(),
}

// When AppConfig.SidebarOverlay is true, the sidebar is added as a page to the table.Page.
// When AppConfig.SidebarOverlay is false, the sidebar is added to the table.SidebarContainer.
table.Page.AddPage(pageNameSidebar, table.Sidebar, false, false)

table.SetSelectable(true, true)
table.SetBorders(true)
table.SetFixed(1, 0)
table.SetInputCapture(table.tableInputCapture)
table.SetSelectedStyle(tcell.StyleDefault.Background(app.Styles.SecondaryTextColor).Foreground(tview.Styles.ContrastSecondaryTextColor))
table.Page.AddPage(pageNameSidebar, table.Sidebar, false, false)

table.SetSelectionChangedFunc(func(row, col int) {
table.SetSelectionChangedFunc(func(_, _ int) {
if table.GetShowSidebar() {
logger.Info("table.SetSelectionChangedFunc", map[string]any{"row": row, "col": col})
go table.UpdateSidebar()
}
})
Expand All @@ -133,10 +138,23 @@ func (table *ResultsTable) WithFilter() *ResultsTable {
table.Menu = menu
table.Filter = filter

table.Wrapper.AddItem(menu.Flex, 3, 0, false)
table.Wrapper.AddItem(filter.Flex, 3, 0, false)
table.Wrapper.AddItem(table, 0, 1, true)
table.Wrapper.AddItem(table.Pagination, 3, 0, false)
if App.Config().SidebarOverlay {
table.Wrapper.AddItem(menu, 3, 0, false)
table.Wrapper.AddItem(filter, 3, 0, false)
table.Wrapper.AddItem(table, 0, 1, true)
table.Wrapper.AddItem(table.Pagination, 3, 0, false)
} else {
tableContainer := tview.NewFlex().SetDirection(tview.FlexColumnCSS)
tableContainer.AddItem(menu, 3, 0, false)
tableContainer.AddItem(filter, 3, 0, false)
tableContainer.AddItem(table, 0, 1, true)
tableContainer.AddItem(table.Pagination, 3, 0, false)
tableContainer.SetBorder(true)

table.SidebarContainer.AddItem(tableContainer, 0, 4, true)

table.Wrapper.AddItem(table.SidebarContainer, 0, 1, true)
}

go table.subscribeToFilterChanges()

Expand Down Expand Up @@ -1313,10 +1331,19 @@ func (table *ResultsTable) ShowSidebar(show bool) {

if show {
table.UpdateSidebar()
table.Page.SendToFront(pageNameSidebar)
table.Page.ShowPage(pageNameSidebar)

if App.Config().SidebarOverlay {
table.Page.SendToFront(pageNameSidebar)
table.Page.ShowPage(pageNameSidebar)
} else {
table.SidebarContainer.AddItem(table.Sidebar, 0, 1, true)
}
} else {
table.Page.HidePage(pageNameSidebar)
if App.Config().SidebarOverlay {
table.Page.HidePage(pageNameSidebar)
} else {
table.SidebarContainer.RemoveItem(table.Sidebar)
}
App.SetFocus(table)
}
}
Expand All @@ -1326,22 +1353,19 @@ func (table *ResultsTable) UpdateSidebar() {
selectedRow, _ := table.GetSelection()

if selectedRow > 0 {
tableX, _, _, tableHeight := table.GetRect()
_, _, tableInnerWidth, _ := table.GetInnerRect()
_, tableMenuY, _, tableMenuHeight := table.Menu.GetRect()
_, _, _, tableFilterHeight := table.Filter.GetRect()
_, _, _, tablePaginationHeight := table.Pagination.GetRect()

sidebarWidth := (tableInnerWidth / 4)
sidebarHeight := tableHeight + tableMenuHeight + tableFilterHeight + tablePaginationHeight + 1
if App.Config().SidebarOverlay {
table.recomputeSidebarPosition()
}

table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, sidebarHeight)
table.Sidebar.Clear()

for i := 1; i < len(columns); i++ {
name := columns[i][0]
colType := columns[i][1]

sidebarWidth := table.getSidebarWidth()

text := table.GetCell(selectedRow, i-1).Text
title := name

Expand Down Expand Up @@ -1373,6 +1397,31 @@ func (table *ResultsTable) UpdateSidebar() {
}
}

func (table *ResultsTable) getSidebarWidth() int {
if App.Config().SidebarOverlay {
_, _, tableInnerWidth, _ := table.GetInnerRect()
return (tableInnerWidth / 4)
}

_, _, width, _ := table.SidebarContainer.GetInnerRect()

return width
}

// Only used when AppConfig.SidebarOverlay is true.
func (table *ResultsTable) recomputeSidebarPosition() {
tableX, _, _, tableHeight := table.GetRect()
_, _, tableInnerWidth, _ := table.GetInnerRect()
_, tableMenuY, _, tableMenuHeight := table.Menu.GetRect()
_, _, _, tableFilterHeight := table.Filter.GetRect()
_, _, _, tablePaginationHeight := table.Pagination.GetRect()

sidebarWidth := (tableInnerWidth / 4)
sidebarHeight := tableHeight + tableMenuHeight + tableFilterHeight + tablePaginationHeight + 1

table.Sidebar.SetRect(tableX+tableInnerWidth-sidebarWidth, tableMenuY, sidebarWidth, sidebarHeight)
}

func (table *ResultsTable) isAnInsertedRow(rowIndex int) (isAnInsertedRow bool, DBChangeIndex int) {
for i, dmlChange := range *table.state.listOfDBChanges {
for _, value := range dmlChange.Values {
Expand Down
1 change: 1 addition & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
type AppConfig struct {
DefaultPageSize int
DisableSidebar bool
SidebarOverlay bool
}

type Connection struct {
Expand Down

0 comments on commit 0d3a141

Please sign in to comment.