Skip to content

Commit 200ee92

Browse files
committed
feat: adds standard borders
fix: responsiveness with qr code
1 parent 35f988f commit 200ee92

File tree

3 files changed

+59
-92
lines changed

3 files changed

+59
-92
lines changed

ui/pages/transaction/controller.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package transaction
33
import (
44
"encoding/base64"
55
"fmt"
6+
"github.com/algorandfoundation/hack-tui/ui/style"
7+
"github.com/charmbracelet/lipgloss"
68

79
"github.com/algorand/go-algorand-sdk/v2/types"
810
"github.com/algorandfoundation/algourl/encoder"
@@ -99,7 +101,6 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
99101
var cmd tea.Cmd
100102
switch msg := msg.(type) {
101103
// When the participation key updates, set the models data
102-
103104
case *api.ParticipationKey:
104105
m.Data = *msg
105106

@@ -110,13 +111,9 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
110111

111112
// Handle View Size changes
112113
case tea.WindowSizeMsg:
113-
if msg.Width != 0 && msg.Height != 0 {
114-
m.Width = msg.Width
115-
m.Height = msg.Height
116-
}
114+
borderRender := style.Border.Render("")
115+
m.Width = max(0, msg.Width-lipgloss.Width(borderRender))
116+
m.Height = max(0, msg.Height-lipgloss.Height(borderRender))
117117
}
118-
119-
// Pass messages to controls
120-
m.controls, cmd = m.controls.HandleMessage(msg)
121118
return m, cmd
122119
}

ui/pages/transaction/model.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ import (
44
"fmt"
55
"github.com/algorandfoundation/hack-tui/api"
66
"github.com/algorandfoundation/hack-tui/internal"
7-
"github.com/algorandfoundation/hack-tui/ui/controls"
8-
"github.com/charmbracelet/lipgloss"
7+
"github.com/algorandfoundation/hack-tui/ui/style"
98
)
109

