1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/arrangement.go

215 lines
4.9 KiB
Go
Raw Normal View History

2020-05-17 13:32:17 +02:00
package gui
2020-05-18 14:00:07 +02:00
func (gui *Gui) mainSectionChildren() []*box {
currentViewName := gui.currentViewName()
// if we're not in split mode we can just show the one main panel. Likewise if
// the main panel is focused and we're in full-screen mode
if !gui.State.SplitMainPanel || (gui.State.ScreenMode == SCREEN_FULL && currentViewName == "main") {
return []*box{
{
viewName: "main",
weight: 1,
},
}
}
2020-05-17 13:32:17 +02:00
main := "main"
secondary := "secondary"
2020-05-18 14:00:07 +02:00
if gui.secondaryViewFocused() {
// when you think you've focused the secondary view, we've actually just swapped them around in the layout
2020-05-17 13:32:17 +02:00
main, secondary = secondary, main
}
2020-05-18 14:00:07 +02:00
return []*box{
2020-05-17 13:32:17 +02:00
{
viewName: main,
weight: 1,
},
2020-05-18 14:00:07 +02:00
{
2020-05-17 13:32:17 +02:00
viewName: secondary,
weight: 1,
2020-05-18 14:00:07 +02:00
},
2020-05-17 13:32:17 +02:00
}
2020-05-18 14:00:07 +02:00
}
func (gui *Gui) getMidSectionWeights() (int, int) {
currentViewName := gui.currentViewName()
2020-05-17 13:32:17 +02:00
2020-05-17 13:44:59 +02:00
// we originally specified this as a ratio i.e. .20 would correspond to a weight of 1 against 4
sidePanelWidthRatio := gui.Config.GetUserConfig().GetFloat64("gui.sidePanelWidth")
// we could make this better by creating ratios like 2:3 rather than always 1:something
mainSectionWeight := int(1/sidePanelWidthRatio) - 1
sideSectionWeight := 1
if gui.State.SplitMainPanel {
mainSectionWeight = 5 // need to shrink side panel to make way for main panels if side-by-side
}
2020-05-18 14:00:07 +02:00
2020-05-17 13:44:59 +02:00
if currentViewName == "main" {
if gui.State.ScreenMode == SCREEN_HALF || gui.State.ScreenMode == SCREEN_FULL {
sideSectionWeight = 0
}
} else {
if gui.State.ScreenMode == SCREEN_HALF {
mainSectionWeight = 1
} else if gui.State.ScreenMode == SCREEN_FULL {
mainSectionWeight = 0
}
}
2020-05-18 14:00:07 +02:00
return sideSectionWeight, mainSectionWeight
}
func (gui *Gui) getViewDimensions() map[string]dimensions {
width, height := gui.g.Size()
sideSectionWeight, mainSectionWeight := gui.getMidSectionWeights()
2020-05-17 13:44:59 +02:00
sidePanelsDirection := COLUMN
portraitMode := width <= 84 && height > 50
if portraitMode {
sidePanelsDirection = ROW
}
root := &box{
direction: ROW,
children: []*box{
{
direction: sidePanelsDirection,
weight: 1,
children: []*box{
{
direction: ROW,
weight: sideSectionWeight,
conditionalChildren: gui.sidePanelChildren,
},
{
conditionalDirection: func(width int, height int) int {
if width < 160 && height > 30 { // 2 80 character width panels
return ROW
} else {
return COLUMN
}
},
direction: COLUMN,
weight: mainSectionWeight,
2020-05-18 14:00:07 +02:00
children: gui.mainSectionChildren(),
2020-05-17 13:44:59 +02:00
},
},
},
// TODO: actually handle options here. Currently we're just hard-coding it to be set on the bottom row in our layout function given that we need some custom logic to have it share space with other views on that row.
{
viewName: "options",
size: 1,
},
},
}
return gui.arrangeViews(root, 0, 0, width, height)
}
func (gui *Gui) sidePanelChildren(width int, height int) []*box {
2020-05-17 13:32:17 +02:00
currentCyclableViewName := gui.currentCyclableViewName()
if gui.State.ScreenMode == SCREEN_FULL || gui.State.ScreenMode == SCREEN_HALF {
fullHeightBox := func(viewName string) *box {
if viewName == currentCyclableViewName {
return &box{
viewName: viewName,
weight: 1,
}
} else {
return &box{
viewName: viewName,
size: 0,
}
}
}
2020-05-17 13:44:59 +02:00
return []*box{
2020-05-17 13:32:17 +02:00
fullHeightBox("status"),
fullHeightBox("files"),
fullHeightBox("branches"),
fullHeightBox("commits"),
fullHeightBox("stash"),
}
2020-05-17 13:44:59 +02:00
} else if height >= 28 {
return []*box{
2020-05-17 13:32:17 +02:00
{
viewName: "status",
size: 3,
},
{
viewName: "files",
weight: 1,
},
{
viewName: "branches",
weight: 1,
},
{
viewName: "commits",
weight: 1,
},
{
viewName: "stash",
size: 3,
},
}
} else {
squashedHeight := 1
if height >= 21 {
squashedHeight = 3
}
squashedSidePanelBox := func(viewName string) *box {
if viewName == currentCyclableViewName {
return &box{
viewName: viewName,
weight: 1,
}
} else {
return &box{
viewName: viewName,
size: squashedHeight,
}
}
}
2020-05-17 13:44:59 +02:00
return []*box{
2020-05-17 13:32:17 +02:00
squashedSidePanelBox("status"),
squashedSidePanelBox("files"),
squashedSidePanelBox("branches"),
squashedSidePanelBox("commits"),
squashedSidePanelBox("stash"),
}
}
2020-05-17 13:44:59 +02:00
}
2020-05-17 13:32:17 +02:00
2020-05-17 13:44:59 +02:00
func (gui *Gui) currentCyclableViewName() string {
currView := gui.g.CurrentView()
currentCyclebleView := gui.State.PreviousView
if currView != nil {
viewName := currView.Name()
usePreviousView := true
2020-05-17 13:54:51 +02:00
for _, view := range gui.getCyclableViews() {
2020-05-17 13:44:59 +02:00
if view == viewName {
currentCyclebleView = viewName
usePreviousView = false
break
}
2020-05-17 13:32:17 +02:00
}
2020-05-17 13:44:59 +02:00
if usePreviousView {
currentCyclebleView = gui.State.PreviousView
2020-05-17 13:32:17 +02:00
}
}
2020-05-17 13:44:59 +02:00
// unfortunate result of the fact that these are separate views, have to map explicitly
if currentCyclebleView == "commitFiles" {
return "commits"
2020-05-17 13:32:17 +02:00
}
2020-05-17 13:44:59 +02:00
return currentCyclebleView
2020-05-17 13:32:17 +02:00
}