Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 53 additions & 43 deletions docs/content/guides/09-refs-and-clicks.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,13 @@ Both input methods call the same `adjust*` methods, so the behavior stays consis
Modals handle mouse clicks internally. You don't need refs or `HandleMouse` for buttons inside a modal. Use the `onActivate` attribute on focusable elements instead:

```gsx
<modal open={s.showDialog} class="justify-center items-center">
<div class="border-rounded p-2 flex-col gap-1 w-40">
<span class="font-bold">Delete Item?</span>
<modal open={c.showResetModal} class="justify-center items-center">
<div class="border-rounded p-2 flex-col gap-1 w-36 items-center">
<span class="font-bold text-yellow">Reset Colors?</span>
<span class="font-dim">This will restore default values.</span>
<div class="flex gap-2 justify-center">
<button class="px-2 border-rounded focusable" onActivate={s.onDelete}>Delete</button>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these other changes are just to keep code the guide in sync with the example, working, code (which has minor changes).

<button class="px-2 border-rounded focusable" onActivate={s.onCancel}>Cancel</button>
<button class="px-2 text-green font-bold border-single focusable" onActivate={c.cancelReset}>Cancel</button>
<button class="px-2 text-red font-bold border-single focusable" onActivate={c.resetColors}>Yes, Reset</button>
</div>
</div>
</modal>
Expand Down Expand Up @@ -229,31 +230,35 @@ type colorMixer struct {
green *tui.State[int]
blue *tui.State[int]

redUpBtn *tui.Ref
redDnBtn *tui.Ref
greenUpBtn *tui.Ref
greenDnBtn *tui.Ref
blueUpBtn *tui.Ref
blueDnBtn *tui.Ref
presetBtns *tui.RefMap[string]
activePreset *tui.State[string]
showReset *tui.State[bool]
redUpBtn *tui.Ref
redDnBtn *tui.Ref
greenUpBtn *tui.Ref
greenDnBtn *tui.Ref
blueUpBtn *tui.Ref
blueDnBtn *tui.Ref
presetBtns *tui.RefMap[string]
activePreset *tui.State[string]

resetBtn *tui.Ref
showResetModal *tui.State[bool]
}

func ColorMixer() *colorMixer {
return &colorMixer{
red: tui.NewState(128),
green: tui.NewState(64),
blue: tui.NewState(200),
redUpBtn: tui.NewRef(),
redDnBtn: tui.NewRef(),
greenUpBtn: tui.NewRef(),
greenDnBtn: tui.NewRef(),
blueUpBtn: tui.NewRef(),
blueDnBtn: tui.NewRef(),
presetBtns: tui.NewRefMap[string](),
activePreset: tui.NewState(""),
showReset: tui.NewState(false),
red: tui.NewState(128),
green: tui.NewState(64),
blue: tui.NewState(200),
redUpBtn: tui.NewRef(),
redDnBtn: tui.NewRef(),
greenUpBtn: tui.NewRef(),
greenDnBtn: tui.NewRef(),
blueUpBtn: tui.NewRef(),
blueDnBtn: tui.NewRef(),
presetBtns: tui.NewRefMap[string](),
activePreset: tui.NewState(""),

resetBtn: tui.NewRef(),
showResetModal: tui.NewState(false),
}
}

Expand Down Expand Up @@ -294,16 +299,16 @@ func (c *colorMixer) adjustBlue(delta int) {
c.activePreset.Set("")
}

func (c *colorMixer) confirmReset() {
c.showReset.Set(false)
func (c *colorMixer) resetColors() {
c.red.Set(128)
c.green.Set(64)
c.blue.Set(200)
c.activePreset.Set("")
c.showResetModal.Set(false)
}

func (c *colorMixer) cancelReset() {
c.showReset.Set(false)
c.showResetModal.Set(false)
}

func (c *colorMixer) applyPreset(name string) {
Expand All @@ -328,7 +333,7 @@ func (c *colorMixer) KeyMap() tui.KeyMap {
tui.On(tui.Rune('G'), func(ke tui.KeyEvent) { c.adjustGreen(-16) }),
tui.On(tui.Rune('b'), func(ke tui.KeyEvent) { c.adjustBlue(16) }),
tui.On(tui.Rune('B'), func(ke tui.KeyEvent) { c.adjustBlue(-16) }),
tui.On(tui.Rune('x'), func(ke tui.KeyEvent) { c.showReset.Set(true) }),
tui.On(tui.Rune('x'), func(ke tui.KeyEvent) { c.showResetModal.Set(true) }),
}
}

Expand All @@ -341,6 +346,7 @@ func (c *colorMixer) HandleMouse(me tui.MouseEvent) bool {
tui.Click(c.greenDnBtn, func() { c.adjustGreen(-16) }),
tui.Click(c.blueUpBtn, func() { c.adjustBlue(16) }),
tui.Click(c.blueDnBtn, func() { c.adjustBlue(-16) }),
tui.Click(c.resetBtn, func() { c.showResetModal.Set(true) }),
) {
return true
}
Expand Down Expand Up @@ -447,22 +453,26 @@ templ (c *colorMixer) Render() {
}
</div>

<div class="flex gap-2 justify-center">
<button ref={c.resetBtn} class="px-1 text-red">Reset</button>
</div>

<div class="flex justify-center">
<span class="font-dim">r/g/b increase | R/G/B decrease | x reset | click buttons/presets | q quit</span>
<span class="font-dim">x reset | r/g/b increase | R/G/B decrease | click buttons/presets | q quit</span>
</div>
</div>

// Reset confirmation modal (uses onActivate, not refs)
<modal open={c.showReset} class="justify-center items-center">
<div class="border-rounded p-2 flex-col gap-1 w-40 border-cyan">
<span class="font-bold text-cyan">Reset Colors?</span>
<span>This restores the default RGB values.</span>
<div class="flex gap-2 justify-center">
<button class="px-2 border-rounded focusable" onActivate={c.confirmReset}>Yes</button>
<button class="px-2 border-rounded focusable" onActivate={c.cancelReset}>No</button>
// Reset confirmation modal (uses onActivate, not refs)
<modal open={c.showResetModal} class="justify-center items-center">
<div class="border-rounded p-2 flex-col gap-1 w-36 items-center">
<span class="font-bold text-yellow">Reset Colors?</span>
<span class="font-dim">This will restore default values.</span>
<div class="flex gap-2 justify-center">
<button class="px-2 text-green font-bold border-single focusable" onActivate={c.cancelReset}>Cancel</button>
<button class="px-2 text-red font-bold border-single focusable" onActivate={c.resetColors}>Yes, Reset</button>
</div>
</div>
</div>
</modal>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modal was outside of the parent div, so everything inside the modal was not working: tab navigation, buttons not triggering their actions...

</modal>
</div>
}
```

