Skip to content

Commit

Permalink
add mouse support to buttons & fix other issues
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jul 10, 2024
1 parent 48e245c commit 3f00453
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 98 deletions.
16 changes: 10 additions & 6 deletions cmd/gopad.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {
log.Println("pprof enabled")
}

if mouse {
zone.NewGlobal()
defer zone.Close()
}

loadConfig(configDir, defaultConfigs)
if err := file.LoadLanguages(defaultConfigs); err != nil {
return err
Expand All @@ -84,7 +79,16 @@ func NewRootCmd(version string, defaultConfigs embed.FS) *cobra.Command {
lsClient := ls.New(version, config.LanguageServers, lspLogFile)
e := gopad.New(lsClient, version, getWorkspace(workspace, args), args)

p := tea.NewProgram(e, tea.WithAltScreen(), tea.WithMouseCellMotion(), tea.WithFilter(lsClient.Filter))
opts := []tea.ProgramOption{
tea.WithAltScreen(),
tea.WithFilter(lsClient.Filter),
}
if mouse {
zone.NewGlobal()
defer zone.Close()
opts = append(opts, tea.WithMouseCellMotion())
}
p := tea.NewProgram(e, opts...)
lsClient.SetProgram(p)
log.Println("running gopad")
if _, err := p.Run(); err != nil {
Expand Down
11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ module go.gopad.dev/gopad

go 1.22

replace go.lsp.dev/protocol => github.com/gopad-dev/protocol v0.0.0-20240529205148-623e5abff393
replace (
github.com/charmbracelet/bubbletea => github.com/gopad-dev/bubbletea v0.0.0-20240710221044-5b61a3ed2acc
github.com/charmbracelet/lipgloss => github.com/gopad-dev/lipgloss v0.0.0-20240710213804-a96d334a0998
github.com/lrstanley/bubblezone => github.com/gopad-dev/bubblezone v0.0.0-20240710213337-e95605959844
go.lsp.dev/protocol => github.com/gopad-dev/protocol v0.0.0-20240529205148-623e5abff393
)

require (
github.com/atotto/clipboard v0.1.4
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.26.5-0.20240605151746-bca513b1982b
github.com/charmbracelet/lipgloss v0.11.1-0.20240605151253-a724723338e3
github.com/charmbracelet/x/ansi v0.1.2
github.com/charmbracelet/x/ansi v0.1.3
github.com/charmbracelet/x/input v0.1.2
github.com/dustin/go-humanize v1.0.1
github.com/lrstanley/bubblezone v0.0.0-20240624011428-67235275f80c
Expand Down Expand Up @@ -45,6 +50,6 @@ require (
go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2 // indirect
go.lsp.dev/uri v0.3.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwN
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.26.5-0.20240605151746-bca513b1982b h1:IqnzS1grN+x+JrAxhBdZ+9dXffMA5r9E/10OC5sgPxE=
github.com/charmbracelet/bubbletea v0.26.5-0.20240605151746-bca513b1982b/go.mod h1:n5kk+aidUsbwmVVdIlFPfwHB44/aKFGZk4AKVxWWi90=
github.com/charmbracelet/lipgloss v0.11.1-0.20240605151253-a724723338e3 h1:mr/tgxpMK+EUnQguGGByux9bxLQMG3at24Xs+07BBYY=
github.com/charmbracelet/lipgloss v0.11.1-0.20240605151253-a724723338e3/go.mod h1:aA4Y43qJqCRSpm/LoDYWDsLFP9U/Z6h0O99AIjiwThc=
github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY=
github.com/charmbracelet/x/ansi v0.1.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.1.3 h1:RBh/eleNWML5R524mjUF0yVRePTwqN9tPtV+DPgO5Lw=
github.com/charmbracelet/x/ansi v0.1.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/input v0.1.2 h1:QJAZr33eOhDowkkEQ24rsJy4Llxlm+fRDf/cQrmqJa0=
github.com/charmbracelet/x/input v0.1.2/go.mod h1:LGBim0maUY4Pitjn/4fHnuXb4KirU3DODsyuHuXdOyA=
github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=
Expand All @@ -29,12 +25,16 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gopad-dev/bubbletea v0.0.0-20240710221044-5b61a3ed2acc h1:U8u/UL4GTOqHaFaIWoaxi8Z2nGUn2Qi6dMrQueMucxk=
github.com/gopad-dev/bubbletea v0.0.0-20240710221044-5b61a3ed2acc/go.mod h1:n5kk+aidUsbwmVVdIlFPfwHB44/aKFGZk4AKVxWWi90=
github.com/gopad-dev/bubblezone v0.0.0-20240710213337-e95605959844 h1:ldJatoCjFdpFvkA4UkOKgd/xY5Sn32LBpOVS4ihn0OM=
github.com/gopad-dev/bubblezone v0.0.0-20240710213337-e95605959844/go.mod h1:fg1a8uI49m1r+PmPmM3J1IPOaqPdm3wNMph9RH5MaUc=
github.com/gopad-dev/lipgloss v0.0.0-20240710213804-a96d334a0998 h1:wmIBy1/x4wDWglUY91CIZLg0DYIQxQS+IXHR7Ais3kw=
github.com/gopad-dev/lipgloss v0.0.0-20240710213804-a96d334a0998/go.mod h1:1Vsf4Pll1NIRR73U9dYxSt6KJHJzELfjlLFANbh7vVI=
github.com/gopad-dev/protocol v0.0.0-20240529205148-623e5abff393 h1:yWHxEIPaVShkstlusTFyzrlEBiDuTTPSLszg07AfYiw=
github.com/gopad-dev/protocol v0.0.0-20240529205148-623e5abff393/go.mod h1:ov/M9MmwMqAffXvXc19fwCUHHJxNosA74TdyMW6oCH8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lrstanley/bubblezone v0.0.0-20240624011428-67235275f80c h1:hu82xYs8yOIM1TSq+L5VIZeRsHVROpe3gL0qscUlXJA=
github.com/lrstanley/bubblezone v0.0.0-20240624011428-67235275f80c/go.mod h1:fMHACHXouhQO+NLAFvHEeKdVSzG7L/O1khqsvswCTmk=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand Down Expand Up @@ -89,8 +89,8 @@ golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
4 changes: 2 additions & 2 deletions gopad/config/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (c RawThemeConfig) Theme(ctx tea.Context) ThemeConfig {
PlaceholderStyle: ctx.NewStyle(),
},
Button: button.Styles{
Default: ctx.NewStyle().Padding(0, 1).Margin(0, 1),
Focus: ctx.NewStyle().Padding(0, 1).Margin(0, 1),
Default: c.UI.Menu.Entry.Style(ctx, colors).Padding(0, 1).Margin(0, 1),
Focus: c.UI.Menu.SelectedEntry.Style(ctx, colors).Padding(0, 1).Margin(0, 1),
},
FilePicker: filepicker.Styles{
DisabledCursor: ctx.NewStyle(),
Expand Down
32 changes: 16 additions & 16 deletions gopad/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@ func (e Editor) Update(ctx tea.Context, msg tea.Msg) (Editor, tea.Cmd) {
case tea.MouseDownMsg:
for _, z := range zone.GetPrefix(file.ZoneFileLinePrefix) {
switch {
case mouse.MatchesZone(msg, z, tea.MouseLeft):
row, col := f.GetFileZoneCursorPos(msg, z)
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseLeft):
row, col := f.GetFileZoneCursorPos(tea.MouseEvent(msg), z)
f.SetMark(row, col)
f.SetCursor(row, col)
return e, tea.Batch(cmds...)
Expand All @@ -615,34 +615,34 @@ func (e Editor) Update(ctx tea.Context, msg tea.Msg) (Editor, tea.Cmd) {
case tea.MouseUpMsg:
for _, z := range zone.GetPrefix(file.ZoneFileLinePrefix) {
switch {
case mouse.MatchesZone(msg, z, tea.MouseLeft):
row, col := f.GetFileZoneCursorPos(msg, z)
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseLeft):
row, col := f.GetFileZoneCursorPos(tea.MouseEvent(msg), z)
f.SetCursor(row, col)
if s := f.Selection(); s == nil || s.Zero() {
f.ResetMark()
}
cmds = append(cmds, f.Autocomplete().Update())
return e, tea.Batch(cmds...)
case mouse.MatchesZone(msg, z, tea.MouseLeft):
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseLeft):
i, _ := strconv.Atoi(strings.TrimPrefix(z.ID(), ZoneFilePrefix))
e.SetFile(i)
return e, tea.Batch(cmds...)
case mouse.MatchesZone(msg, z, tea.MouseRight):
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseRight):
// TODO: open context menu?
return e, tea.Batch(cmds...)
case mouse.MatchesZone(msg, z, tea.MouseMiddle):
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseMiddle):
i, _ := strconv.Atoi(strings.TrimPrefix(z.ID(), ZoneFilePrefix))
cmds = append(cmds, file.CloseFile(e.files[i].Name()))
return e, tea.Batch(cmds...)

case mouse.Matches(msg, ZoneFileLanguage, tea.MouseLeft):
case mouse.Matches(tea.MouseEvent(msg), ZoneFileLanguage, tea.MouseLeft):
cmds = append(cmds, overlay.Open(NewSetLanguageOverlay()))
return e, tea.Batch(cmds...)
case mouse.Matches(msg, ZoneFileLineEnding, tea.MouseLeft):
case mouse.Matches(tea.MouseEvent(msg), ZoneFileLineEnding, tea.MouseLeft):
log.Println("file line ending zone")
// cmds = append(cmds, overlay.Open(NewSetLineEndingOverlay()))
return e, tea.Batch(cmds...)
case mouse.Matches(msg, ZoneFileEncoding, tea.MouseLeft):
case mouse.Matches(tea.MouseEvent(msg), ZoneFileEncoding, tea.MouseLeft):
log.Println("file encoding zone")
// cmds = append(cmds, overlay.Open(NewSetEncodingOverlay()))
return e, tea.Batch(cmds...)
Expand All @@ -651,28 +651,28 @@ func (e Editor) Update(ctx tea.Context, msg tea.Msg) (Editor, tea.Cmd) {
case tea.MouseMotionMsg:
for _, z := range zone.GetPrefix(file.ZoneFileLinePrefix) {
switch {
case mouse.MatchesZone(msg, z, tea.MouseLeft):
row, col := f.GetFileZoneCursorPos(msg, z)
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseLeft):
row, col := f.GetFileZoneCursorPos(tea.MouseEvent(msg), z)
f.SetCursor(row, col)
return e, tea.Batch(cmds...)
}
}
case tea.MouseWheelMsg:
for _, z := range zone.GetPrefix(file.ZoneFileLinePrefix) {
switch {
case mouse.MatchesZone(msg, z, tea.MouseWheelLeft), mouse.MatchesZone(msg, z, tea.MouseWheelDown, tea.Shift):
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseWheelLeft), mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseWheelDown, tea.Shift):
f.MoveCursorLeft(1)
cmds = append(cmds, f.Autocomplete().Update())
return e, tea.Batch(cmds...)
case mouse.MatchesZone(msg, z, tea.MouseWheelRight), mouse.MatchesZone(msg, z, tea.MouseWheelUp, tea.Shift):
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseWheelRight), mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseWheelUp, tea.Shift):
f.MoveCursorRight(1)
cmds = append(cmds, f.Autocomplete().Update())
return e, tea.Batch(cmds...)
case mouse.MatchesZone(msg, z, tea.MouseWheelUp):
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseWheelUp):
f.MoveCursorUp(1)
cmds = append(cmds, f.Autocomplete().Update())
return e, tea.Batch(cmds...)
case mouse.MatchesZone(msg, z, tea.MouseWheelDown):
case mouse.MatchesZone(tea.MouseEvent(msg), z, tea.MouseWheelDown):
f.MoveCursorDown(1)
cmds = append(cmds, f.Autocomplete().Update())
return e, tea.Batch(cmds...)
Expand Down
10 changes: 2 additions & 8 deletions gopad/editor/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package file

import (
"fmt"
"log"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -466,14 +465,9 @@ func (f File) GetCursorForCharPos(row int, col int) (int, int) {
return p.row, p.col
}

func (f File) GetFileZoneCursorPos(msg any, z *zone.ZoneInfo) (int, int) {
mouseMsg := tea.MouseEvent(msg)
if !ok {
log.Println("msg is not a mouse event")
return 0, 0
}
func (f File) GetFileZoneCursorPos(msg tea.MouseEvent, z *zone.ZoneInfo) (int, int) {
row, _ := strconv.Atoi(strings.TrimPrefix(z.ID(), ZoneFileLinePrefix))
col, _ := z.Pos(mouseMsg)
col, _ := z.Pos(msg)
row, col = f.GetCursorForCharPos(row, col)
return row, col
}
Expand Down
18 changes: 10 additions & 8 deletions gopad/gopad.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
"github.com/lrstanley/bubblezone"

"go.gopad.dev/gopad/gopad/config"
"go.gopad.dev/gopad/gopad/editor"
"go.gopad.dev/gopad/gopad/ls"
"go.gopad.dev/gopad/internal/bubbles"
"go.gopad.dev/gopad/internal/bubbles/cursor"
"go.gopad.dev/gopad/internal/bubbles/mouse"
"go.gopad.dev/gopad/internal/bubbles/notifications"
"go.gopad.dev/gopad/internal/bubbles/overlay"
)
Expand Down Expand Up @@ -79,15 +80,16 @@ func (g Gopad) Update(ctx tea.Context, msg tea.Msg) (tea.Model, tea.Cmd) {
g.editor.Blur()
return g, tea.Batch(cmds...)

case tea.MouseEvent, tea.MouseDownMsg, tea.MouseUpMsg, tea.MouseMotionMsg:
case tea.MouseEvent, tea.MouseDownMsg, tea.MouseMotionMsg:
log.Printf("MouseMsg: %#v\n", msg)

//case tea.MouseEvent:
// switch {
// case mouse.Matches(msg, ZoneTheme, tea.MouseLeft /*tea.MouseActionRelease*/):
// cmds = append(cmds, overlay.Open(NewSetThemeOverlay()))
// return g, tea.Batch(cmds...)
// }
case tea.MouseUpMsg:
log.Printf("MouseMsg: %#v\n", msg)
switch {
case mouse.Matches(tea.MouseEvent(msg), ZoneTheme, tea.MouseLeft):
cmds = append(cmds, overlay.Open(NewSetThemeOverlay()))
return g, tea.Batch(cmds...)
}

case tea.KeyMsg:
log.Printf("KeyMsg: %#v\n", msg)
Expand Down
38 changes: 27 additions & 11 deletions internal/bubbles/button/button.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package button

import (
"fmt"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/lrstanley/bubblezone"

"go.gopad.dev/gopad/internal/bubbles/mouse"
)

type KeyMap struct {
Expand Down Expand Up @@ -32,8 +37,9 @@ func New(label string, onclick func() tea.Cmd) Model {
Label: label,
OnClick: onclick,

Styles: DefaultStyles,
KeyMap: DefaultKeyMap,
Styles: DefaultStyles,
KeyMap: DefaultKeyMap,
zonePrefix: zone.NewPrefix(),
}
}

Expand All @@ -44,7 +50,8 @@ type Model struct {
Styles Styles
KeyMap KeyMap

focus bool
focus bool
zonePrefix string
}

func (m *Model) Focused() bool {
Expand All @@ -59,17 +66,26 @@ func (m *Model) Blur() {
m.focus = false
}

func (m Model) zoneID() string {
return fmt.Sprintf("button:%s", m.zonePrefix)
}

func (m Model) Update(ctx tea.Context, msg tea.Msg) (Model, tea.Cmd) {
if !m.focus {
return m, nil
}
var cmds []tea.Cmd

switch msg := msg.(type) {
case tea.KeyMsg:
case tea.MouseUpMsg:
switch {
case key.Matches(msg, m.KeyMap.OK):
case mouse.Matches(tea.MouseEvent(msg), m.zoneID(), tea.MouseLeft):
cmds = append(cmds, m.OnClick())
return m, tea.Batch(cmds...)
}
case tea.KeyMsg:
if m.focus {
switch {
case key.Matches(msg, m.KeyMap.OK):
cmds = append(cmds, m.OnClick())
return m, tea.Batch(cmds...)
}
}
}

Expand All @@ -78,8 +94,8 @@ func (m Model) Update(ctx tea.Context, msg tea.Msg) (Model, tea.Cmd) {

func (m Model) View(ctx tea.Context) string {
if m.focus {
return m.Styles.Focus.Render(m.Label)
return zone.Mark(m.zoneID(), m.Styles.Focus.Render(m.Label))
}

return m.Styles.Default.Render(m.Label)
return zone.Mark(m.zoneID(), m.Styles.Default.Render(m.Label))
}
Loading

0 comments on commit 3f00453

Please sign in to comment.