Skip to content

Commit

Permalink
chore: add back navigation and responsive QR window
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Oct 24, 2024
1 parent 8545162 commit 6295d64
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ui/pages/accounts/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func New(state *internal.StateModel) ViewModel {
Width: 0,
Height: 0,
Data: state.Accounts,
controls: controls.New(" (g)enerate | " + green.Render("(a)ccounts") + " | (k)eys "),
controls: controls.New(" (g)enerate | " + green.Render("(a)ccounts") + " | (k)eys | (t)xn "),
}

m.table = table.New(
Expand Down
5 changes: 1 addition & 4 deletions ui/pages/transaction/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
case tea.WindowSizeMsg:
if msg.Width != 0 && msg.Height != 0 {
m.Width = msg.Width
m.Height = max(0, msg.Height-lipgloss.Height(m.controls.View()))

// If the QR code is too large, set the flag
m.QRWontFit = lipgloss.Width(m.asciiQR) > m.Width || lipgloss.Height(m.asciiQR) > m.Height
m.Height = max(0, msg.Height-lipgloss.Height(m.controls.View())-3)
}
}

Expand Down
5 changes: 1 addition & 4 deletions ui/pages/transaction/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ type ViewModel struct {
// Height is the last known vertical lines
Height int

// QRWontFit is a flag to indicate the QR code is too large to display
QRWontFit bool

// Participation Key
Data api.ParticipationKey

Expand All @@ -37,6 +34,6 @@ type ViewModel struct {
func New(state *internal.StateModel) ViewModel {
return ViewModel{
State: state,
controls: controls.New(" (a)ccounts | (k)eys | shift+tab: back "),
controls: controls.New(" (a)ccounts | (k)eys | " + green.Render("(t)xn") + " | shift+tab: back "),
}
}
35 changes: 26 additions & 9 deletions ui/pages/transaction/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,47 @@ package transaction

import (
"github.com/charmbracelet/lipgloss"
"strings"
)

func (m ViewModel) View() string {
qrRender := lipgloss.JoinVertical(
lipgloss.Center,
yellow.Render(m.hint),
"",
qrStyle.Render(m.asciiQR),
urlStyle.Render(m.urlTxn),
)

if m.asciiQR == "" || m.urlTxn == "" {
return lipgloss.JoinVertical(
lipgloss.Left,
lipgloss.Center,
"No QR Code or TxnURL available",
"\n",
m.controls.View())
}

if m.QRWontFit {
if lipgloss.Height(qrRender) > m.Height {
padHeight := max(0, m.Height-lipgloss.Height(m.controls.View())-1)
padHString := strings.Repeat("\n", padHeight/2)
text := red.Render("QR Code too large to display... Please adjust terminal dimensions or font.")
padWidth := max(0, m.Width-lipgloss.Width(text))
padWString := strings.Repeat(" ", padWidth/2)
return lipgloss.JoinVertical(
lipgloss.Left,
red.Width(m.Width-2).Render("QR Code too large to display... Please adjust terminal dimensions or font."),
padHString,
lipgloss.JoinHorizontal(lipgloss.Left, padWString, red.Render("QR Code too large to display... Please adjust terminal dimensions or font.")),
padHString,
m.controls.View())
}

qrRenderPadHeight := max(0, m.Height-(lipgloss.Height(qrRender)-lipgloss.Height(m.controls.View()))-1)
qrPad := strings.Repeat("\n", qrRenderPadHeight/2)
return lipgloss.JoinVertical(
lipgloss.Left,
yellow.Width(m.Width-2).Render(m.hint),
Padding1.Render(),
qrStyle.Render(m.asciiQR),
urlStyle.Width(m.Width-2).Render(m.urlTxn),
m.controls.View())
lipgloss.Center,
qrPad,
qrRender,
qrPad,
m.controls.View(),
)
}
12 changes: 7 additions & 5 deletions ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.page = KeysPage
return m, accounts.EmitAccountSelected(m.accountsPage.SelectedAccount())
case "t":
if m.page == AccountsPage {
return m, nil
}
m.page = TransactionPage
// If there isn't a key already, select the first record
// If there isn't a key already, select the first record for that account
if m.keysPage.SelectedKey() == nil && m.Data != nil {
data := *m.Data.ParticipationKeys
return m, keys.EmitKeySelected(&data[0])
acct := m.accountsPage.SelectedAccount()
for i, key := range data {
if key.Address == acct.Address {
return m, keys.EmitKeySelected(&data[i])
}
}
}
// Navigate to the transaction page
return m, keys.EmitKeySelected(m.keysPage.SelectedKey())
Expand Down

0 comments on commit 6295d64

Please sign in to comment.