From da24deb881c42beeb6ecf5c12a56bbf056c10165 Mon Sep 17 00:00:00 2001
From: Stefan Haller <stefan@haller-berlin.de>
Date: Wed, 23 Apr 2025 08:27:33 +0200
Subject: [PATCH] Allow changing diff context size and rename threshold when
 main view is focused

---
 pkg/gui/controllers/context_lines_controller.go  | 16 ++++++++++++++--
 .../rename_similarity_threshold_controller.go    | 16 ++++++++++++++--
 .../diff/rename_similarity_threshold_change.go   | 12 ++++++++++++
 .../file/rename_similarity_threshold_change.go   | 11 +++++++++++
 4 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/pkg/gui/controllers/context_lines_controller.go b/pkg/gui/controllers/context_lines_controller.go
index 432da5bc0..8e35d5103 100644
--- a/pkg/gui/controllers/context_lines_controller.go
+++ b/pkg/gui/controllers/context_lines_controller.go
@@ -22,6 +22,8 @@ var CONTEXT_KEYS_SHOWING_DIFFS = []types.ContextKey{
 	context.STAGING_SECONDARY_CONTEXT_KEY,
 	context.PATCH_BUILDING_MAIN_CONTEXT_KEY,
 	context.PATCH_BUILDING_SECONDARY_CONTEXT_KEY,
+	context.NORMAL_MAIN_CONTEXT_KEY,
+	context.NORMAL_SECONDARY_CONTEXT_KEY,
 }
 
 type ContextLinesController struct {
@@ -97,7 +99,7 @@ func (self *ContextLinesController) applyChange() error {
 	self.c.Toast(fmt.Sprintf(self.c.Tr.DiffContextSizeChanged, self.c.AppState.DiffContextSize))
 	self.c.SaveAppStateAndLogError()
 
-	currentContext := self.c.Context().CurrentStatic()
+	currentContext := self.currentSidePanel()
 	switch currentContext.GetKey() {
 	// we make an exception for our staging and patch building contexts because they actually need to refresh their state afterwards.
 	case context.PATCH_BUILDING_MAIN_CONTEXT_KEY:
@@ -121,6 +123,16 @@ func (self *ContextLinesController) checkCanChangeContext() error {
 func (self *ContextLinesController) isShowingDiff() bool {
 	return lo.Contains(
 		CONTEXT_KEYS_SHOWING_DIFFS,
-		self.c.Context().CurrentStatic().GetKey(),
+		self.currentSidePanel().GetKey(),
 	)
 }
+
+func (self *ContextLinesController) currentSidePanel() types.Context {
+	currentContext := self.c.Context().CurrentStatic()
+	if currentContext.GetKey() == context.NORMAL_MAIN_CONTEXT_KEY ||
+		currentContext.GetKey() == context.NORMAL_SECONDARY_CONTEXT_KEY {
+		return currentContext.GetParentContext()
+	}
+
+	return currentContext
+}
diff --git a/pkg/gui/controllers/rename_similarity_threshold_controller.go b/pkg/gui/controllers/rename_similarity_threshold_controller.go
index f602ca9d8..88c611723 100644
--- a/pkg/gui/controllers/rename_similarity_threshold_controller.go
+++ b/pkg/gui/controllers/rename_similarity_threshold_controller.go
@@ -15,6 +15,8 @@ var CONTEXT_KEYS_SHOWING_RENAMES = []types.ContextKey{
 	context.SUB_COMMITS_CONTEXT_KEY,
 	context.LOCAL_COMMITS_CONTEXT_KEY,
 	context.STASH_CONTEXT_KEY,
+	context.NORMAL_MAIN_CONTEXT_KEY,
+	context.NORMAL_SECONDARY_CONTEXT_KEY,
 }
 
 type RenameSimilarityThresholdController struct {
@@ -82,7 +84,7 @@ func (self *RenameSimilarityThresholdController) applyChange() error {
 	self.c.Toast(fmt.Sprintf(self.c.Tr.RenameSimilarityThresholdChanged, self.c.AppState.RenameSimilarityThreshold))
 	self.c.SaveAppStateAndLogError()
 
-	currentContext := self.c.Context().CurrentStatic()
+	currentContext := self.currentSidePanel()
 	switch currentContext.GetKey() {
 	// we make an exception for our files context, because it actually need to refresh its state afterwards.
 	case context.FILES_CONTEXT_KEY:
@@ -96,6 +98,16 @@ func (self *RenameSimilarityThresholdController) applyChange() error {
 func (self *RenameSimilarityThresholdController) isShowingRenames() bool {
 	return lo.Contains(
 		CONTEXT_KEYS_SHOWING_RENAMES,
-		self.c.Context().CurrentStatic().GetKey(),
+		self.currentSidePanel().GetKey(),
 	)
 }
+
+func (self *RenameSimilarityThresholdController) currentSidePanel() types.Context {
+	currentContext := self.c.Context().CurrentStatic()
+	if currentContext.GetKey() == context.NORMAL_MAIN_CONTEXT_KEY ||
+		currentContext.GetKey() == context.NORMAL_SECONDARY_CONTEXT_KEY {
+		return currentContext.GetParentContext()
+	}
+
+	return currentContext
+}
diff --git a/pkg/integration/tests/diff/rename_similarity_threshold_change.go b/pkg/integration/tests/diff/rename_similarity_threshold_change.go
index 170838fd3..397e4e222 100644
--- a/pkg/integration/tests/diff/rename_similarity_threshold_change.go
+++ b/pkg/integration/tests/diff/rename_similarity_threshold_change.go
@@ -37,5 +37,17 @@ var RenameSimilarityThresholdChange = NewIntegrationTest(NewIntegrationTestArgs{
 				Contains("original => renamed"),
 				Contains("1 file changed, 5 insertions(+)"),
 			)
+
+		t.Views().Commits().
+			Press(keys.Universal.FocusMainView)
+
+		t.Views().Main().
+			Press(keys.Universal.IncreaseRenameSimilarityThreshold).
+			Tap(func() {
+				t.ExpectToast(Equals("Changed rename similarity threshold to 50%"))
+			}).
+			ContainsLines(
+				Contains("2 files changed, 10 insertions(+), 5 deletions(-)"),
+			)
 	},
 })
diff --git a/pkg/integration/tests/file/rename_similarity_threshold_change.go b/pkg/integration/tests/file/rename_similarity_threshold_change.go
index b2a6fc376..ac3ae37d3 100644
--- a/pkg/integration/tests/file/rename_similarity_threshold_change.go
+++ b/pkg/integration/tests/file/rename_similarity_threshold_change.go
@@ -31,6 +31,17 @@ var RenameSimilarityThresholdChange = NewIntegrationTest(NewIntegrationTestArgs{
 			}).
 			Lines(
 				Equals("R  original → renamed"),
+			).
+			Press(keys.Universal.FocusMainView).
+			Tap(func() {
+				t.Views().Main().
+					Press(keys.Universal.IncreaseRenameSimilarityThreshold)
+				t.ExpectToast(Equals("Changed rename similarity threshold to 50%"))
+			}).
+			Lines(
+				Equals("▼ /"),
+				Equals("  D  original"),
+				Equals("  A  renamed"),
 			)
 	},
 })