Skip to content

Commit

Permalink
Merge pull request #486 from gucio321/split-layout-fix
Browse files Browse the repository at this point in the history
SplitLayout: make `sashPos` argument a pointer
  • Loading branch information
AllenDang authored Mar 28, 2022
2 parents d84b1ad + 30094dd commit a85dc09
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
27 changes: 13 additions & 14 deletions SplitLayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const (
var _ Disposable = &splitLayoutState{}

type splitLayoutState struct {
delta float32
sashPos float32
delta float32
}

// Dispose implements disposable interface.
Expand All @@ -39,12 +38,12 @@ type SplitLayoutWidget struct {
originItemSpacingY float32
originFramePaddingX float32
originFramePaddingY float32
sashPos float32
sashPos *float32
border bool
}

// SplitLayout creates split layout widget.
func SplitLayout(direction SplitDirection, sashPos float32, layout1, layout2 Widget) *SplitLayoutWidget {
func SplitLayout(direction SplitDirection, sashPos *float32, layout1, layout2 Widget) *SplitLayoutWidget {
return &SplitLayoutWidget{
direction: direction,
sashPos: sashPos,
Expand Down Expand Up @@ -75,33 +74,33 @@ func (s *SplitLayoutWidget) Build() {

var layout Layout

splitLayoutState.sashPos += splitLayoutState.delta
if splitLayoutState.sashPos < 1 {
splitLayoutState.sashPos = 1
*s.sashPos += splitLayoutState.delta
if *s.sashPos < 1 {
*s.sashPos = 1
}

switch s.direction {
case DirectionHorizontal:
availableW, _ := GetAvailableRegion()
if splitLayoutState.sashPos >= availableW {
splitLayoutState.sashPos = availableW
if *s.sashPos >= availableW {
*s.sashPos = availableW
}

layout = Layout{
Row(
s.buildChild(splitLayoutState.sashPos, 0, s.layout1),
s.buildChild(*s.sashPos, 0, s.layout1),
VSplitter(&(splitLayoutState.delta)).Size(s.originItemSpacingX, 0),
s.buildChild(Auto, Auto, s.layout2),
),
}
case DirectionVertical:
_, availableH := GetAvailableRegion()
if splitLayoutState.sashPos >= availableH {
splitLayoutState.sashPos = availableH
if *s.sashPos >= availableH {
*s.sashPos = availableH
}
layout = Layout{
Column(
s.buildChild(Auto, splitLayoutState.sashPos, s.layout1),
s.buildChild(Auto, *s.sashPos, s.layout1),
HSplitter(&(splitLayoutState.delta)).Size(0, s.originItemSpacingY),
s.buildChild(Auto, Auto, s.layout2),
),
Expand Down Expand Up @@ -161,7 +160,7 @@ func (s *SplitLayoutWidget) buildChild(width, height float32, layout Widget) Wid

func (s *SplitLayoutWidget) getState() (state *splitLayoutState) {
if st := Context.GetState(s.id); st == nil {
state = &splitLayoutState{delta: 0.0, sashPos: s.sashPos}
state = &splitLayoutState{delta: 0.0}
Context.SetState(s.id, state)
} else {
var isOk bool
Expand Down
7 changes: 5 additions & 2 deletions examples/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"github.com/AllenDang/giu"
)

var markdown string = getExampleMarkdownText()
var (
markdown string = getExampleMarkdownText()
splitLayoutPos float32 = 320
)

func getExampleMarkdownText() string {
return strings.Join([]string{
Expand Down Expand Up @@ -49,7 +52,7 @@ func getExampleMarkdownText() string {

func loop() {
giu.SingleWindow().Layout(
giu.SplitLayout(giu.DirectionHorizontal, 320,
giu.SplitLayout(giu.DirectionHorizontal, &splitLayoutPos,
giu.Layout{
giu.Row(
giu.Label("Markdown Edition:"),
Expand Down
15 changes: 11 additions & 4 deletions examples/splitter/splitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ import (
g "github.com/AllenDang/giu"
)

var (
sashPos1 float32 = 200
sashPos2 float32 = 200
sashPos3 float32 = 200
sashPos4 float32 = 100
)

func loop() {
g.SingleWindow().Layout(
g.SplitLayout(g.DirectionHorizontal, 200,
g.SplitLayout(g.DirectionHorizontal, &sashPos1,
g.Layout{
g.Label("Left panel"),
g.Row(g.Button("Button1"), g.Button("Button2")),
},
g.SplitLayout(g.DirectionVertical, 200,
g.SplitLayout(g.DirectionVertical, &sashPos2,
g.Layout{},
g.SplitLayout(g.DirectionHorizontal, 200,
g.SplitLayout(g.DirectionHorizontal, &sashPos3,
g.Layout{},
g.SplitLayout(g.DirectionVertical, 100,
g.SplitLayout(g.DirectionVertical, &sashPos4,
g.Layout{},
g.Layout{},
),
Expand Down

0 comments on commit a85dc09

Please sign in to comment.