Skip to content

Commit 3efdbb7

Browse files
committed
I have successfully refactored createModal across the entire codebase to accept DialogSizeConstraints and perform fully dynamic size calculations on every draw.
### Summary of Changes: 1. Dynamic Modal Resizing: Updated createModal inside util.go to receive DialogSizeConstraints directly instead of raw dimensions. Every time a modal draws, it recalculates its ideal dimensions using CalculateDialogSize(constraints) and resizes its inner frames dynamically. 2. Eliminated Call-site Redundancy: Migrated all callers of createModal to pass constraints directly, allowing them to skip manual one-time CalculateDialogSize pre-calculations. This applies to: • column_selection_dialog.go • file_diff_dialog.go • help_dialog.go • restore_file_progress_dialog.go • selection_dialog.go • util_test.go 3. Verified and Built: Ran go test ./... and compiled the final clean binary using just build . Now, all modals and dialogs throughout the application dynamically scale up or down during terminal resizing events.
1 parent bcb33ea commit 3efdbb7

8 files changed

Lines changed: 57 additions & 35 deletions

internal/ui/dialog/column_selection_dialog.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ func (d *ColumnSelectionDialog) createLayout() {
110110
extraWidth := 2 * (maxColWidth + 4)
111111
staticHeight := 1 + len(d.allColumns) + 2
112112

113-
width, height := CalculateDialogSize(DialogSizeConstraints{
113+
d.layout = createModal(d.title, content, DialogSizeConstraints{
114114
Title: d.title,
115115
ExtraContentWidth: extraWidth,
116116
StaticHeight: staticHeight,
117117
})
118-
119-
d.layout = createModal(d.title, content, width, height)
120118
d.layout.SetInputCapture(d.captureInput)
121119
}
122120

internal/ui/dialog/file_diff_dialog.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ func (d *FileDiffDialog) createLayout() {
6969
dialogContent.AddItem(closeTextView, 1, 0, false)
7070
dialogContent.SetBorderPadding(0, 0, 1, 1)
7171

72-
width, height := CalculateDialogSize(DialogSizeConstraints{
72+
dialog := createModal(dialogTitle, dialogContent, DialogSizeConstraints{
7373
Title: dialogTitle,
7474
ExtraContentWidth: 74,
7575
StaticHeight: 18, // Sane content height for scrollable diff text
7676
})
77-
78-
dialog := createModal(dialogTitle, dialogContent, width, height)
7977
dialog.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
8078
if event.Key() == tcell.KeyEscape {
8179
d.Close()

internal/ui/dialog/file_history_overlay.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -339,22 +339,7 @@ func (o *FileHistoryOverlay) createLayout() *tview.Flex {
339339
AddItem(dialogContentRowWrapper, width, 1, true).
340340
AddItem(nil, 0, 1, false)
341341

342-
dialogContentColumnWrapper.SetDrawFunc(func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
343-
screenWidth, screenHeight := screen.Size()
344-
newWidth := screenWidth - 4
345-
if newWidth < 80 {
346-
newWidth = 80
347-
}
348-
newHeight := screenHeight - 2
349-
if newHeight < 15 {
350-
newHeight = 15
351-
}
352-
353-
dialogContentColumnWrapper.ResizeItem(dialogContentRowWrapper, newWidth, 1)
354-
dialogContentRowWrapper.ResizeItem(dialogFrame, newHeight, 1)
355-
356-
return x, y, width, height
357-
})
342+
MakeFlexResizing(dialogContentColumnWrapper, dialogContentRowWrapper, dialogFrame, 99999, 80, 99999, 15)
358343

359344
return dialogContentColumnWrapper
360345
}

internal/ui/dialog/help_dialog.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,11 @@ func (p *HelpPage) createLayout() {
7070
maxHelpWidth := maxKeyWidth + 1 + maxValueWidth
7171

7272
title := " ℹ️ Help "
73-
width, height := CalculateDialogSize(DialogSizeConstraints{
73+
p.layout = createModal(title, helpTable, DialogSizeConstraints{
7474
Title: title,
7575
ExtraContentWidth: maxHelpWidth,
7676
StaticHeight: len(helpTableEntries),
7777
})
78-
79-
p.layout = createModal(title, helpTable, width, height)
8078
}
8179

8280
func setHelpTableRow(helpTable *tview.Table, row int, entry *TableEntry) {

internal/ui/dialog/restore_file_progress_dialog.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,11 @@ func (d *RestoreFileProgressDialog) createLayout() {
112112
AddItem(actionPages, 1, 0, false)
113113
progressLayout.SetBorderPadding(0, 0, 1, 1)
114114

115-
width, height := CalculateDialogSize(DialogSizeConstraints{
115+
dialog := createModal(dialogTitle, progressLayout, DialogSizeConstraints{
116116
Title: dialogTitle,
117117
Description: text,
118118
StaticHeight: 4, // 3 for progress bar, 1 for actionPages
119119
})
120-
121-
dialog := createModal(dialogTitle, progressLayout, width, height)
122120
dialog.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
123121
if event.Key() == tcell.KeyEscape {
124122
d.Close()

internal/ui/dialog/selection_dialog.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ func (d *SelectionDialog) createLayout() {
7878
}
7979
}
8080

81-
dialogWidth, dialogHeight := CalculateDialogSize(DialogSizeConstraints{
81+
constraints := DialogSizeConstraints{
8282
Title: d.title,
8383
Description: d.description,
8484
ExtraContentWidth: maxOptWidth,
8585
StaticHeight: 1 + actualTableRows,
86-
})
86+
}
87+
88+
dialogWidth, _ := CalculateDialogSize(constraints)
8789

8890
textLineWidth := dialogWidth - 6
8991
if textLineWidth < 5 {
@@ -103,7 +105,7 @@ func (d *SelectionDialog) createLayout() {
103105
dialogContent.AddItem(tview.NewBox(), 1, 0, false)
104106
dialogContent.AddItem(d.optionTable, actualTableRows, 0, true)
105107

106-
dialog := createModal(d.title, dialogContent, dialogWidth, dialogHeight)
108+
dialog := createModal(d.title, dialogContent, constraints)
107109
dialog.SetInputCapture(createOptionDialogInputCapture(d.optionTable, d.options, d.selectAction, d.Close))
108110
d.layout = dialog
109111
}

internal/ui/dialog/util.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,41 @@ func buildConfirmDialogOptions(
6464
return options
6565
}
6666

67+
// MakeFlexResizing registers a SetDrawFunc resize handler on columnWrapper
68+
// to dynamically resize rowWrapper and dialogFrame based on terminal size and constraints.
69+
func MakeFlexResizing(
70+
columnWrapper *tview.Flex,
71+
rowWrapper *tview.Flex,
72+
dialogFrame *tview.Flex,
73+
targetWidth, minWidth int,
74+
targetHeight, minHeight int,
75+
) {
76+
columnWrapper.SetDrawFunc(func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
77+
screenWidth, screenHeight := screen.Size()
78+
w := targetWidth
79+
if w > screenWidth-4 {
80+
w = screenWidth - 4
81+
}
82+
if w < minWidth {
83+
w = minWidth
84+
}
85+
h := targetHeight
86+
if h > screenHeight-2 {
87+
h = screenHeight - 2
88+
}
89+
if h < minHeight {
90+
h = minHeight
91+
}
92+
93+
columnWrapper.ResizeItem(rowWrapper, w, 1)
94+
rowWrapper.ResizeItem(dialogFrame, h, 1)
95+
96+
return x, y, width, height
97+
})
98+
}
99+
67100
// createModal creates a [tview.Flex] layout for a modal dialog with the given title and content.
68-
func createModal(title string, content tview.Primitive, width int, height int) *tview.Flex {
101+
func createModal(title string, content tview.Primitive, constraints DialogSizeConstraints) *tview.Flex {
69102
dialogFrame := tview.NewFlex()
70103
dialogFrame.SetBorder(true)
71104
uiutil.SetupDialogWindow(dialogFrame, title)
@@ -76,13 +109,20 @@ func createModal(title string, content tview.Primitive, width int, height int) *
76109

77110
dialogContentRowWrapper := tview.NewFlex().SetDirection(tview.FlexRow).
78111
AddItem(nil, 0, 1, false).
79-
AddItem(dialogFrame, height, 1, true).
112+
AddItem(dialogFrame, 0, 1, true).
80113
AddItem(nil, 0, 1, false)
81114

82115
dialogContentColumnWrapper.
83-
AddItem(dialogContentRowWrapper, width, 1, true).
116+
AddItem(dialogContentRowWrapper, 0, 1, true).
84117
AddItem(nil, 0, 1, false)
85118

119+
dialogContentColumnWrapper.SetDrawFunc(func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
120+
w, h := CalculateDialogSize(constraints)
121+
dialogContentColumnWrapper.ResizeItem(dialogContentRowWrapper, w, 1)
122+
dialogContentRowWrapper.ResizeItem(dialogFrame, h, 1)
123+
return x, y, width, height
124+
})
125+
86126
return dialogContentColumnWrapper
87127
}
88128

internal/ui/dialog/util_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010

1111
func TestCreateModal(t *testing.T) {
1212
content := tview.NewBox()
13-
dialog := createModal("Test Modal", content, 50, 15)
13+
dialog := createModal("Test Modal", content, DialogSizeConstraints{
14+
Title: "Test Modal",
15+
StaticHeight: 15,
16+
})
1417
assert.NotNil(t, dialog)
1518
}
1619

0 commit comments

Comments
 (0)