Skip to content

Commit

Permalink
io/semantic: [API] replace DisabledOp with EnabledOp
Browse files Browse the repository at this point in the history
The double-negative DisabledOp is harder to understand than a
straightforward EnabledOp. Note that the absence of an EnabledOp
implies still means that the widget is enabled.

Signed-off-by: Elias Naur <[email protected]>
  • Loading branch information
eliasnaur committed Oct 6, 2023
1 parent b66dcc4 commit e1b3928
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions internal/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const (
TypeSemanticDesc
TypeSemanticClass
TypeSemanticSelected
TypeSemanticDisabled
TypeSemanticEnabled
TypeSnippet
TypeSelection
TypeActionInput
Expand Down Expand Up @@ -170,7 +170,7 @@ const (
TypeSemanticDescLen = 1
TypeSemanticClassLen = 2
TypeSemanticSelectedLen = 2
TypeSemanticDisabledLen = 2
TypeSemanticEnabledLen = 2
TypeSnippetLen = 1 + 4 + 4
TypeSelectionLen = 1 + 2*4 + 2*4 + 4 + 4
TypeActionInputLen = 1 + 1
Expand Down Expand Up @@ -454,7 +454,7 @@ var opProps = [0x100]opProp{
TypeSemanticDesc: {Size: TypeSemanticDescLen, NumRefs: 1},
TypeSemanticClass: {Size: TypeSemanticClassLen, NumRefs: 0},
TypeSemanticSelected: {Size: TypeSemanticSelectedLen, NumRefs: 0},
TypeSemanticDisabled: {Size: TypeSemanticDisabledLen, NumRefs: 0},
TypeSemanticEnabled: {Size: TypeSemanticEnabledLen, NumRefs: 0},
TypeSnippet: {Size: TypeSnippetLen, NumRefs: 2},
TypeSelection: {Size: TypeSelectionLen, NumRefs: 1},
TypeActionInput: {Size: TypeActionInputLen, NumRefs: 0},
Expand Down
4 changes: 2 additions & 2 deletions io/router/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ func (c *pointerCollector) semanticSelected(selected bool) {
area.semantic.content.selected = selected
}

func (c *pointerCollector) semanticDisabled(disabled bool) {
func (c *pointerCollector) semanticEnabled(enabled bool) {
areaID := c.currentArea()
area := &c.q.areas[areaID]
area.semantic.valid = true
area.semantic.content.disabled = disabled
area.semantic.content.disabled = !enabled
}

func (c *pointerCollector) cursor(cursor pointer.Cursor) {
Expand Down
6 changes: 3 additions & 3 deletions io/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,11 @@ func (q *Router) collect() {
} else {
pc.semanticSelected(false)
}
case ops.TypeSemanticDisabled:
case ops.TypeSemanticEnabled:
if encOp.Data[1] != 0 {
pc.semanticDisabled(true)
pc.semanticEnabled(true)
} else {
pc.semanticDisabled(false)
pc.semanticEnabled(false)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion io/router/semantic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestSemanticDescription(t *testing.T) {
semantic.DescriptionOp("description").Add(&ops)
semantic.LabelOp("label").Add(&ops)
semantic.Button.Add(&ops)
semantic.DisabledOp(true).Add(&ops)
semantic.EnabledOp(false).Add(&ops)
semantic.SelectedOp(true).Add(&ops)
var r Router
r.Frame(&ops)
Expand Down
12 changes: 6 additions & 6 deletions io/semantic/semantic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const (
// boolean state.
type SelectedOp bool

// DisabledOp describes the disabled state.
type DisabledOp bool
// EnabledOp describes the enabled state.
type EnabledOp bool

func (l LabelOp) Add(o *op.Ops) {
data := ops.Write1String(&o.Internal, ops.TypeSemanticLabelLen, string(l))
Expand All @@ -63,10 +63,10 @@ func (s SelectedOp) Add(o *op.Ops) {
}
}

func (d DisabledOp) Add(o *op.Ops) {
data := ops.Write(&o.Internal, ops.TypeSemanticDisabledLen)
data[0] = byte(ops.TypeSemanticDisabled)
if d {
func (e EnabledOp) Add(o *op.Ops) {
data := ops.Write(&o.Internal, ops.TypeSemanticEnabledLen)
data[0] = byte(ops.TypeSemanticEnabled)
if e {
data[1] = 1
}
}
Expand Down
2 changes: 1 addition & 1 deletion widget/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (b *Bool) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
b.changed = true
}
semantic.SelectedOp(b.Value).Add(gtx.Ops)
semantic.DisabledOp(gtx.Queue == nil).Add(gtx.Ops)
semantic.EnabledOp(gtx.Queue != nil).Add(gtx.Ops)
return w(gtx)
})
return dims
Expand Down
6 changes: 3 additions & 3 deletions widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimension
dims := w(gtx)
c := m.Stop()
defer clip.Rect(image.Rectangle{Max: dims.Size}).Push(gtx.Ops).Pop()
disabled := gtx.Queue == nil
semantic.DisabledOp(disabled).Add(gtx.Ops)
enabled := gtx.Queue != nil
semantic.EnabledOp(enabled).Add(gtx.Ops)
b.click.Add(gtx.Ops)
if !disabled {
if enabled {
keys := key.Set("⏎|Space")
if !b.focused {
keys = ""
Expand Down
6 changes: 3 additions & 3 deletions widget/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ func (e *Enum) Layout(gtx layout.Context, k string, content layout.Widget) layou
}

clk.Add(gtx.Ops)
disabled := gtx.Queue == nil
if !disabled {
enabled := gtx.Queue != nil
if enabled {
key.InputOp{Tag: &state.tag, Keys: "⏎|Space"}.Add(gtx.Ops)
} else if e.focus == k {
e.focused = false
}
semantic.SelectedOp(k == e.Value).Add(gtx.Ops)
semantic.DisabledOp(disabled).Add(gtx.Ops)
semantic.EnabledOp(enabled).Add(gtx.Ops)
c.Add(gtx.Ops)

return dims
Expand Down

0 comments on commit e1b3928

Please sign in to comment.