2022-08-06 10:05:00 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2022-08-06 10:50:52 +02:00
|
|
|
"math"
|
|
|
|
|
2022-08-06 10:05:00 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/mergeconflicts"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2022-08-06 10:50:52 +02:00
|
|
|
"github.com/sasha-s/go-deadlock"
|
2022-08-06 10:05:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type MergeConflictsContext struct {
|
|
|
|
types.Context
|
|
|
|
viewModel *ConflictsViewModel
|
2023-03-23 03:35:07 +02:00
|
|
|
c *ContextCommon
|
2022-08-07 01:44:50 +02:00
|
|
|
mutex *deadlock.Mutex
|
2022-08-06 10:05:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConflictsViewModel struct {
|
|
|
|
state *mergeconflicts.State
|
|
|
|
|
|
|
|
// userVerticalScrolling tells us if the user has started scrolling through the file themselves
|
|
|
|
// in which case we won't auto-scroll to a conflict.
|
|
|
|
userVerticalScrolling bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMergeConflictsContext(
|
2023-03-23 03:35:07 +02:00
|
|
|
c *ContextCommon,
|
2022-08-06 10:05:00 +02:00
|
|
|
) *MergeConflictsContext {
|
|
|
|
viewModel := &ConflictsViewModel{
|
|
|
|
state: mergeconflicts.NewState(),
|
|
|
|
userVerticalScrolling: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MergeConflictsContext{
|
|
|
|
viewModel: viewModel,
|
2022-08-07 01:44:50 +02:00
|
|
|
mutex: &deadlock.Mutex{},
|
2022-08-06 10:05:00 +02:00
|
|
|
Context: NewSimpleContext(
|
|
|
|
NewBaseContext(NewBaseContextOpts{
|
2023-02-16 12:31:10 +02:00
|
|
|
Kind: types.MAIN_CONTEXT,
|
2023-03-21 12:06:39 +02:00
|
|
|
View: c.Views().MergeConflicts,
|
2023-02-16 12:31:10 +02:00
|
|
|
WindowName: "main",
|
|
|
|
Key: MERGE_CONFLICTS_CONTEXT_KEY,
|
|
|
|
Focusable: true,
|
|
|
|
HighlightOnFocus: true,
|
2022-08-06 10:05:00 +02:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
c: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 10:50:52 +02:00
|
|
|
func (self *MergeConflictsContext) GetState() *mergeconflicts.State {
|
|
|
|
return self.viewModel.state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MergeConflictsContext) SetState(state *mergeconflicts.State) {
|
|
|
|
self.viewModel.state = state
|
|
|
|
}
|
|
|
|
|
2022-08-07 01:44:50 +02:00
|
|
|
func (self *MergeConflictsContext) GetMutex() *deadlock.Mutex {
|
2022-08-06 10:50:52 +02:00
|
|
|
return self.mutex
|
|
|
|
}
|
|
|
|
|
2022-08-06 10:05:00 +02:00
|
|
|
func (self *MergeConflictsContext) SetUserScrolling(isScrolling bool) {
|
|
|
|
self.viewModel.userVerticalScrolling = isScrolling
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MergeConflictsContext) IsUserScrolling() bool {
|
|
|
|
return self.viewModel.userVerticalScrolling
|
|
|
|
}
|
|
|
|
|
2022-08-06 10:50:52 +02:00
|
|
|
func (self *MergeConflictsContext) RenderAndFocus(isFocused bool) error {
|
|
|
|
self.setContent(isFocused)
|
2023-02-16 12:31:10 +02:00
|
|
|
self.FocusSelection()
|
2022-08-06 10:50:52 +02:00
|
|
|
|
|
|
|
self.c.Render()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MergeConflictsContext) Render(isFocused bool) error {
|
|
|
|
self.setContent(isFocused)
|
|
|
|
|
|
|
|
self.c.Render()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MergeConflictsContext) GetContentToRender(isFocused bool) string {
|
|
|
|
if self.GetState() == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergeconflicts.ColoredConflictFile(self.GetState(), isFocused)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MergeConflictsContext) setContent(isFocused bool) {
|
|
|
|
self.GetView().SetContent(self.GetContentToRender(isFocused))
|
|
|
|
}
|
|
|
|
|
2023-02-16 12:31:10 +02:00
|
|
|
func (self *MergeConflictsContext) FocusSelection() {
|
2022-08-06 10:50:52 +02:00
|
|
|
if !self.IsUserScrolling() {
|
2023-02-16 12:31:10 +02:00
|
|
|
_ = self.GetView().SetOriginY(self.GetOriginY())
|
2022-08-06 10:50:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MergeConflictsContext) GetOriginY() int {
|
|
|
|
view := self.GetView()
|
|
|
|
conflictMiddle := self.GetState().GetConflictMiddle()
|
|
|
|
return int(math.Max(0, float64(conflictMiddle-(view.Height()/2))))
|
2022-08-06 10:05:00 +02:00
|
|
|
}
|