Expand Down Expand Up @@ -506,7 +516,7 @@ Click the +/- buttons, use r/g/b keys to adjust colors, or click a preset to app

![Refs and Click Handling screenshot](/guides/09a.png)

Click "reset" to open the confirmation dialog and click on an option.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Match the button text.

Click "Reset" to open the confirmation dialog and click on an option.

![Reset confirmation modal](/guides/09b.png)

Expand Down
16 changes: 15 additions & 1 deletion editor/vscode/test/complex_gsx.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't touch this file at all. I just ran the tests to make sure everything was all right. I guess this is generated/modified automatically as part of that.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/09-refs-and-clicks/clicks.gsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ func (c *colorMixer) KeyMap() tui.KeyMap {
return tui.KeyMap{
tui.On(tui.KeyEscape, func(ke tui.KeyEvent) { ke.App().Stop() }),
tui.On(tui.Rune('q'), func(ke tui.KeyEvent) { ke.App().Stop() }),
tui.On(tui.Rune('r'), func(ke tui.KeyEvent) { c.showResetModal.Set(true) }),
tui.On(tui.Rune('r'), func(ke tui.KeyEvent) { c.adjustRed(16) }),
tui.On(tui.Rune('R'), func(ke tui.KeyEvent) { c.adjustRed(-16) }),
tui.On(tui.Rune('g'), func(ke tui.KeyEvent) { c.adjustGreen(16) }),
tui.On(tui.Rune('G'), func(ke tui.KeyEvent) { c.adjustGreen(-16) }),
tui.On(tui.Rune('b'), func(ke tui.KeyEvent) { c.adjustBlue(16) }),
tui.On(tui.Rune('B'), func(ke tui.KeyEvent) { c.adjustBlue(-16) }),
tui.On(tui.Rune('x'), func(ke tui.KeyEvent) { c.showResetModal.Set(true) }),
}
}

Expand Down Expand Up @@ -237,7 +238,7 @@ templ (c *colorMixer) Render() {
</div>

<div class="flex justify-center">
<span class="font-dim">r reset | g/b increase | G/B decrease | click buttons/presets | q quit</span>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better we keep it this way in both places (the guide and the example). It's the way it's in the guide and it's more intuitive to have both lowercase and uppercase keybinds for every color instead of making an exception for the lowercase r because of reset and then having a bit more convoluted keybinds. Plus in the old text the keybind for the red was missing.

<span class="font-dim">x reset | r/g/b increase | R/G/B decrease | click buttons/presets | q quit</span>
</div>

// Confirmation modal for resetting colors
Expand Down
5 changes: 3 additions & 2 deletions examples/09-refs-and-clicks/clicks_gsx.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading