1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00
neonmodem/ui/windows/postcreate/postcreate.go

123 lines
2.1 KiB
Go
Raw Normal View History

2023-01-02 15:33:12 -05:00
package postcreate
2023-01-02 15:29:29 -05:00
import (
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
2023-01-02 15:29:29 -05:00
tea "github.com/charmbracelet/bubbletea"
"github.com/mrusme/gobbs/aggregator"
"github.com/mrusme/gobbs/ui/ctx"
2023-01-03 16:46:16 -05:00
"github.com/mrusme/gobbs/ui/toolkit"
2023-01-02 15:29:29 -05:00
)
2023-01-02 15:33:12 -05:00
var (
WIN_ID = "postcreate"
)
2023-01-02 15:29:29 -05:00
type Model struct {
2023-01-03 16:46:16 -05:00
ctx *ctx.Ctx
tk *toolkit.ToolKit
xywh [4]int
2023-01-03 13:19:21 -05:00
textinput textinput.Model
textarea textarea.Model
inputFocused int
2023-01-02 15:29:29 -05:00
2023-01-03 13:19:21 -05:00
a *aggregator.Aggregator
2023-01-02 15:29:29 -05:00
action string
iface interface{}
replyToIdx int
replyTo string
2023-01-02 18:46:26 -05:00
viewcache string
viewcacheTextareaXY []int
2023-01-02 15:29:29 -05:00
}
func (m Model) Init() tea.Cmd {
return nil
}
func NewModel(c *ctx.Ctx) Model {
m := Model{
2023-01-03 16:46:16 -05:00
ctx: c,
tk: toolkit.New(
WIN_ID,
c.Theme,
c.Logger,
),
xywh: [4]int{0, 0, 0, 0},
2023-01-02 18:46:26 -05:00
inputFocused: 0,
action: "",
iface: nil,
replyToIdx: 0,
replyTo: "",
2023-01-02 18:46:26 -05:00
viewcache: "",
viewcacheTextareaXY: []int{0, 0, 0, 0},
2023-01-02 15:29:29 -05:00
}
m.textinput = textinput.New()
m.textinput.Placeholder = "Subject goes here"
m.textinput.Prompt = ""
2023-01-02 18:46:26 -05:00
m.textarea = textarea.New()
m.textarea.Placeholder = "Type in your post ..."
2023-01-02 18:46:26 -05:00
m.textarea.Prompt = ""
m.tk.KeymapAdd("tab", "tab", "tab") // TODO CONTINUE HERE
2023-01-03 16:46:16 -05:00
m.tk.KeymapAdd("submit", "submit", "ctrl+s")
2023-01-02 15:29:29 -05:00
m.a, _ = aggregator.New(m.ctx)
2023-01-03 16:46:16 -05:00
m.tk.SetViewFunc(buildView)
m.tk.SetMsgHandling(toolkit.MsgHandling{
OnKeymapKey: []toolkit.MsgHandlingKeymapKey{
{
ID: "tab",
Handler: handleTab,
},
2023-01-03 16:46:16 -05:00
{
ID: "submit",
Handler: handleSubmit,
},
},
OnWinOpenCmd: handleWinOpenCmd,
OnWinCloseCmd: handleWinCloseCmd,
})
2023-01-02 15:29:29 -05:00
return m
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
2023-01-03 16:46:16 -05:00
ret, cmds := m.tk.HandleMsg(&m, msg)
if ret {
return m, tea.Batch(cmds...)
2023-01-02 15:29:29 -05:00
}
2023-01-02 18:46:26 -05:00
var tcmd tea.Cmd
2023-01-02 15:29:29 -05:00
switch m.inputFocused {
case 0:
if !m.textinput.Focused() {
cmds = append(cmds, m.textinput.Focus())
}
m.textinput, tcmd = m.textinput.Update(msg)
case 1:
if !m.textarea.Focused() {
cmds = append(cmds, m.textarea.Focus())
}
m.textarea, tcmd = m.textarea.Update(msg)
2023-01-02 18:46:26 -05:00
}
cmds = append(cmds, tcmd)
2023-01-02 15:29:29 -05:00
return m, tea.Batch(cmds...)
}