Skip to content

Commit 73d886b

Browse files
committed
fix: responsiveness with qr code, remove controls
1 parent 96dbae0 commit 73d886b

File tree

9 files changed

+67
-166
lines changed

9 files changed

+67
-166
lines changed

ui/controls/controller.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

ui/controls/controls_test.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

ui/controls/model.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

ui/controls/style.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

ui/controls/view.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

ui/error.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package ui
22

33
import (
4-
"github.com/algorandfoundation/hack-tui/ui/controls"
54
"github.com/algorandfoundation/hack-tui/ui/style"
65
tea "github.com/charmbracelet/bubbletea"
76
"github.com/charmbracelet/lipgloss"
87
"strings"
98
)
109

1110
type ErrorViewModel struct {
12-
Height int
13-
Width int
14-
controls controls.Model
15-
Message string
11+
Height int
12+
Width int
13+
Message string
1614
}
1715

1816
func NewErrorViewModel(message string) ErrorViewModel {

ui/pages/transaction/controller.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ 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"
911
"github.com/algorandfoundation/hack-tui/api"
1012
tea "github.com/charmbracelet/bubbletea"
11-
"github.com/charmbracelet/lipgloss"
1213
)
1314

1415
func (m ViewModel) Init() tea.Cmd {
@@ -34,10 +35,10 @@ func (m *ViewModel) UpdateTxnURLAndQRCode() error {
3435
case "Not Participating": // This status means the account can never participate in consensus
3536
m.urlTxn = ""
3637
m.asciiQR = ""
37-
m.hint = fmt.Sprintf("%s is NotParticipating. Cannot register key.", m.Data.Address)
38+
m.hint = fmt.Sprintf("%s is NotParticipating. Cannot register key.", m.FormatedAddress())
3839
return nil
3940
}
40-
41+
m.IsOnline = isOnline
4142
fee := uint64(1000)
4243

4344
kr := &encoder.AUrlTxn{}
@@ -69,7 +70,7 @@ func (m *ViewModel) UpdateTxnURLAndQRCode() error {
6970
},
7071
}
7172

72-
m.hint = fmt.Sprintf("Scan this QR code to take %s Online.", m.Data.Address)
73+
m.hint = fmt.Sprintf("Scan this QR code to take %s Online.", m.FormatedAddress())
7374

7475
} else {
7576

@@ -81,7 +82,7 @@ func (m *ViewModel) UpdateTxnURLAndQRCode() error {
8182
Fee: &fee,
8283
}}
8384

84-
m.hint = fmt.Sprintf("Scan this QR code to take %s Offline.", m.Data.Address)
85+
m.hint = fmt.Sprintf("Scan this QR code to take %s Offline.", m.FormatedAddress())
8586
}
8687

8788
qrCode, err := kr.ProduceQRCode()
@@ -100,7 +101,6 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
100101
var cmd tea.Cmd
101102
switch msg := msg.(type) {
102103
// When the participation key updates, set the models data
103-
104104
case *api.ParticipationKey:
105105
m.Data = *msg
106106

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

112112
// Handle View Size changes
113113
case tea.WindowSizeMsg:
114-
if msg.Width != 0 && msg.Height != 0 {
115-
m.Width = msg.Width
116-
m.Height = max(0, msg.Height-lipgloss.Height(m.controls.View())-3)
117-
}
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))
118117
}
119-
120-
// Pass messages to controls
121-
m.controls, cmd = m.controls.HandleMessage(msg)
122118
return m, cmd
123119
}

ui/pages/transaction/model.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package transaction
22

33
import (
4+
"fmt"
45
"github.com/algorandfoundation/hack-tui/api"
56
"github.com/algorandfoundation/hack-tui/internal"
6-
"github.com/algorandfoundation/hack-tui/ui/controls"
7-
"github.com/charmbracelet/lipgloss"
7+
"github.com/algorandfoundation/hack-tui/ui/style"
88
)
99

10-
var green = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
11-
1210
type ViewModel struct {
1311
// Width is the last known horizontal lines
1412
Width int
@@ -19,21 +17,29 @@ type ViewModel struct {
1917
Data api.ParticipationKey
2018

2119
// Pointer to the State
22-
State *internal.StateModel
20+
State *internal.StateModel
21+
IsOnline bool
2322

2423
// Components
25-
controls controls.Model
24+
controls string
25+
navigation string
2626

2727
// QR Code, URL and hint text
2828
asciiQR string
2929
urlTxn string
3030
hint string
3131
}
3232

33+
func (m ViewModel) FormatedAddress() string {
34+
return fmt.Sprintf("%s...%s", m.Data.Address[0:4], m.Data.Address[len(m.Data.Address)-4:])
35+
}
36+
3337
// New creates and instance of the ViewModel with a default controls.Model
3438
func New(state *internal.StateModel) ViewModel {
3539
return ViewModel{
36-
State: state,
37-
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 )",
3844
}
3945
}

ui/pages/transaction/view.go

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +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-
)
10+
qrCode := qrStyle.Render(m.asciiQR)
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+
}
1719

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())
20+
url := ""
21+
if lipgloss.Width(m.urlTxn) > qrWidth {
22+
url = m.urlTxn[:(qrWidth-3)] + "..."
23+
} else {
24+
url = m.urlTxn
2425
}
2526

26-
if lipgloss.Height(qrRender) > m.Height {
27-
padHeight := max(0, m.Height-lipgloss.Height(m.controls.View())-1)
28-
padHString := strings.Repeat("\n", padHeight/2)
27+
var render string
28+
if qrWidth > m.Width || qrHeight+2 > m.Height {
2929
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)
3032
padWidth := max(0, m.Width-lipgloss.Width(text))
3133
padWString := strings.Repeat(" ", padWidth/2)
32-
return lipgloss.JoinVertical(
34+
paddedStr := lipgloss.JoinVertical(
3335
lipgloss.Left,
3436
padHString,
3537
lipgloss.JoinHorizontal(lipgloss.Left, padWString, style.Red.Render("QR Code too large to display... Please adjust terminal dimensions or font.")),
36-
padHString,
37-
m.controls.View())
38-
}
38+
)
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))
3948

40-
qrRenderPadHeight := max(0, m.Height-(lipgloss.Height(qrRender)-lipgloss.Height(m.controls.View()))-1)
41-
qrPad := strings.Repeat("\n", qrRenderPadHeight/2)
42-
return lipgloss.JoinVertical(
43-
lipgloss.Center,
44-
qrPad,
45-
qrRender,
46-
qrPad,
47-
m.controls.View(),
49+
}
50+
render = style.ApplyBorder(m.Width, m.Height, "8").Render(qrCode)
51+
}
52+
return style.WithNavigation(
53+
m.navigation,
54+
style.WithControls(
55+
m.controls,
56+
style.WithTitle(
57+
title,
58+
render,
59+
),
60+
),
4861
)
4962
}

0 commit comments

Comments
 (0)