feat: shift focus from one window to another. and make code consistent

This commit is contained in:
2021-12-31 00:04:58 +02:00
parent b32af9d55f
commit ba7e881516
5 changed files with 85 additions and 43 deletions
+7 -7
View File
@@ -143,30 +143,30 @@ func (s State) View() string {
} }
} }
checkbox := &blurredCheckbox checkbox := &BlurredCheckbox
// if the checkbox is focused // if the checkbox is focused
if s.FocusIndex == len(s.Inputs) { if s.FocusIndex == len(s.Inputs) {
checkbox = &focusedCheckbox checkbox = &FocusedCheckbox
// if tls is enabled // if tls is enabled
if s.TLS { if s.TLS {
checkbox = &focusedCheckedCheckbox checkbox = &FocusedCheckboxChecked
} }
} }
// if the checkbox is not focused and tls is enabled // if the checkbox is not focused and tls is enabled
if s.FocusIndex != len(s.Inputs) && s.TLS { if s.FocusIndex != len(s.Inputs) && s.TLS {
checkbox = &blurredCheckedCheckbox checkbox = &BlurredCheckboxChecked
} }
fmt.Fprintf(&sb, "\n%s\n", *checkbox) fmt.Fprintf(&sb, "\n%s\n", *checkbox)
button := &blurredButton button := &BlurredButton
if s.FocusIndex == len(s.Inputs)+1 { if s.FocusIndex == len(s.Inputs)+1 {
button = &focusedButton button = &FocusedButton
} }
fmt.Fprintf(&sb, "\n%s\n", *button) fmt.Fprintf(&sb, "\n%s\n", *button)
screen := lipgloss.JoinVertical(0, welcomeMsgStyle.Render(WelcomeMsg), ui.DialogStyle.Render(sb.String())) screen := lipgloss.JoinVertical(0, WelcomeMsgStyle.Render(WelcomeMsg), ui.DialogStyle.Render(sb.String()))
final := lipgloss.Place(ui.MainStyle.GetWidth(), ui.MainStyle.GetHeight(), lipgloss.Center, lipgloss.Top, screen) final := lipgloss.Place(ui.MainStyle.GetWidth(), ui.MainStyle.GetHeight(), lipgloss.Center, lipgloss.Top, screen)
+7 -7
View File
@@ -33,18 +33,18 @@ const WelcomeMsg = `
` `
var ( var (
welcomeMsgStyle = lipgloss.NewStyle(). WelcomeMsgStyle = lipgloss.NewStyle().
Padding(0, 5). Padding(0, 5).
MarginBottom(5) MarginBottom(5)
focusedButton = lipgloss.NewStyle(). FocusedButton = lipgloss.NewStyle().
Background(lipgloss.Color("105")). Background(lipgloss.Color("105")).
Foreground(lipgloss.Color("255")). Foreground(lipgloss.Color("255")).
MarginTop(1). MarginTop(1).
Padding(0, 2). Padding(0, 2).
Align(lipgloss.Center). Align(lipgloss.Center).
Render("Connect") Render("Connect")
blurredButton = lipgloss.NewStyle(). BlurredButton = lipgloss.NewStyle().
Background(lipgloss.Color("#809070")). Background(lipgloss.Color("#809070")).
Foreground(lipgloss.Color("#EEEEEE")). Foreground(lipgloss.Color("#EEEEEE")).
MarginTop(1). MarginTop(1).
@@ -52,22 +52,22 @@ var (
Align(lipgloss.Center). Align(lipgloss.Center).
Render("Connect") Render("Connect")
focusedCheckbox = lipgloss.NewStyle(). FocusedCheckbox = lipgloss.NewStyle().
Foreground(lipgloss.Color("105")). Foreground(lipgloss.Color("105")).
MarginTop(1). MarginTop(1).
Render("[ ] Enable TLS") Render("[ ] Enable TLS")
blurredCheckbox = lipgloss.NewStyle(). BlurredCheckbox = lipgloss.NewStyle().
Foreground(lipgloss.Color("#EEEEEE")). Foreground(lipgloss.Color("#EEEEEE")).
MarginTop(1). MarginTop(1).
Render("[ ] Enable TLS") Render("[ ] Enable TLS")
focusedCheckedCheckbox = lipgloss.NewStyle(). FocusedCheckboxChecked = lipgloss.NewStyle().
Foreground(lipgloss.Color("105")). Foreground(lipgloss.Color("105")).
MarginTop(1). MarginTop(1).
Render("[x] Enable TLS") Render("[x] Enable TLS")
blurredCheckedCheckbox = lipgloss.NewStyle(). BlurredCheckboxChecked = lipgloss.NewStyle().
Foreground(lipgloss.Color("#EEEEEE")). Foreground(lipgloss.Color("#EEEEEE")).
MarginTop(1). MarginTop(1).
Render("[x] Enable TLS") Render("[x] Enable TLS")
+3 -9
View File
@@ -30,22 +30,16 @@ type InputState struct {
func NewInputBox() InputState { func NewInputBox() InputState {
input := textinput.NewModel() input := textinput.NewModel()
input.Placeholder = "Send a message..." input.Placeholder = "Send a message..."
// input.Focus() input.Focus()
state := InputState{input, InputboxStyle} state := InputState{input, InputboxStyle.Copy()}
return state return state
} }
func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) { func (s InputState) Update(msg tea.Msg) (InputState, tea.Cmd) {
cmd := s.updateInputs(msg)
return s, cmd
}
func (s *InputState) updateInputs(msg tea.Msg) tea.Cmd {
var cmd tea.Cmd var cmd tea.Cmd
s.Input, cmd = s.Input.Update(msg) s.Input, cmd = s.Input.Update(msg)
return s, cmd
return cmd
} }
func (s *InputState) SetSize(width int) { func (s *InputState) SetSize(width int) {
+53 -5
View File
@@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
@@ -29,20 +30,29 @@ import (
"github.com/illusionman1212/gorc/ui" "github.com/illusionman1212/gorc/ui"
) )
type Window int
const (
Viewport = iota
InputBox
)
type State struct { type State struct {
Content string Content string
Reader *bufio.Reader Reader *bufio.Reader
Client *client.Client Client *client.Client
Viewport viewport.Model Viewport viewport.Model
Style lipgloss.Style Style lipgloss.Style
FocusIndex Window
InputBox InputState InputBox InputState
} }
func NewMainScreen() State { func NewMainScreen() State {
newViewport := viewport.Model{} newViewport := viewport.Model{
newViewport.HighPerformanceRendering = false HighPerformanceRendering: false,
newViewport.SetContent("") Wrap: true,
}
state := State{ state := State{
Content: "", Content: "",
@@ -50,6 +60,7 @@ func NewMainScreen() State {
Client: nil, Client: nil,
Viewport: newViewport, Viewport: newViewport,
Style: MessagesStyle.Copy(), Style: MessagesStyle.Copy(),
FocusIndex: InputBox,
InputBox: NewInputBox(), InputBox: NewInputBox(),
} }
return state return state
@@ -77,13 +88,50 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
} }
return s, s.readFromServer return s, s.readFromServer
case tea.KeyMsg:
key := msg.String()
switch key {
case "tab", "shift+tab":
if key == "tab" {
s.FocusIndex++
} else {
s.FocusIndex--
} }
s.Viewport, cmd = s.Viewport.Update(msg) if s.FocusIndex > 1 {
cmds = append(cmds, cmd) s.FocusIndex = 0
} else if s.FocusIndex < 0 {
s.FocusIndex = 1
}
switch s.FocusIndex {
case Viewport:
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("105"))
s.InputBox.Style = s.InputBox.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
s.InputBox.Input.Blur()
case InputBox:
s.Style = s.Style.Copy().BorderForeground(lipgloss.Color("#EEE"))
s.InputBox.Style = s.InputBox.Style.Copy().BorderForeground(lipgloss.Color("105"))
s.InputBox, cmd = s.InputBox.Update(msg) s.InputBox, cmd = s.InputBox.Update(msg)
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
cmds = append(cmds, textinput.Blink)
s.InputBox.Input.Focus()
}
return s, tea.Batch(cmds...)
}
}
switch s.FocusIndex {
case Viewport:
s.Viewport, cmd = s.Viewport.Update(msg)
cmds = append(cmds, cmd)
case InputBox:
s.InputBox, cmd = s.InputBox.Update(msg)
cmds = append(cmds, cmd)
}
return s, tea.Batch(cmds...) return s, tea.Batch(cmds...)
} }
+1 -1
View File
@@ -30,7 +30,7 @@ var (
MessagesStyle = lipgloss.NewStyle(). MessagesStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), true). Border(lipgloss.NormalBorder(), true).
Width(ui.MainStyle.GetWidth()). Width(ui.MainStyle.GetWidth()).
BorderForeground(lipgloss.Color("105")) BorderForeground(lipgloss.Color("#EEE"))
InputBoxHeight = 1 InputBoxHeight = 1
) )