IDEA-314590 Implement factory default tool window layout

Use the empty string as its name because it's an invalid name
for a custom layout.

The Store Layout action is disabled when the default layout is active
because it's read-only. However, if someone tries to do the same
thing through the API (ToolWindowDefaultLayoutManager.setLayout),
as Rider does, for example, then we instead overwrite the "Custom"
layout and make it the current one. This isn't the best thing to do,
but it'll have to work until Rider implements multiple tool window layouts
on its side.

The Restore Current Layout action works the same way as usual
even if the currently active layout is the factory default one,
we only substitute the description because there's no name to use.

GitOrigin-RevId: 36f6f98bb4d34e4bc613088c7a1c0487be62c98f
This commit is contained in:
Sergei Tachenov
2023-07-31 14:43:24 +03:00
committed by intellij-monorepo-bot
parent 6c0d8cde6f
commit c59a373b00
6 changed files with 50 additions and 8 deletions

View File

@@ -19,10 +19,12 @@ public final class RestoreDefaultLayoutAction extends DumbAwareAction {
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(e.getProject() != null);
e.getPresentation().setDescription(ActionsBundle.message(
"action.RestoreDefaultLayout.named.description",
ToolWindowDefaultLayoutManager.getInstance().getActiveLayoutName()
));
String activeLayout = ToolWindowDefaultLayoutManager.getInstance().getActiveLayoutName();
e.getPresentation().setDescription(
ToolWindowDefaultLayoutManager.FACTORY_DEFAULT_LAYOUT_NAME.equals(activeLayout)
? ActionsBundle.message("action.RestoreFactoryDefaultLayout.description")
: ActionsBundle.message("action.RestoreDefaultLayout.named.description", activeLayout)
);
}
@Override

View File

@@ -0,0 +1,24 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ide.actions
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.DumbAwareToggleAction
import com.intellij.openapi.wm.ex.ToolWindowManagerEx
import com.intellij.toolWindow.ToolWindowDefaultLayoutManager
class RestoreFactoryDefaultLayoutAction : DumbAwareToggleAction() {
override fun getActionUpdateThread() = ActionUpdateThread.BGT
override fun isSelected(e: AnActionEvent): Boolean =
ToolWindowDefaultLayoutManager.getInstance().activeLayoutName == ToolWindowDefaultLayoutManager.FACTORY_DEFAULT_LAYOUT_NAME
override fun setSelected(e: AnActionEvent, state: Boolean) {
val project = e.project ?: return
val layoutManager = ToolWindowDefaultLayoutManager.getInstance()
layoutManager.activeLayoutName = ToolWindowDefaultLayoutManager.FACTORY_DEFAULT_LAYOUT_NAME
ToolWindowManagerEx.getInstanceEx(project).setLayout(layoutManager.getLayoutCopy())
}
}

View File

@@ -15,7 +15,9 @@ public final class StoreDefaultLayoutAction extends StoreNamedLayoutAction {
@Override
public void update(@NotNull AnActionEvent e) {
super.update(e);
e.getPresentation().setDescription(ActionsBundle.message("action.StoreDefaultLayout.named.description", getLayoutNameSupplier().invoke()));
String layoutName = getLayoutNameSupplier().invoke();
e.getPresentation().setEnabled(!ToolWindowDefaultLayoutManager.FACTORY_DEFAULT_LAYOUT_NAME.equals(layoutName));
e.getPresentation().setDescription(ActionsBundle.message("action.StoreDefaultLayout.named.description", layoutName));
}
}

View File

@@ -29,6 +29,7 @@ class ToolWindowDefaultLayoutManager(private val isNewUi: Boolean)
fun getInstance(): ToolWindowDefaultLayoutManager = service()
const val INITIAL_LAYOUT_NAME: String = "Custom"
const val FACTORY_DEFAULT_LAYOUT_NAME: String = ""
}
@Volatile
@@ -53,9 +54,16 @@ class ToolWindowDefaultLayoutManager(private val isNewUi: Boolean)
fun setLayout(name: String, layout: DesktopLayout) {
tracker.incModificationCount()
var layoutName = name
if (layoutName == FACTORY_DEFAULT_LAYOUT_NAME) {
// Saving over the factory default layout makes it non-default.
// This shouldn't normally happen because we validate the name in the platform.
// But it could happen if it's an external call, e.g., from Rider.
layoutName = INITIAL_LAYOUT_NAME
}
val list = layout.getSortedList().map(::convertWindowStateToDescriptor)
val weights = convertUnifiedWeightsToDescriptor(layout.unifiedWeights)
state = state.withUpdatedLayout(name, list, isNewUi, weights)
state = state.withUpdatedLayout(layoutName, list, isNewUi, weights)
}
fun renameLayout(oldName: String, newName: String) {
@@ -91,10 +99,12 @@ class ToolWindowDefaultLayoutManager(private val isNewUi: Boolean)
ToolWindowLayoutStorageManagerState(layouts = mapOf(INITIAL_LAYOUT_NAME to LayoutDescriptor(v1 = state.v1, v2 = state.v2)))
}
else {
state
state.withoutLayout(FACTORY_DEFAULT_LAYOUT_NAME) // Just in case the storage is corrupted and actually has an empty name.
}
this.state = newState
setLayout(newState.activeLayoutName, newState.getActiveLayoutCopy(isNewUi))
if (newState.activeLayoutName != FACTORY_DEFAULT_LAYOUT_NAME) {
setLayout(newState.activeLayoutName, newState.getActiveLayoutCopy(isNewUi))
}
}
/**

View File

@@ -1035,6 +1035,8 @@ action.AutoShowProcessWindow.text=Auto Show
action.AutoShowProcessWindow.description=Show background tasks window on starting of any progress
group.LayoutsGroup.text=_Layouts
action.RestoreFactoryDefaultLayout.text=Default
action.RestoreFactoryDefaultLayout.description=Reset the tool window layout to factory default
group.CustomLayoutsGroup.text=Tool Window Layouts
group.CustomLayoutActionsGroup.text=Tool Window Layout
group.CustomLayoutActionsGroup.current.text={0} (Current)

View File

@@ -744,6 +744,8 @@
<action id="MinimizeCurrentWindow" class="com.intellij.ide.actions.MinimizeCurrentWindowAction"/>
<action id="ZoomCurrentWindow" class="com.intellij.ide.actions.ZoomCurrentWindowAction"/>
<group id="LayoutsGroup" popup="true">
<action id="RestoreFactoryDefaultLayout" class="com.intellij.ide.actions.RestoreFactoryDefaultLayoutAction" />
<separator/>
<group id="CustomLayoutsGroup" class="com.intellij.ide.actions.CustomLayoutsActionGroup" />
<separator/>
<action id="RestoreDefaultLayout" class="com.intellij.ide.actions.RestoreDefaultLayoutAction"/>