Skip to content

Commit

Permalink
Migrate app.State to newer app.StateKey API
Browse files Browse the repository at this point in the history
Note: this breaks PR #190.
  • Loading branch information
diamondburned committed Nov 11, 2023
1 parent a36db6a commit 282ba97
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/diamondburned/chatkit v0.0.0-20231111052812-ca02e3a0ae01
github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20230307050941-20a05fa3a9df
github.com/diamondburned/gotk4/pkg v0.0.6-0.20230825053034-ad325703aa2e
github.com/diamondburned/gotkit v0.0.0-20231108044328-9ac96cd5a778
github.com/diamondburned/gotkit v0.0.0-20231111065921-0542a145d61e
github.com/diamondburned/ningen/v3 v3.0.1-0.20231109131259-9140d0873f98
github.com/dustin/go-humanize v1.0.0
github.com/enescakir/emoji v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20230307050941-20a05fa3a9df h1
github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20230307050941-20a05fa3a9df/go.mod h1:f+Kv3QPlRGEW0nv0oyEZ4qhCcOY9qhxKnmPmrzmBkLo=
github.com/diamondburned/gotk4/pkg v0.0.6-0.20230825053034-ad325703aa2e h1:fH47UZxCz7xvmJHQ1w+nizBZpDNHdYmHwMwnUAByaLg=
github.com/diamondburned/gotk4/pkg v0.0.6-0.20230825053034-ad325703aa2e/go.mod h1:Jr5xzUuGyLsoMtE9LzE/Lay1gD0ONiFWAmQ0+Z6KLeA=
github.com/diamondburned/gotkit v0.0.0-20231108044328-9ac96cd5a778 h1:Zu0e0YUhoz9IknVI1NfYPSV2qbsRevdrcrgISSmiJVA=
github.com/diamondburned/gotkit v0.0.0-20231108044328-9ac96cd5a778/go.mod h1:N9XvN0kELfwPD/6qn1tEZIhni1vphE91lo7Pn0CxA+Q=
github.com/diamondburned/gotkit v0.0.0-20231111065921-0542a145d61e h1:Ck/mTbJsDIrKKgZA5b91eJ7B5VNGaoYqwNSUZJig0Eg=
github.com/diamondburned/gotkit v0.0.0-20231111065921-0542a145d61e/go.mod h1:N9XvN0kELfwPD/6qn1tEZIhni1vphE91lo7Pn0CxA+Q=
github.com/diamondburned/ningen/v3 v3.0.1-0.20231109131259-9140d0873f98 h1:X6MZzOCMwLCUh193M6k66NARFIGy1fxWuINSOgBsYOI=
github.com/diamondburned/ningen/v3 v3.0.1-0.20231109131259-9140d0873f98/go.mod h1:PUr2nk5xo+A5hguLIe6qJzCFhTJxdN5/1i7Lr8iCWpQ=
github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=
Expand Down
15 changes: 9 additions & 6 deletions internal/message/composer/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ var inputWYSIWYG = prefs.NewBool(true, prefs.PropMeta{
Description: "Enable a semi-WYSIWYG feature that decorates the input Markdown text.",
})

// inputStateKey is the app state that stores the last input message.
var inputStateKey = app.NewStateKey[string]("input-state")

// NewInput creates a new Input widget.
func NewInput(ctx context.Context, ctrl InputController, chID discord.ChannelID) *Input {
i := Input{
Expand Down Expand Up @@ -119,12 +122,12 @@ func NewInput(ctx context.Context, ctrl InputController, chID discord.ChannelID)
start, end := i.Buffer.Bounds()

// Persist input.
cfg := app.AcquireState(ctx, "input-state")
inputState := inputStateKey.Acquire(ctx)
if end.Offset() == 0 {
cfg.Delete(chID.String())
inputState.Delete(chID.String())
} else {
text := i.Buffer.Text(start, end, false)
cfg.Set(chID.String(), text)
inputState.Set(chID.String(), text)
}
})

Expand All @@ -133,10 +136,10 @@ func NewInput(ctx context.Context, ctrl InputController, chID discord.ChannelID)
i.AddController(enterKeyer)

gtkutil.Async(ctx, func() func() {
var oldMessage string
inputState := inputStateKey.Acquire(ctx)

cfg := app.AcquireState(ctx, "input-state")
if !cfg.Get(chID.String(), &oldMessage) {
oldMessage, ok := inputState.Get(chID.String())
if !ok {
return nil
}

Expand Down

0 comments on commit 282ba97

Please sign in to comment.