11-
var green = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
12-
1310
type ViewModel struct {
1411
// Width is the last known horizontal lines
1512
Width int
@@ -20,10 +17,12 @@ type ViewModel struct {
2017
Data api.ParticipationKey
2118

2219
// Pointer to the State
23-
State *internal.StateModel
20+
State *internal.StateModel
21+
IsOnline bool
2422

2523
// Components
26-
controls controls.Model
24+
controls string
25+
navigation string
2726

2827
// QR Code, URL and hint text
2928
asciiQR string
@@ -38,7 +37,9 @@ func (m ViewModel) FormatedAddress() string {
3837
// New creates and instance of the ViewModel with a default controls.Model
3938
func New(state *internal.StateModel) ViewModel {
4039
return ViewModel{
41-
State: state,
42-
controls: controls.New(" (a)ccounts | (k)eys | " + green.Render("(t)xn") + " | shift+tab: back "),
40+
State: state,
41+
IsOnline: false,
42+
navigation: "| (a)ccounts | (k)eys | " + style.Green.Render("(t)xn") + " |",
43+
controls: "( shift+tab: back )",
4344
}
4445
}

ui/pages/transaction/view.go

Lines changed: 45 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,56 @@ import (
77
)
88

99
func (m ViewModel) View() string {
10-
qrRender := lipgloss.JoinVertical(
11-
lipgloss.Center,
12-
style.Yellow.Render(m.hint),
13-
"",
14-
qrStyle.Render(m.asciiQR),
15-
urlStyle.Render(m.urlTxn),
16-
)
17-
18-
if m.asciiQR == "" || m.urlTxn == "" {
19-
return lipgloss.JoinVertical(
20-
lipgloss.Center,
21-
"No QR Code or TxnURL available",
22-
"\n",
23-
m.controls.View())
24-
}
25-
26-
// Build QR Parts
27-
hint := yellow.Render(m.hint)
2810
qrCode := qrStyle.Render(m.asciiQR)
29-
url := urlStyle.Render(strings.Replace(m.urlTxn, m.Data.Address, m.FormatedAddress(), 1))
30-
31-
controls := m.controls.View()
32-
33-
qrFullRender := lipgloss.JoinVertical(
34-
lipgloss.Center,
35-
hint,
36-
"",
37-
qrCode,
38-
url,
39-
)
11+
qrWidth := lipgloss.Width(qrCode)
12+
qrHeight := lipgloss.Height(qrCode)
13+
title := ""
14+
if m.IsOnline {
15+
title = "Online Transaction"
16+
} else {
17+
title = "Offline Transaction"
18+
}
4019

41-
remainingHeight := max(0, m.Height-lipgloss.Height(controls))
42-
isLargeScreen := lipgloss.Height(qrFullRender) <= remainingHeight && lipgloss.Width(qrFullRender) <= m.Width
43-
isSmallScreen := lipgloss.Height(qrCode) <= remainingHeight && lipgloss.Width(qrCode) < m.Width
20+
url := ""
21+
if lipgloss.Width(m.urlTxn) > qrWidth {
22+
url = m.urlTxn[:(qrWidth-3)] + "..."
23+
} else {
24+
url = m.urlTxn
25+
}
4426

45-
if isLargeScreen {
46-
qrRenderPadHeight := max(0, remainingHeight-2-lipgloss.Height(qrFullRender))
47-
qrPad := strings.Repeat("\n", max(0, qrRenderPadHeight/2))
48-
if qrRenderPadHeight > 2 {
49-
return lipgloss.JoinVertical(
50-
lipgloss.Center,
51-
qrPad,
52-
qrFullRender,
53-
qrPad,
54-
m.controls.View(),
55-
)
56-
}
57-
return lipgloss.JoinVertical(
58-
lipgloss.Center,
59-
qrFullRender,
60-
m.controls.View(),
27+
var render string
28+
if qrWidth > m.Width || qrHeight+2 > m.Height {
29+
text := style.Red.Render("QR Code too large to display... Please adjust terminal dimensions or font.")
30+
padHeight := max(0, m.Height-lipgloss.Height(text))
31+
padHString := strings.Repeat("\n", padHeight/2)
32+
padWidth := max(0, m.Width-lipgloss.Width(text))
33+
padWString := strings.Repeat(" ", padWidth/2)
34+
paddedStr := lipgloss.JoinVertical(
35+
lipgloss.Left,
36+
padHString,
37+
lipgloss.JoinHorizontal(lipgloss.Left, padWString, style.Red.Render("QR Code too large to display... Please adjust terminal dimensions or font.")),
6138
)
62-
}
63-
if isSmallScreen {
64-
isQrPadded := lipgloss.Height(qrCode) < remainingHeight && lipgloss.Width(qrCode) < m.Width
65-
if isQrPadded {
66-
qrRenderPadHeight := max(0, remainingHeight-2-lipgloss.Height(qrCode))
67-
qrPad := strings.Repeat("\n", max(0, qrRenderPadHeight/2))
68-
return lipgloss.JoinVertical(
69-
lipgloss.Center,
70-
qrPad,
71-
qrCode,
72-
qrPad,
73-
controls)
39+
render = style.ApplyBorder(m.Width, m.Height, "8").Render(paddedStr)
40+
} else {
41+
qRemainingWidth := max(0, (m.Width-lipgloss.Width(qrCode))/2)
42+
qrCode = lipgloss.JoinHorizontal(lipgloss.Left, strings.Repeat(" ", qRemainingWidth), qrCode, strings.Repeat(" ", qRemainingWidth))
43+
qRemainingHeight := max(0, (m.Height-2-lipgloss.Height(qrCode))/2)
44+
if qrHeight+2 == m.Height {
45+
qrCode = lipgloss.JoinVertical(lipgloss.Center, style.Yellow.Render(m.hint), qrCode, urlStyle.Render(url))
46+
} else {
47+
qrCode = lipgloss.JoinVertical(lipgloss.Center, strings.Repeat("\n", qRemainingHeight), style.Yellow.Render(m.hint), qrCode, urlStyle.Render(url))
48+
7449
}
75-
return lipgloss.JoinVertical(
76-
lipgloss.Center,
77-
qrCode,
78-
controls)
50+
render = style.ApplyBorder(m.Width, m.Height, "8").Render(qrCode)
7951
}
80-
81-
padHeight := max(0, remainingHeight-lipgloss.Height(controls))
82-
padHString := strings.Repeat("\n", padHeight/2)
83-
text := red.Render("QR Code too large to display... Please adjust terminal dimensions or font.")
84-
padWidth := max(0, m.Width-lipgloss.Width(text))
85-
padWString := strings.Repeat(" ", padWidth/2)
86-
return lipgloss.JoinVertical(
87-
lipgloss.Left,
88-
padHString,
89-
lipgloss.JoinHorizontal(lipgloss.Left, padWString, red.Render("QR Code too large to display... Please adjust terminal dimensions or font.")),
90-
padHString,
91-
m.controls.View(),
52+
return style.WithNavigation(
53+
m.navigation,
54+
style.WithControls(
55+
m.controls,
56+
style.WithTitle(
57+
title,
58+
render,
59+
),
60+
),
9261
)
9362
}

0 commit comments

Comments
 (0)