Files
gorc/ui/app/app.go
T

130 lines
3.0 KiB
Go
Raw Normal View History

// gorc project
2022-01-24 04:24:58 +02:00
// Copyright (C) 2022 IllusionMan1212
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see https://www.gnu.org/licenses.
package app
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/illusionman1212/gorc/cmds"
2022-01-24 02:26:24 +02:00
"github.com/illusionman1212/gorc/irc"
"github.com/illusionman1212/gorc/irc/handler"
"github.com/illusionman1212/gorc/ui"
"github.com/illusionman1212/gorc/ui/login"
"github.com/illusionman1212/gorc/ui/mainscreen"
)
type Screen int
const (
Login Screen = iota
MainScreen
)
type UI struct {
2022-01-05 00:54:54 +02:00
CurrentScreen Screen
Login login.State
MainScreen mainscreen.State
}
type State struct {
2022-01-05 00:54:54 +02:00
UI UI
2022-01-24 02:26:24 +02:00
Client *irc.Client
}
2022-01-24 02:26:24 +02:00
func initialUiState(client *irc.Client) UI {
return UI{
2022-01-05 00:54:54 +02:00
Login: login.NewLogin(),
MainScreen: mainscreen.NewMainScreen(client),
}
}
func InitialState() *State {
2022-01-24 02:26:24 +02:00
client := &irc.Client{}
2022-01-05 00:54:54 +02:00
return &State{
2022-01-05 00:54:54 +02:00
Client: client,
UI: initialUiState(client),
}
}
func (s State) Init() tea.Cmd {
return textinput.Blink
}
func (s State) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return s, cmds.Quit(s.Client)
}
case tea.WindowSizeMsg:
ui.MainStyle = ui.MainStyle.
Width(msg.Width).
Height(msg.Height)
s.UI.Login.SetSize(msg.Width, msg.Height)
2022-01-05 00:54:54 +02:00
s.UI.MainScreen.SetSize(msg.Width, msg.Height)
return s, nil
case cmds.ConnectMsg:
2022-01-05 00:54:54 +02:00
host := s.UI.Login.Inputs[0].Value()
port := s.UI.Login.Inputs[1].Value()
channel := s.UI.Login.Inputs[2].Value()
nickname := s.UI.Login.Inputs[3].Value()
password := s.UI.Login.Inputs[4].Value()
tlsEnabled := s.UI.Login.TLS
2022-01-05 00:54:54 +02:00
s.UI.CurrentScreen = MainScreen
s.Client.Initialize(host, port, tlsEnabled)
2022-01-05 00:54:54 +02:00
s.Client.Register(nickname, password, channel)
2022-02-19 16:24:39 +02:00
s.Client.SetDay()
2022-01-20 05:08:39 +02:00
2022-01-24 02:26:24 +02:00
go handler.ReadLoop(s.Client)
2022-01-09 04:30:22 +02:00
return s, textinput.Blink
}
// switch between which screen is currently active and update its state
2022-01-05 00:54:54 +02:00
switch s.UI.CurrentScreen {
case Login:
2022-01-05 00:54:54 +02:00
state, cmd := s.UI.Login.Update(msg)
s.UI.Login = state
return s, cmd
case MainScreen:
2022-01-05 00:54:54 +02:00
state, cmd := s.UI.MainScreen.Update(msg)
s.UI.MainScreen = state
return s, cmd
}
return s, nil
}
func (s State) View() string {
// switch between which screen is currently active and render it.
2022-01-05 00:54:54 +02:00
switch s.UI.CurrentScreen {
case Login:
2022-01-05 00:54:54 +02:00
return s.UI.Login.View()
case MainScreen:
2022-01-05 00:54:54 +02:00
return s.UI.MainScreen.View()
}
return "Error: this screen shouldn't ever show"
}